From Sqli To Root
A SQL injection in a shop's category parameter allowed extraction of admin credentials, leading to CMS access. The attacker then uploaded a PHP webshell, gained a reverse shell, and escalated to root via a local kernel exploit.
medium.com
Published on medium.com
So, days ago a friend told me about a strange error in a website because he accidentally put the single quote. So I immediately went to check the “problem”.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
The target was a shop application running under /shop/ on a host the author refers to as website.redacted. The feature in question is the catalogue page, which takes a categoryID and a subCategoryID from the query string and uses them to select which products to display. The URL the author examined was:
https://website.redacted/shop/index.php?action=showCatalogue&categoryID=212&subCategoryID=325
A friend had triggered a strange error on the site by accidentally entering a single quote, which is what drew the author's attention to the page.
Setup
The tools used across the engagement were:
- A browser, for manual testing of the parameter.
- SQLMap, to automate extraction once the injection was confirmed.
- hashkiller, a public hash cracking website, for the MD5 hashes recovered from the database.
- Metasploit, for a web delivery with a python payload. The author also mentions trying Empire and PoshC2 successfully.
wgetandgccon the compromised host, to download and compile a local privilege escalation exploit.- SSH, to log in with the account created by the exploit.
No credentials, versions of SQLMap or Metasploit, or account details on the shop are given.
Finding it
Testing the subCategoryID parameter with a single quote produced a MySQL error. The error message also printed the full query, which the author notes made exploitation easier.

The screenshot shows the MySQL error returned by the page after the quote was supplied.
To confirm the parameter was injectable, the author compared responses arithmetically: requesting subCategoryID=325 and subCategoryID=326-1 returned the same response, and the same held for other pairs such as 315 and 325-10. Matching responses for a value and its arithmetic equivalent indicate the value is being evaluated by the database rather than treated as a plain string, which is the signature of an error based SQL injection.
Exploitation
Step 1: Extract the admin credentials with SQLMap
With the injection confirmed, the author ran SQLMap against the parameter and recovered the admin data. The passwords were stored as MD5 hashes, and one of them was cracked easily using the hashkiller website.

The screenshot shows SQLMap confirming the injection.

The screenshot shows the author logged in as Admin to the CMS.
Step 2: Upload a PHP webshell through the product image upload
Logging in from the /admin page gave access to the CMS. Browsing the CMS, the author focused on the List Product page, where an admin can edit product information and images. Replacing a product image with a PHP webshell succeeded: the upload page did not filter the php5 extension. The webshell used was c99, edited by locus7s.

The screenshot shows the uploaded webshell.
While exploring the filesystem through the webshell, the author found that someone, possibly the owner, had backdoored the payment form with a fake PayPal form that was saving credit card numbers and customer information. The author deleted it immediately. Further progress was limited because the user account in use had very few permissions.
Step 3: Obtain a Meterpreter shell
To get a better foothold, the author used Metasploit with a web delivery and a python payload. The server was running Python 2.6 and the user could execute it, so the Meterpreter shell came back.

The screenshot shows the Meterpreter session.
Step 4: Local privilege escalation with DirtyCow
Running uname showed the server was CentOS 6.6 with Linux kernel 2.6.32. The author searched for a local privilege escalation exploit and chose the DirtyCow exploit published as exploit-db 40839. The code was downloaded with wget to a path writable by the user, and gcc was found installed and usable. After compiling, the exploit ran and completed successfully.

The screenshot shows the exploit downloaded and compiled, and the output after running it.
The author set the user as firefart and the password as imcool2. The author closed the Meterpreter shell and logged in over SSH with that account. Running id showed root privileges and permissions.

The screenshot shows the SSH session logged in as firefart with root privileges.
Step 5: Plesk access from the message of the day
On login, the SSH server returned a message of the day listing commands, one of which was a URL token to log in to the Plesk dashboard. With that, the author had total control of the server.


