-
Notifications
You must be signed in to change notification settings - Fork 21
/
asm_test.go
100 lines (85 loc) · 2.62 KB
/
asm_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
package sploit
import (
"bytes"
"os"
"testing"
)
func TestDisasm(t *testing.T) {
addr := uint64(0x1135)
n := 32
e, _ := NewELF(elfFile)
disasm, err := e.Disasm(addr, n)
if err != nil {
t.Fatal(err)
}
expected := "00001135: push rbp\n" +
"00001136: mov rbp, rsp\n" +
"00001139: sub rsp, 0x10\n" +
"0000113d: mov dword ptr [rbp - 4], edi\n" +
"00001140: mov qword ptr [rbp - 0x10], rsi\n" +
"00001144: lea rdi, [rip + 0xeb9]\n" +
"0000114b: call 0x1030\n" +
"00001150: mov eax, 0\n"
if disasm != expected {
t.Fatal("Disassembly does not match expected")
}
}
func TestAsmX8664(t *testing.T) {
code := "mov rdi, 1337\nmov rsi, 1337\nmov rdx, 1337\nmov rcx, 1337\nnop\n"
processor := &Processor{
Architecture: ArchX8664,
Endian: LittleEndian,
}
opcode, err := Asm(processor, code)
if err != nil {
t.Fatal(err)
}
expected := []byte{0x48, 0xc7, 0xc7, 0x39, 0x05, 0x00, 0x00, 0x48, 0xc7, 0xc6, 0x39, 0x05, 0x00, 0x00, 0x48, 0xc7, 0xc2, 0x39, 0x05, 0x00, 0x00, 0x48, 0xc7, 0xc1, 0x39, 0x05, 0x00, 0x00, 0x90}
if bytes.Compare(opcode, expected) != 0 {
t.Fatal("Opcode bytes does not match expected")
}
}
func TestMakeELF(t *testing.T) {
code := `
jmp past
message:
.ascii "See, I am drow, and I'd like to say hello,\n"
.ascii "To the black, to the white, the red and the brown,\n"
.ascii "The purple and yellow. But first, I gotta\n"
.ascii "Bang bang, the boogie to the boogie,\n"
.ascii "Say up jump the boogie to the bang bang boogie,\n"
.ascii "Let's rock, you don't stop ...\n\n"
past:
mov rdi, 1 /* STDOUT file descriptor */
lea rsi, [rip + message] /* Pointer to message string */
mov rdx, 253 /* Message size */
mov rax, 1 /* Write syscall number */
syscall /* Execute system call */
mov rdi, 0 /* Success */
mov rax, 60 /* Exit syscall number */
syscall /* Execute system call */`
processor := &Processor{
Architecture: ArchX8664,
Endian: LittleEndian,
}
err := MakeELF(processor, code, "/tmp/test.elf")
defer os.Remove("/tmp/test.elf")
if err != nil {
t.Fatal(err)
}
}
func TestAsmARM(t *testing.T) {
code := "mov r2, r1\nmov r3, r4\nmov r5, r6\n"
processor := &Processor{
Architecture: ArchARM,
Endian: LittleEndian,
}
opcode, err := Asm(processor, code)
if err != nil {
t.Fatal(err)
}
expected := []byte{0x01, 0x20, 0xa0, 0xe1, 0x04, 0x30, 0xa0, 0xe1, 0x06, 0x50, 0xa0, 0xe1}
if bytes.Compare(opcode, expected) != 0 {
t.Fatal("Opcode bytes does not match expected")
}
}