
Generazione universale di firme per qualsiasi funzione di sistema da tutte le build di Windows utilizzando Winbindex
Firme binarie e offset RVA cross-version per funzioni Windows PE
pe-signgen è uno strumento per reverse engineer e ricercatori di sicurezza che genera automaticamente:
L'idea centrale è fornire un modo sistematico e robusto per accedere a funzioni non esportate nelle build di Windows 10/11. Si basa su:
⚠️ Supporto versioni Windows
pe-signgensupporta solo Windows 10 e Windows 11. Questa è una scelta progettuale deliberata: Winbindex non fornisce dati completi per le versioni precedenti.
Internals Windows non esportati
Genera firme per funzioni come LdrpInitializeTls, RtlpInsertInvertedFunctionTableEntry, ecc.
Game hacking / ricerca anti-cheat Genera firme stabili che sopravvivono agli aggiornamenti dei giochi
Ricerca sulla sicurezza Individua routine critiche per la sicurezza tra le build di Windows
Automazione Generazione scriptabile di firme e offset per interi set di API interne
pe-signgen fornisce tre distinti formati di output per diversi casi d'uso:
Dati strutturati per automazione, scripting e integrazione con altri strumenti.```bash pe-signgen --signature ntdll!NtCreateFile -o ntcreatefile.json --output-format json
**Struttura dell'output:**```json
{
"dll_name": "ntdll",
"function_name": "NtCreateFile",
"architecture": "x64",
"generated": "2024-12-11T15:30:00.123456",
"total_builds": 1247,
"unique_signatures": 3,
"signature_groups": [
{
"matched_symbol": "NtCreateFile",
"signature": "4C 8B DC 49 89 5B 08 49 89 6B 10 49 89 73 18 ...",
"length": 48,
"build_count": 845,
"versions": [
{ "major": 10240, "minor": 16384, "build": "10240.16384" },
{ "major": 10586, "minor": 0, "build": "10586.0" }
]
}
]
}
Notes:
major e minor sono derivati dalla stringa di build dividendo al primo ..
Esempio: "10240.16384" → major = 10240, minor = 16384.build è la chiave stringa di build originale utilizzata internamente.Formati binari compatti, pronti all'esecuzione, ottimizzati per sistemi embedded e scansione a basso overhead.
Questi corrispondono al layout su disco implementato in write_wsig() e write_woff().
Magic: WSO\0 (0x57 0x53 0x4F 0x00)
Versione corrente: 1
Scopo: Memorizzare firme binarie con maschere wildcard e versioni di build Windows associate
┌─────────────────────────────────────┐ │ Header (36 bytes) │ ├─────────────────────────────────────┤ │ DLL Name (variable) │ ├─────────────────────────────────────┤ │ Function Name (variable) │ ├─────────────────────────────────────┤ │ Signature / Mask / Build blobs │ ← Arbitrary order, see notes ├─────────────────────────────────────┤ ← Aligned to 4 bytes │ Groups Table (24 × N bytes) │ └─────────────────────────────────────┘
**Note importanti sul layout (corrisponde a `write_wsig`)**
* Dopo l'header, i nomi della DLL e della funzione sono scritti come byte UTF‑8.
* Per ogni gruppo di firme, vengono scritti i byte del pattern e i byte della maschera, seguiti dall'array di build per quel gruppo.
* Queste regioni per gruppo **non** sono raggruppate globalmente per tipo: pattern, maschere e array di build possono essere intervallati.
* Il builder si allinea a **4 byte** prima di ogni array di build e prima della tabella dei gruppi. Ciò può introdurre padding.
* I consumatori devono **sempre** seguire gli offset nell'header e nelle voci dei gruppi; **non** fare affidamento sul diagramma concettuale per la contiguità fisica.
##### Layout dell'header (36 byte)```c
// Packed as: "<4sIIIIIIII" (little-endian)
typedef struct {
char magic[4]; // "WSO\0" (WSIG_MAGIC)
uint32_t version; // FORMAT_VERSION (currently 1)
uint32_t arch; // Architecture code (1=x64, 2=ARM64, 3=WoW64)
uint32_t dll_off; // Offset to DLL name string
uint32_t dll_len; // Length of DLL name in bytes
uint32_t func_off; // Offset to function name string
uint32_t func_len; // Length of function name in bytes
uint32_t group_count;// Number of signature groups
uint32_t groups_off; // Offset to groups table
} wsig_header_t; // 36 bytes
Ogni gruppo di firme rappresenta un pattern unico che si applica a una o più build di Windows.```c // Packed as: "<IIIIII" (little-endian)
typedef struct { uint32_t sig_off; // Offset to signature pattern bytes uint32_t sig_len; // Length of signature pattern (in bytes) uint32_t mask_off; // Offset to wildcard mask bytes uint32_t mask_len; // Length of wildcard mask (≈ ceil(sig_len/8)) uint32_t builds_off; // Offset to build version array uint32_t build_cnt; // Number of builds using this signature } wsig_group_t; // 24 bytes
##### Voce della versione della build (8 byte)
Ogni voce di build identifica una versione specifica di Windows che utilizza questa firma.```c
typedef struct {
uint32_t major; // e.g. 19041
uint32_t minor; // e.g. 1234
} wsig_build_t; // 8 bytes
major e minor derivano dalla suddivisione della stringa di build ("A.B" → A, B). La stringa di build originale non è memorizzata nel formato binario; se ti serve, conservala esternamente (è presente nell'output JSON).
La maschera è una maschera di bit in cui ogni bit corrisponde a un byte nel pattern della firma:
Esempio:``` Signature: 4C 8B DC 49 89 ?? 08 49 Mask bits: 1 1 1 1 1 0 1 1 (MSB first within each byte) Mask byte: 0xBF (binary: 10111111)
I byte della maschera sono memorizzati e interpretati in **ordine dei bit little-endian** all'interno di ogni byte (esattamente come usato negli helper C e in `parse_signature`):```c
uint8_t bit = (mask_bytes[byte_index >> 3] >> (byte_index & 7)) & 1u;
*_len per determinare la lunghezza; non leggere oltre.Magic: WOF\0 (0x57 0x4F 0x46 0x00)
Versione corrente: 1
Scopo: memorizzare RVA diretti e offset di file per le funzioni in tutte le build di Windows
┌─────────────────────────────────────┐ │ Header (36 bytes) │ ├─────────────────────────────────────┤ │ DLL Name (variable) │ ├─────────────────────────────────────┤ │ Function Name (variable) │ ├─────────────────────────────────────┤ │ Matched Symbol Names (variable) │ ← One UTF‑8 string per entry ├─────────────────────────────────────┤ ← Aligned to 4 bytes │ Entries Table (32 × N bytes) │ └─────────────────────────────────────┘
Dettagli del layout (corrisponde a `write_woff`):
* Dopo il segnaposto dell'intestazione, i nomi delle DLL e delle funzioni vengono scritti come byte UTF‑8.
* Per ogni build, il nome del simbolo corrispondente viene scritto come stringa UTF‑8 (senza terminatore). Questi formano un semplice string pool.
* Il writer si allinea quindi a 4 byte e scrive la tabella delle voci a dimensione fissa.
* Ogni voce contiene offset (`matched_off`, `matched_len`) che puntano a questo string pool.
##### Layout dell'intestazione (36 byte)```c
// Packed as: "<4sIIIIIIII" (little-endian)
typedef struct {
char magic[4]; // "WOF\0" (WOFF_MAGIC)
uint32_t version; // FORMAT_VERSION (currently 1)
uint32_t arch; // Architecture code (1=x64, 2=ARM64, 3=WoW64)
uint32_t dll_off; // Offset to DLL name string
uint32_t dll_len; // Length of DLL name in bytes
uint32_t func_off; // Offset to function name string
uint32_t func_len; // Length of function name in bytes
uint32_t entry_cnt; // Number of offset entries
uint32_t entries_off;// Offset to entries table
} woff_header_t; // 36 bytes
Ogni voce associa una build di Windows alla posizione della funzione in quella build.```c // Packed as: "<IIQQII" (little-endian)
typedef struct { uint32_t major; // Windows major version (e.g., 19041) uint32_t minor; // Windows minor version (e.g., 1234) uint64_t rva; // Relative Virtual Address in the DLL uint64_t file_offset; // Raw file offset in the DLL on disk uint32_t matched_off; // Offset to matched symbol name string uint32_t matched_len; // Length of matched symbol name } woff_entry_t; // 32 bytes
##### Note sull'uso
* **RVA** è l'offset di memoria quando la DLL viene caricata alla sua base preferita.
* **File offset** è la posizione grezza nel file PE su disco.
* **Matched symbol** può differire dalla funzione richiesta (ad esempio, esportazioni inoltrate).
La stringa viene memorizzata una sola volta nel pool di stringhe; `matched_off`/`matched_len` vi fanno riferimento.
* Le voci sono **ordinate per versione di build** (`major`, poi `minor`) per una ricerca efficiente.
---
#### Codici di architettura
Entrambi i formati binari usano la stessa codifica di architettura (tramite `ARCH_CODE_MAP`):
| Code | Architecture | Description |
| ---- | ------------ | ---------------------------- |
| 1 | x64 | 64-bit AMD64/Intel64 |
| 2 | ARM64 | 64-bit ARM (AArch64) |
| 3 | WoW64 | x86 a 32 bit su Windows a 64 bit |
Le stringhe di architettura sconosciute vengono impostate internamente su `1` (x64); la CLI limita i valori all'insieme supportato.
---
### 3. **Formato Header C**
Header C pronti da compilare con strutture type-safe e array di dati.
`pe-signgen` può generare due *tipi* di header C:
* **Header WSIG** – per i dati di firma e maschera (da `write_wsig_header`).
* **Header WOFF** – per tabelle dirette RVA/offset di file (da `write_woff_header`).
L'opzione `--output-format cheader` seleziona gli header C; combinandola con `--offsets` si passa dalle varianti WSIG a quelle WOFF.
#### Header C WSIG```bash
pe-signgen --signature ntdll!RtlpInitializeThreadActivationContextStack \
-o rtlp_init_actx.h --output-format cheader
Struttura dell'header generato (semplificata, corrisponde a write_wsig_header):```c
/* Auto-generated WSIG header for ntdll ! RtlpInitializeThreadActivationContextStack ! x64. */
#ifndef WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_H
#define WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_H
#include <stdint.h> #include <stddef.h>
#define WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_DLL_NAME "ntdll" #define WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_FUNCTION_NAME "RtlpInitializeThreadActivationContextStack" #define WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_ARCH "x64"
/* Per-version build identifier. */ typedef struct { uint32_t major; uint32_t minor; } WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_version_t;
/* Signature group entry. */ typedef struct { const uint8_t *pattern; const uint8_t *mask; uint32_t length; uint32_t build_count; const WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_version_t *versions; } WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group_t;
/* One pattern/mask/versions triple per group. / static const uint8_t WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group0_pattern[] = { / ... / }; static const uint8_t WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group0_mask[] = { / ... / }; static const WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_version_t WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group0_versions[] = { { 10240u, 16384u }, / 10240.16384 / / ... */ };
static const WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group_t WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_GROUPS[] = { { WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group0_pattern, WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group0_mask, (uint32_t)(sizeof(WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group0_pattern) / sizeof(WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group0_pattern[0])), (uint32_t)(sizeof(WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group0_versions) / sizeof(WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group0_versions[0])), WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group0_versions }, /* group 0 (RtlpInitializeThreadActivationContextStack) / / ... */ };
static const size_t WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_GROUP_COUNT = sizeof(WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_GROUPS) / sizeof(WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_GROUPS[0]);
#endif /* WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_H */
**Esempio di integrazione (corretto per corrispondere ai tipi generati):**```c
#include "rtlp_init_actx.h"
static inline int match_byte(uint8_t want, uint8_t got,
const uint8_t *mbits, uint32_t i) {
uint8_t bit = (mbits[i >> 3] >> (i & 7)) & 1u;
return bit ? (want == got) : 1;
}
static const uint8_t *
find_signature(const uint8_t *base, size_t size,
const uint8_t *pattern,
const uint8_t *mbits,
uint32_t sig_len) {
if (!base || !pattern || !mbits || sig_len == 0)
return NULL;
if (size < sig_len)
return NULL;
// Find first non-wildcard byte as anchor
uint32_t anchor = sig_len;
for (uint32_t i = 0; i < sig_len; ++i) {
if ((mbits[i >> 3] >> (i & 7)) & 1u) {
anchor = i;
break;
}
}
if (anchor == sig_len)
return base; // all wildcards
const uint8_t anchor_val = pattern[anchor];
const size_t last_pos = size - (size_t)sig_len;
for (size_t pos = 0; pos <= last_pos; ++pos) {
if (base[pos + anchor] != anchor_val)
continue;
uint32_t i = 0;
for (; i < sig_len; ++i) {
if (!match_byte(pattern[i], base[pos + i], mbits, i))
break;
}
if (i == sig_len)
return base + pos;
}
return NULL;
}
static void
fetch_signature(uint32_t major, uint32_t minor,
const WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group_t *groups,
size_t group_len,
const uint8_t **signature_dest,
const uint8_t **mask_dest,
uint32_t *signature_len_dest) {
*signature_dest = NULL;
*mask_dest = NULL;
*signature_len_dest = 0;
const WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group_t *closest = NULL;
uint32_t best_distance = 0xFFFFFFFFu;
for (size_t gi = 0; gi < group_len; ++gi) {
const WSIG_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_group_t *g = &groups[gi];
for (uint32_t vi = 0; vi < g->build_count; ++vi) {
uint32_t m = g->versions[vi].major;
uint32_t n = g->versions[vi].minor;
uint32_t distance = (m > major ? m - major : major - m) * 10000u +
(n > minor ? n - minor : minor - n);
if (distance < best_distance) {
best_distance = distance;
closest = g;
}
if (m == major && n == minor) {
*signature_dest = g->pattern;
*mask_dest = g->mask;
*signature_len_dest = g->length;
return;
}
}
}
if (closest) {
*signature_dest = closest->pattern;
*mask_dest = closest->mask;
*signature_len_dest = closest->length;
}
}
Puoi quindi collegare questo al tuo codice specifico per il loader (ad es. usando GetModuleHandleA, attraversando le sezioni PE, ecc.). L'header fornisce intenzionalmente solo dati; le funzioni di supporto sono lasciate al consumatore.
Per i casi d'uso basati solo su offset, write_woff_header emette un piccolo header che descrive una tabella ordinata di voci (major, minor, rva, file_offset).
Layout (corrisponde a write_woff_header):```c
/* Auto-generated WOFF header for ntdll ! RtlpInitializeThreadActivationContextStack ! x64. */
#ifndef WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_H
#define WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_H
#include <stdint.h> #include <stddef.h>
#define WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_DLL_NAME "ntdll" #define WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_FUNCTION_NAME "RtlpInitializeThreadActivationContextStack" #define WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_ARCH "x64"
/* Per-build offset entry. */ typedef struct { uint32_t major; uint32_t minor; uint64_t rva; uint64_t file_offset; } WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_entry_t;
static const WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_entry_t WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_ENTRIES[] = { { 10240u, 16384u, 0x5B195ULL, 0x5A595ULL }, /* 10240.16384 (RtlpInitializeThreadActivationContextStack) / / ... (sorted by major, then minor) ... */ };
static const size_t WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_ENTRY_COUNT = sizeof(WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_ENTRIES) / sizeof(WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_ENTRIES[0]);
#endif /* WOFF_NTDLL_RTLPINITIALIZETHREADACTIVATIONCONTEXTSTACK_X64_H */
Questo è utile quando ti fidi degli offset stessi e non hai bisogno di pattern-matching.
---
## Installazione
### Prerequisiti
* Python 3.8+
* Git
* Connessione a Internet (primo avvio)
* ~10GB di spazio su disco per la cache completa
### Installazione da Pip```bash
pip install pe-signgen
git clone https://github.com/forentfraps/pe-signgen.git cd pe-signgen pip install -r requirements.txt pip install -e .
---
## Avvio rapido
### Genera una firma```bash
pe-signgen --signature ntdll!LdrLoadDll
pe-signgen --signature kernel32!CreateFileW --offsets
### Salva come JSON```bash
pe-signgen --signature ntdll!NtCreateFile -o out.json --output-format json
pe-signgen --signature DLL!FUNCTION [OPTIONS]
### Architettura```bash
--arch x64 # default
--arch arm64
--arch wow64
--os-version win10 # Only Windows 10 --os-version win11 # Only Windows 11 --min-version 10.0 # Minimum version --max-version 11.0 # Maximum version
### Controllo della lunghezza della firma```bash
--min-length 32 # Minimum signature length
--max-length 64 # Maximum signature length
-o, --output PATH # Output file path --output-format FORMAT # json | binary | cheader --offsets # Generate offsets instead of signatures
### Performance```bash
--workers 16 # Parallel workers (default: CPU count)
--no-cache # Disable caching
--no-git-update # Skip Winbindex updates
--verbose # Detailed output --quiet # Minimal output --no-progress # Disable progress bars
---
## Caching
### Layout della cache```text
~/.cache/pe-signgen/
│
├── dlls/ # Downloaded DLLs
├── pdbs/ # Downloaded PDBs
├── signatures/ # Generated signatures
└── winbindex_data/ # Winbindex metadata
pe-signgen --signature ntdll!NtCreateFile --no-cache
rm -rf ~/.cache/pe-signgen
export PE_SIGNGEN_CACHE=/custom/path pe-signgen --signature ntdll!NtCreateFile
---
## Prestazioni
**Esempio di prestazioni (CPU a 12 core, 100 Mbps):**
| Operazione | Tempo |
| --------------------------------- | ----------- |
| Prima esecuzione (senza cache) | 5–10 min |
| Esecuzione con cache | < 1 sec |
| Analisi per build | 0.1–0.5 sec |
| Esecuzione completa (1000 build, 8 worker) | 2–4 min |
**Requisiti di risorse:**
* **Disco:** ~10 GB per la cache completa di DLL/PDB
* **Memoria:** ~500 MB di utilizzo di picco
* **Rete:** Diversi GB alla prima esecuzione
---
## Limitazioni Note
* **Copertura versioni di Windows:** Solo Windows **10 e 11** (limitazione di Winbindex)
* **Disponibilità delle build:** Non tutte le build di Win10/11 sono presenti in Winbindex
---
## Sviluppo```bash
git clone https://github.com/forentfraps/pe-signgen.git
cd pe-signgen
pip install -e ".[dev]"
# Code formatting
black pe_signgen/
# Type checking
mypy pe_signgen/
Licenza MIT – vedi LICENSE.
Ispirato dalla necessità di una generazione di firme robusta e automatizzata per le API Windows interne.
Contributi benvenuti! Per favore: