Skip to content

Instantly share code, notes, and snippets.

@pirtleshell
Created August 10, 2020 01:08
Show Gist options
  • Save pirtleshell/571aaf7bc48418d678fb0d7b2fd8d210 to your computer and use it in GitHub Desktop.
Save pirtleshell/571aaf7bc48418d678fb0d7b2fd8d210 to your computer and use it in GitHub Desktop.
Dynamic webpage generation with Apache & PHP from a binary
<IfModule mod_rewrite.c>
RewriteEngine on
# redirect all non-existent requests to index.php
RewriteCond %{REQUEST_FILENAME} !-s [OR]
RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [NC,L,QSA]
</IfModule>

what

This is a simple proof of concept for generating a webpage in apache from any executable binary.

how

The binary creates a webpage, supposedly by running the bin with the arg that is the request path. It's run and the output is returned from PHP.

  • A user requests a path, the .htaccess redirects to index.php with the path as a query param.
  • index.php grabs the path, runs the binary (generate-site.sh) with the path as an argument.
  • the binary builds some dynamic html and PHP spits it back as the response.

why

idk. kinda cool i guess. obviously there's way more overhead than necessary, but most shared hosting servers run apache and this lets you have some simple script-generated webpages!

#!/bin/bash
REQUEST_URI=$1
cat <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Testing!</title>
</head>
<body>
<h1>Hello there!</h1>
<p>I was generated from a bash binary.</p>
<p>You requested <code>$REQUEST_URI</code></p>
</body>
</html>
EOF
<?php
$content = [];
// run the binary with the request URI as an argument
// just be warned that there's probably someway to do bash injection to this... use with caution XD
exec('./generate-content.sh \'' . $_SERVER['REQUEST_URI'] . '\'', $content);
// print and return the binary output
print(implode('', $content));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment