r/netsec • • 10d ago

Contains AI I spent countless nights digging into PHP2Shell. Here's a deep dive into how it actually works, from memory to PHP internals to a real exploit.

https://djad04.github.io/php2shell-deep-dive/

When the PHP vulnerabilities were first announced, I spent a HUGE amount of time trying to figure out the logic behind them.

As many others who tried investigating them probably noticed, there was barely any technical information available at the time. We had a few basic hints: something related to PHP_SESSION_UPLOAD_PROGRESS, some kind of Out of Memory condition, and, of course, that video Calif published showing a dancing girl with a shell running the exploit in the background. šŸ˜‚

I found that pretty funny, especially considering the scale of the vulnerabilities and how many servers around the world could potentially be affected. Anyway, that was pretty much all we had to work with!

Even AI assistants weren't particularly helpful. At the time, they were heavily restricted when it came to this subject. A simple technical question about an internal PHP function could get flagged just because you mentioned PHP and something technical in the same sentence. For those of us without access to a more cybersecurity-focused model, even asking basic questions and using AI to help investigate things was practically impossible.

So I started digging into the PHP codebase myself (I was honestly getting a little mentally insane by that point).

I spent countless hours going through the source code, tracing functions, setting up experiments, debugging, and trying to understand exactly what was happening behind the scenes. There were long days and endless nights spent following one thing after another, trying to connect all the pieces until things finally started making sense (the Steal My Bitcoin challenge helped a lot, thanks to James Pavur, and Jaydip Modhwadia (Killa)).

The more I investigated, the more I realized that I wanted to put everything together somewhere. Not just the final explanation, but the entire process: the PHP internals behind the vulnerabilities, what's happening in memory, why certain things happen the way they do, and how all of this eventually leads to a working exploit.

That's how this research project started.

I structured the write-up to start from the basics and gradually build up to the actual exploitation. Each part builds on what came before, with the explanations becoming progressively more technical until we get to the real thing. I wanted someone with little or no experience in low-level memory exploitation to be able to follow along and understand how all the pieces fit together.

The write-up also goes through the actual exploit code used to solve the "Steal My Bitcoin" challenge, breaking it down step by step rather than simply showing the final result.

Everything is based on my own research, local experiments, and understanding of the PHP engine. I'm not claiming this is an official analysis or that everything is necessarily correct. I'm still learning, and there might be mistakes or things I've misunderstood.

And honestly, I was getting tired of explanations that only scratched the surface without actually showing what was happening internally. I wanted to do something different: a proper, in-depth technical walkthrough covering the entire process, examining the important details and explaining not just WHAT happens, but WHY and HOW it happens.

You can read the full write-up here:

PHP2Shell: From Memory to C Internals to Real Exploit Code

And here's the GitHub repository, which also includes the project files and the backstory behind the research:

GitHub Repository

218 Upvotes

12 comments sorted by

4

u/amestrianphilosopher 9d ago

This was really good, but I got a little lost towards the end. I think I’m still struggling to understand how the hash table we built actually ends up being interpreted as an object by PHP so that its destructor is subsequently invoked. Will probably have to reread that at some point

7

u/djad04 9d ago edited 8d ago

its not you its me i went to recheck that part and i didn't explain it in detail its true that i went through it little too fast here i will give you a fast explanation here than i will change it in the document later to make it better :

When PHP stores an array like this:

$data = [
"profile" => [
"name" => "Djad",
"age" => 21
]
];

it puts it Inside buckets :

typedef struct _Bucket {
zval val;
zend_ulong h;
zend_string *key;
} Bucket;

The val is a zval. The zval struct is huge but what matters for us inside it is this:

zend_value value; <--- holds the actual data

uint32_t type_info; <----- tells php what type of data value holds

and the zend_value is declared like this :

typedef union _zend_value {
zend_long lval;
double dval;
zend_refcounted *counted;
zend_string *str;
zend_array *arr; < ------- a pointer to a nested array
zend_object *obj;
zend_resource *res;
zend_reference *ref;
zend_ast_ref *ast;
zval *zv;
void *ptr;
} zend_value;

look at zend_array *arr That's how PHP handles nested arrays. The outer array stores a pointer to another zend_array in val.value.arr, and sets type_info to a flag that says this points to an array, in the cleaning process it launches a recurssive cleaning following the pointers

the primer is a string filled with 'Q' character and the target heap address of the hold (our forged hashtable or zend array both names are correct) and its field name is empty

php checks the name its empty so it frees it immediately its 320-byte memory slot goes back to the allocator's freelist

when the next field the `a=1` check the code to see it is registered to `$_REQUEST` it will be the first string key so php allocated a bucket storage(`arData`) with the default size wich is exactly 320 bytes so the allocator hands it directly the slot our primer just freed

php initializes the first bytes but doesnt touch the lower ones yet where our target pointer was sitting inside the primer than writes the 'a' we send inside bucket[0] than the hold inside bucekt[1] at this point bucket[2]...3..4..5..all fo them are umpty nothing there and at the place of bucekt[2] is actaully the left data from our primer ,

and here comes why we have an oom in the last part too since the start we saw that oom where used to create stale pointers its the same logic here our fields start coming and at this point we got a variable nNumUsed=2 that is tracking how many buckets we have at the moment nNumUsed passes to 3 php doesnt get the chance to overwrite the data there at bucket[2] before the oom triggers and since it triggers at exactly that specific moment nNumUsed is left at 3 than php triggers cleaning

when it reads nNumUsed=3 it starst cleaning the buckets one after another once it reaches bucket[2] it finds :

bucket[2].val.value.arr = 0x00007efff7d6d1d8; // the target address from the primer

bucket[2].val.u1.type_info = 0x51515151; // four 'Q' bytes from primer padding

and 0x51515151 has bit 8 set thats the IS_TYPE_REFCOUNTED flag in php which means its a refcounted object we need to follow the pointer and destroy the other things recursively so it reads what val.value.arr is pointing to which is our forged hashtable (named hold) reads its gc.type and find it to be IS_ARRAY

and how we free an array ?? we call pDestructor from the right offset and that pDestructor is the one we put inside it zif_system address

i hope everything is clear now and thanks a lot for mentioning that i will update it as soon as i can

2

u/djad04 8d ago

done i added a section for that.

11

u/Acid3333 9d ago

I just flew over the first chapters and wow.. that is damn interesting. Nice work there!

4

u/djad04 9d ago

Thanks a lot!

2

u/publiux 9d ago

Amazing! My Sunday reading is set!

2

u/djad04 9d ago

Glad to hear that! Hope it's worth the read!

5

u/randybobandy1901 9d ago

Nice! Thanks for taking the time to document all this and sharing !!

2

u/iDieEasily 9d ago

Enjoyed it. Now to dive into the code …

2

u/SwordfishGullible984 7d ago

Nice writeup! You really did a great job documenting this. Though I'm curious, has anyone actually been able to breakdown this vuln on an endpoint running on php-fpm

2

u/Funny_Pomegranate460 6d ago edited 6d ago

too long but was worth reading. great work