[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)
154 lines · 4.0 KB
| 1 | package soak |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "sync" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/shehzade/contraband/pkg/common" |
| 11 | ) |
| 12 | |
| 13 | // SoakLogger manages thread-safe, continuous append-only logging to the evidence soak test log. |
| 14 | // All durations, latencies, and timestamps are formatted in milliseconds (ms), |
| 15 | // with zero runtime microseconds. |
| 16 | type SoakLogger struct { |
| 17 | mu sync.Mutex |
| 18 | filePath string |
| 19 | file *os.File |
| 20 | } |
| 21 | |
| 22 | // NewSoakLogger initializes the soak logger, ensuring the destination directory exists. |
| 23 | func NewSoakLogger(filePath string) (*SoakLogger, error) { |
| 24 | if filePath == "" { |
| 25 | filePath = "evidence/soak_test.log" |
| 26 | } |
| 27 | |
| 28 | dir := filepath.Dir(filePath) |
| 29 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 30 | return nil, fmt.Errorf("failed to create soak log directory %q: %w", dir, err) |
| 31 | } |
| 32 | |
| 33 | f, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) |
| 34 | if err != nil { |
| 35 | return nil, fmt.Errorf("failed to open soak log file %q: %w", filePath, err) |
| 36 | } |
| 37 | |
| 38 | return &SoakLogger{ |
| 39 | filePath: filePath, |
| 40 | file: f, |
| 41 | }, nil |
| 42 | } |
| 43 | |
| 44 | // Close flushes and closes the underlying log file. |
| 45 | func (l *SoakLogger) Close() error { |
| 46 | l.mu.Lock() |
| 47 | defer l.mu.Unlock() |
| 48 | if l.file != nil { |
| 49 | err := l.file.Close() |
| 50 | l.file = nil |
| 51 | return err |
| 52 | } |
| 53 | return nil |
| 54 | } |
| 55 | |
| 56 | // writeLine handles formatting, timestamping, and flushing. |
| 57 | func (l *SoakLogger) writeLine(level, msg string) { |
| 58 | now := time.Now().UTC() |
| 59 | // Format timestamp with millisecond precision: YYYY-MM-DDTHH:MM:SS.mmmZ |
| 60 | tsStr := now.Format("2006-01-02T15:04:05.000Z") |
| 61 | line := fmt.Sprintf("[%s] [%s] %s\n", tsStr, level, msg) |
| 62 | |
| 63 | l.mu.Lock() |
| 64 | defer l.mu.Unlock() |
| 65 | |
| 66 | if l.file != nil { |
| 67 | _, _ = l.file.WriteString(line) |
| 68 | _ = l.file.Sync() |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // LogProbe records a completed or failed exfiltration probe with millisecond latency formatting. |
| 73 | func (l *SoakLogger) LogProbe( |
| 74 | zone string, |
| 75 | protocol string, |
| 76 | volumeStr string, |
| 77 | verdict common.Verdict, |
| 78 | latencyMs float64, |
| 79 | heapAllocMB float64, |
| 80 | ceilingMB float64, |
| 81 | payloadHash string, |
| 82 | errMsg string, |
| 83 | ) { |
| 84 | level := "EXFIL" |
| 85 | if verdict != common.VerdictSuccess { |
| 86 | level = "WARN" |
| 87 | } |
| 88 | |
| 89 | hashShort := payloadHash |
| 90 | if len(hashShort) > 16 { |
| 91 | hashShort = hashShort[:16] + "..." |
| 92 | } |
| 93 | |
| 94 | var detail string |
| 95 | if errMsg != "" { |
| 96 | detail = fmt.Sprintf(" err=%q", errMsg) |
| 97 | } |
| 98 | |
| 99 | msg := fmt.Sprintf("[ZONE=%s] [PROTO=%s] [VOL=%s] [VERDICT=%s] [LATENCY=%.2fms] [HEAP=%.2fMB/%.2fMB] [HASH=%s]%s", |
| 100 | zone, protocol, volumeStr, verdict, latencyMs, heapAllocMB, ceilingMB, hashShort, detail) |
| 101 | |
| 102 | l.writeLine(level, msg) |
| 103 | } |
| 104 | |
| 105 | // LogNegativeTest records a simulated disruption / negative test injection result. |
| 106 | func (l *SoakLogger) LogNegativeTest( |
| 107 | negType string, |
| 108 | zone string, |
| 109 | target string, |
| 110 | actualVerdict common.Verdict, |
| 111 | expectedVerdict common.Verdict, |
| 112 | latencyMs float64, |
| 113 | passed bool, |
| 114 | ) { |
| 115 | level := "NEG_PASS" |
| 116 | if !passed { |
| 117 | level = "NEG_FAIL" |
| 118 | } |
| 119 | |
| 120 | msg := fmt.Sprintf("[NEGATIVE_TEST=%s] [ZONE=%s] [TARGET=%s] [EXPECTED=%s] [ACTUAL=%s] [LATENCY=%.2fms] [PASSED=%t]", |
| 121 | negType, zone, target, expectedVerdict, actualVerdict, latencyMs, passed) |
| 122 | |
| 123 | l.writeLine(level, msg) |
| 124 | } |
| 125 | |
| 126 | // LogMemory records a memory health status or invariant check. |
| 127 | func (l *SoakLogger) LogMemory(stats MemoryStats) { |
| 128 | level := "MEM_OK" |
| 129 | if stats.Breached { |
| 130 | level = "MEM_ALERT" |
| 131 | } |
| 132 | |
| 133 | msg := fmt.Sprintf("[MEMORY_INVARIANT] [HEAP=%.2fMB/%.2fMB] [PEAK=%.2fMB] [SYS=%.2fMB] [TOTAL=%.2fMB] [NUM_GC=%d] [BREACHED=%t]", |
| 134 | stats.HeapAllocMB, stats.CeilingMB, stats.PeakHeapMB, stats.SysMB, stats.TotalAllocMB, stats.NumGC, stats.Breached) |
| 135 | |
| 136 | l.writeLine(level, msg) |
| 137 | } |
| 138 | |
| 139 | // LogReportUploaded records dashboard report synchronization. |
| 140 | func (l *SoakLogger) LogReportUploaded(reportID, zone string, eeiScore float64, posture string, durationMs float64) { |
| 141 | msg := fmt.Sprintf("[REPORT_SYNC] [ID=%s] [ZONE=%s] [EEI=%.2f%%] [POSTURE=%s] [DURATION=%.2fms]", |
| 142 | reportID, zone, eeiScore, posture, durationMs) |
| 143 | l.writeLine("REPORT", msg) |
| 144 | } |
| 145 | |
| 146 | // LogInfo records an operational information event. |
| 147 | func (l *SoakLogger) LogInfo(msg string) { |
| 148 | l.writeLine("INFO", msg) |
| 149 | } |
| 150 | |
| 151 | // LogAlert records a high-priority alert or failure. |
| 152 | func (l *SoakLogger) LogAlert(msg string) { |
| 153 | l.writeLine("ALERT", msg) |
| 154 | } |