-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
320 lines (286 loc) · 6.29 KB
/
main.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// https://cedriczirtacic.github.io/
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"golang.org/x/arch/x86/x86asm"
"./create_elf"
)
/*
#include <stdio.h>
#include <termios.h>
int get() {
struct termios old, new;
tcgetattr(0,&old);
new=old;
new.c_lflag &=~(ECHO | ICANON | IEXTEN);
tcsetattr(0,TCSANOW,&new);
char c = getchar();
tcsetattr(0,TCSANOW,&old);
return (int)c;
}
*/
import "C"
const (
// architecture modes
MOD_16 = (1 << 4)
MOD_32 = (MOD_16 << 1)
MOD_64 = (MOD_32 << 1)
// flavors
SYN_INTEL = (0 << 1)
SYN_ATT = (1 << 1)
SYN_GO = (2 << 1)
// key codes
KBD_TAB = 9
KBD_ENTER = 10
KBD_LF = 13
PROMPT = "> "
)
// main options
var (
mode int = MOD_64 /* default is 64-bit mode */
syntax uint8 = SYN_INTEL
output *os.File
out string
json_output bool = false
prettify bool = false
custom_elf *os.File
rgex = regexp.MustCompile(`([^\s\t]+?)[\s\t]+(.+)*`)
)
// json output struct
type json_opcode struct {
Len int `json:"length"`
Inst string `json:"instruction"`
Args []string `json:"args"`
}
func output_file(f string) *os.File {
fd, err := os.OpenFile(f, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return nil
}
return fd
}
func pretty_print(s string) {
//colors
var (
Normal = 0
Red = 31 // operands
Blue = 34 // instruction
)
var escape = []byte{0x1b}
matches := rgex.FindStringSubmatch(s)
if len(matches) > 0 {
fmt.Printf("%s[%dm%s ", escape, Blue, matches[1])
if matches[2] != "" {
fmt.Printf("%s[%dm%s", escape, Red, matches[2])
}
fmt.Printf("%s[%dm\n", escape, Normal)
}
}
func PrintPrompt() {
fmt.Printf("%c[2K\r%s", 27, PROMPT)
}
func main() {
hist, err := NewHist()
if err != nil {
fmt.Fprintf(os.Stderr, "Error setting up the history: %s\n", err)
}
defer hist.Save()
hist.Populate()
for {
PrintPrompt()
hist.ResetPos()
var input []byte
for {
c := int(C.get())
if c < 0x20 || c > 0x7e {
if c == KBD_LF || c == KBD_ENTER {
println()
break
}
// del
if c == 0x7f {
if len(input) == 0 {
continue
}
input = input[:len(input)-1]
PrintPrompt()
print(string(input))
}
// arrows
if c == 0x1b {
_ = int(C.get()) // 0x5b
c2 := int(C.get())
var cmd *string
switch c2 {
case 0x41:
if len(hist.cmds) > 0 {
cmd = hist.Up()
} else {
continue
}
case 0x42:
if len(hist.cmds) > 0 {
cmd = hist.Down()
} else {
continue
}
}
PrintPrompt()
print(*cmd)
input = []byte(*cmd)
}
} else {
fmt.Printf("%c", c)
input = append(input, byte(c))
}
}
input_codes := string(input)
if input_codes == "" {
continue
} else if strings.Index(input_codes, "history") == 0 {
if len(input_codes) == len("history") {
hist.PrintHistory()
} else if input_codes == "history clear" {
hist.Clear()
hist.Save()
}
continue
} else if input_codes == "quit" || input_codes == "q" {
return
}
hist.AppendCmd(input_codes)
if len(input_codes) > 6 && input_codes[0:6] == "create" {
create := strings.Split(input_codes, " ")
custom_elf, _ = create_elf.Create(create[1])
defer custom_elf.Close()
continue
} else if len(input_codes) > 3 && input_codes[0:3] == "set" {
set := strings.Split(input_codes, "=")
switch set[0] {
case "set mode":
if len(set) < 2 {
fmt.Printf("Error: must specify a mode (eg. set mode=32)\n")
} else {
tmp_mode, _ := strconv.Atoi(set[1])
if tmp_mode != MOD_16 && tmp_mode != MOD_32 && tmp_mode != MOD_64 {
fmt.Printf("Error: invalid mode\n")
} else {
mode = tmp_mode
}
}
case "set json":
fmt.Printf("json = ")
if !json_output {
json_output = true
fmt.Printf("true")
} else {
json_output = false
fmt.Printf("false")
}
println()
case "set flavor":
if set[1] == "intel" {
syntax = SYN_INTEL
} else if set[1] == "att" {
syntax = SYN_ATT
} else if set[1] == "go" {
syntax = SYN_GO
} else {
fmt.Fprintf(os.Stderr, "Error: unknown flavor.\n")
}
break
case "set output":
output = output_file(set[1])
if output != nil {
defer output.Close()
}
case "set colors":
fmt.Printf("colors = ")
if !prettify {
prettify = true
fmt.Printf("true")
} else {
prettify = false
fmt.Printf("false")
}
println()
default:
fmt.Fprintf(os.Stderr, "Error: couldn't set an option.\n")
}
continue
} else if len(input_codes)%2 != 0 {
fmt.Fprintf(os.Stderr, "Error: unable to parse opcodes.\n")
continue
}
opcodes := make([]byte, len(input_codes)/2)
for j, i := 0, 0; i < len(input_codes); i = i + 2 {
o, err := hex.DecodeString(input_codes[i : i+2])
if err == nil {
opcodes[j] = o[0]
j++
}
}
// if we are creating a custom ELF binary
// then write all opcodes directly to the
// specified file with create command
if custom_elf != nil {
err := create_elf.Write(custom_elf, opcodes)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: unable to write to custom ELF.\n")
}
}
inst, _ := x86asm.Decode(opcodes, mode)
// inst.Opcode > 0 to be a valid opcode
if inst.Opcode == 0 {
println("Wrong or invalid opcode")
continue
}
// get parsed data
switch syntax {
case SYN_ATT:
out = fmt.Sprintf("%s\n", x86asm.GNUSyntax(inst, 0, nil))
case SYN_INTEL:
out = fmt.Sprintf("%s\n", x86asm.IntelSyntax(inst, 0, nil))
case SYN_GO:
out = fmt.Sprintf("%s\n", x86asm.GoSyntax(inst, 0, nil))
}
if json_output {
// JSON output
op_data := &json_opcode{
Len: inst.Len,
}
s := rgex.FindAllStringSubmatch(out, -1)
op_data.Inst = s[0][1]
if s[0][2] != "" {
a := strings.Split(s[0][2], ",")
for _, b := range a {
if b[0:1] == " " {
op_data.Args = append(op_data.Args, b[1:])
} else {
op_data.Args = append(op_data.Args, b)
}
}
}
json_data, _ := json.MarshalIndent(op_data, "", " ")
println(string(json_data))
} else {
// NORMAL output
if output == nil {
if !prettify {
print(out)
} else {
pretty_print(out)
}
} else {
output.WriteString(out)
}
}
}
}