# 0xsch1zo/nullgate architecture

> Architecture diagram of the GitHub repository 0xsch1zo/nullgate, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0xsch1zo/nullgate
- Repository: https://github.com/0xsch1zo/nullgate

## Overview

##### Project Type and Purpose
- NullGate is a native C++ library and command-line tool (hasher) that provides a modern, type-safe interface for invoking Windows NT API functions via indirect syscalls with dynamic syscall number resolution and simple obfuscation routines.
- It also includes a proof-of-concept sample injector demonstrating how to bypass Windows Defender’s memory scans using its syscall and obfuscation primitives.

##### High-Level Architecture
- Components:
- NullGate Core Library
– Modules:
· syscalls      (dynamic lookup of syscall numbers & wrappers)
· obfuscation  (FNV-1 hashing, xor/b64 encode‐decode, hex2bin)
– Interfaces: templated SCall<>, non-templated Call()
– Dependencies: Windows ntdll.dll, FreshyCalls technique
- Hasher CLI Executable
– Built from src/hasher/hasher.cpp
– Reads stdin, applies xor+base64 via obfuscation module, outputs payload
- Sample Injector Executable
– Built from src/sample/sample.cpp
– Uses NullGate::syscalls to allocate memory, write shellcode, bypass Defender
- Build System
– Top-level CMakeLists.txt orchestrates building library, hasher, sample
– FetchContent support for inclusion in external CMake projects

##### Component Interactions and Data Flow
- At build time: CMake builds the static NullGate library, links it into hasher.exe and sample.exe.
- At runtime:
1. Sample.exe calls into NullGate::syscalls
2. syscalls module reads ntdll exports, matches hashed names via obfuscation::fnv1Runtime
3. Executes indirect syscall stubs (syscalls.S, .asm) to perform NtAllocateVirtualMemory, NtWriteVirtualMemory, NtResumeThread
4. Obfuscation routines handle shellcode encoding/decoding as needed
5. Memory scan bypass logic: allocate with PAGE_NOACCESS/junk write, create suspended thread, write real shellcode, resume thread
- Hasher.exe: stdin → obfuscation::xorEncode → obfuscation::base64Encode → stdout

##### Architectural Patterns and Design Principles
- Modularity: separate compilation units for syscalls, obfuscation, sample, hasher
- Type Safety: use of C++ templates for compile-time signature enforcement
- Separation of Concerns: library core versus CLI tools versus sample PoC
- Static Linking: avoids symbol export and makes integration seamless
- Build-time configuration: CMake flags for sample build and cross-compilation

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Build Process
    subgraph "Build Process"
        CMake["CMakeLists.txt"]:::build
        CMake -->|builds| NullGateLib["NullGate Library"]:::library
        CMake -->|builds| HasherExe["Hasher.exe"]:::exe
        CMake -->|builds| SampleExe["Sample.exe"]:::exe
        CMake_Hasher["Hasher CMakeLists.txt"]:::build
        CMake -->|includes| CMake_Hasher
    end

    %% NullGate Library Modules
    subgraph "NullGate Library"
        subgraph "syscalls Module"
            SysHdr["syscalls.hpp"]:::code
            SysCpp["syscalls.cpp"]:::code
            SysS["syscalls.S"]:::code
            SysMasm["syscalls_masm.asm"]:::code
        end
        subgraph "obfuscation Module"
            ObfHdr["obfuscation.hpp"]:::code
            ObfCpp["obfuscation.cpp"]:::code
        end
        Interface["SCall<>, Call()"]:::library
        ObfHdr --> ObfCpp
        SysHdr --> SysCpp
        SysHdr --> SysS
        SysHdr --> SysMasm
        SysCpp --> Interface
        ObfCpp --> Interface
    end

    %% CLI Tools
    HasherExe -->|uses obfuscation Module| ObfCpp
    SampleExe -->|uses syscalls Module| SysHdr

    %% Runtime Flow for Hasher
    stdin["stdin"]:::external
    stdout["stdout"]:::external
    stdin -->|plain data| ObfCpp
    ObfCpp -->|"xorEncode"| ObfCpp
    ObfCpp -->|"base64Encode"| stdout

    %% Runtime Flow for Sample Injector
    SampleExe -->|calls| Interface
    Interface -->|lookup exports| ntdll["ntdll.dll"]:::external
    ntdll -->|syscall stub| Kernel["Kernel"]:::external
    SampleExe -->|bypass memory scan| Defender["Windows Defender"]:::external

    %% Memory Scan Bypass Sequence
    subgraph "Bypass Sequence"
        Allocate["NtAllocateVirtualMemory"]:::process
        JunkWrite["JunkWrite(PAGE_NOACCESS)"]:::process
        CreateThread["CreateSuspendedThread"]:::process
        RealWrite["NtWriteVirtualMemory"]:::process
        Resume["NtResumeThread"]:::process
        SampleExe --> Allocate
        Allocate --> JunkWrite
        JunkWrite --> CreateThread
        CreateThread --> RealWrite
        RealWrite --> Resume
        Resume -->|payload execution| Kernel
    end

    %% Click Events
    click SysHdr "https://github.com/0xsch1zo/nullgate/blob/main/include/nullgate/syscalls.hpp"
    click SysCpp "https://github.com/0xsch1zo/nullgate/blob/main/src/nullgate/syscalls.cpp"
    click SysS "https://github.com/0xsch1zo/nullgate/blob/main/src/nullgate/syscalls.S"
    click SysMasm "https://github.com/0xsch1zo/nullgate/blob/main/src/nullgate/syscalls_masm.asm"
    click ObfHdr "https://github.com/0xsch1zo/nullgate/blob/main/include/nullgate/obfuscation.hpp"
    click ObfCpp "https://github.com/0xsch1zo/nullgate/blob/main/src/nullgate/obfuscation.cpp"
    click HasherExe "https://github.com/0xsch1zo/nullgate/blob/main/src/hasher/hasher.cpp"
    click CMake_Hasher "https://github.com/0xsch1zo/nullgate/blob/main/src/hasher/CMakeLists.txt"
    click SampleExe "https://github.com/0xsch1zo/nullgate/blob/main/src/sample/sample.cpp"
    click Defender "https://github.com/0xsch1zo/nullgate/blob/main/include/sample/ntapi.hpp"
    click CMake "https://github.com/0xsch1zo/nullgate/blob/main/CMakeLists.txt"

    %% Styles
    classDef library fill:#bbdefb,stroke:#333,stroke-width:1px
    classDef exe fill:#c8e6c9,stroke:#333,stroke-width:1px
    classDef build fill:#ffe0b2,stroke:#333,stroke-width:1px
    classDef code fill:#e1bee7,stroke:#333,stroke-width:1px
    classDef external fill:#eeeeee,stroke:#333,stroke-width:1px
    classDef process fill:#fff9c4,stroke:#333,stroke-width:1px
```
