Import Parser [Go]
What?
Source
package main
import (
"debug/pe"
"fmt"
"os"
"strings"
)
type PEData struct {
Handle *os.File // Handle to the file
Filename string // Filename we are loading- not necessary but good to keep in mind
LibsToFuncs map[string][]string // All libs carry an array of imported functions
}
var (
PED PEData
)
func CE(x error, m string) {
if x != nil {
fmt.Println(m + x.Error())
os.Exit(0)
}
}
func ElemExists(arr []string, target string) bool {
for _, elem := range arr {
if elem == target {
return true
}
}
return false
}
func ParsePE(filename string) {
var x error
PED.Handle, x = os.Open(filename)
CE(x, "[-] | Error opening file - ")
defer PED.Handle.Close()
parsed, x1 := pe.NewFile(PED.Handle)
CE(x1, "[-] | Error parsing PE file - ")
IT, x2 := parsed.ImportedSymbols()
CE(x2, "[-] | Error grabbing the imported symbol list - ")
PED.LibsToFuncs = make(map[string][]string, 0)
// Init modules in map
for _, module := range IT {
splti := strings.Split(module, ":")
module := splti[1]
_, exists := PED.LibsToFuncs[module]
if !exists {
PED.LibsToFuncs[module] = []string{}
}
}
for _, symbol := range IT {
split := strings.Split(symbol, ":")
mod := split[1]
sym := split[0]
if v2, ok := PED.LibsToFuncs[mod]; ok {
if !ElemExists(v2, sym) {
PED.LibsToFuncs[mod] = append(PED.LibsToFuncs[mod], sym)
}
}
}
}
func main() {
if len(os.Args) != 2 {
fmt.Println("[-] | go run main.go <fname>")
os.Exit(0)
}
ParsePE(os.Args[1])
for k, v := range PED.LibsToFuncs {
fmt.Println("Library - " + k)
for c, symbols := range v {
fmt.Println("\t Sym(", c, ") - "+symbols)
}
}
}Output / Example

Last updated
