Skip to content

Commit 12ff1cd

Browse files
author
Alex Levinson
committed
alright seriously done for the day
1 parent f4ec3a5 commit 12ff1cd

File tree

10 files changed

+392
-341
lines changed

10 files changed

+392
-341
lines changed

compiler/compiler.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,6 @@ func (c *Compiler) Do() {
301301
cmd.Stderr = os.Stderr
302302
cmd.Run()
303303
}
304-
// TODO: Fix GOTTI Binary Obfuscator #sadpanda
305-
// c.Logger.Infof("Mordor-ifying Binary...")
306-
// switch c.OS {
307-
// case "darwin":
308-
// c.MordorifyDarwin()
309-
// case "linux":
310-
// c.MordorifyLinux()
311-
// case "windows":
312-
// c.MordorifyWindows()
313-
// default:
314-
// c.Logger.Fatalf("GOOS specified is not supported.")
315-
// }
316304
}
317305
os.Chdir(cwd)
318306
os.RemoveAll(c.BuildDir)

engine/core_vm.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ func (e *Engine) VMIsVM(call otto.FunctionCall) otto.Value {
1111
return otto.FalseValue()
1212
}
1313

14-
func (e *Engine) VMHalt(call otto.FunctionCall) otto.Value {
14+
func (e *Engine) VMImplode(call otto.FunctionCall) otto.Value {
1515
e.Logger.WithField("trace", "true").Errorf("Function Not Implemented")
1616
return otto.FalseValue()
1717
}
1818

19-
func (e *Engine) VMImplode(call otto.FunctionCall) otto.Value {
19+
func (e *Engine) VMMatches(call otto.FunctionCall) otto.Value {
2020
e.Logger.WithField("trace", "true").Errorf("Function Not Implemented")
2121
return otto.FalseValue()
2222
}
2323

24-
func (e *Engine) VMMatches(call otto.FunctionCall) otto.Value {
25-
e.Logger.WithField("trace", "true").Errorf("Function Not Implemented")
24+
func (e *Engine) VMHalt(call otto.FunctionCall) otto.Value {
25+
e.Halted = true
26+
e.VM.Interrupt <- func() {
27+
panic(errTimeout)
28+
}
2629
return otto.FalseValue()
2730
}
2831

engine/engine.go

Lines changed: 2 additions & 319 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
package engine
22

33
import (
4-
"bytes"
5-
"encoding/binary"
6-
"fmt"
74
"io/ioutil"
8-
"os"
9-
"os/user"
10-
"runtime"
11-
"strconv"
12-
"strings"
135

146
"github.com/robertkrimen/otto"
157
"github.com/sirupsen/logrus"
@@ -22,6 +14,7 @@ type Engine struct {
2214
Name string
2315
DebuggerEnabled bool
2416
Timeout int
17+
Halted bool
2518
}
2619

2720
func New(name string) *Engine {
@@ -33,6 +26,7 @@ func New(name string) *Engine {
3326
Imports: map[string]func() []byte{},
3427
Logger: logger,
3528
DebuggerEnabled: false,
29+
Halted: false,
3630
Timeout: 30,
3731
}
3832
}
@@ -53,65 +47,6 @@ func (e *Engine) SetName(name string) {
5347
e.Name = name
5448
}
5549

56-
func (e *Engine) CurrentUser() map[string]string {
57-
userInfo := map[string]string{}
58-
u, err := user.Current()
59-
if err != nil {
60-
e.Logger.WithField("trace", "true").Errorf("User Loading Error: %s", err.Error())
61-
return userInfo
62-
}
63-
userInfo["uid"] = u.Uid
64-
userInfo["gid"] = u.Gid
65-
userInfo["username"] = u.Username
66-
userInfo["name"] = u.Name
67-
userInfo["home_dir"] = u.HomeDir
68-
groups, err := u.GroupIds()
69-
if err != nil {
70-
e.Logger.WithField("trace", "true").Errorf("Group Loading Error: %s", err.Error())
71-
return userInfo
72-
}
73-
userInfo["groups"] = strings.Join(groups, ":")
74-
return userInfo
75-
}
76-
77-
func (e *Engine) InjectVars() {
78-
userInfo, err := e.VM.ToValue(e.CurrentUser())
79-
if err != nil {
80-
e.Logger.WithField("trace", "true").Errorf("Could not inject user info into VM: %s", err.Error())
81-
} else {
82-
e.VM.Set("USER_INFO", userInfo)
83-
}
84-
osVal, err := e.VM.ToValue(runtime.GOOS)
85-
if err != nil {
86-
e.Logger.WithField("trace", "true").Errorf("Could not inject os info into VM: %s", err.Error())
87-
} else {
88-
e.VM.Set("OS", osVal)
89-
}
90-
hn, err := os.Hostname()
91-
if err != nil {
92-
e.Logger.WithField("trace", "true").Errorf("Could not obtain hostname info: %s", err.Error())
93-
} else {
94-
hostnameVal, err := e.VM.ToValue(hn)
95-
if err != nil {
96-
e.Logger.Errorf("Could not inject hostname info into VM: %s", err.Error())
97-
} else {
98-
e.VM.Set("HOSTNAME", hostnameVal)
99-
}
100-
}
101-
archVal, err := e.VM.ToValue(runtime.GOARCH)
102-
if err != nil {
103-
e.Logger.WithField("trace", "true").Errorf("Could not inject arch info into VM: %s", err.Error())
104-
} else {
105-
e.VM.Set("ARCH", archVal)
106-
}
107-
ipVals, err := e.VM.ToValue(GetLocalIPs())
108-
if err != nil {
109-
e.Logger.WithField("trace", "true").Errorf("Could not inject ip info into VM: %s", err.Error())
110-
} else {
111-
e.VM.Set("IP_ADDRS", ipVals)
112-
}
113-
}
114-
11550
func (e *Engine) CreateVM() {
11651
e.VM = otto.New()
11752
e.InjectVars()
@@ -193,255 +128,3 @@ func (e *Engine) CreateVM() {
193128
e.Logger.WithField("trace", "true").Fatalf("Syntax error in preload: %s", err.Error())
194129
}
195130
}
196-
197-
func (e *Engine) ValueToByteSlice(v otto.Value) []byte {
198-
valueBytes := []byte{}
199-
if v.IsNull() || v.IsUndefined() {
200-
return valueBytes
201-
}
202-
if v.IsString() {
203-
str, err := v.Export()
204-
if err != nil {
205-
e.Logger.WithField("trace", "true").Errorf("Cannot convert string to byte slice: %s", v)
206-
return valueBytes
207-
}
208-
valueBytes = []byte(str.(string))
209-
} else if v.IsNumber() {
210-
num, err := v.Export()
211-
if err != nil {
212-
e.Logger.WithField("trace", "true").Errorf("Cannot convert string to byte slice: %s", v)
213-
return valueBytes
214-
}
215-
buf := new(bytes.Buffer)
216-
err = binary.Write(buf, binary.LittleEndian, num)
217-
if err != nil {
218-
fmt.Println("binary.Write failed:", err)
219-
}
220-
valueBytes = buf.Bytes()
221-
} else if v.Class() == "Array" || v.Class() == "GoArray" {
222-
arr, err := v.Export()
223-
if err != nil {
224-
e.Logger.WithField("trace", "true").Errorf("Cannot convert array to byte slice: %x", v)
225-
return valueBytes
226-
}
227-
switch t := arr.(type) {
228-
case []uint:
229-
for _, i := range t {
230-
valueBytes = append(valueBytes, byte(i))
231-
}
232-
case []uint8:
233-
for _, i := range t {
234-
valueBytes = append(valueBytes, byte(i))
235-
}
236-
case []uint16:
237-
for _, i := range t {
238-
valueBytes = append(valueBytes, byte(i))
239-
}
240-
case []uint32:
241-
for _, i := range t {
242-
valueBytes = append(valueBytes, byte(i))
243-
}
244-
case []uint64:
245-
for _, i := range t {
246-
valueBytes = append(valueBytes, byte(i))
247-
}
248-
case []int:
249-
for _, i := range t {
250-
valueBytes = append(valueBytes, byte(i))
251-
}
252-
case []int16:
253-
for _, i := range t {
254-
valueBytes = append(valueBytes, byte(i))
255-
}
256-
case []int32:
257-
for _, i := range t {
258-
valueBytes = append(valueBytes, byte(i))
259-
}
260-
case []int64:
261-
for _, i := range t {
262-
valueBytes = append(valueBytes, byte(i))
263-
}
264-
case []float32:
265-
for _, i := range t {
266-
valueBytes = append(valueBytes, byte(i))
267-
}
268-
case []float64:
269-
for _, i := range t {
270-
valueBytes = append(valueBytes, byte(i))
271-
}
272-
case []string:
273-
for _, i := range t {
274-
for _, c := range i {
275-
valueBytes = append(valueBytes, byte(c))
276-
}
277-
}
278-
default:
279-
_ = t
280-
e.Logger.WithField("trace", "true").Errorf("Failed to cast array to byte slice array=%v", arr)
281-
}
282-
} else {
283-
e.Logger.WithField("trace", "true").Errorf("Unknown class to cast to byte slice")
284-
}
285-
286-
return valueBytes
287-
}
288-
289-
func (e *Engine) VMLogWarn(call otto.FunctionCall) otto.Value {
290-
if e.Logger != nil {
291-
for _, arg := range call.ArgumentList {
292-
newArg, err := arg.ToString()
293-
if err != nil {
294-
e.Logger.WithField(
295-
"script",
296-
e.Name,
297-
).WithField(
298-
"line",
299-
strconv.Itoa(e.VM.Context().Line),
300-
).WithField(
301-
"caller",
302-
e.VM.Context().Callee,
303-
).Errorf("Parameter parsing error: %s", err.Error())
304-
continue
305-
}
306-
e.Logger.WithField(
307-
"script",
308-
e.Name,
309-
).WithField(
310-
"line",
311-
strconv.Itoa(e.VM.Context().Line),
312-
).WithField(
313-
"caller",
314-
e.VM.Context().Callee,
315-
).Warnf("%s", newArg)
316-
}
317-
}
318-
return otto.Value{}
319-
}
320-
321-
func (e *Engine) VMLogError(call otto.FunctionCall) otto.Value {
322-
if e.Logger != nil {
323-
for _, arg := range call.ArgumentList {
324-
newArg, err := arg.ToString()
325-
if err != nil {
326-
e.Logger.WithField(
327-
"script",
328-
e.Name,
329-
).WithField(
330-
"line",
331-
strconv.Itoa(e.VM.Context().Line),
332-
).WithField(
333-
"caller",
334-
e.VM.Context().Callee,
335-
).Errorf("Parameter parsing error: %s", err.Error())
336-
continue
337-
}
338-
e.Logger.WithField(
339-
"script",
340-
e.Name,
341-
).WithField(
342-
"line",
343-
strconv.Itoa(e.VM.Context().Line),
344-
).WithField(
345-
"caller",
346-
e.VM.Context().Callee,
347-
).Errorf("%s", newArg)
348-
}
349-
}
350-
return otto.Value{}
351-
}
352-
353-
func (e *Engine) VMLogDebug(call otto.FunctionCall) otto.Value {
354-
if e.Logger != nil {
355-
for _, arg := range call.ArgumentList {
356-
newArg, err := arg.ToString()
357-
if err != nil {
358-
e.Logger.WithField(
359-
"script",
360-
e.Name,
361-
).WithField(
362-
"line",
363-
strconv.Itoa(e.VM.Context().Line),
364-
).WithField(
365-
"caller",
366-
e.VM.Context().Callee,
367-
).Errorf("Parameter parsing error: %s", err.Error())
368-
continue
369-
}
370-
e.Logger.WithField(
371-
"script",
372-
e.Name,
373-
).WithField(
374-
"line",
375-
strconv.Itoa(e.VM.Context().Line),
376-
).WithField(
377-
"caller",
378-
e.VM.Context().Callee,
379-
).Debugf("%s", newArg)
380-
}
381-
}
382-
return otto.Value{}
383-
}
384-
385-
func (e *Engine) VMLogCrit(call otto.FunctionCall) otto.Value {
386-
if e.Logger != nil {
387-
for _, arg := range call.ArgumentList {
388-
newArg, err := arg.ToString()
389-
if err != nil {
390-
e.Logger.WithField(
391-
"script",
392-
e.Name,
393-
).WithField(
394-
"line",
395-
strconv.Itoa(e.VM.Context().Line),
396-
).WithField(
397-
"caller",
398-
e.VM.Context().Callee,
399-
).Errorf("Parameter parsing error: %s", err.Error())
400-
continue
401-
}
402-
e.Logger.WithField(
403-
"script",
404-
e.Name,
405-
).WithField(
406-
"line",
407-
strconv.Itoa(e.VM.Context().Line),
408-
).WithField(
409-
"caller",
410-
e.VM.Context().Callee,
411-
).Fatalf("%s", newArg)
412-
}
413-
}
414-
return otto.Value{}
415-
}
416-
417-
func (e *Engine) VMLogInfo(call otto.FunctionCall) otto.Value {
418-
if e.Logger != nil {
419-
for _, arg := range call.ArgumentList {
420-
newArg, err := arg.ToString()
421-
if err != nil {
422-
e.Logger.WithField(
423-
"script",
424-
e.Name,
425-
).WithField(
426-
"line",
427-
strconv.Itoa(e.VM.Context().Line),
428-
).WithField(
429-
"caller",
430-
e.VM.Context().Callee,
431-
).Errorf("Parameter parsing error: %s", err.Error())
432-
continue
433-
}
434-
e.Logger.WithField(
435-
"script",
436-
e.Name,
437-
).WithField(
438-
"line",
439-
strconv.Itoa(e.VM.Context().Line),
440-
).WithField(
441-
"caller",
442-
e.VM.Context().Callee,
443-
).Infof("%s", newArg)
444-
}
445-
}
446-
return otto.Value{}
447-
}

0 commit comments

Comments
 (0)