0xma Cyber Security Articles




February 15, 2024

Newline Injection

In this tutorial, we will see how to perform a newline injection attack. The website offers a game that records the number of times you click on the page. However, the PHP function that saves the user score is vulnerable to newline injection attack.

This is the main page of the web application.

Main page of the web application.

Let's register a new user.

User Registration.

Log in as the newly created user.

Logging into the web application.

After logging in, the page provides three options: view the profile page, logout, and play the game.

Main page after logging in.

The profile page shows the user score.

Profile page of the user.

This is the game page. Clicking on the white circular area increases the number of clicks.

Game page.

I clicked on the circular area five times, so the score shows 5. It gives us the option to save the game and then exit.

Playing the game.

After exiting, it says that the game is saved.

Game saved.

The profile page shows the updated score.

Profile page with the updated score.

This is the request that is passed to the web application when we save the game. It passes two parameters: clicks and level.

HTTP request that saves the game.

The PHP code for the site includes a check to see if the user is trying to set the 'role' parameter.

PHP function that checks if the user is admin.

This is the function that takes the parameters and their values from the URL.

PHP function that parses the URL.

After passing the values for 'clicks' and 'level', this is how the update statement is supposed to look like.

SQL update statement.

We can add a new parameter called 'role' and set its value to 'Admin'. Note that after 'role', we are adding the newline character '%0a' and then we are assigning 'Admin' to it.

Adding the 'role' parameter to the HTTP request.

After adding the newline character, this is how the new update statement looks like. And since SQL allows whitespace and it is a recommended best practice to use whitespace to increase readability, the check "strtolower($key) === 'role'" won't return true.

The new update statement with the newline injection.

Now that we have logged in with the 'Admin' role, we get a new link to 'Administration'.

A new link to the Administration page after newline injection attack.

The 'Administration' page shows a list of all the top players.

List of top players.

If you liked reading this article, you can follow me on Twitter: mujtabareads.