PHP code injection

By
Rejah Rehim
Published on
19 Aug 2024
4 min read
Vulnerability

A PHP object injection is a vulnerability at the application level that allows attackers to carry out malicious actions, including SQL injection, path traversal, code injection, and denial of service (DoS) attacks.

The three primary types of PHP injection attacks include:

  • PHP object injection, where attackers deliver malicious input to the PHP unserialize function, leading to its execution on the server.

  • Remote Code Execution (RCE), where attackers upload files containing harmful PHP scripts to the server.

  • SQL injection, where attackers exploit vulnerable PHP code that interacts with a back-end database, bypassing input sanitization.

Example

When a developer uses the PHP eval() function, an attacker has the potential to modify and inject code into the application.

       //The URL to access this https://www.example.beaglesecurity.com/index.php?arg=1 
        $myvar = "varname"; 
        $x = $_GET['arg']; 
        eval("\$myvar = \$x;"); 
        Run on IDE

In this web application, there is no input validation.

https://www.example.beaglesecurity.com/index.php?arg=1; phpinfo()

The above URL will show all the info about the PHP version.

An attacker can execute system commands by requesting the below URL.

https://www.example.beaglesecurity.com/index.php?arg=1; system(‘id’)

The above URL will give the users all the processes running in the server.

uid=33(www-data) gid=33(www-data) groups=33(www-data)

What are the impacts of PHP code injection?

The impacts of PHP injection can be severe and include:

1. Unauthorized access

Attackers can gain access to sensitive data, including user information, credentials, and system files. This may cause damage to the entire organization and reputation of the company.

2. Data manipulation

They may alter or delete critical data in the database, leading to data integrity issues or loss.

3. Remote Code Execution (RCE)

Attackers can run arbitrary code on the server, potentially taking full control of the system. This allows the attacker to gain unauthorized access to the server, alter or delete files. It can also install malicious software like backdoors, keyloggers, or botnets.

4. Privilege escalation

Exploiting vulnerabilities might enable attackers to escalate their privileges and perform unauthorized actions.

5. Defacement or malware injection

Websites could be defaced, or malicious code could be injected to infect users or propagate further attacks.

How can you prevent PHP code injection?

To prevent PHP code injection attacks, follow these security best practices:

1. Input validation and sanitization

Ensure all user inputs are strictly validated and sanitized. Use functions like filter_input() to filter and validate data before processing.

2. Avoid unserialize() on untrusted data

Avoid using unserialize() on untrusted user input. If necessary, consider safer alternatives like json_decode().

3. Use prepared statements

For database queries, use prepared statements with parameterized queries to prevent SQL injection vulnerabilities.

4. Disable dangerous functions

Disable potentially risky PHP functions such as eval(), exec(), system (), passthru(), and shell_exec() unless necessary.

5. File upload restrictions

When allowing file uploads, restrict file types and validate content. Ensure that the uploaded files are not directly executable and are stored in secure directories.

6. Use security libraries

Employ PHP security libraries and frameworks that have built-in protection mechanisms, such as Laravel or Symfony, which offer protection against code injection attacks.

7. Keep PHP and software updated

Regularly update PHP, its dependencies, and any associated software to mitigate risks from known vulnerabilities.

Wrapping up

PHP code injection is a serious security risk that can have far-reaching consequences, from compromising server integrity to exposing sensitive data. It highlights the importance of secure coding practices in web development, where validating user inputs, avoiding unsafe functions, and keeping software up to date are essential steps to mitigate potential threats.

By prioritizing these proactive measures, developers can significantly reduce the chances of exploitation, ensuring a more secure and reliable environment for both their applications and users.

Automated human-like penetration testing for your web apps & APIs
Teams using Beagle Security are set up in minutes, embrace release-based CI/CD security testing and save up to 65% with timely remediation of vulnerabilities. Sign up for a free account to see what it can do for you.

Written by
Rejah Rehim
Rejah Rehim
Co-founder, Director
Experience the Beagle Security platform
Unlock one full penetration test and all Advanced plan features free for 10 days
Find surface-level website security issues in under a minute
Free website security assessment
Experience the power of automated penetration testing & contextual reporting.