-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsg.go
More file actions
354 lines (306 loc) · 7.58 KB
/
psg.go
File metadata and controls
354 lines (306 loc) · 7.58 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// psg.go
package main
import (
"flag"
"fmt"
"os"
"sort"
"strconv"
"strings"
"syscall"
"unicode/utf8"
"unsafe"
"github.com/disiqueira/gotree"
)
type ulong int32
type ulong_ptr uintptr
type PROCESSENTRY32 struct {
dwSize ulong
cntUsage ulong
th32ProcessID ulong
th32DefaultHeapID ulong_ptr
th32ModuleID ulong
cntThreads ulong
th32ParentProcessID ulong
pcPriClassBase ulong
dwFlags ulong
szExeFile [260]byte
}
const LARGE_BUFFER_SIZE = 256 * 1024 * 1024
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
psapi = syscall.NewLazyDLL("psapi.dll")
GetProcessImageFileName = psapi.NewProc("GetProcessImageFileNameA")
)
type Process struct {
Name string
Pid int
PPid int
}
type ProcSlice []Process
func (p ProcSlice) Len() int {
return len(p)
}
func (p ProcSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p ProcSlice) Less(i, j int) bool {
return p[i].Pid < p[j].Pid
}
func main() {
var (
Name string
Pid int = -1
PPid int = -1
Tree bool
)
flag.IntVar(&Pid, "p", -1, "pid")
flag.BoolVar(&Tree, "t", false, "tree")
flag.Parse()
procs := getAllProcess()
if flag.NArg() > 0 {
Name = flag.Arg(0)
Ppid, err := strconv.Atoi(Name)
if err == nil {
PPid = Ppid
}
}
if !sort.IsSorted(ProcSlice(procs)) {
sort.Sort(ProcSlice(procs))
}
if Tree {
PrintTree(procs, Name, Pid)
os.Exit(0)
}
fmt.Println("PID\t PPID\t CMD")
for _, p := range procs {
if Pid != -1 && p.Pid != Pid {
continue
}
if flag.NArg() > 0 {
if p.PPid == PPid || strings.Contains(p.Name, Name) {
fmt.Println(p.Pid, "\t", p.PPid, "\t", p.Name)
}
} else {
fmt.Println(p.Pid, "\t", p.PPid, "\t", p.Name)
}
}
}
type tree struct {
pid int
name string
child []int
}
func (t tree) String() string {
return fmt.Sprintf("(%d)%s", t.pid, t.name)
}
func (t *tree) Tree(Pmaps map[int]*tree) gotree.Tree {
artist := gotree.New(t.String())
for _, v := range t.child {
if v1, ok := Pmaps[v]; ok {
artist.AddTree(v1.Tree(Pmaps))
}
}
return artist
}
func PrintTree(procs []Process, pid int) {
Pmaps := make(map[int]*tree)
h, _ := os.Hostname()
host := tree{00, h, make([]int, 0, 20)}
Pmaps[00] = &host
for _, v := range procs {
t := tree{v.Pid, v.Name, make([]int, 0, 20)}
Pmaps[v.Pid] = &t
}
for _, v := range procs {
v1, ok := Pmaps[v.PPid]
if ok && v.Pid != v.PPid {
v1.child = append(v1.child, v.Pid)
} else {
host.child = append(host.child, v.Pid)
}
}
if pid != -1 {
if v, ok := Pmaps[pid]; ok {
fmt.Println(v.Tree(Pmaps).Print())
}
} else if Name != "" {
for _, v := range procs {
if strings.Contains(v.Name, Name) {
if v1, ok := Pmaps[v.pid]; ok {
fmt.Println(v1.Tree(Pmaps).Print())
}
}
}
} else {
fmt.Println(host.Tree(Pmaps).Print())
}
}
func getAllProcess() []Process {
snapshot := kernel32.NewProc("CreateToolhelp32Snapshot")
pHandle, _, _ := snapshot.Call(uintptr(0x2), uintptr(0x0))
if int(pHandle) == -1 {
return nil
}
Process32Next := kernel32.NewProc("Process32Next")
procs := make([]Process, 0, 300)
for {
var proc PROCESSENTRY32
proc.dwSize = ulong(unsafe.Sizeof(proc))
if rt, _, _ := Process32Next.Call(uintptr(pHandle), uintptr(unsafe.Pointer(&proc))); int(rt) == 1 {
//name, err := queryProc(int(proc.th32ProcessID))
//if err != nil {
name := string(proc.szExeFile[0:23])
//}
procs = append(procs, Process{name, int(proc.th32ProcessID), int(proc.th32ParentProcessID)})
//fmt.Println(name, ":", proc.th32ProcessID)
} else {
break
}
}
CloseHandle := kernel32.NewProc("CloseHandle")
_, _, _ = CloseHandle.Call(pHandle)
return procs
}
func queryProc(pid int) (string, error) {
var szExeFile [260]byte
handle, err := syscall.OpenProcess(0X0400|0X0010, false, uint32(pid))
if err != nil {
//fmt.Println(err)
return "", err
}
defer syscall.CloseHandle(handle)
GetProcessImageFileName.Call(uintptr(handle), uintptr(unsafe.Pointer(&szExeFile)), 260)
fullName := string(szExeFile[0:])
name := PathMap[fullName[0:23]]
result := strings.Replace(fullName, fullName[0:23], name, 23)
return result, nil
}
/*func FullName() {
GetProcessImageFileName := psapi.NewProc("GetProcessImageFileNameA")
GetProcessImageFileName.Call(uintptr(handle), uintptr(unsafe.Pointer(&szExeFile)), 260)
mystring CCommon::DosDevicePath2LogicalPath(LPCTSTR lpszDosPath)
{
mystring strResult;
// Translate path with device name to drive letters.
TCHAR szTemp[MAX_PATH];
szTemp[0] = '\0';
if ( lpszDosPath==NULL || !GetLogicalDriveStrings(_countof(szTemp)-1, szTemp) ){
return strResult;
}
TCHAR szName[MAX_PATH];
TCHAR szDrive[3] = TEXT(" :");
BOOL bFound = FALSE;
TCHAR* p = szTemp;
do{
// Copy the drive letter to the template string
*szDrive = *p;
// Look up each device name
if ( QueryDosDevice(szDrive, szName, _countof(szName)) ){
UINT uNameLen = (UINT)_tcslen(szName);
if (uNameLen < MAX_PATH)
{
bFound = _tcsnicmp(lpszDosPath, szName, uNameLen) == 0;
if ( bFound ){
// Reconstruct pszFilename using szTemp
// Replace device path with DOS path
TCHAR szTempFile[MAX_PATH];
_stprintf_s(szTempFile, TEXT("%s%s"), szDrive, lpszDosPath+uNameLen);
strResult = szTempFile;
}
}
}
// Go to the next NULL character.
while (*p++);
} while (!bFound && *p); // end of string
return strResult;
}
}*/
var PathMap map[string]string
func getDiskInfo() {
PathMap = make(map[string]string)
GetLogicalDriveStringsW := kernel32.NewProc("GetLogicalDriveStringsW")
lpBuffer := make([]byte, 254)
lpBuffer1 := make([]byte, 100)
diskret, _, _ := GetLogicalDriveStringsW.Call(
uintptr(len(lpBuffer)),
uintptr(unsafe.Pointer(&lpBuffer[0])))
if diskret == 0 {
return
}
QueryDosDeviceW := kernel32.NewProc("QueryDosDeviceW")
for _, v := range lpBuffer {
if v >= 65 && v <= 90 {
path := string(v) + ":"
if path == "A:" || path == "B:" {
continue
}
_, _, e := QueryDosDeviceW.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
uintptr(unsafe.Pointer(&lpBuffer1[0])),
uintptr(len(lpBuffer1)))
if e != nil {
//fmt.Println(e)
}
//fmt.Println(path, "=", string(lpBuffer1[0:]), len(lpBuffer1[0:]))
a := TrimAll(lpBuffer1[0:], "\x00")
//fmt.Println(path, "=", string(a), len(a))
PathMap[string(a)] = path
}
}
}
func TrimAll(s []byte, cutset string) []byte {
return TrimAllFunc(s, makeCutsetFunc(cutset))
}
func TrimAllFunc(s []byte, f func(r rune) bool) []byte {
n := make([]byte, 0, len(s))
start := 0
for start < len(s) {
wid := 1
r := rune(s[start])
if r >= utf8.RuneSelf {
r, wid = utf8.DecodeRune(s[start:])
}
if f(r) == false {
n = append(n, s[start])
}
start += wid
}
return n
}
func makeCutsetFunc(cutset string) func(r rune) bool {
if len(cutset) == 1 && cutset[0] < utf8.RuneSelf {
return func(r rune) bool {
return r == rune(cutset[0])
}
}
if as, isASCII := makeASCIISet(cutset); isASCII {
return func(r rune) bool {
return r < utf8.RuneSelf && as.contains(byte(r))
}
}
return func(r rune) bool {
for _, c := range cutset {
if c == r {
return true
}
}
return false
}
}
type asciiSet [8]uint32
// makeASCIISet creates a set of ASCII characters and reports whether all
// characters in chars are ASCII.
func makeASCIISet(chars string) (as asciiSet, ok bool) {
for i := 0; i < len(chars); i++ {
c := chars[i]
if c >= utf8.RuneSelf {
return as, false
}
as[c>>5] |= 1 << uint(c&31)
}
return as, true
}
// contains reports whether c is inside the set.
func (as *asciiSet) contains(c byte) bool {
return (as[c>>5] & (1 << uint(c&31))) != 0
}