mirror of
https://github.com/lukaszraczylo/go-obfuscation.git
synced 2026-10-12 05:32:33 +00:00
Anti-RE features (10 new packages/files):
- pkg/antidebug/breakpoint_{linux,darwin,other}.go: HW breakpoint
detection via ptrace GETREGS (DR0-DR3 on linux/amd64).
- pkg/antidebug/singlestep.go: SIGTRAP-count-based single-step
detection.
- pkg/antidbi/frida_deep.go: deep Frida scan — /proc/self/maps
signature check, /tmp named-pipe scan, /proc/self/task thread
name scan. Wired into antidbi.Check().
- pkg/antisandbox/ldpreload_{linux,other}.go: LD_PRELOAD and
/etc/ld.so.preload detection on linux; env-unset helper.
- pkg/antire/response.go: dynamic response framework — Severity/Action
enums, DefaultPolicy, SetPolicy, Trigger with switch over actions
(Log, Exit, CorruptMem, InfiniteLoop, ForkBomb-disabled, FakeResult).
- pkg/integrity/monitor.go: StartMemoryRegionMonitor — periodic
re-hash of a live memory region, drift detection, manual Check.
(Distinct from existing StartMonitor which re-hashes the file.)
- pkg/antivm/timer_resolution.go: time.Now() granularity check —
real CPUs have < 1us resolution, VMs often 1ms+. Exposed as
CheckTimerResolution().
- pkg/smc/smc.go: self-modifying code MVP — XOR encrypt/decrypt
via pageman.SecureBuffer. (Full atomic decrypt+exec is future work.)
- cmd/packer/main.go: custom packer MVP — AES-256-GCM payload
encryption, generates Go stub with embedded ciphertext. (Native
stub is future work.)
- Makefile: pure-syscall build target (CGO_ENABLED=0,
-Wl,--exclude-libs,ALL) added to .PHONY and help.
Transformer bug fixes (block make obfuscated/compare/pure-syscall):
- internal/transform/constblind.go: helper hardcoded to return
uint64 but used in int contexts (loop bounds, function args).
Changed to int. Also added overflow guards on generated key/
blinded values (must fit in int). Added missing upper-bound
check in cbBlindDecimal.
- internal/transform/fakesigs.go: _initAuth_obf template generated
`%s & 0xFF` where %s was a string param, producing invalid Go
(string & int). Changed to `%s[0] & 0xFF` (byte index).
Naming collision fix:
- pkg/integrity/monitor.go: renamed StartMemoryMonitor →
StartMemoryRegionMonitor with comment distinguishing it from the
existing file-based StartMonitor in integrity.go.
Test coverage (8 new test files, ~30 new tests):
- pkg/antidebug/breakpoint_test.go: no-panic, concurrent.
- pkg/antidebug/singlestep_test.go: install/uninstall, no-false-positive.
- pkg/antidbi/frida_deep_test.go: all three scans return cleanly.
- pkg/antisandbox/ldpreload_test.go: no-panic on linux, clean state.
- pkg/antire/response_test.go: Severity.String, SetPolicy, Trigger.
- pkg/integrity/monitor_test.go: invalid params, start/stop/check,
drift detection, -race clean.
- pkg/antivm/timer_resolution_test.go: no-panic, returns bool, repeatable.
- pkg/smc/smc_test.go: encrypt/decrypt roundtrip, empty-key error,
execute no-panic, concurrent.
- cmd/packer/packer_test.go: pack/unpack roundtrip, invalid key, missing flags.
Verification:
- go test -count=1 ./... — all 17 packages pass.
- make compare — full pipeline succeeds (normal + obfuscated + integrity).
- make pure-syscall — succeeds, produces minimal-libc binary.
- Obfuscated binary runs correctly, exits 0, all 6 demo secrets hidden.
- Symbol count: 2864 → 58. String count: 15287 → 4346.
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
// Package smc provides MVP self-modifying code primitives.
|
|
//
|
|
// MVP scope: XOR-encrypt a function payload, hold it locked in a
|
|
// pageman.SecureBuffer, decrypt briefly inside an Access callback,
|
|
// re-encrypt on callback return. Production SMC needs per-arch
|
|
// assembly for atomic decrypt+execute (not in MVP).
|
|
package smc
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/lukaszraczylo/go-obfuscation/pkg/pageman"
|
|
)
|
|
|
|
// Encrypt XOR-encrypts code with key. key must be non-empty.
|
|
func Encrypt(code, key []byte) ([]byte, error) {
|
|
if len(key) == 0 {
|
|
return nil, fmt.Errorf("smc: empty key")
|
|
}
|
|
out := make([]byte, len(code))
|
|
for i, b := range code {
|
|
out[i] = b ^ key[i%len(key)]
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Decrypt reverses Encrypt.
|
|
func Decrypt(code, key []byte) ([]byte, error) {
|
|
return Encrypt(code, key) // XOR is symmetric
|
|
}
|
|
|
|
// Execute demonstrates the encrypt/decrypt cycle on an encrypted payload.
|
|
//
|
|
// It parks the payload inside a pageman.SecureBuffer (locked at rest),
|
|
// unlocks it via Access to do an in-place decrypt, then locks it back.
|
|
// The "execute" step is a no-op: jumping to arbitrary decrypted bytes
|
|
// would require platform-specific func() cast and an RX window, both
|
|
// outside MVP scope.
|
|
//
|
|
// MVP limitation: the brief window where plaintext exists inside the
|
|
// Access callback is observable. Production needs arch-specific atomic
|
|
// decrypt+exec.
|
|
func Execute(encrypted, key []byte) error {
|
|
if len(encrypted) == 0 {
|
|
return fmt.Errorf("smc: empty encrypted payload")
|
|
}
|
|
if len(key) == 0 {
|
|
return fmt.Errorf("smc: empty key")
|
|
}
|
|
|
|
sb := pageman.NewSecureBuffer(encrypted, 0)
|
|
defer sb.Close()
|
|
|
|
// First cycle: unlock -> decrypt in place (verify) -> relock.
|
|
if err := sb.Access(func(plain []byte) {
|
|
for i, b := range plain {
|
|
plain[i] = b ^ key[i%len(key)]
|
|
}
|
|
}); err != nil {
|
|
return fmt.Errorf("smc: decrypt access failed: %w", err)
|
|
}
|
|
|
|
// Second cycle: unlock -> re-encrypt in place -> relock.
|
|
if err := sb.Access(func(plain []byte) {
|
|
for i, b := range plain {
|
|
plain[i] = b ^ key[i%len(key)]
|
|
}
|
|
}); err != nil {
|
|
return fmt.Errorf("smc: re-encrypt access failed: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ExecuteFunc calls fn after running it through a no-op encrypt/decrypt
|
|
// cycle on a side buffer. MVP: the function is invoked normally; the
|
|
// cycle just exercises the smc primitives around it.
|
|
func ExecuteFunc(fn func(), key []byte) error {
|
|
// Build a synthetic payload, run the cycle, then call the function.
|
|
payload := []byte{0xDE, 0xAD, 0xBE, 0xEF}
|
|
if err := Execute(payload, key); err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = recover() }()
|
|
fn()
|
|
return nil
|
|
}
|