←
Mark Shust17,383 posts
Mark Shust avatar

Mark Shust

@MarkShust

I explain how PHP actually works under the hood. Architect of the Marko PHP framework (marko.build). 25+ years of shipping code. I simplify the complex.

◷ Joined June 2008▧ 3,679 Photos and videos
Mark Shust avatar
Mark Shust@MarkShust·Oct 22

If you patched PHP yesterday, you're not done yet 🚨 php2shell started with an error every PHP dev has seen a thousand times: "Allowed memory size of 134217728 bytes exhausted" Here's what actually went wrong, and what the update didn't fix 👇

Mark Shust avatar
Mark Shust@MarkShust·1h

php2shell was about the PHP binary. This one's about your uploads folder, and no update fixes it. I'd take a dumb upload form that re-encodes everything over a clever one that trusts the name. What else do you check on uploads? Drop it below, it'll help a lot of people out.

Mark Shust avatar
Mark Shust@MarkShust·1h

- name the file yourself, using the extension from the type you detected, never the one the user sent - store uploads outside the web root, or turn PHP off in that folder - never include or require an uploaded file (if your code does this, we need to talk)

Mark Shust avatar
Mark Shust@MarkShust·1h

- a polyglot passes finfo too (it really is a JPEG), so re-encode every image: $img = imagecreatefromstring(file_get_contents($tmp)); imagejpeg($img, $dest, 90); That rebuilds the pixels and drops the comments where payloads hide (not bulletproof, so keep going)

Mark Shust avatar
Mark Shust@MarkShust·1h

What to check today, in this order: - check the bytes, not the name: $tmp = $_FILES["avatar"]["tmp_name"]; $mime = (new finfo(FILEINFO_MIME_TYPE))->file($tmp); then allowlist a couple of types ("image/jpeg", "image/png") and reject everything else (yes, even "harmless" SVG)

Mark Shust avatar
Mark Shust@MarkShust·1h

Nginx has its own version: With cgi.fix_pathinfo=1 and a loose \.php$ block, /uploads/avatar.jpg/x.php can get handed to PHP-FPM, which runs avatar.jpg as PHP. Add try_files $uri =404; to that block, and keep security.limit_extensions in your FPM pool at .php

Mark Shust avatar
Mark Shust@MarkShust·1h

Why "shell.php.jpg" matters: Older Apache setups map PHP with AddHandler, and mod_mime matches every extension in the name, not just the last one. So that .jpg runs as PHP (and your check happily waved it through). The fix: SetHandler inside a <FilesMatch "\.php$"> block.

Mark Shust avatar
Mark Shust@MarkShust·1h

The name check is weaker than it looks: - "shell.php.jpg" passes, since pathinfo() only returns the LAST extension - "photo.JPG" fails, someone adds strtolower(), and nobody looks again - $_FILES["avatar"]["type"] is set by the browser (so yea, the attacker picks that too)

Mark Shust avatar
Mark Shust@MarkShust·1h

A polyglot is one file that's valid as two formats at once. Take a real JPEG, stuff some PHP into its comment field, and you get a file that opens fine as an image... and also runs as PHP the moment anything executes it. Your extension check never even looks at the bytes.

Mark Shust avatar
Mark Shust@MarkShust·1h

A lot of PHP upload checks look like this: if (pathinfo($name, PATHINFO_EXTENSION) === "jpg") That checks the file's name, not what's inside it, which is the whole trick behind polyglot upload attacks. Here's what goes wrong, and what to check today 👇

Mark Shust avatar
Mark Shust@MarkShust·19h

It’s imperative that moving forward, you have an automatic update process in place for all of your production sites.

🚨 Google has discovered hackers using AI to turn SOFTWARE UPDATES into working cyberattacks, without a human directing the process. The system spots a newly released patch, compares the old and new code, figures out which vulnerability was fixed, then builds and tests an exploit against systems that haven't updated yet. All AUTOMATICALLY. Google says it has observed this happening in real-world operations for the first time. Think about that. A company releases a security fix, and attackers can have AI reverse-engineer it into a weapon before customers even install the update. The race between hackers and defenders is moving from human speed to MACHINE speed. Source: @clashreport / Writer: Oliver

Post attachment
Mark Shust avatar
Mark Shust@MarkShust·20h

Same kind of hidden runtime gotcha: the |> pipe operator and a composer.json line that breaks PHP 8.4 users:

PHP package authors: a single |> in your code can fatal every user on PHP 8.4!! PHP 8.5 shipped the pipe operator and clone with almost a year ago, and one line in your composer.json still decides if you can use them 👇

Mark Shust avatar
Mark Shust@MarkShust·Nov 17

- reload FPM (systemctl reload php8.5-fpm), then rerun the ps command after the next busy day to confirm sizes flattened out - redo your pm.max_children math using the size workers reach right before they're recycled, not a quiet-morning average

Mark Shust avatar
Mark Shust@MarkShust·Nov 17

- if the biggest workers are way bigger than fresh ones, they're growing and need a recycle limit - set pm.max_requests in your pool config (500 is a common starting point), but not something crazy low like 10, or FPM spends its time respawning instead of serving pages