Re shellcode

From dslsdzc/rev-skills · View interactive version

Re shellcode is an AI agent skill from the GitHub repository dslsdzc/rev-skills. Shellcode 分析:提取、解码循环、模拟执行。 触发词:shellcode、shell code、位置无关、解码循环、恶意载荷

How to use this skill

Skill file

Shellcode 分析

何时使用 / 何时不用

工具准备

所有工具先验证再使用。本技能静态解码可免沙箱;模拟执行默认沙箱内进行([[re-analyze/platform-tips]] 最高原则)。

python3 —— 解码/模拟脚本运行环境

unicorn —— 模拟执行主力(pip,Python 3)

capstone —— 反汇编输出(pip,Python 3)

binwalk —— 从宿主文件里扫出嵌入 blob

xxd —— 十六进制查看/特征定位

objdump(binutils)—— 裸二进制反汇编扫描

反汇编器 —— 按 `RE_DECOMPILER` 或环境选

scdbg —— 可选(shellcode 调试器)

msfvenom —— 可选(生成对照样本/编码器特征参考)

操作步骤

按顺序执行,每步产物(blob/解码结果/模拟输出)存档 sha256 + 路径([[re-ioc]] 证据链要求)。

  1. 提取 shellcode blob(文件/内存/流量三来源):

```sh

# 文件内嵌: 从宿主文件里扫嵌入段

binwalk sample.bin

binwalk -e sample.bin # 按签名自动提取

# 定长段提取(已知偏移):

dd if=sample.bin of=blob.bin bs=1 skip=<off> count=<len>

# ELF .text 段导出(载荷伪装成代码段时):

objcopy --dump-section .text=blob.bin sample.elf

# 内存: 沙箱内 gcore 转储后按特征找(见 [[re-memdump]])

# 流量: tshark 提取 pcap 中的载荷字节(见 [[re-netcap]])

tshark -r c2.pcap -Y 'http' -T fields -e http.file_data > payload.hex

xxd -r -p payload.hex blob.bin

```

  1. 定位入口(0xEB 短跳 / 反汇编扫描):

```sh

xxd blob.bin | head -20 # 头部特征

# 入口候选: 首字节 0xEB(jmp short,跳到解码器)、0xE8(call,自定位取 EIP)

objdump -D -b binary -m i386 blob.bin | head -40 # 32 位 x86 裸反汇编

objdump -D -b binary -m i386:x86-64 blob.bin | head -40 # 64 位

```

  1. 解码循环分析(XOR/ROL 自解码):

```sh

# 特征指令(反汇编中找):

# XOR byte ptr [reg+disp], imm 单字节 XOR 解码

# ROL/ROR byte ptr [reg], imm 循环移位解码

# ADD/SUB byte ptr [reg], imm 加减常量解码

# LOOP / JECXZ 循环控制(计数在 ECX/RCX)

```

```python

data = bytearray(open('blob.bin','rb').read())

key, start, length = 0x55, 0x00, len(data) # 参数来自反汇编

for i in range(start, start + length):

data[i] ^= key

open('decoded.bin','wb').write(data)

```

  1. 模拟执行(Unicorn 无环境运行、hook 系统调用):

```python

from unicorn import *

from unicorn.x86_const import *

blob = open('blob.bin','rb').read()

mu = Uc(UC_ARCH_X86, UC_MODE_32) # 按步骤 2 确认的架构选模式

CODE, STACK = 0x1000, 0x3000

mu.mem_map(CODE, 0x1000); mu.mem_map(STACK, 0x1000)

mu.mem_write(CODE, blob)

mu.reg_write(UC_X86_REG_ESP, STACK + 0x800) # 设栈(不设必崩)

def hook_code(uc, addr, size, user):

if addr in API_ADDRS: # 步骤 5 定位的 API 调用点

uc.reg_write(UC_X86_REG_EIP, addr + size) # 跳过 stub

print("hit api", hex(addr))

mu.hook_add(UC_HOOK_CODE, hook_code)

try:

mu.emu_start(CODE, 0x100000) # until 给大上限,非法访问即终止

except UcError as e:

print("stop:", e) # 缺 API/越界 = 预期终止点

open('decoded.bin','wb').write(mu.mem_read(CODE, len(blob))) # 解码产物

```

  1. 与宿主交互面(API 解析:kernel32 遍历、hash 查找):

```python

def djb2(s):

h = 5381

for c in s:

h = ((h << 5) + h + c) & 0xffffffff

return h

for name in ['LoadLibraryA','VirtualAlloc','VirtualProtect',

'WriteProcessMemory','CreateThread','WinExec']:

print(name, hex(djb2(name.encode())))

```

跨域联合

常见坑与陷阱

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.