Initial commit

This commit is contained in:
2024-10-01 22:38:57 +02:00
commit 20d9dbcf9f
5 changed files with 138 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
main.log

5
go.mod Normal file
View File

@@ -0,0 +1,5 @@
module hitman
go 1.23.0
require golang.org/x/sys v0.25.0

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

38
main.go Normal file
View File

@@ -0,0 +1,38 @@
package main
import (
"fmt"
"io"
"log"
"os"
)
var Error *log.Logger
var Warning *log.Logger
func init() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
logFile, err := os.Create("main.log")
if err != nil {
log.Printf("Error creating log file: %v", err)
os.Exit(1)
}
logger := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(logger)
Error = log.New(io.MultiWriter(logFile, os.Stderr, os.Stdout),
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
log.Lmicroseconds|log.Lshortfile)
Warning = log.New(io.MultiWriter(logFile, os.Stdout),
fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"),
log.Lmicroseconds|log.Lshortfile)
}
func main() {
procmap, err := BuildProcessMap()
if err != nil {
Error.Printf("Error building process map: %v", err)
return
}
log.Printf("%#v", procmap)
}

92
procmap.go Normal file
View File

@@ -0,0 +1,92 @@
package main
import (
"unsafe"
"golang.org/x/sys/windows"
)
type (
ProcessMap struct {
Map map[uint32]*ProcessMapNode
NameIndex map[string][]*ProcessMapNode
}
ProcessMapNode struct {
Proc windows.ProcessEntry32
Name string
Children map[uint32]*ProcessMapNode
}
)
func (pt *ProcessMap) add(proc *windows.ProcessEntry32) error {
if pt.Map == nil {
pt.Map = make(map[uint32]*ProcessMapNode)
}
if pt.NameIndex == nil {
pt.NameIndex = make(map[string][]*ProcessMapNode)
}
procNode := &ProcessMapNode{
Proc: *proc,
Name: windows.UTF16ToString(proc.ExeFile[:]),
}
_, ok := pt.NameIndex[procNode.Name]
if !ok {
pt.NameIndex[procNode.Name] = make([]*ProcessMapNode, 0)
}
pt.NameIndex[procNode.Name] = append(pt.NameIndex[procNode.Name], procNode)
parent, parentExists := pt.Map[proc.ParentProcessID]
if !parentExists {
parent = procNode
pt.Map[proc.ParentProcessID] = parent
}
child, childExists := pt.Map[proc.ProcessID]
if !childExists {
child = procNode
pt.Map[proc.ProcessID] = child
}
if parent.Children == nil {
parent.Children = make(map[uint32]*ProcessMapNode)
}
parent.Children[proc.ProcessID] = child
return nil
}
func (pt *ProcessMap) findByName(name string) ([]*ProcessMapNode, bool) {
val, ok := pt.NameIndex[name]
return val, ok
}
func (pt *ProcessMap) findByPid(pid uint32) (*ProcessMapNode, bool) {
val, ok := pt.Map[pid]
return val, ok
}
func BuildProcessMap() (*ProcessMap, error) {
tree := &ProcessMap{}
snapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
return nil, err
}
defer windows.CloseHandle(snapshot)
var pe32 windows.ProcessEntry32
pe32.Size = uint32(unsafe.Sizeof(pe32))
var i int
err = windows.Process32First(snapshot, &pe32)
for err == nil {
tree.add(&pe32)
i++
if i > 500 {
break
}
err = windows.Process32Next(snapshot, &pe32)
}
return tree, nil
}