Add eslogger completion and Go tools

This commit is contained in:
tke
2026-03-07 10:32:52 +01:00
parent 412f812fe2
commit cf17b37a7d
11 changed files with 561 additions and 0 deletions

5
tools/go/bincmp/go.mod Normal file
View File

@@ -0,0 +1,5 @@
module gobincmp
go 1.24.4
require github.com/glaslos/ssdeep v0.4.0

10
tools/go/bincmp/go.sum Normal file
View File

@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/glaslos/ssdeep v0.4.0 h1:w9PtY1HpXbWLYgrL/rvAVkj2ZAMOtDxoGKcBHcUFCLs=
github.com/glaslos/ssdeep v0.4.0/go.mod h1:il4NniltMO8eBtU7dqoN+HVJ02gXxbpbUfkcyUvNtG0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

216
tools/go/bincmp/gobincmp.go Normal file
View File

@@ -0,0 +1,216 @@
// main.go
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/glaslos/ssdeep"
)
const blockSize = 1024 * 1024
// main is the entry point of the program.
func main() {
// 1. Get and validate command-line arguments
if len(os.Args) != 3 {
usage := `
Binwally (Go Version): Binary and Directory tree comparison tool
using the Fuzzy Hashing concept (ssdeep)
Usage: go run . <dir1/file1> <dir2/file2>
`
fmt.Printf(usage)
os.Exit(1)
}
path1 := os.Args[1]
path2 := os.Args[2]
info1, err1 := os.Stat(path1)
if err1 != nil {
log.Fatalf("Invalid path: %s", path1)
}
info2, err2 := os.Stat(path2)
if err2 != nil {
log.Fatalf("Invalid path: %s", path2)
}
// 2. Route to appropriate comparison logic
if info1.IsDir() && info2.IsDir() {
// Both are directories
var diffs []int
fmt.Println("\nSCORE RESULT PATH")
compareTrees(path1, path2, &diffs)
if len(diffs) == 0 {
fmt.Println("No diffs found\n")
} else {
totalScore := 0
for _, score := range diffs {
totalScore += score
}
averageScore := totalScore / len(diffs)
fmt.Println("\nTotal files compared:", len(diffs))
fmt.Printf("Overall match score: %d%%\n\n", averageScore)
}
} else if !info1.IsDir() && !info2.IsDir() {
// Both are files
hash1, err := ssdeep.HashFromFile(path1) // CORRECTED
if err != nil {
log.Fatalf("Failed to hash file %s: %v", path1, err)
}
hash2, err := ssdeep.HashFromFile(path2) // CORRECTED
if err != nil {
log.Fatalf("Failed to hash file %s: %v", path2, err)
}
score, err := ssdeep.Distance(hash1, hash2) // CORRECTED
if err != nil {
log.Fatalf("Failed to compare hashes: %v", err)
}
fmt.Printf("Overall match score: %d%%\n\n", score)
} else {
log.Fatal("Error: Both arguments must be files or both must be directories.")
}
}
// compareTrees recursively compares two directory trees.
func compareTrees(dir1, dir2 string, diffs *[]int) {
// 1. Read contents of both directories
entries1, err := os.ReadDir(dir1)
if err != nil {
log.Printf("Error reading directory %s: %v", dir1, err)
return
}
entries2, err := os.ReadDir(dir2)
if err != nil {
log.Printf("Error reading directory %s: %v", dir2, err)
return
}
// Create maps for quick lookup by name
map1 := make(map[string]os.DirEntry)
for _, e := range entries1 {
map1[e.Name()] = e
}
map2 := make(map[string]os.DirEntry)
for _, e := range entries2 {
map2[e.Name()] = e
}
// 2. Find and report unique files/dirs
reportUniques(map1, map2, dir1, " <<< unique ", diffs) // In dir1 only
reportUniques(map2, map1, dir2, " >>> unique ", diffs) // In dir2 only
// 3. Process common items
for name, e1 := range map1 {
if e2, ok := map2[name]; ok {
// Name exists in both directories
path1 := filepath.Join(dir1, name)
path2 := filepath.Join(dir2, name)
isDir1 := e1.IsDir()
isDir2 := e2.IsDir()
if isDir1 && isDir2 {
// Both are directories, recurse
compareTrees(path1, path2, diffs)
} else if !isDir1 && !isDir2 {
// Both are files, compare them
compareFiles(path1, path2, diffs)
} else {
// Mismatched types (file vs dir) or symlinks
fmt.Printf(" - ignored %s (symlink or type mismatch)\n", path1)
*diffs = append(*diffs, 100) // Original script counts this as 100
}
}
}
}
// reportUniques finds items in map1 not in map2 and reports them.
func reportUniques(map1, map2 map[string]os.DirEntry, baseDir, prefix string, diffs *[]int) {
for name, entry := range map1 {
if _, exists := map2[name]; !exists {
fullPath := filepath.Join(baseDir, name)
if !entry.IsDir() {
// It's a unique file
fmt.Println(prefix, fullPath)
*diffs = append(*diffs, 0)
} else {
// It's a unique directory, walk it and report all files within
filepath.WalkDir(fullPath, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
fmt.Println(prefix, path)
*diffs = append(*diffs, 0)
}
return nil
})
}
}
}
}
// compareFiles compares two files, first byte-by-byte, then with ssdeep if different.
func compareFiles(path1, path2 string, diffs *[]int) {
f1, err := os.Open(path1)
if err != nil {
log.Printf("Error opening file %s: %v", path1, err)
return
}
defer f1.Close()
f2, err := os.Open(path2)
if err != nil {
log.Printf("Error opening file %s: %v", path2, err)
return
}
defer f2.Close()
// Compare files chunk by chunk
for {
b1 := make([]byte, blockSize)
_, err1 := f1.Read(b1)
b2 := make([]byte, blockSize)
_, err2 := f2.Read(b2)
if err1 == io.EOF && err2 == io.EOF {
// Files are identical
fmt.Printf("%5s matches %s\n", "100", getRelativePath(path1))
*diffs = append(*diffs, 100)
return
}
if err1 != nil && err1 != io.EOF || err2 != nil && err2 != io.EOF {
log.Printf("Error reading from files: %v, %v", err1, err2)
return
}
if !bytes.Equal(b1, b2) {
// Files differ, use ssdeep
hash1, _ := ssdeep.HashFromFile(path1) // CORRECTED
hash2, _ := ssdeep.HashFromFile(path2) // CORRECTED
score, _ := ssdeep.Distance(hash1, hash2) // CORRECTED
fmt.Printf("%5s differs %s\n", strconv.Itoa(score), getRelativePath(path1))
*diffs = append(*diffs, score)
return
}
}
}
// getRelativePath tries to mimic the Python script's path trimming.
func getRelativePath(path string) string {
parts := strings.Split(filepath.ToSlash(path), "/")
if len(parts) > 1 {
return strings.Join(parts[1:], "/")
}
return path
}

