winapi

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Rendered for windows/amd64

Overview

Package winapi centralizes low-level Windows API access used across HackBrowserData. It exposes typed wrappers around specific syscalls that golang.org/x/sys/windows does not cover, plus shared LazyDLL handles and a small error-handling helper.

Callers: abe/injector, masterkey.

Index

Constants

View Source
const (

	// FileTypeDisk is the GetFileType return value for a normal disk file.
	FileTypeDisk uint32 = 0x0001
)

Variables

View Source
var (
	Kernel32 = windows.NewLazySystemDLL("kernel32.dll")
	Ntdll    = windows.NewLazySystemDLL("ntdll.dll")
	Crypt32  = windows.NewLazySystemDLL("crypt32.dll")
	User32   = windows.NewLazySystemDLL("user32.dll")
)

Package-level LazyDLL handles. Declaring them once here avoids the NewLazySystemDLL boilerplate previously spread across injector, filemanager, and crypto.

View Source
var ErrExecutableNotFound = errors.New("browser executable not found")

ErrExecutableNotFound is returned when a browser's executable cannot be located via registry App Paths or any install-location fallback.

Functions

func AddrGetProcAddress

func AddrGetProcAddress() uintptr

func AddrLoadLibraryA

func AddrLoadLibraryA() uintptr

func AddrNtFlushInstructionCache

func AddrNtFlushInstructionCache() uintptr

func AddrVirtualAlloc

func AddrVirtualAlloc() uintptr

func AddrVirtualProtect

func AddrVirtualProtect() uintptr

func CallBoolErr

func CallBoolErr(p *windows.LazyProc, args ...uintptr) (uintptr, error)

CallBoolErr wraps the common "r1 == 0 means failure" Win32 convention. Win32 GetLastError often returns ERROR_SUCCESS (errno 0) even on failure, so we distinguish the "no-errno" case explicitly to avoid emitting a misleading "operation completed successfully" message. errors.As is used instead of a type assertion so the check stays correct if x/sys/windows ever wraps the underlying errno.

func CreateRemoteThread

func CreateRemoteThread(proc windows.Handle, startAddr, param uintptr) (windows.Handle, error)

CreateRemoteThread wraps kernel32!CreateRemoteThread. Returns the new thread's handle, which the caller must CloseHandle.

func DecryptDPAPI

func DecryptDPAPI(ciphertext []byte) ([]byte, error)

DecryptDPAPI decrypts a DPAPI-protected blob using the current user's master key. It is the Windows counterpart to macOS/Linux os_crypt fallbacks and is called by masterkey.

func EnumProcesses

func EnumProcesses() ([]uint32, error)

EnumProcesses returns all PIDs currently visible to the caller. Backed by kernel32!K32EnumProcesses (available on Windows 7+), so we do not need a separate psapi.dll handle. The buffer doubles on overflow up to a 1M-entry safety cap.

func ExecutablePath

func ExecutablePath(exeName string, fallbacks []string) (string, error)

ExecutablePath resolves a browser's .exe, its own install locations first: an executable name such as browser.exe is shared by more than one vendor, so App Paths or a running process may name another vendor's program.

  1. Hard-coded install fallbacks — the vendor's own directories.
  2. Registry App Paths in HKLM, then HKCU — installs elsewhere.
  3. Running-process probe — scan EnumProcesses for a match by exe name and return the owner's QueryFullProcessImageName. Picks up portable builds and non-standard installs that never wrote to App Paths.

func ExpandEnvString

func ExpandEnvString(s string) (string, error)

ExpandEnvString is the Go-friendly wrapper around kernel32!ExpandEnvironmentStringsW. Use it when you need to resolve Windows-style %VAR% placeholders — Go's stdlib os.ExpandEnv only recognizes Unix-style $VAR / ${VAR} and leaves %VAR% untouched.

func FlushInstructionCache

func FlushInstructionCache(proc windows.Handle, addr, size uintptr) error

FlushInstructionCache wraps kernel32!FlushInstructionCache. Windows requires this after writing bytes that are about to be executed as code; pass addr=0, size=0 to flush the whole process. amd64 caches happen to be coherent, but the API contract is not.

func GetFileSizeEx

func GetFileSizeEx(h windows.Handle) (int64, error)

GetFileSizeEx returns the size of the file referenced by h.

func GetFileType

func GetFileType(h windows.Handle) uint32

GetFileType returns the Windows FileType for h (e.g., FileTypeDisk).

func GetFinalPathName

func GetFinalPathName(h windows.Handle) (string, error)

GetFinalPathName returns the normalized file path for h, with the \\?\ prefix stripped.

func HideConsoleWindow

func HideConsoleWindow() bool

HideConsoleWindow hides the console window attached to the current process. Returns true if the window was previously visible.

func MapFile

func MapFile(h windows.Handle, size int) ([]byte, error)

MapFile creates a read-only file mapping over h, copies the first size bytes into a Go-owned slice, and releases the mapping. Reads go through the OS kernel's file cache, which includes SQLite WAL data that has not yet been checkpointed into the main file.

func QueryFullProcessImageName

func QueryFullProcessImageName(h windows.Handle) (string, error)

QueryFullProcessImageName returns the full file-system path of the executable backing the given process handle. Open the handle with PROCESS_QUERY_LIMITED_INFORMATION (available to non-admin callers).

func ReadLockedFile

func ReadLockedFile(path string) ([]byte, error)

ReadLockedFile reads a file that is locked by another process (e.g., Chrome's Cookies database with PRAGMA locking_mode=EXCLUSIVE).

Approach: DuplicateHandle + FileMapping

  1. Enumerate all open file handles via NtQuerySystemInformation
  2. Find the handle matching the target file path
  3. Duplicate that handle into our process via DuplicateHandle
  4. Read file content through memory-mapped I/O (CreateFileMapping + MapViewOfFile)

This requires only normal user privileges (no admin needed).

func VirtualAllocEx

func VirtualAllocEx(proc windows.Handle, size uintptr, flAllocType, flProtect uint32) (uintptr, error)

VirtualAllocEx wraps kernel32!VirtualAllocEx. Returns the allocated base address in the target process, or an error surfacing Win32 errno-0 explicitly via CallBoolErr.

func VirtualProtectEx

func VirtualProtectEx(proc windows.Handle, addr, size uintptr, flNewProtect uint32) (uint32, error)

VirtualProtectEx wraps kernel32!VirtualProtectEx and returns the previous protection for the region.

Types

type SystemHandleEntry

type SystemHandleEntry struct {
	Object                uintptr
	UniqueProcessID       uintptr
	HandleValue           uintptr
	GrantedAccess         uint32
	CreatorBackTraceIndex uint16
	ObjectTypeIndex       uint16
	HandleAttributes      uint32
	Reserved              uint32
}

SystemHandleEntry mirrors SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX, the extended entry returned by SystemExtendedHandleInformation (class 64).

Layout on 64-bit Windows (40 bytes):

PVOID      Object;
ULONG_PTR  UniqueProcessId;
ULONG_PTR  HandleValue;
ULONG      GrantedAccess;
USHORT     CreatorBackTraceIndex;
USHORT     ObjectTypeIndex;
ULONG      HandleAttributes;
ULONG      Reserved;

func QuerySystemHandles

func QuerySystemHandles() ([]SystemHandleEntry, error)

QuerySystemHandles enumerates all open handles system-wide via NtQuerySystemInformation(SystemExtendedHandleInformation). The buffer size grows on STATUS_INFO_LENGTH_MISMATCH until it succeeds or exceeds the safety cap.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL