Repository navigation
Expand file tree
/
Copy pathinject.go
More file actions
88 lines (77 loc) · 2.38 KB
/
Copy pathinject.go
File metadata and controls
88 lines (77 loc) · 2.38 KB
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
package main
import (
"errors"
"os"
"syscall"
clog "github.com/charmbracelet/log"
ps "github.com/mitchellh/go-ps"
"golang.org/x/sys/windows"
)
// Inject injects a DLL into a running process.
//
// Takes a UserSelection parameter containing the process name and DLL path to inject.
// Returns an error if any operation fails.
func Inject(userSlection *UserSelection) error {
// var dPath string
var _pId int
// var pName string
pName := userSlection.SelectedProc
dPath := userSlection.SelectedDll
// flag.StringVar(&pName, "process", "", "Process name to inject to")
// flag.StringVar(&dPath, "dll", "", "DLL to inject")
// flag.Parse()
pList, err := ps.Processes()
if err != nil {
return err
}
for pI := range pList {
process := pList[pI]
if process.Executable() == pName {
_pId = process.Pid()
break
}
}
if _pId == 0 {
// clog.Warn("Process not found (from inject.go)")
return errors.New("process not found")
}
pId := uintptr(_pId)
if _, err := os.Stat(dPath); errors.Is(err, os.ErrNotExist) {
return err
}
clog.Info("Selected process id: ", "pId", pId)
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 {
return err
}
clog.Info("Selected 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)
if err != nil {
clog.Warn(err)
}
clog.Info("Memory allocated")
bPtrDpath, err := windows.BytePtrFromString(dPath)
if err != nil {
return err
}
Zero := uintptr(0)
err = windows.WriteProcessMemory(pHandle, vAlloc, bPtrDpath, uintptr(len(dPath)+1), &Zero)
if err != nil {
return err
}
clog.Info("Allocated memory written")
LoadLibAddy, err := syscall.GetProcAddress(syscall.Handle(kernel32.Handle()), "LoadLibraryA")
if err != nil {
clog.Warn(err)
}
tHandle, _, err := kernel32.NewProc("CreateRemoteThread").Call(uintptr(pHandle), 0, 0, LoadLibAddy, vAlloc, 0, 0)
if err != nil {
clog.Warn(err)
}
defer syscall.CloseHandle(syscall.Handle(tHandle))
clog.Info("Thread created")
clog.Info("DLL injected successfully into " + userSlection.SelectedProc + "!")
return nil
}