Skip to content

Commit bba66ae

Browse files
committed
main.go: add a develop tool for calculate hash about Windows API.
1 parent aba4590 commit bba66ae

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

hashapi/main.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package main
2+
3+
import (
4+
"encoding/hex"
5+
"flag"
6+
"fmt"
7+
"log"
8+
"runtime"
9+
"strings"
10+
11+
"github.com/For-ACGN/hash-api/rorwk"
12+
)
13+
14+
var (
15+
format string
16+
modName string
17+
funcName string
18+
hexKey string
19+
concise bool
20+
)
21+
22+
func init() {
23+
var defaultFormat string
24+
switch runtime.GOARCH {
25+
case "386":
26+
defaultFormat = "32"
27+
case "amd64":
28+
defaultFormat = "64"
29+
}
30+
flag.StringVar(&format, "fmt", defaultFormat, "binary format: 32 or 64")
31+
flag.StringVar(&modName, "mod", "kernel32.dll", "module name")
32+
flag.StringVar(&funcName, "func", "WinExec", "function name")
33+
flag.StringVar(&hexKey, "key", "", "specific key, it must be hex format")
34+
flag.BoolVar(&concise, "conc", false, "print concise result for development")
35+
flag.Parse()
36+
}
37+
38+
func main() {
39+
var (
40+
numZero string
41+
apiHash []byte
42+
hashKey []byte
43+
err error
44+
)
45+
if hexKey != "" {
46+
hashKey, err = hex.DecodeString(hexKey)
47+
if err != nil {
48+
log.Fatalln("invalid hash key:", err)
49+
}
50+
}
51+
switch format {
52+
case "64":
53+
if hashKey != nil {
54+
apiHash, err = rorwk.HashAPI64WithKey(modName, funcName, hashKey)
55+
} else {
56+
apiHash, hashKey, err = rorwk.HashAPI64(modName, funcName)
57+
}
58+
numZero = "16"
59+
case "32":
60+
if hashKey != nil {
61+
apiHash, err = rorwk.HashAPI32WithKey(modName, funcName, hashKey)
62+
} else {
63+
apiHash, hashKey, err = rorwk.HashAPI32(modName, funcName)
64+
}
65+
numZero = "8"
66+
default:
67+
log.Fatalln("invalid format:", format)
68+
}
69+
if err != nil {
70+
log.Fatalln("failed to calculate hash:", err)
71+
}
72+
if concise {
73+
h := rorwk.BytesToUint64(apiHash)
74+
k := rorwk.BytesToUint64(hashKey)
75+
fmt.Printf("0x%0"+numZero+"X, "+"0x%0"+numZero+"X // %s\n", h, k, funcName)
76+
return
77+
}
78+
fmt.Println("module: ", modName)
79+
fmt.Println("function:", funcName)
80+
fmt.Printf("format: %s bit\n", format)
81+
fmt.Println()
82+
fmt.Printf("Hash: 0x%0"+numZero+"X\n", rorwk.BytesToUint64(apiHash))
83+
fmt.Printf("Key: 0x%0"+numZero+"X\n", rorwk.BytesToUint64(hashKey))
84+
fmt.Printf("Hash: %s\n", dumpBytesHex(apiHash))
85+
fmt.Printf("Key: %s\n", dumpBytesHex(hashKey))
86+
}
87+
88+
func dumpBytesHex(b []byte) string {
89+
n := len(b)
90+
builder := strings.Builder{}
91+
builder.Grow(len("0xFF, ")*n - len(", "))
92+
for i := 0; i < n; i++ {
93+
builder.WriteString("0x")
94+
v := hex.EncodeToString([]byte{b[i]})
95+
v = strings.ToUpper(v)
96+
builder.WriteString(v)
97+
if i == n-1 {
98+
break
99+
}
100+
builder.WriteString(", ")
101+
}
102+
return builder.String()
103+
}

0 commit comments

Comments
 (0)