Metadata-Version: 2.4
Name: flag-capturer
Version: 0.5.1
Summary: Point it at a file or a string and it digs out CTF flags through layers of encoding.
Author: Alex Min
License-Expression: LicenseRef-PolyForm-Strict-1.0.0
Project-URL: Homepage, https://github.com/icanseethejupiter/ctf_tool_kit
Keywords: ctf,flag,forensics,decoder,security
Classifier: Programming Language :: Python :: 3
Classifier: Environment :: Console
Classifier: Topic :: Security
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: dnfile<0.19,>=0.18
Requires-Dist: dncil<2,>=1.0.2
Requires-Dist: dpkt<2,>=1.9.8
Requires-Dist: capstone<6,>=5
Requires-Dist: pyelftools<0.33,>=0.32
Requires-Dist: pefile>=2023.2.7
Dynamic: license-file

# flag_capturer

Point it at a file, a folder or a string and it digs out CTF flags, peeling off
layers of encoding until something looks like `flag{...}`. Every hit shows the
path it took.

```
$ flag_capturer challenge.bin
[binary] challenge.bin
[+] flag{x0r_1s_n0t_encrypt10n}
    challenge.bin  via  strings -> base64 -> xor(0x12)
```

## Install

```
pip install flag-capturer
```

No admin rights? Use `pip install --user flag-capturer`. Then run
`flag_capturer` (or the short `flagcap`). If the command is not found,
`python -m flag_capturer` always works.

## Use

```
flag_capturer                            # everything in the current folder
flag_capturer file.bin                   # one file
flag_capturer ./challenge_folder         # every file in a folder
flag_capturer -s "ZmxhZ3toaX0="          # a string
cat dump.txt | flag_capturer             # stdin
flag_capturer file -p DUCTF -p picoCTF   # extra flag prefixes (name only)
flag_capturer file -r "SECRET-[0-9]{6}"  # your own regex
flag_capturer file --loose               # also show anything shaped like WORD{...}
flag_capturer file --json                # for scripts
flag_capturer challenge.exe -e            # Ghidra steps and a CyberChef recipe under each hit
```

`-e` / `--explain` records the winning path's byte ranges and known addresses.
Load the original input into CyberChef to apply the printed recipe. Specialized
steps include manual or Python instructions. With `--json --explain`, each hit
also has `steps` and `cyberchef` (null when the complete path needs manual work).

Built-in prefixes: `flag`, anything ending in `CTF`, `HTB`, `THM`. Flags with
other prefixes are still listed as `[?]` suggestions.

### 中文快速上手

在资源管理器打开题目文件夹，地址栏输入 `powershell` 回车，然后运行
`python -m flag_capturer .`（点号 = 当前文件夹）。flag 前缀不常见的话加
`-p 前缀名`。PowerShell 出现 `>>` 时按 Ctrl+C 退出。

## What it handles

**Encodings and layers** (stacked up to depth 5)
- ASCII and UTF-16 strings inside binaries
- base64 / base64url / base32 / base85 / ascii85, hex, binary, decimal, octal, URL encoding
- rot13 and every other Caesar shift, rot47, reversed text
- single-byte XOR (all 255 keys, pre-filtered against known prefixes)
- gzip / zlib / bz2 / lzma streams and zip archives
- base64/hex/binary-looking chunks cut out of longer text

**File formats** (the detected format is printed before each scan)
- **Windows PE (MinGW x64):** constant arrays and split comparison fragments,
  repeating-key XOR on Base64 text, byte subtraction on flag-shaped text
- **Dropped PowerShell IOC puzzles:** recognizes an indexed-XOR decoder feeding
  a script and a NUL-separated configuration. Tracks the buffer and byte count
  passed to a `WriteFile` wrapper and the configuration field passed to an
  `InternetConnectA` wrapper. Formats the first 16 SHA-256 hex digits of the
  written bytes, host and original assembly-load method spelling as a candidate.
- **Deterministic aim puzzles:** recognizes a 64-bit linear congruential target
  generator, two coordinate ranges, a multiply-XOR coordinate hash (XOR before
  multiplication), and a three-shift XOR release loop. Replays the target
  centers using constants from the image and checks the resulting flag syntax.
- **.NET assemblies:** IL string literals, including adjacent literals joined by
  `String.Concat`; read statically, never executed
- **PCAP / PCAPNG:** UDP payloads and reassembled TCP streams per direction
  (no TLS decryption or IP fragment reassembly)
- **Linux ELF x86-64:** literal IPv4 addresses passed to `connect`, built on the
  stack or read from read-only data; also from Ghidra text listings
- **Intel assembly listings:** little-endian immediates

## Limits

Runtime-derived candidates require a recognized computation and recoverable
inputs. The two native rules above recognize specific MinGW x64 instruction
patterns and use PE unwind information to locate functions. Compiler changes,
different algorithms, or inputs supplied by a remote service can require
manual analysis. Each result includes the extraction path.

## Extending

Implement `formats.base.Analyzer` (`identify` and `extract`) and register the
instance in `formats.FORMATS`. Each extracted `Part` carries its bytes and
provenance; set `nested=True` for embedded files that should be identified again.

For an additional native PE puzzle family, implement `recognizes(data)` and
`extract(image)` and register the rule in `formats.pe_rules.RULES`. The rules
share a static image reader, import names and disassembled functions.

## Play fair

Use it on CTFs and practice boxes that allow tools. If a competition or exam says
no automated tools, leave it at home.

## License

PolyForm Strict 1.0.0: free to install and use for noncommercial purposes, but
no redistribution and no modified versions. See `LICENSE`.
