● 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 / evasion / polyglot_pdf_jar.go
← Back to directory
130 lines · 3.7 KB
Raw
1package evasion
2
3import (
4 "archive/zip"
5 "bytes"
6 "errors"
7 "fmt"
8 "io"
9)
10
11// GenerateProceduralPDF generates a minimalist, fully valid %PDF-1.4 single-page invoice document.
12func GenerateProceduralPDF(title string, seed int64) []byte {
13 if title == "" {
14 title = "Contraband Security Audit Invoice"
15 }
16
17 var buf bytes.Buffer
18 // Minimal valid PDF-1.4 structure
19 buf.WriteString("%PDF-1.4\n")
20 buf.WriteString("%\xE2\xE3\xCF\xD3\n") // Binary comment
21
22 // Object 1: Catalog
23 offObj1 := buf.Len()
24 buf.WriteString("1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n")
25
26 // Object 2: Pages
27 offObj2 := buf.Len()
28 buf.WriteString("2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n")
29
30 // Object 3: Page
31 offObj3 := buf.Len()
32 buf.WriteString("3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << >> >>\nendobj\n")
33
34 // Object 4: Content stream
35 streamContent := fmt.Sprintf("BT /F1 18 Tf 50 700 Td (%s) Tj ET", title)
36 offObj4 := buf.Len()
37 buf.WriteString(fmt.Sprintf("4 0 obj\n<< /Length %d >>\nstream\n%s\nendstream\nendobj\n", len(streamContent), streamContent))
38
39 // xref table
40 offXref := buf.Len()
41 buf.WriteString("xref\n0 5\n")
42 buf.WriteString("0000000000 65535 f \n")
43 buf.WriteString(fmt.Sprintf("%010d 00000 n \n", offObj1))
44 buf.WriteString(fmt.Sprintf("%010d 00000 n \n", offObj2))
45 buf.WriteString(fmt.Sprintf("%010d 00000 n \n", offObj3))
46 buf.WriteString(fmt.Sprintf("%010d 00000 n \n", offObj4))
47
48 // trailer
49 buf.WriteString(fmt.Sprintf("trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n", offXref))
50
51 return buf.Bytes()
52}
53
54// BuildPDFJARPolyglot creates a polyglot file valid as both a PDF document and a JAR/ZIP archive.
55func BuildPDFJARPolyglot(pdfBytes []byte, payload []byte, payloadFilename string) ([]byte, error) {
56 if len(pdfBytes) < 8 || !bytes.HasPrefix(pdfBytes, []byte("%PDF-")) {
57 return nil, errors.New("invalid PDF data (missing %PDF- header)")
58 }
59
60 if payloadFilename == "" {
61 payloadFilename = "contraband_data.bin"
62 }
63
64 // 1. Build valid JAR/ZIP archive in memory
65 var zipBuf bytes.Buffer
66 zw := zip.NewWriter(&zipBuf)
67
68 // Required for valid JAR: META-INF/MANIFEST.MF
69 manifestW, err := zw.Create("META-INF/MANIFEST.MF")
70 if err != nil {
71 return nil, err
72 }
73 if _, err := manifestW.Write([]byte("Manifest-Version: 1.0\r\nCreated-By: Contraband Egress Agent\r\nMain-Class: com.contraband.Agent\r\n\r\n")); err != nil {
74 return nil, err
75 }
76
77 // Payload file
78 payloadW, err := zw.Create(payloadFilename)
79 if err != nil {
80 return nil, err
81 }
82 if _, err := payloadW.Write(payload); err != nil {
83 return nil, err
84 }
85
86 if err := zw.Close(); err != nil {
87 return nil, err
88 }
89
90 rawZIP := zipBuf.Bytes()
91 pdfLen := uint32(len(pdfBytes))
92
93 // 2. Adjust ZIP offsets to account for PDF prefix
94 adjustedZIP, err := adjustZIPOffsets(rawZIP, pdfLen)
95 if err != nil {
96 return nil, fmt.Errorf("failed to adjust ZIP offsets in PDF-JAR polyglot: %w", err)
97 }
98
99 // 3. Concatenate PDF + Adjusted JAR/ZIP
100 var polyglot bytes.Buffer
101 polyglot.Write(pdfBytes)
102 polyglot.Write(adjustedZIP)
103
104 return polyglot.Bytes(), nil
105}
106
107// ExtractPDFJAR extracts payload from a PDF+JAR polyglot.
108func ExtractPDFJAR(polyglotBytes []byte, payloadFilename string) ([]byte, error) {
109 if payloadFilename == "" {
110 payloadFilename = "contraband_data.bin"
111 }
112
113 reader, err := zip.NewReader(bytes.NewReader(polyglotBytes), int64(len(polyglotBytes)))
114 if err != nil {
115 return nil, fmt.Errorf("failed to open JAR/ZIP reader on PDF polyglot: %w", err)
116 }
117
118 for _, file := range reader.File {
119 if file.Name == payloadFilename {
120 rc, err := file.Open()
121 if err != nil {
122 return nil, err
123 }
124 defer rc.Close()
125 return io.ReadAll(rc)
126 }
127 }
128
129 return nil, fmt.Errorf("payload %q not found in PDF-JAR polyglot", payloadFilename)
130}