August 31, 2026 · 4 min read
Why we write our red team tooling in Zig
We compared several languages for our own red team tooling and settled on Zig for shellcode, stagers, and command and control payloads.
We build our own tooling for red team engagements. All of it is used only where the system owner has given written authorization.
We have been experimenting with different languages for that tooling. The parts we care most about are shellcode, stagers, and the main command and control payload. Zig has been the best language we have used for all of them.
What this code needs
Payload code has unusual requirements. It has to be small, and it has to do exactly what the source says with nothing added by a runtime. Shellcode has to be position independent and cannot rely on a loader or a C library being present. The data structures have to match formats defined by someone else, such as an executable file header or a message on the wire, byte for byte.
Cross-compiling also has to be routine, because we build on Linux and many targets run Windows.
The other candidates
C can do all of this, and most public tooling is written in it. Our problems with C are mostly in its tooling. Each target needs its own cross toolchain, and undefined behavior turns a small mistake into a crash on a client’s machine.
Rust is harder to use for this kind of code. A freestanding build means no_std and a custom panic handler, and shrinking the binary means fighting the formatting and panic code that the standard library pulls in. Most of the code that matters is raw pointer work inside unsafe, where the borrow checker cannot help.
Go cross-compiles by setting GOOS and GOARCH. Every binary carries the runtime and the garbage collector, though, so the smallest program is well over a megabyte, and position independent shellcode is not practical.
Why Zig
Zig has no runtime and no hidden allocations. A function that allocates memory takes an allocator as an argument, so we can read a payload’s source and know everything it will do. Freestanding targets are supported directly, and the ReleaseSmall build mode produces binaries close to the size of hand-written C.
Cross-compiling is built into the compiler. One flag selects the target, and the compiler ships what it needs to link for that target. The same Linux machine builds our Windows and Linux payloads with no extra toolchains installed.
comptime lets ordinary Zig code run during compilation. We use it to build tables and check assumptions before a payload exists, which moves a class of mistakes from the engagement to the build.
Control over layout is what we rely on most. A packed struct has exactly the bits we declare, in the order we declare them.
const std = @import("std");
// A message header with an exact, documented layout.
const Header = packed struct {
magic: u32,
kind: u8,
flags: u8,
length: u16,
};
comptime {
// The build fails if the header ever stops being 8 bytes.
std.debug.assert(@sizeOf(Header) == 8);
}
Zig also translates C headers as part of the build, so calling an operating system API does not require writing bindings by hand. Inline assembly and naked functions are part of the language for the few places that need them.
The standard library in shellcode
Much of Zig’s standard library has no dependency on an operating system, so it still works on a freestanding target. That includes std.crypto, which is written entirely in Zig.
Authenticated encryption and key exchange from the standard library compile directly into freestanding code, and the same code can be shared with the server it talks to. In a test build, ChaCha20-Poly1305 and X25519 together came to about 10 KB as a freestanding object file. A freestanding build has to supply its own source of randomness.
In C we would have to vendor a cryptography library and make it build without libc. In Rust we would be choosing among no_std crates and checking what each one pulls in.
What it costs
Zig has not reached 1.0. New compiler releases break existing source, and the standard library changes between versions. That means pinning a compiler version for each project and planning time for upgrades.
The ecosystem is small. Libraries we would take for granted in C or Rust often do not exist, and fewer engineers know the language. For payloads we count that as acceptable, because we want few dependencies in that code anyway.
Use in engagements
This tooling supports our red teaming work. Custom payloads let a client test its defenses against something its security products have not seen before, which is closer to what a capable attacker would bring than a public framework is.
- Red teaming
- Tooling