● 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)
Code 174 Commits 1
contraband / pkg / client / dlp / entropy.go
← Back to directory
111 lines · 3.2 KB
Raw
1package dlp
2
3import (
4 "math"
5 "math/rand"
6)
7
8// CalculateShannonEntropy calculates Shannon entropy in bits per byte with 0 heap allocations.
9func CalculateShannonEntropy(data []byte) float64 {
10 if len(data) == 0 {
11 return 0.0
12 }
13 var counts [256]uint32
14 for _, b := range data {
15 counts[b]++
16 }
17 n := float64(len(data))
18 var entropy float64
19 for _, c := range counts {
20 if c == 0 {
21 continue
22 }
23 p := float64(c) / n
24 entropy -= p * math.Log2(p)
25 }
26 return entropy
27}
28
29type EntropyTuner struct {
30 MinEntropy float64
31 MaxEntropy float64
32}
33
34func NewEntropyTuner(min, max float64) *EntropyTuner {
35 if min <= 0 {
36 min = 3.5
37 }
38 if max <= 0 {
39 max = 5.5
40 }
41 return &EntropyTuner{
42 MinEntropy: min,
43 MaxEntropy: max,
44 }
45}
46
47var ProseReservoir = []string{
48 "The system architecture employs microservices communicating through secure channels. Enterprise egress testing requires rigorous verification of transport layer filters and proxy inspection policies.",
49 "Information security management systems mandate comprehensive data loss prevention controls across all corporate network boundaries and endpoints.",
50 "Network perimeter defenses inspect outbound application layer traffic for unauthorized cryptographic encapsulation and unexpected protocol tunneling.",
51 "Audit logging standards require microsecond timestamp synchronization across distributed logging servers and forensic collectors.",
52 "Compliance guidelines mandate annual assessment of data encryption in transit and confidential record handling procedures.",
53}
54
55var HighEntropyReservoir = []string{
56 "7f8a9b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a",
57 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
58 "MIIEowIBAAKCAQEA0rK1Z5mNq7Y/9K1tHj0kXfQzL0u8vV2W4y5b9a8c7d6e5f4a",
59 "3f5b7d9a1c2e4f6a8b0c2d4e6f8a0b1c3d5e7f9a1b2c3d4e5f6a7b8c9d0e1f2a",
60 "c0e8f7a6b5c4d3e2f1a0b9c8d7e6f5a4b3c2d1e0f9a8b7c6d5e4f3a2b1c0d9e8",
61}
62
63// Modulate adjusts the byte slice so its Shannon entropy lies within [MinEntropy, MaxEntropy].
64func (t *EntropyTuner) Modulate(data []byte, r *rand.Rand) []byte {
65 current := CalculateShannonEntropy(data)
66 if current >= t.MinEntropy && current <= t.MaxEntropy {
67 return data
68 }
69
70 result := append([]byte(nil), data...)
71 maxIterations := 40
72
73 for i := 0; i < maxIterations; i++ {
74 if current >= t.MinEntropy && current <= t.MaxEntropy {
75 break
76 }
77
78 vol := len(result)
79 if vol < 150 {
80 vol = 150
81 }
82
83 if current < t.MinEntropy {
84 // Inject high entropy tokens scaled to volume
85 for added := 0; added < vol; {
86 token := HighEntropyReservoir[r.Intn(len(HighEntropyReservoir))]
87 result = append(result, '\n')
88 result = append(result, token...)
89 added += len(token) + 1
90 }
91 } else if current > t.MaxEntropy {
92 // Inject low entropy prose or filler scaled to volume
93 if t.MaxEntropy < 4.16 {
94 pad := " \n"
95 for added := 0; added < vol; {
96 result = append(result, pad...)
97 added += len(pad)
98 }
99 } else {
100 for added := 0; added < vol; {
101 prose := ProseReservoir[r.Intn(len(ProseReservoir))]
102 result = append(result, '\n')
103 result = append(result, prose...)
104 added += len(prose) + 1
105 }
106 }
107 }
108 current = CalculateShannonEntropy(result)
109 }
110 return result
111}