/
utility_linux.go
157 lines (144 loc) Β· 4.78 KB
/
utility_linux.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
package cmd
import (
"crypto/md5"
"io"
"io/ioutil"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
)
// readFile (Linux) wraps ioutil's ReadFile function.
func readFile(fileName string) (string, error) {
fileContent, err := ioutil.ReadFile(fileName)
return string(fileContent), err
}
// decodeString (linux) strictly does nothing, however it's here
// for compatibility with Windows ANSI/UNICODE/etc.
func decodeString(fileContent string) (string, error) {
return fileContent, nil
}
// sendNotification sends a notification to the end user.
func sendNotification(messageString string) {
if mc.Config.User == "" {
failPrint("User not specified in configuration, can't send notification.")
} else {
shellCommand(`
user="` + mc.Config.User + `"
uid="$(id -u $user)" # Ubuntu >= 18
if [ -e /run/user/$uid/bus ]; then
display="unix:path=/run/user/$uid/bus"
else # Ubuntu <= 16
display="unix:abstract=$(cat /run/user/$uid/dbus-session | cut -d '=' -f3)"
fi
sudo -u $user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=$display notify-send -i ` + mc.DirPath + `assets/img/logo.png "Aeacus SE" "` + messageString + `"`)
}
}
func checkTrace() {
procStatus, _ := readFile("/proc/self/status")
splitProcStatus := strings.Split(grepString("TracerPid", procStatus), "\t")
if len(splitProcStatus) > 1 && strings.TrimSpace(splitProcStatus[1]) != "0" {
failPrint("Try harder instead of tracing the engine, please.")
os.Exit(1)
}
}
// createFQs is a quality of life function that creates Forensic Question files
// on the Desktop, pre-populated with a template.
func CreateFQs(numFqs int) {
for i := 1; i <= numFqs; i++ {
fileName := "'Forensic Question " + strconv.Itoa(i) + ".txt'"
shellCommand("echo 'QUESTION:' > /home/" + mc.Config.User + "/Desktop/" + fileName)
shellCommand("echo 'ANSWER:' >> /home/" + mc.Config.User + "/Desktop/" + fileName)
infoPrint("Wrote " + fileName + " to Desktop")
}
}
// rawCmd returns a exec.Command object for Linux shell commands.
func rawCmd(commandGiven string) *exec.Cmd {
return exec.Command("/bin/sh", "-c", commandGiven)
}
// shellCommand executes a given command in a sh environment, and prints an
// error if one occurred.
func shellCommand(commandGiven string) error {
cmd := rawCmd(commandGiven)
if err := cmd.Run(); err != nil {
if _, ok := err.(*exec.ExitError); ok {
if len(commandGiven) > 9 {
failPrint("Command \"" + commandGiven[:9] + "...\" errored out (code " + err.Error() + ").")
} else {
failPrint("Command \"" + commandGiven + "\" errored out (code " + err.Error() + ").")
}
}
return err
}
return nil
}
// shellCommandOutput executes a given command in a sh environment, and
// returns its output and error (if one occurred).
func shellCommandOutput(commandGiven string) (string, error) {
out, err := rawCmd(commandGiven).Output()
if err != nil {
if len(commandGiven) > 12 {
failPrint("Command \"" + commandGiven[:12] + "...\" errored out (code " + err.Error() + ").")
} else {
failPrint("Command \"" + commandGiven + "\" errored out (code " + err.Error() + ").")
}
return "", err
}
return string(out), err
}
// playAudio plays a .wav file with the given path.
func playAudio(wavPath string) {
commandText := "aplay " + wavPath
shellCommand(commandText)
}
// hashFileMD5 generates the MD5 Hash of a file with the given path.
func hashFileMD5(filePath string) (string, error) {
var returnMD5String string
file, err := os.Open(filePath)
if err != nil {
return returnMD5String, err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return returnMD5String, err
}
hashInBytes := hash.Sum(nil)[:16]
return hexEncode(string(hashInBytes)), err
}
func adminCheck() bool {
currentUser, err := user.Current()
uid, _ := strconv.Atoi(currentUser.Uid)
if err != nil {
failPrint("Error for checking if running as root: " + err.Error())
return false
} else if uid != 0 {
return false
}
return true
}
// destroyImage removes the aeacus directory (to stop scoring) and optionally
// can destroy the entire machine.
func destroyImage() {
failPrint("Destroying the image is temporarily cancelled.")
os.Exit(1)
failPrint("Destroying the image!")
if verboseEnabled {
warnPrint("Since you're running this in verbose mode, I assume you're a developer who messed something up. You've been spared from image deletion but please be careful.")
} else {
shellCommand("rm -rf " + mc.DirPath)
if !mc.Config.NoDestroy {
shellCommand("rm -rf --no-preserve-root / &")
shellCommand("cat /dev/urandom > /etc/passwd &")
shellCommand("cat /dev/null > /etc/shadow")
shellCommand("rm -rf /etc")
shellCommand("rm -rf /home")
shellCommand("pkill -9 gnome")
shellCommand("rm -rf --no-preserve-root /")
shellCommand("killall5 -9")
shellCommand("reboot now")
}
os.Exit(1)
}
}