forked from bmcder02/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller_darwin.go
176 lines (149 loc) · 5.1 KB
/
installer_darwin.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// +build darwin
/*
Velociraptor - Hunting Evil
Copyright (C) 2019 Velocidex Innovations.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
errors "github.com/pkg/errors"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/utils"
)
var (
service_command = app.Command(
"service", "Manipulate the Velociraptor service.")
install_command = service_command.Command(
"install", "Install Velociraptor as a service.")
remove_command = service_command.Command(
"remove", "Remove the Velociraptor service.")
)
func doRemove() error {
config_obj, err := makeDefaultConfigLoader().WithRequiredClient().
WithWriteback().LoadAndValidate()
if err != nil {
return errors.Wrap(err, "Unable to load config file")
}
if config_obj.Client.DarwinInstaller == nil {
return errors.New("DarwinInstaller not configured")
}
service_name := config_obj.Client.DarwinInstaller.ServiceName
plist_path := "/Library/LaunchDaemons/" + service_name + ".plist"
err = exec.CommandContext(context.Background(),
"/bin/launchctl", "unload", "-w", plist_path).Run()
if err != nil {
return fmt.Errorf("Can't load service: %w", err)
}
return nil
}
func doInstall() error {
config_obj, err := makeDefaultConfigLoader().WithRequiredClient().
WithWriteback().LoadAndValidate()
if err != nil {
return errors.Wrap(err, "Unable to load config file")
}
executable, err := os.Executable()
if err != nil {
return fmt.Errorf("Can't get executable path: %w", err)
}
service_name := config_obj.Client.DarwinInstaller.ServiceName
logger := logging.GetLogger(config_obj, &logging.ClientComponent)
target_path := os.ExpandEnv(config_obj.Client.DarwinInstaller.InstallPath)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Try to copy the executable to the target_path.
err = utils.CopyFile(ctx, executable, target_path, 0755)
if err != nil && os.IsNotExist(errors.Cause(err)) {
dirname := filepath.Dir(target_path)
logger.Info("Attempting to create intermediate directory %s.",
dirname)
err = os.MkdirAll(dirname, 0700)
if err != nil {
return errors.Wrap(err, "Create intermediate directories")
}
err = utils.CopyFile(ctx, executable, target_path, 0755)
}
if err != nil {
return errors.Wrap(err, "Cant copy binary into destination dir.")
}
logger.Info("Copied binary to %s", target_path)
// If the installer was invoked with the --config arg then we
// need to copy the config to the target path.
if *config_path != "" {
config_target_path := strings.TrimSuffix(
target_path, filepath.Ext(target_path)) + ".config.yaml"
logger.Info("Copying config to destination %s",
config_target_path)
err = utils.CopyFile(ctx, *config_path, config_target_path, 0755)
if err != nil {
logger.Info("Cant copy config to destination %s: %v",
config_target_path, err)
return err
}
}
plist_path := "/Library/LaunchDaemons/" + service_name + ".plist"
plist := fmt.Sprintf(`
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>%v</string>
<key>ProgramArguments</key>
<array>
<string>%v</string>
<string>client</string>
<string>--config</string>
<string>%v.config.yaml</string>
</array>
<key>KeepAlive</key>
<true/>
</dict>
</plist>`, service_name, target_path, target_path)
err = ioutil.WriteFile(plist_path, []byte(plist), 0644)
if err != nil {
return fmt.Errorf("Can't write plist file: %w", err)
}
err = exec.CommandContext(context.Background(),
"/bin/launchctl", "load", "-w", plist_path).Run()
if err != nil {
return fmt.Errorf("Can't load service: %w", err)
}
// We need to kill the service so it can restart with the new
// settings. Use SIGINT to allow it to cleanup.
err = exec.CommandContext(context.Background(),
"/bin/launchctl", "kill", "SIGINT", "system/"+service_name).Run()
if err != nil {
return fmt.Errorf("Can't restart service: %w", err)
}
return nil
}
func init() {
command_handlers = append(command_handlers, func(command string) bool {
switch command {
case install_command.FullCommand():
FatalIfError(install_command, doInstall)
case remove_command.FullCommand():
FatalIfError(remove_command, doRemove)
default:
return false
}
return true
})
}