-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·151 lines (124 loc) · 3.27 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"golang.org/x/sys/windows"
"github.com/audibleblink/dllinquent"
"github.com/audibleblink/getsystem"
"github.com/audibleblink/memutils"
)
const (
RPCRT4DLL = `RPCRT4.dll`
serverRole = "RpcServerListen"
clientRole = "RpcStringBindingCompose"
)
type Result struct {
Pid int `json:"pid"`
Name string `json:"name"`
User string `json:"user"`
Cmd string `json:"cmd"`
Path string `json:"path"`
Role string `json:"role"`
}
type PebLdrDataTableEntry64 struct {
InOrderLinks [16]byte
InMemoryOrderLinks [16]byte
InInitializationOrderLinks [16]byte
DllBase uint64
EntryPoint uint64
SizeOfImage uint64
FullDllName windows.NTString
BaseDllName windows.NTString
Flags uint32
LoadCount uint16 // named ObseleteLoadCount OS6.2+
TlsIndex uint16
HashLinks [16]byte // increase by PVOID+ULONG if <OS6.2
}
var doSystem bool
func init() {
flag.BoolVar(&doSystem, "system", false, "launch system prompt")
flag.Parse()
}
func main() {
if doSystem {
pid := memutils.PidForName("winlogon.exe")
err := getsystem.InNewProcess(pid, "cmd.exe", false)
if err != nil {
fmt.Fprintln(os.Stderr, "You sure you're admin?")
}
os.Exit(0)
}
processes, err := memutils.Processes()
bail(err, "failed to list procs")
getsystem.DebugPriv()
for _, proc := range processes {
walker, err := dllinquent.NewPebWalker(proc.Pid)
bail(err, "couldn't read peb for pid %d | %s", proc.Pid, err)
var pe dllinquent.Dll
for walker.Walk() {
dll := walker.Dll()
if pe == (dllinquent.Dll{}) {
pe = dll
}
if strings.ToLower(dll.DllBaseName) != strings.ToLower(RPCRT4DLL) {
continue
}
peFile, err := memutils.CarveOutPE(walker.Handle, walker.PEB, pe.LdrDataTableEntry.SizeOfImage)
bail(err, "couldn't carve PE for pid %d | %s\n", proc.Pid, err)
imports, err := peFile.ImportedSymbols()
bail(err, "can't read imports (%d) | %s\n", proc.Pid, err)
role := getRole(imports)
if role == "" {
continue
}
user, err := getsystem.TokenOwnerFromPid(proc.Pid)
bail(err, "%s (%d) | %s\n", proc.Exe, proc.Pid, err)
result := Result{
Pid: proc.Pid,
Name: proc.Exe,
User: user,
Cmd: walker.PEB.ProcessParameters.CommandLine.String(),
Path: walker.PEB.ProcessParameters.ImagePathName.String(),
Role: role,
}
out, err := json.Marshal(result)
if err != nil {
fmt.Fprintf(os.Stderr, "jsonMarshal: %s\n", err)
continue
}
fmt.Println(string(out))
}
}
}
// searches imported functions and checks if any indicate
// the role of hosting image as being and client or server
func getRole(imports []string) string {
var isClient bool
var isServer bool
for _, imp := range imports {
if strings.HasPrefix(imp, serverRole) {
isServer = true
}
if strings.HasPrefix(imp, clientRole) {
isClient = true
}
}
if isClient && isServer {
return "BOTH"
} else if isClient {
return "CLIENT"
} else if isServer {
return "SERVER"
} else {
return ""
}
}
func bail(err error, msg string, i ...interface{}) {
if err != nil {
fmt.Fprintf(os.Stderr, msg, i...)
os.Exit(1)
}
}