● 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 / server / state / reassembly.go
← Back to directory
69 lines · 1.7 KB
Raw
1package state
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "fmt"
7 "strings"
8
9 "github.com/shehzade/contraband/pkg/common"
10)
11
12// ReassemblyResult contains the assembled bytes, hash, and verdict.
13type ReassemblyResult struct {
14 Payload []byte
15 ComputedHash string
16 Verdict common.Verdict
17 Status string
18 Error error
19}
20
21// ReassembleChunks validates contiguous sequences, constructs the full binary payload,
22// computes the rolling SHA-256 hash, and performs cryptographic verification.
23func ReassembleChunks(chunks map[uint32][]byte, total uint32, expectedHash string) ReassemblyResult {
24 if total == 0 {
25 return ReassemblyResult{
26 Status: "corrupted",
27 Verdict: common.VerdictBlockedL7,
28 Error: fmt.Errorf("%w: total chunks is zero", ErrInvalidChunk),
29 }
30 }
31
32 var fullPayload []byte
33 hasher := sha256.New()
34
35 for s := uint32(1); s <= total; s++ {
36 chunkData, found := chunks[s]
37 if !found {
38 return ReassemblyResult{
39 Status: "corrupted",
40 Verdict: common.VerdictBlockedL7,
41 Error: fmt.Errorf("%w: chunk %d missing during final assembly", ErrSessionIncomplete, s),
42 }
43 }
44 if len(fullPayload)+len(chunkData) <= 2*1024*1024 {
45 fullPayload = append(fullPayload, chunkData...)
46 }
47 hasher.Write(chunkData)
48 }
49
50 computedHash := hex.EncodeToString(hasher.Sum(nil))
51
52 if expectedHash != "" && !strings.EqualFold(computedHash, expectedHash) {
53 return ReassemblyResult{
54 Payload: fullPayload,
55 ComputedHash: computedHash,
56 Status: "corrupted",
57 Verdict: common.VerdictBlockedDLP,
58 Error: ErrHashMismatch,
59 }
60 }
61
62 return ReassemblyResult{
63 Payload: fullPayload,
64 ComputedHash: computedHash,
65 Status: "completed",
66 Verdict: common.VerdictSuccess,
67 Error: nil,
68 }
69}