-
Notifications
You must be signed in to change notification settings - Fork 21
/
rop.go
188 lines (159 loc) · 4.81 KB
/
rop.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
178
179
180
181
182
183
184
185
186
187
188
package sploit
import (
"bytes"
"errors"
"fmt"
"regexp"
"strings"
)
// GadgetExpr is a structure used to match gadget opcodes
type GadgetExpr struct {
Operation []byte
RegStart uint8
RegEnd uint8
}
// Gadget stores information about a ROP gadgets including the address, instructions, and opcode bytes
type Gadget struct {
Address uint64
Instrs string
Opcode []byte
}
// ROP is a interface for working with ROP gadgets
type ROP []*Gadget
// Dump is a ROP method that locates and prints ROP gadgets contained in the ELF file to stdout
func (r *ROP) Dump() {
for _, gadget := range []*Gadget(*r) {
fmt.Printf("0x%08x: %v\n", gadget.Address, gadget.Instrs)
}
}
// InstrSearch is a ROP method that returns a ROP object containing gadgets with a sub-string match to the user-defined regex
func (r *ROP) InstrSearch(regex string) (ROP, error) {
re, err := regexp.Compile(regex)
if err != nil {
return nil, err
}
matchGadgets := ROP{}
for _, gadget := range []*Gadget(*r) {
if re.FindAllString(gadget.Instrs, 1) != nil {
matchGadgets = append(matchGadgets, gadget)
}
}
return matchGadgets, nil
}
func disasmInstrsFromRet(processor *Processor, data []byte, index int, address uint64, maxOpcodes int, instrSize int) ([]*Gadget, error) {
stop := index - maxOpcodes
if stop < 0 {
stop = 0
}
gadgets := []*Gadget{}
for i := index - instrSize; i > stop; i -= instrSize {
instr, err := disasmGadget(address+uint64(i), data[i:index+instrSize], processor)
if err != nil {
continue
}
gadgets = append(
gadgets,
&Gadget{
Address: address + uint64(i),
Instrs: instr,
Opcode: data[i : index+instrSize],
},
)
}
return gadgets, nil
}
func scanForGadgets(processor *Processor, data []byte, address uint64, maxOpcodes int, instrSize int, branches [][]byte) ([]*Gadget, error) {
gadgets := []*Gadget{}
for i := 0; i < len(data); i += instrSize {
for j := 0; j < len(branches); j++ {
if i+len(branches[j]) > len(data) {
break
}
if bytes.Compare(data[i:i+len(branches[j])], branches[j]) == 0 {
gadgetsRet, err := disasmInstrsFromRet(processor, data, i, address, maxOpcodes, instrSize)
if err != nil {
return nil, err
}
gadgets = append(gadgets, gadgetsRet...)
}
}
}
return gadgets, nil
}
func filterGadgetsIntel(gadgets []*Gadget) []*Gadget {
suffixes := []string{"ret", "retf", "int 0x80", "syscall", "sysenter"}
filtered := []*Gadget{}
for i := 0; i < len(gadgets); i++ {
hasSuffix := false
for j := 0; j < len(suffixes); j++ {
if strings.HasSuffix(strings.TrimSpace(gadgets[i].Instrs), suffixes[j]) {
hasSuffix = true
}
}
if !hasSuffix || strings.Count(gadgets[i].Instrs, "ret") > 1 {
continue
}
filtered = append(filtered, gadgets[i])
}
return filtered
}
func findGadgetsIntel(processor *Processor, data []byte, address uint64) ([]*Gadget, error) {
branches := [][]byte{
[]byte{0xc3}, // ret
[]byte{0xcb}, // retf
[]byte{0xcd, 0x80}, // int 0x80
[]byte{0x0f, 0x34}, // sysenter
[]byte{0x0f, 0x05}, // syscall
}
gadgets, err := scanForGadgets(processor, data, address, 16, 1, branches)
if err != nil {
return nil, err
}
return filterGadgetsIntel(gadgets), nil
}
func reverseSlice(s []byte) []byte {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func generateBranchOpcodesARM(processor *Processor) [][]byte {
gadgetExprs := []GadgetExpr{
GadgetExpr{[]byte{0xe1, 0x2f, 0xff}, 0x10, 0x1e}, // bx reg
GadgetExpr{[]byte{0xe1, 0x2f, 0xff}, 0x30, 0x3e}, // blx reg
GadgetExpr{[]byte{0xe8, 0xbd, 0x80}, 0x01, 0xff}, // pop {,pc}
GadgetExpr{[]byte{0xe1, 0xa0, 0xf0}, 0x00, 0x0f}, // mov pc, reg
GadgetExpr{[]byte{0xe8, 0xbd, 0x80}, 0x01, 0x01}, // ldm !sp, {pc}
}
var opcodes [][]byte
for i := 0; i < len(gadgetExprs); i++ {
for j := gadgetExprs[i].RegStart; j < gadgetExprs[i].RegEnd; j++ {
opcode := make([]byte, len(gadgetExprs[i].Operation)+1)
copy(opcode, gadgetExprs[i].Operation)
opcode[len(opcode)-1] = byte(j)
if processor.Endian == LittleEndian {
opcode = reverseSlice(opcode)
}
opcodes = append(opcodes, opcode)
}
}
return opcodes
}
func findGadgetsARM(processor *Processor, data []byte, address uint64) ([]*Gadget, error) {
branches := generateBranchOpcodesARM(processor)
return scanForGadgets(processor, data, address, 20, 4, branches)
}
func findGadgets(processor *Processor, data []byte, address uint64) ([]*Gadget, error) {
switch processor.Architecture {
case ArchX8664:
return findGadgetsIntel(processor, data, address)
case ArchIA64:
return findGadgetsIntel(processor, data, address)
case ArchI386:
return findGadgetsIntel(processor, data, address)
case ArchARM:
return findGadgetsARM(processor, data, address)
default:
return nil, errors.New("ROP interface currently only supports Intel and ARM32 binaries")
}
}