-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.go
More file actions
201 lines (171 loc) · 5.57 KB
/
shell.go
File metadata and controls
201 lines (171 loc) · 5.57 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"fmt"
"log"
"os"
"unsafe"
"github.com/kardianos/service"
"golang.org/x/sys/windows"
)
const (
PROCESS_ALL_ACCESS = 0x1F0FFF
MEM_COMMIT = 0x1000
SE_ASSIGNPRIMARYTOKEN_NAME = "SeAssignPrimaryTokenPrivilege"
SE_LOAD_DRIVER_NAME = "SeLoadDriverPrivilege"
SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege"
SE_TAKE_OWNERSHIP_NAME = "SeTakeOwnershipPrivilege"
CREATE_UNICODE_ENVIRONMENT = 0x00000400
SE_DEBUG_NAME = "SeDebugName"
SE_TCB_NAME = "SeTcbName"
SE_INCREASE_QUOTA_NAME = "SeIncreaseQuotaName"
SE_SECURITY_NAME = "SeSecurityName"
SE_SYSTEMTIME_NAME = "SeSytemtimeName"
SE_BACKUP_NAME = "SeBackupName"
SE_RESTORE_NAME = "SeRestoreName"
SE_SHUTDOWN_NAME = "SeShutdownName"
SE_UNDOCK_NAME = "SeUndockName"
SE_MANAGE_VOLUME_NAME = "SeManageVolumeName"
)
type patriotService struct {
Service service.Service
}
func (s *patriotService) Start(service.Service) error {
go s.run()
return nil
}
func (s *patriotService) Stop(service.Service) error {
// Here, you can add any cleanup code or stop any long-running operations
return nil
}
func (s *patriotService) run() {
// Call your existing runPatriot function here
s.startGUI()
}
func (s *patriotService) runWithPrivileges() {
// Enable the required privileges
privileges := []string{
SE_ASSIGNPRIMARYTOKEN_NAME,
SE_LOAD_DRIVER_NAME,
SE_SYSTEM_ENVIRONMENT_NAME,
SE_TAKE_OWNERSHIP_NAME,
SE_DEBUG_NAME,
SE_TCB_NAME,
SE_INCREASE_QUOTA_NAME,
SE_SECURITY_NAME,
SE_SYSTEMTIME_NAME,
SE_BACKUP_NAME,
SE_RESTORE_NAME,
SE_SHUTDOWN_NAME,
SE_UNDOCK_NAME,
SE_MANAGE_VOLUME_NAME,
}
for _, privilege := range privileges {
err := enablePrivilege(privilege)
if err != nil {
log.Fatalf("Failed to enable %s: %v", privilege, err)
}
}
// Run the provided function with the required privileges
_, _ = s.startGUI()
}
func enablePrivilege(privilegeName string) error {
var token windows.Token
currentProcess, _ := windows.GetCurrentProcess()
err := windows.OpenProcessToken(currentProcess, windows.TOKEN_ADJUST_PRIVILEGES|windows.TOKEN_QUERY, &token)
if err != nil {
return err
}
defer token.Close()
var luid windows.LUID
err = windows.LookupPrivilegeValue(nil, windows.StringToUTF16Ptr(privilegeName), &luid)
if err != nil {
return err
}
privileges := windows.Tokenprivileges{
PrivilegeCount: 1,
Privileges: [1]windows.LUIDAndAttributes{
{
Luid: luid,
Attributes: windows.SE_PRIVILEGE_ENABLED,
},
},
}
err = windows.AdjustTokenPrivileges(token, false, &privileges, 0, nil, nil)
if err != nil && err != windows.ERROR_NOT_ALL_ASSIGNED {
return err
}
return nil
}
func (s *patriotService) startGUI() (uint32, error) {
// Path to the PowerShell executable
powershellPath := `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`
// Get current process token
var currentProcessToken windows.Token
currentProcess, _ := windows.GetCurrentProcess()
err := windows.OpenProcessToken(currentProcess, windows.TOKEN_DUPLICATE|windows.TOKEN_QUERY|windows.TOKEN_ADJUST_DEFAULT|windows.TOKEN_ASSIGN_PRIMARY, ¤tProcessToken)
if err != nil {
log.Fatalf("Failed to get current process token: %v", err)
}
// Duplicate the current process token with TOKEN_ALL_ACCESS to create a primary token
var duplicatedToken windows.Token
err = windows.DuplicateTokenEx(currentProcessToken, windows.TOKEN_ALL_ACCESS, nil, windows.SecurityIdentification, windows.TokenPrimary, &duplicatedToken)
if err != nil {
log.Fatalf("Failed to duplicate token: %v", err)
}
// Get the user session ID
sessionID := windows.WTSGetActiveConsoleSessionId()
// Set the token session ID to the active session
sessionIDBytes := (*byte)(unsafe.Pointer(&sessionID))
err = windows.SetTokenInformation(duplicatedToken, windows.TokenSessionId, sessionIDBytes, uint32(unsafe.Sizeof(sessionID)))
if err != nil {
log.Fatalf("Failed to set token information: %v", err)
}
// Get the user environment block
var envBlock *uint16
err = windows.CreateEnvironmentBlock(&envBlock, duplicatedToken, false)
if err != nil {
return 0, fmt.Errorf("Failed to create environment block: %w", err)
}
// Define the process startup information
si := new(windows.StartupInfo)
si.Cb = uint32(unsafe.Sizeof(*si))
si.Flags = windows.STARTF_USESHOWWINDOW
si.ShowWindow = windows.SW_SHOWDEFAULT
si.Desktop = windows.StringToUTF16Ptr("Winsta0\\Default") // new line here
// Create the process
pi := new(windows.ProcessInformation)
err = windows.CreateProcessAsUser(duplicatedToken, windows.StringToUTF16Ptr(powershellPath), nil, nil, nil, false, CREATE_UNICODE_ENVIRONMENT, envBlock, nil, si, pi)
if err != nil {
return 0, fmt.Errorf("Failed to create process: %w", err)
}
return uint32(pi.ProcessId), nil
}
func main() {
svcConfig := &service.Config{
Name: "PatriotService",
DisplayName: "The Patriot Service",
Description: "This is The Patriot Service.",
}
p := &patriotService{}
s, err := service.New(p, svcConfig)
if err != nil {
log.Fatal(err)
}
p.Service = s
logger, err := s.Logger(nil)
if err != nil {
log.Fatal(err)
}
if len(os.Args) > 1 {
err := service.Control(s, os.Args[1])
if err != nil {
log.Printf("Valid actions: %q\n", service.ControlAction)
log.Fatal(err)
}
return
}
err = s.Run()
if err != nil {
logger.Error(err)
}
}