Friday, June 2, 2023

Learning Web Pentesting With DVWA Part 2: SQL Injection

In the last article Learning Web Pentesting With DVWA Part 1: Installation, you were given a glimpse of SQL injection when we installed the DVWA app. In this article we will explain what we did at the end of that article and much more.
Lets start by defining what SQL injection is, OWASP defines it as: "A SQL injection attack consists of insertion or "injection" of a SQL query via the input data from the client to the application. A successful SQL injection exploit can read sensitive data from the database, modify database data (Insert/Update/Delete), execute administration operations on the database (such as shutdown the DBMS), recover the content of a given file present on the DBMS file system and in some cases issue commands to the operating system. SQL injection attacks are a type of injection attack, in which SQL commands are injected into data-plane input in order to effect the execution of predefined SQL commands."
Which basically means that we can use a simple (vulnerable) input field in our web application to get information from the database of the server which hosts the web application. We can command and control (at certain times) the database of the web application or even the server.
In this article we are going to perform SQL injection attack on DVWA, so let's jump in. On the DVWA welcome page click on SQL Injection navigation link. We are presented with a page with an input field for User ID.
Now lets try to input a value like 1 in the input field. We can see a response from server telling us the firstname and surname of the user associated with User ID 1.
If we try to enter a user id which doesn't exist, we get no data back from the server. To determine whether an input field is vulnerable to SQL injection, we first start by sending a single quote (') as input. Which returns an SQL error.
We saw this in the previous article and we also talked about injection point in it. Before diving deeper into how this vulnerability can be exploited lets try to understand how this error might have occurred. Lets try to build the SQL query that the server might be trying to execute. Say the query looks something like this:
SELECT first_name, sur_name FROM users WHERE user_id = '1';
The 1 in this query is the value supplied by the user in the User ID input field. When we input a single quote in the User ID input field, the query looks like this:
SELECT first_name, sur_name FROM users WHERE user_id = '''; 
The quotes around the input provided in the User ID input field are from the server side application code. The error is due to the extra single quote present in the query. Now if we specify a comment after the single quote like this:
'-- -
or
'#
we should get no error. Now our crafted query looks like this:
SELECT first_name, sur_name FROM users WHERE user_id = ''-- -'; 
or
SELECT first_name, sur_name FROM users WHERE user_id = ''#'; 
since everything after the # or -- - are commented out, the query will ignore the extra single quote added by the server side app and whatever comes after it and will not generate any error. However the query returns nothing because we specified nothing ('') as the user_id.
After knowing how things might be working on the server side, we will start to attack the application.
First of all we will try to determine the number of columns that the query outputs because if we try a query which will output the number of columns greater or smaller than what the original query outputs then our query is going to get an error. So we will first figure out the exact number of columns that the query outputs and we will do that with the help of order by sql statement like this:
' order by 1-- - 
This MySQL server might execute the query as:
SELECT first_name, sur_name FROM users WHERE user_id = '' order by 1-- -'; 
you get the idea now.
if we don't get any error message, we will increase the number to 2 like this:
' order by 2-- - 
still no error message, lets add another:
' order by 3-- - 
and there we go we have an error message. Which tells us the number of columns that the server query selects is 2 because it erred out at 3.
Now lets use the union select SQL statement to get information about the database itself.
' union select null, version()-- - 
You should first understand what a union select statement does and only then can you understand what we are doing here. You can read about it here.
We have used null as one column since we need to match the number of columns from the server query which is two. null will act as a dummy column here which will give no output and the second column which in our case here is the version() command will output the database version. Notice the output from the application, nothing is shown for First name since we specified null for it and the maria db version will be displayed in Surname.
Now lets check who the database user is using the user() function of mariadb:
' union select null, user()-- - 
After clicking the submit button you should be able to see the user of the database in surname.

Now lets get some information about the databases in the database.
Lets determine the names of databases from INFORMATION_SCHEMA.SCHEMATA by entering following input in the User ID field:
' union select null, SCHEMA_NAME from INFORMATION_SCHEMA.SCHEMATA-- - 
This lists two databases dvwa and information_schema. information_schema is the built in database. Lets look at the dvwa database.
Get table names for dvwa database from INFORMATION_SCHEMA.TABLES
' union select null, TABLE_NAME from INFORMATION_SCHEMA.TABLES-- - 
It gives a huge number of tables that are present in dvwa database. But what we are really interested in is the users table as it is most likely to contain user passwords. But first we need to determine columns of that table and we will do that by querying INFORMATION_SCHEMA.COLUMNS like this:
' union select null, COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'users'-- - 

We can see the password column in the output now lets get those passwords:
' union select user, password from users-- - 
Of-course those are the hashes and not plain text passwords. You need to crack them.
Hope you learned something about SQL injection in this article. See you next time.

References:

1. SQL Injection: https://owasp.org/www-community/attacks/SQL_Injection
2. MySQL UNION: https://www.mysqltutorial.org/sql-union-mysql.aspx
3. Chapter 25 INFORMATION_SCHEMA Tables: https://dev.mysql.com/doc/refman/8.0/en/information-schema.html
Related links
  1. Hacking Apps
  2. Growth Hacker Tools
  3. Pentest Tools Review
  4. Hacking Tools 2020
  5. Hacking Tools For Mac
  6. Hack Tools Pc
  7. Pentest Tools For Windows
  8. Best Hacking Tools 2020
  9. Ethical Hacker Tools
  10. Pentest Recon Tools
  11. Kik Hack Tools
  12. Pentest Tools Subdomain
  13. Hack And Tools
  14. Blackhat Hacker Tools
  15. Hacking Tools For Kali Linux
  16. Hacker Tools Apk Download
  17. Hacking Tools And Software
  18. Pentest Tools Find Subdomains
  19. Hacks And Tools
  20. Github Hacking Tools
  21. Hack Tools Online
  22. Hack Tools For Games
  23. Hacking Tools Pc
  24. Hacker Tools For Ios
  25. Hacking Tools Usb
  26. Termux Hacking Tools 2019
  27. Hacker Tools For Windows
  28. Hacking Tools And Software
  29. Tools For Hacker
  30. World No 1 Hacker Software
  31. Pentest Tools Download
  32. World No 1 Hacker Software
  33. Blackhat Hacker Tools
  34. Hack Tools Online
  35. Hacking Tools Kit
  36. Pentest Tools List
  37. Hacker Tools Apk Download
  38. Hacking Tools
  39. Hacker Tools Github
  40. Hacker Tools Software
  41. Hack Tools For Mac
  42. Nsa Hack Tools Download
  43. Hacker Tools Software
  44. Usb Pentest Tools
  45. Nsa Hacker Tools
  46. Hacker Tools Hardware
  47. Hacking Tools 2020
  48. Best Hacking Tools 2019
  49. Pentest Tools Windows
  50. Underground Hacker Sites
  51. Hack Apps
  52. Hacker Tools Online
  53. Hacker Tools For Pc
  54. Hack Tools For Pc
  55. Hack And Tools
  56. Physical Pentest Tools
  57. Hacking Tools For Windows Free Download
  58. Hacking Tools And Software
  59. Hack Tools
  60. Hacker Tools For Pc
  61. Black Hat Hacker Tools
  62. Wifi Hacker Tools For Windows
  63. Hacker Tools Apk
  64. Game Hacking
  65. Hacker Tools
  66. Pentest Tools List
  67. Hacker Tools For Ios
  68. Pentest Tools Download
  69. Hacking Tools For Windows
  70. Best Pentesting Tools 2018
  71. Termux Hacking Tools 2019
  72. Growth Hacker Tools
  73. Hacking Apps
  74. Hack Tools
  75. Pentest Tools For Android
  76. Hacker Tools Apk Download
  77. Hacking Tools Windows
  78. Hackers Toolbox
  79. Pentest Tools Bluekeep
  80. Hack Tools Pc
  81. Hacking Tools Software
  82. Hacking Tools
  83. Best Hacking Tools 2019
  84. Hackers Toolbox
  85. Wifi Hacker Tools For Windows
  86. Hacker Tools Online
  87. Hacking Tools Mac
  88. Underground Hacker Sites
  89. Pentest Box Tools Download
  90. Pentest Tools Android
  91. Hack Tool Apk No Root
  92. Hack Tool Apk No Root
  93. Hacks And Tools
  94. Hack Tools
  95. Hack Tools For Games
  96. Best Hacking Tools 2019
  97. Hacking Tools
  98. New Hack Tools
  99. Hacker Security Tools
  100. Hacking Tools For Pc
  101. Growth Hacker Tools
  102. Hak5 Tools
  103. Hacking Tools 2019
  104. Hack Tools
  105. Hacking Apps
  106. Pentest Tools Online
  107. Pentest Tools Review
  108. Hacker Tools For Pc
  109. Hackers Toolbox
  110. Hack App
  111. Hacking Tools For Pc

No comments: