repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

opencode/skills/binary-analysis-patterns/SKILL.md

Raw

name: binary-analysis-patterns description: Analyze compiled binaries — disassembly, control flow graphs, data structure reconstruction, ELF/PE/Mach-O formats. Use when reverse engineering executables, analyzing malware, or understanding compiler output (objdump, Ghidra, radare2). license: MIT

Analyze compiled binaries by recognizing patterns in assembly code.


Calling Conventions

x86-64 System V (Linux/macOS)

Register Purpose
RDI 1st arg
RSI 2nd arg
RDX 3rd arg
RCX 4th arg
R8 5th arg
R9 6th arg
RAX Return value

x86-64 Microsoft (Windows)

Register Purpose
RCX 1st arg
RDX 2nd arg
R8 3rd arg
R9 4th arg
RAX Return value

ARM64

Register Purpose
X0-X7 Arguments
X0 Return value
X29 Frame pointer
X30 Link register

Function Patterns

Prologue/Epilogue

; x86-64
push rbp
mov rbp, rsp
sub rsp, 0x20      ; Allocate locals
; ...
leave               ; mov rsp, rbp; pop rbp
ret

; ARM64
stp x29, x30, [sp, #-16]!
mov x29, sp
; ...
ldp x29, x30, [sp], #16
ret

Leaf Function (no calls)

; May skip frame pointer
sub rsp, 0x18
; ...
add rsp, 0x18
ret

Control Flow

Conditionals

; if (a == b)
cmp eax, ebx
jne skip
; body
skip:

; if (a < b) signed
cmp eax, ebx
jge skip

; if (a < b) unsigned
cmp eax, ebx
jae skip

Loops

; for (i = 0; i < n; i++)
xor ecx, ecx
cmp ecx, [n]
jge loop_end
; body
inc ecx
jmp loop_end

Switch (Jump Table)

mov eax, [var]
cmp eax, max_case
ja default
jmp [jump_table + eax*8]

Data Structures

Array Access

; array[i] - 4-byte elements
mov eax, [rbx + rcx*4]

; array[i][j]
imul eax, [cols]
add eax, [j]
mov edx, [rbx + rax*4]

Structure Access

struct Example {
    int a;      // 0
    char b;     // 4
    long c;     // 8
};
mov eax, [rdi]         ; a (offset 0)
movzx eax, byte [rdi+4]; b (offset 4)
mov rax, [rdi+8]      ; c (offset 8)

Linked List

test rdi, rdi          ; node == NULL?
jz done
; process node
mov rdi, [rdi+8]       ; node = node->next
jmp loop

Common Patterns

String Operations

; strlen
xor ecx, ecx
cmp byte [rdi + rcx], 0
je done
inc ecx
jmp strlen
; ecx = length

Bit Manipulation

test eax, 0x80        ; test bit 7
jnz set

or eax, 0x10         ; set bit 4
and eax, ~0x10       ; clear bit 4
xor eax, 0x10        ; toggle bit 4

popcnt eax, ecx      ; population count

Multiplication by Constant

; x * 3
lea eax, [rax + rax*2]

; x * 5
lea eax, [rax + rax*4]

; x / 8 (power of 2)
cdq
and edx, 7
add eax, edx
sar eax, 3

Type Recovery

Operation Suggests
byte access char, bool
word access short
dword access int, float
qword access long, double, pointer

Analysis Workflow

  1. Identify architecture - File type, imports
  2. Find entry point - main, WinMain, etc.
  3. Map functions - exports, cross-references
  4. Analyze control flow - identify loops, branches
  5. Recover data structures - arrays, structs
  6. Document - rename functions, add types

Red Flags

Pattern Risk
strcpy, gets Buffer overflow
sprintf Format string
system Command injection
CreateRemoteThread Injection
VirtualAlloc Shellcode
Markdown source
---
name: binary-analysis-patterns
description: Analyze compiled binaries — disassembly, control flow graphs, data structure reconstruction, ELF/PE/Mach-O formats. Use when reverse engineering executables, analyzing malware, or understanding compiler output (objdump, Ghidra, radare2).
license: MIT
---

Analyze compiled binaries by recognizing patterns in assembly code.

---

## Calling Conventions

### x86-64 System V (Linux/macOS)

| Register | Purpose      |
| -------- | ------------ |
| RDI      | 1st arg      |
| RSI      | 2nd arg      |
| RDX      | 3rd arg      |
| RCX      | 4th arg      |
| R8       | 5th arg      |
| R9       | 6th arg      |
| RAX      | Return value |

### x86-64 Microsoft (Windows)

| Register | Purpose      |
| -------- | ------------ |
| RCX      | 1st arg      |
| RDX      | 2nd arg      |
| R8       | 3rd arg      |
| R9       | 4th arg      |
| RAX      | Return value |

### ARM64

| Register | Purpose       |
| -------- | ------------- |
| X0-X7    | Arguments     |
| X0       | Return value  |
| X29      | Frame pointer |
| X30      | Link register |

---

## Function Patterns

### Prologue/Epilogue

```asm
; x86-64
push rbp
mov rbp, rsp
sub rsp, 0x20      ; Allocate locals
; ...
leave               ; mov rsp, rbp; pop rbp
ret

; ARM64
stp x29, x30, [sp, #-16]!
mov x29, sp
; ...
ldp x29, x30, [sp], #16
ret
```

### Leaf Function (no calls)

```asm
; May skip frame pointer
sub rsp, 0x18
; ...
add rsp, 0x18
ret
```

---

## Control Flow

### Conditionals

```asm
; if (a == b)
cmp eax, ebx
jne skip
; body
skip:

; if (a < b) signed
cmp eax, ebx
jge skip

; if (a < b) unsigned
cmp eax, ebx
jae skip
```

### Loops

```asm
; for (i = 0; i < n; i++)
xor ecx, ecx
cmp ecx, [n]
jge loop_end
; body
inc ecx
jmp loop_end
```

### Switch (Jump Table)

```asm
mov eax, [var]
cmp eax, max_case
ja default
jmp [jump_table + eax*8]
```

---

## Data Structures

### Array Access

```asm
; array[i] - 4-byte elements
mov eax, [rbx + rcx*4]

; array[i][j]
imul eax, [cols]
add eax, [j]
mov edx, [rbx + rax*4]
```

### Structure Access

```c
struct Example {
    int a;      // 0
    char b;     // 4
    long c;     // 8
};
```

```asm
mov eax, [rdi]         ; a (offset 0)
movzx eax, byte [rdi+4]; b (offset 4)
mov rax, [rdi+8]      ; c (offset 8)
```

### Linked List

```asm
test rdi, rdi          ; node == NULL?
jz done
; process node
mov rdi, [rdi+8]       ; node = node->next
jmp loop
```

---

## Common Patterns

### String Operations

```asm
; strlen
xor ecx, ecx
cmp byte [rdi + rcx], 0
je done
inc ecx
jmp strlen
; ecx = length
```

### Bit Manipulation

```asm
test eax, 0x80        ; test bit 7
jnz set

or eax, 0x10         ; set bit 4
and eax, ~0x10       ; clear bit 4
xor eax, 0x10        ; toggle bit 4

popcnt eax, ecx      ; population count
```

### Multiplication by Constant

```asm
; x * 3
lea eax, [rax + rax*2]

; x * 5
lea eax, [rax + rax*4]

; x / 8 (power of 2)
cdq
and edx, 7
add eax, edx
sar eax, 3
```

---

## Type Recovery

| Operation    | Suggests              |
| ------------ | --------------------- |
| byte access  | char, bool            |
| word access  | short                 |
| dword access | int, float            |
| qword access | long, double, pointer |

---

## Analysis Workflow

1. **Identify architecture** - File type, imports
2. **Find entry point** - main, WinMain, etc.
3. **Map functions** - exports, cross-references
4. **Analyze control flow** - identify loops, branches
5. **Recover data structures** - arrays, structs
6. **Document** - rename functions, add types

---

## Red Flags

| Pattern              | Risk              |
| -------------------- | ----------------- |
| `strcpy`, `gets`     | Buffer overflow   |
| `sprintf`            | Format string     |
| `system`             | Command injection |
| `CreateRemoteThread` | Injection         |
| `VirtualAlloc`       | Shellcode         |