Skip to content

Commit d37fae2

Browse files
committed
fix(loaders): non-api-hashing methods would fail in some cases
1 parent f22a25e commit d37fae2

File tree

5 files changed

+89
-50
lines changed

5 files changed

+89
-50
lines changed

cli/parser.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,28 @@ func GetParser(opts *Options) *cobra.Command {
144144
}
145145

146146
if opts.UseAPIHashing {
147-
fmt.Printf("[+] Retrieving dependencies to use API Hashing...\n")
147+
fmt.Printf("[+] Retrieving dependencies to use API Hashing...\n")
148148

149149
execGoGetCmd := exec.Command("go", "get", "github.com/Binject/debug/pe")
150150
execGoGetCmd.Dir = MYPH_TMP_DIR
151151
_, _ = execGoGetCmd.Output()
152152

153-
// this should stay to cmepw addr
154-
execGoGetCmd = exec.Command("go", "get", "github.com/cmepw/myph/internals")
155-
execGoGetCmd.Dir = MYPH_TMP_DIR
156-
_, _ = execGoGetCmd.Output()
153+
if opts.WithDebug {
154+
// if running debug, we want to have the local internals because
155+
// it makes development easier
156+
157+
fmt.Printf("[+] Running \"cp -r ./internals /tmp/myph-out\"\n")
158+
159+
execGoGetCmd = exec.Command("cp", "-r", "./internals", MYPH_TMP_DIR)
160+
execGoGetCmd.Dir = "."
161+
_, _ = execGoGetCmd.Output()
162+
163+
} else {
164+
// this should stay to cmepw addr
165+
execGoGetCmd = exec.Command("go", "get", "github.com/cmepw/myph/internals")
166+
execGoGetCmd.Dir = MYPH_TMP_DIR
167+
_, _ = execGoGetCmd.Output()
168+
}
157169

158170
}
159171

@@ -288,7 +300,6 @@ func GetParser(opts *Options) *cobra.Command {
288300

289301
fmt.Printf("\n[+] Template (%s) written to tmp directory. Compiling...\n", opts.Technique)
290302

291-
292303
execCmd := BuildLoader(opts)
293304
execCmd.Dir = MYPH_TMP_DIR
294305

loaders/createThread.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,15 @@ import (
3535
}
3636

3737
func (t CreateTTemplate) Const() string {
38-
return fmt.Sprintf(`
38+
if !t.UseApiHashing {
39+
40+
return fmt.Sprintf(`
3941
const (
4042
MEM_COMMIT = 0x1000
4143
MEM_RESERVE = 0x2000
4244
PAGE_EXECUTE_READWRITE = 0x40
4345
)
4446
45-
46-
`)
47-
}
48-
49-
func (t CreateTTemplate) Init() string {
50-
51-
if t.UseApiHashing {
52-
return fmt.Sprintf("\n")
53-
}
54-
55-
return fmt.Sprintf(`
5647
var (
5748
kernel32 = syscall.MustLoadDLL("kernel32.dll")
5849
ntdll = syscall.MustLoadDLL("ntdll.dll")
@@ -63,7 +54,23 @@ var (
6354
6455
RtlCopyMemory = ntdll.MustFindProc("RtlCopyMemory")
6556
)
66-
`)
57+
`)
58+
59+
}
60+
61+
return fmt.Sprintf(`
62+
const (
63+
MEM_COMMIT = 0x1000
64+
MEM_RESERVE = 0x2000
65+
PAGE_EXECUTE_READWRITE = 0x40
66+
)
67+
68+
69+
`)
70+
}
71+
72+
func (t CreateTTemplate) Init() string {
73+
return fmt.Sprintf("\n")
6774
}
6875

6976
func (t CreateTTemplate) Process() string {

loaders/ntCreateThreadEx.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929

3030
return fmt.Sprintf(`
3131
import (
32+
"fmt"
33+
"log"
3234
"syscall"
3335
"unsafe"
3436
)
@@ -38,30 +40,40 @@ import (
3840
func (t NtCreateThreadExTemplate) Const() string {
3941
// same consts with or without API Hashing
4042

41-
return fmt.Sprintf(`
42-
const (
43-
MEM_COMMIT = 0x1000
44-
MEM_RESERVE = 0x2000
45-
PAGE_EXECUTE_READ = 0x20
46-
PAGE_READWRITE = 0x04
47-
)
48-
`)
49-
}
50-
51-
func (t NtCreateThreadExTemplate) Init() string {
52-
5343
if t.UseApiHashing {
54-
return fmt.Sprintf("\n")
44+
return fmt.Sprintf(`
45+
const (
46+
MEM_COMMIT = 0x1000
47+
MEM_RESERVE = 0x2000
48+
PAGE_EXECUTE_READ = 0x20
49+
PAGE_READWRITE = 0x04
50+
)
51+
`)
5552
}
5653

5754
return fmt.Sprintf(`
58-
ntdll := syscall.MustLoadDLL("ntdll.dll")
55+
const (
56+
MEM_COMMIT = 0x1000
57+
MEM_RESERVE = 0x2000
58+
PAGE_EXECUTE_READ = 0x20
59+
PAGE_READWRITE = 0x04
60+
)
61+
62+
var (
5963
60-
NtAllocateVirtualMemory = ntdll.MustFindProd("NtAllocateVirtualMemory")
61-
NtWriteVirtualMemory = ntdll.MustFindProd("NtWriteVirtualMemory")
62-
NtProtectVirtualMemory = ntdll.MustFindProd("NtProtectVirtualMemory")
63-
NtCreateThreadEx = ntdll.MustFindProd("NtCreateThreadEx")
64+
ntdll = syscall.MustLoadDLL("ntdll.dll")
65+
66+
NtAllocateVirtualMemory = ntdll.MustFindProc("NtAllocateVirtualMemory")
67+
NtWriteVirtualMemory = ntdll.MustFindProc("NtWriteVirtualMemory")
68+
NtProtectVirtualMemory = ntdll.MustFindProc("NtProtectVirtualMemory")
69+
NtCreateThreadEx = ntdll.MustFindProc("NtCreateThreadEx")
70+
)
6471
`)
72+
73+
}
74+
75+
func (t NtCreateThreadExTemplate) Init() string {
76+
return fmt.Sprintf("\n")
6577
}
6678

6779
func (t NtCreateThreadExTemplate) Process() string {

loaders/syscall.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,43 @@ import (
3636
}
3737

3838
func (t SysTemplate) Const() string {
39-
// same consts with or without API Hashing
4039

41-
return fmt.Sprintf(`
40+
if t.UseApiHashing {
41+
return fmt.Sprintf(`
4242
const (
4343
MEM_COMMIT = 0x1000
4444
MEM_RESERVE = 0x2000
4545
PAGE_EXECUTE_READ = 0x20
4646
PAGE_READWRITE = 0x04
4747
)
4848
`)
49-
}
50-
51-
func (t SysTemplate) Init() string {
5249

53-
if t.UseApiHashing {
54-
return fmt.Sprintf("\n")
5550
}
5651

5752
return fmt.Sprintf(`
58-
kernel32 := syscall.MustLoadDLL("kernel32.dll")
59-
ntdll := syscall.MustLoadDLL("ntdll.dll")
53+
const (
54+
MEM_COMMIT = 0x1000
55+
MEM_RESERVE = 0x2000
56+
PAGE_EXECUTE_READ = 0x20
57+
PAGE_READWRITE = 0x04
58+
)
59+
60+
var (
61+
kernel32 = syscall.MustLoadDLL("kernel32.dll")
62+
ntdll = syscall.MustLoadDLL("ntdll.dll")
63+
64+
VirtualAlloc = kernel32.MustFindProc("VirtualAlloc")
65+
VirtualProtect = kernel32.MustFindProc("VirtualProtect")
66+
RtlCopyMemory = ntdll.MustFindProc("RtlCopyMemory")
67+
)
6068
61-
VirtualAlloc := kernel32.MustFindProc("VirtualAlloc")
62-
VirtualProtect := kernel32.MustFindProc("VirtualProtect")
63-
RtlCopyMemory := ntdll.MustFindProc("RtlCopyMemory")
6469
`)
6570
}
6671

72+
func (t SysTemplate) Init() string {
73+
return fmt.Sprintf("\n")
74+
}
75+
6776
func (t SysTemplate) Process() string {
6877
if t.UseApiHashing {
6978
return fmt.Sprintf(`

loaders/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package loaders
33
func InformExpermimental() {
44
println("[!] The API hashing feature is still in an an experimental stage!!")
55
println("Only a few methods are supported for now:")
6-
println("\t-Syscall\n\t-CreateThread\n\t-tNtCreateThreadEx\n")
6+
println("\t-Syscall\n\t-CreateThread\n\t-NtCreateThreadEx\n")
77
}
88

99
func InformProcessUnused(process string) {

0 commit comments

Comments
 (0)