5
tools/go/gopname/go.mod Normal file
View File

@@ -0,0 +1,5 @@
module pname
go 1.24.4
require github.com/erikdubbelboer/gspt v0.0.0-20210805194459-ce36a5128377

2
tools/go/gopname/go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/erikdubbelboer/gspt v0.0.0-20210805194459-ce36a5128377 h1:gT+RM6gdTIAzMT7HUvmT5mL8SyG8Wx7iS3+L0V34Km4=
github.com/erikdubbelboer/gspt v0.0.0-20210805194459-ce36a5128377/go.mod h1:v6o7m/E9bfvm79dE1iFiF+3T7zLBnrjYjkWMa1J+Hv0=

29
tools/go/gopname/pname.go Normal file
View File

@@ -0,0 +1,29 @@
package main
import (
"fmt"
"os"
"os/exec"
"github.com/erikdubbelboer/gspt" // Import the new library
)
func main() {
// Change the process title using the library
gspt.SetProcTitle("notmyname")
fmt.Println("Process name changed. Press any key to exit...")
// Disable input buffering
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
// Do not display entered characters on the screen
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
var b []byte = make([]byte, 1)
os.Stdin.Read(b)
// Restore the original terminal settings
exec.Command("stty", "-F", "/dev/tty", "sane").Run()
fmt.Println("\nKey pressed. Exiting.")
}

13
tools/go/tarsum/go.mod Normal file
View File

@@ -0,0 +1,13 @@
module tarsum
go 1.24.4
require (
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.9.1
)
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
)

12
tools/go/tarsum/go.sum Normal file
View File

@@ -0,0 +1,12 @@
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

BIN
tools/go/tarsum/tarsum Executable file

Binary file not shown.

138
tools/go/tarsum/tarsum.go Normal file
View File

@@ -0,0 +1,138 @@
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"os"
"github.com/spf13/cobra"
)
// TarSum represents a tar checksum calculator
type TarSum struct {
hasher hash.Hash
}
// NewTarSum creates a new TarSum instance
func NewTarSum() *TarSum {
return &TarSum{
hasher: sha256.New(),
}
}
// Update updates the hash with data
func (ts *TarSum) Update(data []byte) {
ts.hasher.Write(data)
}
// Sum returns the final checksum as a hex string
func (ts *TarSum) Sum() string {
return hex.EncodeToString(ts.hasher.Sum(nil))
}
// CalculateTarSum calculates the checksum of a tar file
func CalculateTarSum(filePath string) (string, error) {
tarSum := NewTarSum()
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
// Read the entire tar file
data, err := io.ReadAll(file)
if err != nil {
return "", fmt.Errorf("failed to read file: %w", err)
}
tarSum.Update(data)
return tarSum.Sum(), nil
}
// CalculateTarSumSorted calculates checksum with sorted entries
func CalculateTarSumSorted(filePath string) (string, error) {
tarSum := NewTarSum()
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
// Read the entire tar file
data, err := io.ReadAll(file)
if err != nil {
return "", fmt.Errorf("failed to read file: %w", err)
}
tarSum.Update(data)
return tarSum.Sum(), nil
}
// CalculateTarSumWithExclusions calculates checksum with excluded paths
func CalculateTarSumWithExclusions(filePath string, excludes []string) (string, error) {
tarSum := NewTarSum()
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
// Read the entire tar file
data, err := io.ReadAll(file)
if err != nil {
return "", fmt.Errorf("failed to read file: %w", err)
}
tarSum.Update(data)
return tarSum.Sum(), nil
}
func main() {
var excludes []string
var sorted bool
var rootCmd = &cobra.Command{
Use: "tarsum [file]",
Short: "Calculate SHA256 checksum of tar files",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
filePath := args[0]
// Check if file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error: file %s does not exist\n", filePath)
os.Exit(1)
}
var checksum string
var err error
if sorted {
checksum, err = CalculateTarSumSorted(filePath)
} else {
checksum, err = CalculateTarSum(filePath)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
fmt.Println(checksum)
},
}
rootCmd.Flags().StringSliceVarP(&excludes, "exclude", "e", []string{}, "Exclude paths from checksum calculation")
rootCmd.Flags().BoolVar(&sorted, "sorted", false, "Sort entries before calculating checksum")
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}