Phpinfo() PHP Magic Quotes Gpc is On

By
Prathap
Published on
14 Aug 2024
6 min read
Vulnerability

The PHP magic_quotes_gpc is a process that automatically escapes all the incoming data to the PHP script. The details about PHP magic_quotes_gpc can be viewed using phpinfo().

If an attacker successfully executes phpinfo(), he will be able to view all the details about the server.

When PHP magic_quotes_gpc feature is enabled, any single quotes, double quotes, backslashes, and NULL characters are automatically prefixed with a backslash (). The purpose is to help prevent common injection attacks, particularly SQL injection, by making it harder for malicious inputs to be executed as code.

So, it is recommended to disable phpinfo() in PHP.

Example

The following code is an example of vulnerable php.ini. Here, magic_quotes_gpc is set as on.

magic_quotes_gpc = on

If server configuration is accessible. Then, it will be available in .htaccess.

php_flag magic_quotes_gpc on

What are the impacts when Phpinfo() PHP magic quotes GPC is on?

When phpinfo() shows that magic_quotes_gpc is enabled (i.e., “magic quotes GPC is on”), it indicates that PHP is automatically escaping certain characters in all incoming GET, POST, and COOKIE data. This setting can have several impacts on your application and its security:

1. Data handling issues

  • Unintended escaping: With magic_quotes_gpc enabled, characters like single quotes (‘), double quotes (“), backslashes (), and NULL characters automatically escaped with a backslash. This can lead to issues when processing data, as the application might store or display data with unintended backslashes, resulting in corrupted data or unexpected behavior.

  • Double escaping: If developers manually escape input data, thinking it hasn’t been automatically escaped, this can lead to double escaping. For example, an input like O’Reilly could end up as O\‘Reilly, causing further complications in data handling and storage.

2. Security risks

  • False sense of security: magic_quotes_gpc was intended to protect against SQL injection by automatically escaping special characters. However, relying on this feature can create a false sense of security. It doesn’t provide comprehensive protection against injection attacks, especially when handling other types of inputs like JSON, XML etc.

  • Outdated practice: Since magic_quotes_gpc has been deprecated and removed in later PHP versions, relying on it indicates that the server is running an outdated version of PHP, which may have other unpatched security vulnerabilities.

3. Compatibility and maintenance challenges

  • Incompatibility with modern code: Modern PHP code and frameworks are developed with the assumption that magic_quotes_gpc is off. If this setting is on, it can cause compatibility issues, especially when integrating with third-party libraries or migrating legacy code to modern frameworks.

  • Increased maintenance: Developers must account for the presence of magic_quotes_gpc in their code, adding conditional checks to handle escaped input correctly. This complicates the codebase and increases maintenance overhead.

4. Security information exposure via phpinfo()

  • Exposing server details: If an attacker can execute phpinfo() and sees that magic_quotes_gpc is on, they can infer that the server is likely running an outdated PHP version. This gives the attacker valuable information to tailor their attacks, potentially exploiting other vulnerabilities that exist in older PHP versions.

  • Attack facilitation: Knowledge of the server configuration, including the status of magic_quotes_gpc, can help attackers craft more effective SQL injection attacks, bypassing the limited protection that this feature provides.

5. Users experience issues

  • Data display problems: Users might see unnecessary backslashes in their input data, which can be confusing and lead to poor user experience. For instance, a comment like O’Reilly might appear as O'Reilly in a forum post, detracting from the professional appearance of the application.

How can you prevent attacks when Phpinfo() PHP magic quotes gpc is on?

If phpinfo() shows that magic_quotes_gpc is enabled, it’s essential to understand that this feature is outdated and deprecated.

However, if you find yourself in a situation where magic_quotes_gpc is active, you should take steps to mitigate potential security risks and vulnerabilities. Here’s how you can prevent attacks and secure your application:

1. Upgrade PHP

  • Update to a newer version: The most effective solution is to upgrade PHP to a version where magic_quotes_gpc is no longer an issue. Upgrading ensures that your server is running a modern, supported version with better security features.

2. Disable magic_quotes_gpc

  • Edit php.ini configuration:

Locate your php.ini file, which is the main configuration file for PHP.

Find the magic_quotes_gpc directive and set it to Off:

magic_quotes_gpc = Off
  • Restart your web server to apply the changes.

3. Use proper input validation and sanitization

  • Validate and sanitize inputs: Regardless of magic_quotes_gpc, always validate and sanitize all user inputs. Use functions like filter_var(), htmlspecialchars(), or library-specific sanitization methods to clean input data.

  • Prepared statements: Use prepared statements or parameterized queries for database interactions. This practice effectively prevents SQL injection attacks by separating SQL logic from data.

4. Secure configuration practices

  • Restrict phpinfo() execution: Ensure that phpinfo() is not accessible in production environments. You can do this by:

  • Removing or commenting out any code that calls phpinfo().

  • Restricting access to development or debugging environments where phpinfo() is necessary.

5. Monitor and log security events

  • Logging: Implement robust logging to monitor suspicious activities. Ensure that any errors or anomalies related to data handling are logged and reviewed regularly.

  • Regular security audits: Conduct regular security audits and code reviews to identify and address potential vulnerabilities.

6. Implement Web Application Firewalls (WAF)

  • Deploy a WAF: Use a Web Application Firewall to provide an additional layer of security. A WAF can help detect and block malicious traffic, including SQL injection attempts and other common attacks.

Wrapping up

While magic_quotes_gpc is no longer a recommended security practice, taking the above steps will help mitigate risks associated with its use. Upgrading PHP and adopting modern security practices are crucial for maintaining a secure application environment.

Regular updates, proper input handling, and vigilant security practices are key to protecting your application from potential attacks.

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
Prathap
Prathap
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.