Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2 Add binary analysis #3

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package goInspector
CatalinStratu marked this conversation as resolved.
Show resolved Hide resolved
CatalinStratu marked this conversation as resolved.
Show resolved Hide resolved

import (
"encoding/json"
"github.com/goretk/gore"
"path/filepath"
)

type Vendor struct {
Name string `json:"name"`
}

// InspectLibraries return all 3rd party packages used by the binary
func InspectLibraries(filepath string) ([]*gore.Package, error) {
f, err := gore.Open(filepath)
if err != nil {
return nil, err
}

defer func(f *gore.GoFile) {
err := f.Close()
if err != nil {

}
}(f)

packages, err := f.GetVendors()
if err != nil {
return nil, err
}

return packages, nil
}

func ConvertToJSONWithPosixPaths(path string) ([]byte, error) {
CatalinStratu marked this conversation as resolved.
Show resolved Hide resolved
CatalinStratu marked this conversation as resolved.
Show resolved Hide resolved
vendors, err := InspectLibraries(path)
if err != nil {
return nil, err
}

// Convert the data to use POSIX paths
for i := range vendors {
vendors[i].Name = filepath.ToSlash(vendors[i].Name)
}

// Marshal the data to JSON
dataJSON, err := json.Marshal(vendors)
if err != nil {
return nil, err
}

return dataJSON, nil
}
24 changes: 24 additions & 0 deletions examples/InspectLibraries/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
goInspector "github.com/nexB/go-inspector"
"os"
)

func main() {
args := os.Args
if len(args) != 2 {
fmt.Printf("Usage: %v \n", args[0])
fmt.Printf(" Load executable file file, and\n")
fmt.Printf(" print portions of its creation info data.\n")
return
}
jsonData, err := goInspector.ConvertToJSONWithPosixPaths(args[1])
if err != nil {
CatalinStratu marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("Error:", err)
os.Exit(1)
}

fmt.Println(string(jsonData))
}
Binary file added examples/files/testfile.exe
Binary file not shown.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/nexB/go-inspector

go 1.20

require github.com/goretk/gore v0.11.1

require (
golang.org/x/arch v0.0.0-20220412001346-fc48f9fe4c15 // indirect
golang.org/x/mod v0.5.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/goretk/gore v0.11.1 h1:QEn9bI0ty//Dtk1sa2yYtssGzZHJIeROehiJEm953DQ=
github.com/goretk/gore v0.11.1/go.mod h1:wZt6TJIR3gZJplzAApeIXA1BjQGd3PKbc2XincS3TAc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
golang.org/x/arch v0.0.0-20220412001346-fc48f9fe4c15 h1:GVfVkciLYxn5mY5EncwAe0SXUn9Rm81rRkZ0TTmn/cU=
golang.org/x/arch v0.0.0-20220412001346-fc48f9fe4c15/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 h1:dbuHpmKjkDzSOMKAWl10QNlgaZUd3V1q99xc81tt2Kc=