-
Notifications
You must be signed in to change notification settings - Fork 1
/
duster.go
executable file
·127 lines (109 loc) · 2.65 KB
/
duster.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
package main
import (
"compress/gzip"
"encoding/binary"
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"
"strings"
"github.com/StardustOS/duster/cli"
"github.com/StardustOS/duster/debugger"
"github.com/StardustOS/duster/file"
"github.com/StardustOS/duster/xen"
)
func unzipFile(filename string) string {
if strings.Contains(filename, ".gz") {
file, err := os.Open(filename)
if err != nil {
fmt.Println(err)
}
r, err := gzip.NewReader(file)
if err != nil {
fmt.Println(err)
}
bytes := make([]byte, 100)
tmpfile, err := ioutil.TempFile("", "os")
if err != nil {
fmt.Println(err)
}
for n, _ := r.Read(bytes); n != 0; n, _ = r.Read(bytes) {
tmpfile.Write(bytes[:n])
}
filename = tmpfile.Name()
}
return filename
}
func main() {
currentUser, err := user.Current()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// 0 means we're running as root (annoyingly go treats this has a string)
if strings.Compare(currentUser.Uid, "0") != 0 {
fmt.Println("Error: debugger must run as root (so it can interface with the Xen API)")
os.Exit(1)
}
cmd := cli.CLI{}
var id int
var filename string
flag.StringVar(&filename, "path", "", "Path to the Operating System's binary")
flag.IntVar(&id, "id", 0, "The domain id to connect (can be found by running sudo xl list)")
flag.Parse()
if id == -1 {
fmt.Println("Error: no domain id passed")
os.Exit(1)
} else if len(filename) == 0 {
fmt.Println("Error: no image was passed!")
os.Exit(1)
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Printf("Error: %s does not exist\n", filename)
os.Exit(1)
}
domainid := uint32(id)
cntrl := &xen.Xenctrl{DomainID: domainid}
err = cntrl.Init()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = cntrl.SetDebug(domainid, true)
if err != nil {
fmt.Println(err)
fmt.Println("The above error is most likely caused by the domain id not existing")
os.Exit(1)
}
if !cntrl.IsPaused() {
fmt.Println("Error: the VM should be paused before the debugger is run!")
fmt.Println("(To pause the VM on startup use the command: sudo xl create -p <vm name>")
os.Exit(1)
}
mem := &xen.Memory{Domainid: domainid, Vcpu: 0}
err = mem.Init(cntrl)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
filename = unzipFile(filename)
p, err := file.NewSymbolicInformation(filename, binary.LittleEndian)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
f := &file.LineInformation{Name: filename}
err = f.Init()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dbg := debugger.NewDebugger(mem, cntrl, f, cntrl, p)
cmd.Init(dbg)
fmt.Println("Welcome to Duster!")
for {
input := cmd.ReadInput()
cmd.ProcessInput(input)
}
}