Skip to content

Instantly share code, notes, and snippets.

@d0rc
Created September 12, 2024 10:00
Show Gist options
  • Save d0rc/76776e07208a49b7dbf88fae20f0bdcb to your computer and use it in GitHub Desktop.
Save d0rc/76776e07208a49b7dbf88fae20f0bdcb to your computer and use it in GitHub Desktop.
package main
import (
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strings"
)
// #include <cpuid.h>
import "C"
var verbose bool
func init() {
flag.BoolVar(&verbose, "v", false, "Verbose mode: dump final string before hashing")
flag.Parse()
}
func getMACAddresses() ([]string, error) {
var macAddresses []string
netPath := "/sys/class/net"
entries, err := ioutil.ReadDir(netPath)
if err != nil {
return nil, fmt.Errorf("error reading directory %s: %v", netPath, err)
}
for _, entry := range entries {
interfacePath := filepath.Join(netPath, entry.Name())
// Resolve symlink if it exists
realPath, err := filepath.EvalSymlinks(interfacePath)
if err != nil {
continue // Skip if we can't resolve the symlink
}
// Check if it's a PCI device and not virtual
if strings.Contains(realPath, "/devices/pci") && !strings.Contains(realPath, "virtual") {
addressPath := filepath.Join(interfacePath, "address")
macBytes, err := ioutil.ReadFile(addressPath)
if err != nil {
fmt.Printf("Error reading MAC address for %s: %v\n", entry.Name(), err)
continue
}
mac := strings.TrimSpace(string(macBytes))
if mac != "" {
macAddresses = append(macAddresses, mac)
}
}
}
sort.Strings(macAddresses)
return macAddresses, nil
}
func getCPUFeatures() (uint32, uint32, uint32, uint32) {
var eax, ebx, ecx, edx C.uint
C.__get_cpuid(1, &eax, &ebx, &ecx, &edx)
return uint32(eax), uint32(ebx), uint32(ecx), uint32(edx)
}
func applyMD5Times(input string, times int) string {
result := input
for i := 0; i < times; i++ {
hasher := md5.New()
hasher.Write([]byte(result))
result = hex.EncodeToString(hasher.Sum(nil))
}
return result
}
func md5ToUUID(md5sum string) (string, error) {
// Ensure the input is a valid MD5 sum (32 hexadecimal characters)
if len(md5sum) != 32 || !isHex(md5sum) {
return "", fmt.Errorf("invalid MD5 sum: %s", md5sum)
}
// Format the MD5 sum as a UUID
return fmt.Sprintf("%s-%s-%s-%s-%s",
md5sum[0:8],
md5sum[8:12],
md5sum[12:16],
md5sum[16:20],
md5sum[20:32]), nil
}
// Helper function to check if a string is hexadecimal
func isHex(s string) bool {
for _, c := range s {
if !strings.ContainsRune("0123456789abcdefABCDEF", c) {
return false
}
}
return true
}
func main() {
// Get MAC addresses
macAddresses, err := getMACAddresses()
if err != nil {
fmt.Printf("Error getting MAC addresses: %v\n", err)
return
}
// Sort MAC addresses
sort.Strings(macAddresses)
// Concatenate MAC addresses
macConcat := strings.Join(macAddresses, "")
// Get CPU features
eax, ebx, ecx, edx := getCPUFeatures()
// Concatenate MAC addresses and CPU features
cpuFeatures := fmt.Sprintf("%08x%08x%08x%08x", eax, ebx, ecx, edx)
finalString := macConcat + cpuFeatures
if verbose {
fmt.Printf("Final string before hashing: %s\nCPU: %s\nMACs: %s\n",
finalString,
cpuFeatures,
macConcat)
}
// Calculate MD5 hash 32 times
hashString := applyMD5Times(finalString, 32)
uuid, err := md5ToUUID(hashString)
if err != nil {
fmt.Printf("Error parsing hashString: %s to UUID\n", hashString)
}
fmt.Printf("Final Hash (MD5 applied 32 times): %s\nFinal UUID: %s\n", hashString, uuid)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment