-
Notifications
You must be signed in to change notification settings - Fork 26
/
windows_test.go
177 lines (139 loc) · 3.36 KB
/
windows_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// +build windows
package universal
import (
"fmt"
"io/ioutil"
"log"
"syscall"
"testing"
"github.com/Binject/debug/pe"
"github.com/awgh/rawreader"
)
const PtrSize = 32 << uintptr(^uintptr(0)>>63) // are we on a 32bit or 64bit system?
func Test_Windows_MSVC_1(t *testing.T) {
var image []byte
var err error
if PtrSize == 64 {
image, err = ioutil.ReadFile("test\\64\\TestDLL.dll")
} else {
image, err = ioutil.ReadFile("test\\32\\TestDLL.dll")
}
if err != nil {
t.Fatal(err)
}
loader, err := NewLoader()
if err != nil {
t.Fatal(err)
}
library, err := loader.LoadLibrary("main", &image)
if err != nil {
t.Fatal(err)
}
loadedDll, err := pe.NewFileFromMemory(rawreader.New(library.BaseAddress, int(^uint(0)>>1)))
if err != nil {
t.Fatal(err)
}
exports, err := loadedDll.Exports()
if err != nil {
log.Fatal(err)
}
log.Printf("Exported Symbols from Loaded DLL: %+v\n", exports)
runmeProc := library.BaseAddress + uintptr(exports[0].VirtualAddress)
var a []uintptr
a = append(a, uintptr(7))
r1, r2, errno := syscall.Syscall(runmeProc, uintptr(len(a)), a[0], 0, 0)
log.Println(r1, r2, errno)
}
func Test_Windows_gcc_2(t *testing.T) {
var image []byte
var err error
if PtrSize == 64 {
image, err = ioutil.ReadFile("test\\64\\main.dll")
} else {
image, err = ioutil.ReadFile("test\\32\\main.dll")
}
loader, err := NewLoader()
if err != nil {
t.Fatal(err)
}
_, err = loader.LoadLibrary("main", &image)
if err != nil {
t.Fatal(err)
}
runmeProc, ok := loader.FindProc("main", "Runme")
if !ok {
t.Fatal("FindProc did not find export Runme")
}
var a []uintptr
a = append(a, uintptr(7))
r1, r2, errno := syscall.Syscall(runmeProc, uintptr(len(a)), a[0], 0, 0)
log.Println(r1, r2, errno)
}
func Test_Windows_gcc_3(t *testing.T) {
var image []byte
var err error
if PtrSize == 64 {
image, err = ioutil.ReadFile("test\\64\\main.dll")
} else {
image, err = ioutil.ReadFile("test\\32\\main.dll")
}
loader, err := NewLoader()
if err != nil {
t.Fatal(err)
}
library, err := loader.LoadLibrary("main", &image)
if err != nil {
t.Fatal(err)
}
runmeProc, ok := library.FindProc("Runme")
if !ok {
t.Fatal("FindProc did not find export Runme")
}
var a []uintptr
a = append(a, uintptr(7))
r1, r2, errno := syscall.Syscall(runmeProc, uintptr(len(a)), a[0], 0, 0)
log.Println(r1, r2, errno)
}
func Test_Windows_gcc_4(t *testing.T) {
var image []byte
var err error
if PtrSize == 64 {
image, err = ioutil.ReadFile("test\\64\\main.dll")
} else {
image, err = ioutil.ReadFile("test\\32\\main.dll")
}
loader, err := NewLoader()
if err != nil {
t.Fatal(err)
}
library, err := loader.LoadLibrary("main", &image)
if err != nil {
t.Fatal(err)
}
val, err := library.Call("Runme", 7)
if err != nil {
t.Fatal(err)
}
log.Printf("%+v\n", val)
}
func Test_Windows_WrongArch_4(t *testing.T) {
var image []byte
var err error
if PtrSize == 32 { // this SHOULD try to load the WRONG architecture DLL and error out on load!!!
image, err = ioutil.ReadFile("test\\64\\TestDLL.dll")
} else {
image, err = ioutil.ReadFile("test\\32\\TestDLL.dll")
}
if err != nil {
t.Fatal(err)
}
loader, err := NewLoader()
if err != nil {
t.Fatal(err)
}
_, err = loader.LoadLibrary("main", &image)
if err == nil {
t.Fatal("Did not error out when loading the wrong architecture!")
}
fmt.Println("Correctly returned error:", err)
}