-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
71 lines (62 loc) · 1.85 KB
/
main.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
// DLL Injector
package main
import (
"errors"
"flag"
"log"
"os"
"syscall"
clog "github.com/charmbracelet/log"
ps "github.com/mitchellh/go-ps"
"golang.org/x/sys/windows"
)
func main() {
var dPath string
var _pId int
var pName string
flag.StringVar(&pName, "process", "", "Process name to inject to")
flag.StringVar(&dPath, "dll", "", "DLL to inject")
flag.Parse()
pList, err := ps.Processes()
for pI := range pList {
process := pList[pI]
if process.Executable() == pName {
_pId = process.Pid()
break
}
}
if _pId == 0 {
log.Fatal("Process not found")
}
pId := uintptr(_pId)
if _, err := os.Stat(dPath); errors.Is(err, os.ErrNotExist) {
log.Fatal(err)
}
kernel32 := windows.NewLazyDLL("kernel32.dll")
pHandle, err := windows.OpenProcess(windows.PROCESS_CREATE_THREAD|windows.PROCESS_VM_OPERATION|windows.PROCESS_VM_WRITE|windows.PROCESS_VM_READ|windows.PROCESS_QUERY_INFORMATION, false, uint32(pId))
if err != nil {
log.Fatal(err)
}
clog.Info("Process opened")
VirtualAllocEx := kernel32.NewProc("VirtualAllocEx")
vAlloc, _, err := VirtualAllocEx.Call(uintptr(pHandle), 0, uintptr(len(dPath)+1), windows.MEM_RESERVE|windows.MEM_COMMIT, windows.PAGE_EXECUTE_READWRITE)
clog.Info("Memory allocated")
bPtrDpath, err := windows.BytePtrFromString(dPath)
if err != nil {
log.Fatal(err)
}
Zero := uintptr(0)
err = windows.WriteProcessMemory(pHandle, vAlloc, bPtrDpath, uintptr(len(dPath)+1), &Zero)
if err != nil {
log.Fatal(err)
}
clog.Info("Memory written")
LoadLibAddy, err := syscall.GetProcAddress(syscall.Handle(kernel32.Handle()), "LoadLibraryA")
if err != nil {
log.Fatal(err)
}
tHandle, _, err := kernel32.NewProc("CreateRemoteThread").Call(uintptr(pHandle), 0, 0, LoadLibAddy, vAlloc, 0, 0)
defer syscall.CloseHandle(syscall.Handle(tHandle))
clog.Info("Thread created")
clog.Info("DLL injected successfully!")
}