[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)
165 lines · 4.5 KB
| 1 | package evasion |
| 2 | |
| 3 | import ( |
| 4 | "archive/zip" |
| 5 | "bytes" |
| 6 | "encoding/binary" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "image" |
| 10 | "image/color" |
| 11 | "image/jpeg" |
| 12 | "io" |
| 13 | "math/rand" |
| 14 | ) |
| 15 | |
| 16 | // GenerateProceduralJPEG creates a valid, clean-rendering JPEG image with procedural gradient. |
| 17 | func GenerateProceduralJPEG(width, height int, seed int64) ([]byte, error) { |
| 18 | img := image.NewRGBA(image.Rect(0, 0, width, height)) |
| 19 | rng := rand.New(rand.NewSource(seed)) |
| 20 | |
| 21 | baseR := uint8(rng.Intn(120) + 40) |
| 22 | baseG := uint8(rng.Intn(120) + 40) |
| 23 | baseB := uint8(rng.Intn(120) + 40) |
| 24 | |
| 25 | for y := 0; y < height; y++ { |
| 26 | for x := 0; x < width; x++ { |
| 27 | c := color.RGBA{ |
| 28 | R: uint8((int(baseR) + x*200/width) % 256), |
| 29 | G: uint8((int(baseG) + y*200/height) % 256), |
| 30 | B: uint8((int(baseB) + (x*y)*100/(width*height)) % 256), |
| 31 | A: 255, |
| 32 | } |
| 33 | img.SetRGBA(x, y, c) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | var buf bytes.Buffer |
| 38 | opt := &jpeg.Options{Quality: 85} |
| 39 | if err := jpeg.Encode(&buf, img, opt); err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | return buf.Bytes(), nil |
| 43 | } |
| 44 | |
| 45 | // BuildJPEGZIPPolyglot combines a valid JPEG with an offset-adjusted ZIP archive. |
| 46 | func BuildJPEGZIPPolyglot(jpegBytes []byte, payload []byte, payloadFilename string) ([]byte, error) { |
| 47 | if len(jpegBytes) < 4 || jpegBytes[0] != 0xFF || jpegBytes[1] != 0xD8 { |
| 48 | return nil, errors.New("invalid JPEG data (missing SOI)") |
| 49 | } |
| 50 | |
| 51 | // Verify JPEG ends with EOI \xFF\xD9 |
| 52 | if jpegBytes[len(jpegBytes)-2] != 0xFF || jpegBytes[len(jpegBytes)-1] != 0xD9 { |
| 53 | return nil, errors.New("invalid JPEG data (missing EOI)") |
| 54 | } |
| 55 | |
| 56 | if payloadFilename == "" { |
| 57 | payloadFilename = "contraband_data.bin" |
| 58 | } |
| 59 | |
| 60 | // 1. Build standard ZIP archive in memory |
| 61 | var zipBuf bytes.Buffer |
| 62 | zw := zip.NewWriter(&zipBuf) |
| 63 | |
| 64 | // Add procedural decoy file |
| 65 | decoyW, err := zw.Create("document_metadata.xml") |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | if _, err := decoyW.Write([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?><metadata><status>verified</status></metadata>")); err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | |
| 73 | // Add payload file |
| 74 | payloadW, err := zw.Create(payloadFilename) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | if _, err := payloadW.Write(payload); err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | |
| 82 | if err := zw.Close(); err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | rawZIP := zipBuf.Bytes() |
| 87 | jpegLen := uint32(len(jpegBytes)) |
| 88 | |
| 89 | // 2. Adjust Central Directory offsets for prefix length N |
| 90 | adjustedZIP, err := adjustZIPOffsets(rawZIP, jpegLen) |
| 91 | if err != nil { |
| 92 | return nil, fmt.Errorf("failed to adjust ZIP offsets: %w", err) |
| 93 | } |
| 94 | |
| 95 | // 3. Concatenate JPEG + Adjusted ZIP |
| 96 | var polyglot bytes.Buffer |
| 97 | polyglot.Write(jpegBytes) |
| 98 | polyglot.Write(adjustedZIP) |
| 99 | |
| 100 | return polyglot.Bytes(), nil |
| 101 | } |
| 102 | |
| 103 | func adjustZIPOffsets(zipData []byte, prefixLen uint32) ([]byte, error) { |
| 104 | out := make([]byte, len(zipData)) |
| 105 | copy(out, zipData) |
| 106 | |
| 107 | // Locate EOCD signature 0x06054b50 ("PK\x05\x06") from the end |
| 108 | eocdSig := []byte{0x50, 0x4B, 0x05, 0x06} |
| 109 | eocdOffset := bytes.LastIndex(out, eocdSig) |
| 110 | if eocdOffset == -1 || eocdOffset+22 > len(out) { |
| 111 | return nil, errors.New("cannot locate ZIP EOCD record") |
| 112 | } |
| 113 | |
| 114 | cdSize := binary.LittleEndian.Uint32(out[eocdOffset+12 : eocdOffset+16]) |
| 115 | cdOffset := binary.LittleEndian.Uint32(out[eocdOffset+16 : eocdOffset+20]) |
| 116 | |
| 117 | // Walk through Central Directory entries: signature 0x02014b50 ("PK\x01\x02") |
| 118 | cdSig := []byte{0x50, 0x4B, 0x01, 0x02} |
| 119 | curr := int(cdOffset) |
| 120 | cdEnd := curr + int(cdSize) |
| 121 | |
| 122 | for curr < cdEnd && curr+46 <= len(out) { |
| 123 | if !bytes.Equal(out[curr:curr+4], cdSig) { |
| 124 | break |
| 125 | } |
| 126 | fnLen := binary.LittleEndian.Uint16(out[curr+28 : curr+30]) |
| 127 | extraLen := binary.LittleEndian.Uint16(out[curr+30 : curr+32]) |
| 128 | commentLen := binary.LittleEndian.Uint16(out[curr+32 : curr+34]) |
| 129 | |
| 130 | localOffset := binary.LittleEndian.Uint32(out[curr+42 : curr+46]) |
| 131 | binary.LittleEndian.PutUint32(out[curr+42:curr+46], localOffset+prefixLen) |
| 132 | |
| 133 | curr += 46 + int(fnLen) + int(extraLen) + int(commentLen) |
| 134 | } |
| 135 | |
| 136 | // Update EOCD start of CD offset |
| 137 | binary.LittleEndian.PutUint32(out[eocdOffset+16:eocdOffset+20], cdOffset+prefixLen) |
| 138 | |
| 139 | return out, nil |
| 140 | } |
| 141 | |
| 142 | // ExtractJPEGZIP extracts payload from a JPEG+ZIP polyglot. |
| 143 | func ExtractJPEGZIP(polyglotBytes []byte, payloadFilename string) ([]byte, error) { |
| 144 | if payloadFilename == "" { |
| 145 | payloadFilename = "contraband_data.bin" |
| 146 | } |
| 147 | |
| 148 | reader, err := zip.NewReader(bytes.NewReader(polyglotBytes), int64(len(polyglotBytes))) |
| 149 | if err != nil { |
| 150 | return nil, fmt.Errorf("failed to open ZIP reader on polyglot: %w", err) |
| 151 | } |
| 152 | |
| 153 | for _, file := range reader.File { |
| 154 | if file.Name == payloadFilename { |
| 155 | rc, err := file.Open() |
| 156 | if err != nil { |
| 157 | return nil, err |
| 158 | } |
| 159 | defer rc.Close() |
| 160 | return io.ReadAll(rc) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return nil, fmt.Errorf("file %q not found in polyglot ZIP archive", payloadFilename) |
| 165 | } |