The screenshots show the message of the day after login and the URL token for the Plesk dashboard.
Root cause
The catalogue page produced a MySQL error when a single quote was supplied in subCategoryID, and the error printed the full query. The report does not describe the code itself. The error was displayed to the user and included the full query, which is a second weakness: it confirms the injection point and reveals the query structure. The report does not describe the code itself, so the exact construction is not shown.
The chain continued because of two further weaknesses. The CMS image upload accepted a php5 file, so an authenticated admin could place executable code in a web accessible location. The host ran an old kernel (2.6.32 on CentOS 6.6) that was vulnerable to DirtyCow, and the web user had enough access to download, compile and run the exploit, with gcc and Python 2.6 available.
Impact
An unauthenticated attacker could inject SQL into the catalogue page and extract admin data from the shop database. With those credentials, the attacker could log in to the CMS and upload a PHP webshell, gaining code execution as the web user. From there, local privilege escalation to root was possible on this host, and the message of the day exposed a token for the Plesk dashboard, giving control of the server. The author also found a pre-existing fake PayPal form on the payment page that was capturing card numbers and customer information, which shows the payment flow had already been compromised by someone else.
Fix
Based on the weaknesses shown, the fixes would be:
- Use parameterised queries or prepared statements for
categoryIDandsubCategoryID, so user input is never concatenated into SQL. - Disable detailed database error output in production and log errors server side instead.
- Restrict uploads by extension and content type, and store uploaded files outside the web root or serve them through a handler that does not execute PHP.
- Patch the kernel and keep the host on a supported release, since DirtyCow is fixed in later kernels.
- Run the web service under a low privileged account and remove compilers and interpreters that are not needed on production hosts.
- Audit the payment form and any other pages for injected content, and review the Plesk token exposure in the message of the day.
Variants to test
- Numeric parameters in catalogue and listing pages: send a value and its arithmetic equivalent (for example
325and326-1) and compare responses. Identical output suggests the value is evaluated by the database. - Error based injection on any parameter that produces a database error: a single quote is enough to see whether the error is reflected and whether the full query is printed.
- CMS image upload fields: try a PHP file with a
php5extension and check whether it is stored and reachable. A positive result is a file that executes when requested. - Host fingerprinting after code execution: run
unameand check the kernel and distribution against known local privilege escalation exploits. A positive result is a working exploit that creates a privileged account or returns a root shell. - SSH message of the day: after any login, read the MOTD for tokens, internal URLs or credentials. A positive result is a usable token for an administrative panel such as Plesk.
What happened, in plain words
A friend of the author typed a single apostrophe character (a ', called a single quote) into a shop website and got back a strange error message. That small accident turned out to be the thread that, when pulled, unravelled the whole server: the author ended up extracting admin data from the shop's database, logging in as its administrator, planting a small program that let them run commands on the machine, upgrading their own permissions until they were the most powerful user on the system (called root), and finally finding a login token for the server's control panel.
This article walks through that chain step by step, in the order it happened, explaining each idea from scratch. You only need to know how to use a browser to follow along.
The target and the feature
The target was a shop application, a website that sells products, running under a folder called /shop/ on a host the author refers to as website.redacted. The specific feature is the catalogue page: the part of the shop that lists products in a category. It reads two values from the address bar, categoryID and subCategoryID, and uses them to decide which products to show.
Those values arrive in what is called the query string: the part of a URL after the ?, made of name=value pairs joined by &. When you browse a shop and click a category, the site often builds a URL like this one. The URL the author examined was:
https://website.redacted/shop/index.php?action=showCatalogue&categoryID=212&subCategoryID=325
Reading it piece by piece:
https://website.redactedis the server's address./shop/index.phpis the page being requested.index.phpis a program written in PHP, a language commonly used for websites.?action=showCataloguetells the page which action to perform: show the catalogue.categoryID=212selects a top-level category.subCategoryID=325selects a sub-category inside it.
The page takes those two numbers and looks up matching products in a database. A database is an organised store of data, like a very large, very strict spreadsheet, and the shop's products, users and passwords live in it. The page talks to the database using a language called SQL (Structured Query Language), which is how you ask a database questions like "give me every product in category 325".
Finding it
A friend had triggered a strange error on the site by accidentally entering a single quote. That is what drew the author's attention to the page. The author went to check the "problem" by loading the catalogue URL above and testing the subCategoryID value.
Testing the subCategoryID parameter with a single quote produced a MySQL error. MySQL is one of the most popular database systems, and the shop was using it. The error message also printed the full query, which the author notes made exploitation easier: the page was not only complaining, it was showing the exact SQL sentence it had tried to run.

The screenshot shows the MySQL error returned by the page after the quote was supplied.
Why does a quote cause an error? In SQL, text values are wrapped in single quotes, like 'shoes'. If a program builds a query by gluing your input straight into that sentence, and your input itself contains a quote, the quote closes the text early and the rest of your input is read as SQL code rather than as text. The database then sees a malformed sentence and complains. That complaint is the first sign of a vulnerability class called SQL injection: the ability to slip your own SQL into someone else's query.
To confirm the parameter was injectable, the author compared responses arithmetically: requesting subCategoryID=325 and subCategoryID=326-1 returned the same response, and the same held for other pairs such as 315 and 325-10.
Why does that matter? 326-1 equals 325, and 325-10 equals 315. If the page treated your input as plain text, 326-1 would be a meaningless string and would not match anything. If the page hands the value to the database, the database does the maths and gets 325, so the page shows the same products. Identical output for a value and its arithmetic equivalent is the signature of an error based SQL injection: an injection where the database's own error messages are used to learn about and extract data.
Exploitation
Step 1: Extract the admin credentials with SQLMap
With the injection confirmed, the author ran SQLMap against the parameter and recovered the admin data. SQLMap is a free tool that automates SQL injection: instead of typing payloads by hand, you point it at a URL and it works out how to extract data. The author describes themselves as "a lamer" for using it, which is just self-deprecating humour; automating a confirmed finding is normal practice.
The passwords were stored as MD5 hashes, and one of them was cracked easily using the hashkiller website. A hash is a one-way fingerprint of a value: a program turns a password into a fixed-length string of characters, and you cannot simply reverse it. MD5 is an old hashing algorithm, and because it is fast, common passwords can be guessed by hashing candidate words and comparing the results. Hashkiller is a public website that does exactly that lookup for you.

The screenshot shows SQLMap confirming the injection.

The screenshot shows the author logged in as Admin to the CMS.
Step 2: Upload a PHP webshell through the product image upload
Logging in from the /admin page gave access to the CMS. A CMS, or content management system, is the back-office interface where staff edit a site's content: products, pages, images. Browsing the CMS, the author focused on the List Product page, where an admin can edit product information and images.
Replacing a product image with a PHP webshell succeeded: the upload page did not filter the php5 extension. A webshell is a small program uploaded to a server that lets an attacker run commands through the browser, like a remote control for the machine. The trick works because the upload accepted a file ending in .php5, which the web server treats as a program to execute rather than as a picture to display. The webshell used was c99, edited by locus7s.

The screenshot shows the uploaded webshell.
While exploring the filesystem through the webshell, the author found that someone, possibly the owner, had backdoored the payment form with a fake PayPal form that was saving credit card numbers and customer information. A backdoor here means code secretly placed on the site to capture what customers type. The author deleted it immediately. Further progress was limited because the user account in use had very few permissions: the webshell ran as a low-privilege user, so it could only touch a small part of the machine.
Step 3: Obtain a Meterpreter shell
To get a better foothold, the author used Metasploit with a web delivery and a python payload. Metasploit is a well-known framework for developing and running exploits. A payload is the piece of code that runs on the target once an exploit succeeds; here it was written in Python, a popular programming language. Web delivery means the target is tricked into downloading and running that payload over the web.
The server was running Python 2.6 and the user could execute it, so the Meterpreter shell came back. Meterpreter is Metasploit's interactive shell: once it connects back to the attacker, it offers commands for exploring and controlling the machine. The author also mentions trying Empire and PoshC2 successfully, two other tools for this kind of remote control.

The screenshot shows the Meterpreter session.
Step 4: Local privilege escalation with DirtyCow
Running uname showed the server was CentOS 6.6 with Linux kernel 2.6.32. uname is a standard command that prints information about the operating system and its kernel, the core of the system. CentOS is a Linux distribution, and 6.6 is its version; 2.6.32 is the kernel version.
The author searched for a local privilege escalation exploit and chose the DirtyCow exploit published as exploit-db 40839. Local privilege escalation means going from a limited user to a more powerful one on the same machine. DirtyCow is a well-known Linux kernel bug that lets a normal user overwrite files they should not be able to touch, and exploit-db is a public archive of exploit code.
The code was downloaded with wget to a path writable by the user, and gcc was found installed and usable. wget is a command-line tool for downloading files from the web, and gcc is a compiler, a program that turns source code into a runnable program. After compiling, the exploit ran and completed successfully.

The screenshot shows the exploit downloaded and compiled, and the output after running it.
The author set the user as firefart and the password as imcool2. The author closed the Meterpreter shell and logged in over SSH with that account. SSH is a protocol for logging in to another computer's command line securely over a network. Running id showed root privileges and permissions. id prints which user you are and which groups you belong to; root is the all-powerful administrator account on Linux.

The screenshot shows the SSH session logged in as firefart with root privileges.
Step 5: Plesk access from the message of the day
On login, the SSH server returned a message of the day listing commands, one of which was a URL token to log in to the Plesk dashboard. The message of the day (MOTD) is a short banner shown after you log in, often with notices from the administrator. Plesk is a control panel for managing a web server: websites, databases, email and more. A URL token is a secret string embedded in a link that grants access without a separate password, like a key hidden inside a web address. With that, the author had total control of the server.


The screenshots show the message of the day after login and the URL token for the Plesk dashboard.
Root cause
The catalogue page produced a MySQL error when a single quote was supplied in subCategoryID, and the error printed the full query. The report does not describe the code itself. Sanitisation means cleaning input before using it; parameterisation means handing input to the database separately from the query text, so it can never be read as code. The error was displayed to the user and included the full query, which is a second weakness: it confirms the injection point and reveals the query structure. The report does not describe the code itself, so the exact construction is not shown.
The chain continued because of two further weaknesses. The CMS image upload accepted a php5 file, so an authenticated admin could place executable code in a web accessible location. The host ran an old kernel (2.6.32 on CentOS 6.6) that was vulnerable to DirtyCow, and the web user had enough access to download, compile and run the exploit, with gcc and Python 2.6 available.
Impact
An unauthenticated attacker could inject SQL into the catalogue page and extract admin data from the shop database. With those credentials, the attacker could log in to the CMS and upload a PHP webshell, gaining code execution as the web user. From there, local privilege escalation to root was possible on this host, and the message of the day exposed a token for the Plesk dashboard, giving control of the server. The author also found a pre-existing fake PayPal form on the payment page that was capturing card numbers and customer information, which shows the payment flow had already been compromised by someone else.
Fix
Based on the weaknesses shown, the fixes would be:
- Use parameterised queries or prepared statements for
categoryIDandsubCategoryID, so user input is never concatenated into SQL. - Disable detailed database error output in production and log errors server side instead.
- Restrict uploads by extension and content type, and store uploaded files outside the web root or serve them through a handler that does not execute PHP.
- Patch the kernel and keep the host on a supported release, since DirtyCow is fixed in later kernels.
- Run the web service under a low privileged account and remove compilers and interpreters that are not needed on production hosts.
- Audit the payment form and any other pages for injected content, and review the Plesk token exposure in the message of the day.
Variants to test
- Numeric parameters in catalogue and listing pages: send a value and its arithmetic equivalent (for example
325and326-1) and compare responses. Identical output suggests the value is evaluated by the database. - Error based injection on any parameter that produces a database error: a single quote is enough to see whether the error is reflected and whether the full query is printed.
- CMS image upload fields: try a PHP file with a
php5extension and check whether it is stored and reachable. A positive result is a file that executes when requested. - Host fingerprinting after code execution: run
unameand check the kernel and distribution against known local privilege escalation exploits. A positive result is a working exploit that creates a privileged account or returns a root shell. - SSH message of the day: after any login, read the MOTD for tokens, internal URLs or credentials. A positive result is a usable token for an administrative panel such as Plesk.
Glossary
- Backdoor: code secretly placed on a system to give someone unauthorised access or to capture data.
- CMS (content management system): the back-office interface where staff edit a website's content.
- Compiler: a program that turns source code into a runnable program;
gccis one. - Database: an organised store of data that programs query, like a very strict spreadsheet.
- DirtyCow: a Linux kernel bug that lets a normal user overwrite files they should not be able to touch.
- exploit-db: a public archive of exploit code.
- Hash: a one-way fingerprint of a value; MD5 is an old, fast hashing algorithm.
- Hashkiller: a public website that looks up hashes against known values.
- Kernel: the core of an operating system, which manages hardware and processes.
- Local privilege escalation: going from a limited user to a more powerful one on the same machine.
- Message of the day (MOTD): a short banner shown after you log in, often with administrator notices.
- Meterpreter: Metasploit's interactive shell for controlling a compromised machine.
- Metasploit: a framework for developing and running exploits.
- MySQL: a popular database system.
- Parameter: a named value passed in a URL's query string, such as
subCategoryID=325. - Parameterisation: handing input to a database separately from the query text so it can never be read as code.
- Payload: the piece of code that runs on a target once an exploit succeeds.
- PHP: a programming language commonly used for websites.
- Plesk: a control panel for managing a web server.
- Query string: the part of a URL after the
?, made ofname=valuepairs joined by&. - Root: the all-powerful administrator account on Linux.
- Sanitisation: cleaning input before using it.
- SQL (Structured Query Language): the language used to ask a database questions.
- SQL injection: slipping your own SQL into someone else's query through unsanitised input.
- SQLMap: a tool that automates SQL injection.
- SSH: a protocol for logging in to another computer's command line securely over a network.
- URL token: a secret string embedded in a link that grants access without a separate password.
- Webshell: a small program uploaded to a server that lets an attacker run commands through the browser.
wget: a command-line tool for downloading files from the web.