[SHEHZADE//ABDULLAH ANSARI]
Red Team Consultant @ Google | MS in Cybersecurity @ NYU
● Notice //GitHub (@shehzade) is temporarily suspended pending an automated false-positive review. All tools are hosted on this Code Mirror and available on request (contact); GitHub links will return once reinstated.
PUBLIC MIRRORGo v1.0
Multi-protocol network egress assessment and DLP verification framework featuring automated L4 sweeps, corporate proxy traversal, and covert protocol smuggling across 20+ channels.
$
git clone https://shehzade.io/repos/contraband.git
Copy
Source ZIP (1.93 MB)
146 lines · 3.8 KB
| 1 | package soak |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "runtime" |
| 7 | "sync" |
| 8 | "sync/atomic" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | // DefaultMaxHeapCeiling is the strict invariant ceiling (32 MB resident RAM). |
| 14 | DefaultMaxHeapCeiling uint64 = 32 * 1024 * 1024 |
| 15 | ) |
| 16 | |
| 17 | // MemoryStats carries point-in-time heap and memory statistics. |
| 18 | type MemoryStats struct { |
| 19 | Timestamp time.Time `json:"timestamp"` |
| 20 | HeapAllocBytes uint64 `json:"heap_alloc_bytes"` |
| 21 | HeapAllocMB float64 `json:"heap_alloc_mb"` |
| 22 | PeakHeapMB float64 `json:"peak_heap_mb"` |
| 23 | TotalAllocMB float64 `json:"total_alloc_mb"` |
| 24 | SysMB float64 `json:"sys_mb"` |
| 25 | NumGC uint32 `json:"num_gc"` |
| 26 | CeilingMB float64 `json:"ceiling_mb"` |
| 27 | Breached bool `json:"breached"` |
| 28 | } |
| 29 | |
| 30 | // MemoryMonitor actively samples runtime heap memory to continuously verify |
| 31 | // that resident heap allocations remain strictly < 32MB over extended soak windows. |
| 32 | type MemoryMonitor struct { |
| 33 | mu sync.RWMutex |
| 34 | ceilingBytes uint64 |
| 35 | interval time.Duration |
| 36 | peakAlloc uint64 |
| 37 | currentAlloc uint64 |
| 38 | violations int64 |
| 39 | samples int64 |
| 40 | lastStats MemoryStats |
| 41 | onViolation func(stats MemoryStats) |
| 42 | } |
| 43 | |
| 44 | // NewMemoryMonitor creates a new MemoryMonitor with the specified ceiling and sample interval. |
| 45 | func NewMemoryMonitor(ceilingBytes uint64, interval time.Duration) *MemoryMonitor { |
| 46 | if ceilingBytes == 0 { |
| 47 | ceilingBytes = DefaultMaxHeapCeiling |
| 48 | } |
| 49 | if interval <= 0 { |
| 50 | interval = 500 * time.Millisecond |
| 51 | } |
| 52 | return &MemoryMonitor{ |
| 53 | ceilingBytes: ceilingBytes, |
| 54 | interval: interval, |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // SetOnViolation registers a callback triggered whenever heap allocations breach the ceiling. |
| 59 | func (m *MemoryMonitor) SetOnViolation(cb func(stats MemoryStats)) { |
| 60 | m.mu.Lock() |
| 61 | defer m.mu.Unlock() |
| 62 | m.onViolation = cb |
| 63 | } |
| 64 | |
| 65 | // Start spawns the background monitoring goroutine. |
| 66 | func (m *MemoryMonitor) Start(ctx context.Context) { |
| 67 | go func() { |
| 68 | ticker := time.NewTicker(m.interval) |
| 69 | defer ticker.Stop() |
| 70 | |
| 71 | for { |
| 72 | select { |
| 73 | case <-ctx.Done(): |
| 74 | return |
| 75 | case <-ticker.C: |
| 76 | m.Sample() |
| 77 | } |
| 78 | } |
| 79 | }() |
| 80 | } |
| 81 | |
| 82 | // Sample takes a memory snapshot, updates peak tracking, and enforces the invariant ceiling. |
| 83 | func (m *MemoryMonitor) Sample() MemoryStats { |
| 84 | var ms runtime.MemStats |
| 85 | runtime.ReadMemStats(&ms) |
| 86 | |
| 87 | m.mu.Lock() |
| 88 | defer m.mu.Unlock() |
| 89 | |
| 90 | atomic.AddInt64(&m.samples, 1) |
| 91 | m.currentAlloc = ms.HeapAlloc |
| 92 | |
| 93 | if ms.HeapAlloc > m.peakAlloc { |
| 94 | m.peakAlloc = ms.HeapAlloc |
| 95 | } |
| 96 | |
| 97 | breached := ms.HeapAlloc >= m.ceilingBytes |
| 98 | if breached { |
| 99 | atomic.AddInt64(&m.violations, 1) |
| 100 | } |
| 101 | |
| 102 | stats := MemoryStats{ |
| 103 | Timestamp: time.Now().UTC(), |
| 104 | HeapAllocBytes: ms.HeapAlloc, |
| 105 | HeapAllocMB: float64(ms.HeapAlloc) / (1024 * 1024), |
| 106 | PeakHeapMB: float64(m.peakAlloc) / (1024 * 1024), |
| 107 | TotalAllocMB: float64(ms.TotalAlloc) / (1024 * 1024), |
| 108 | SysMB: float64(ms.Sys) / (1024 * 1024), |
| 109 | NumGC: ms.NumGC, |
| 110 | CeilingMB: float64(m.ceilingBytes) / (1024 * 1024), |
| 111 | Breached: breached, |
| 112 | } |
| 113 | m.lastStats = stats |
| 114 | |
| 115 | if breached && m.onViolation != nil { |
| 116 | go m.onViolation(stats) |
| 117 | } |
| 118 | |
| 119 | return stats |
| 120 | } |
| 121 | |
| 122 | // CurrentStats returns the most recently sampled MemoryStats. |
| 123 | func (m *MemoryMonitor) CurrentStats() MemoryStats { |
| 124 | m.mu.RLock() |
| 125 | defer m.mu.RUnlock() |
| 126 | return m.lastStats |
| 127 | } |
| 128 | |
| 129 | // PeakHeapAllocMB returns peak resident heap allocation observed in MB. |
| 130 | func (m *MemoryMonitor) PeakHeapAllocMB() float64 { |
| 131 | m.mu.RLock() |
| 132 | defer m.mu.RUnlock() |
| 133 | return float64(m.peakAlloc) / (1024 * 1024) |
| 134 | } |
| 135 | |
| 136 | // Violations returns the total count of ceiling breaches. |
| 137 | func (m *MemoryMonitor) Violations() int64 { |
| 138 | return atomic.LoadInt64(&m.violations) |
| 139 | } |
| 140 | |
| 141 | // FormatStatus returns a standardized one-line memory health summary. |
| 142 | func (m *MemoryMonitor) FormatStatus() string { |
| 143 | s := m.CurrentStats() |
| 144 | return fmt.Sprintf("Heap: %.2fMB / %.2fMB (Peak: %.2fMB, Violations: %d, GCs: %d)", |
| 145 | s.HeapAllocMB, s.CeilingMB, s.PeakHeapMB, m.Violations(), s.NumGC) |
| 146 | } |