-
Notifications
You must be signed in to change notification settings - Fork 1
/
elevate.dpr
148 lines (136 loc) · 4.04 KB
/
elevate.dpr
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
program elevate;
uses
Windows,
utils in 'utils.pas';
{$R *.res}
var
command, sourceFile, varName, value, debugFile, varNewName: string;
hk: HKEY;
pipe: THandle;
code: LONG;
br: Cardinal;
debugEnabled: Boolean;
valueLen: Integer;
typ: DWORD;
paramCount: Integer;
procedure readParamCount;
var
cmd: TCommandRec;
begin
ReadFile(pipe, cmd, SizeOf(cmd), br, nil);
paramCount := cmd.ByteCount;
end;
procedure writeParam(AParam: TCommand); overload;
var
cmd: TCommandRec;
begin
cmd.Typ := 2;
cmd.ByteCount := Ord(AParam);
WriteFile(pipe, cmd, SizeOf(cmd), br, nil);
end;
procedure writeParam(const AParam: string); overload;
var
cmd: TCommandRec;
begin
cmd.Typ := 3;
cmd.ByteCount := Length(AParam) * SizeOf(Char);
WriteFile(pipe, cmd, SizeOf(cmd), br, nil);
WriteFile(pipe, PChar(AParam)^, cmd.ByteCount, br, nil);
end;
begin
debugFile := ChangeFileExt(GetModuleName(HInstance), '.debug');
debugEnabled := FileExists(debugFile);
if (debugEnabled) then
MessageBox(0, GetCommandLine, 'Command line', 0);
pipe :=
CreateFile(
'\\.\pipe\EnvVarsElevateGate', GENERIC_WRITE, 0, nil, OPEN_EXISTING, 0, 0);
if (pipe <> INVALID_HANDLE_VALUE) and (pipe <> 0) then
begin
command := ParamStr(1);
varName := ParamStr(2);
sourceFile := ParamStr(3);
varNewName := sourceFile;
if (varName = '') or (sourceFile = '') then
begin
MessageBox(0, 'Invalid command', nil, 0);
ExitCode := ERROR_INVALID_PARAMETER;
end
else
begin
ExitCode :=
RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
// HKEY_CURRENT_USER,
// 'Environment',
0, KEY_READ or KEY_WRITE, hk);
if (ExitCode = ERROR_SUCCESS) then
begin
if (command = '1') then
begin
ExitCode := LoadFileData(sourceFile, value);
if debugEnabled then
MessageBox(0, PChar('Name: ' + varName + ' Value: ' + value), 'Loaded data', 0);
if (ExitCode = ERROR_SUCCESS) then
begin
if (value <> '') then
begin
ExitCode :=
RegSetValueEx(
hk, PChar(varName), 0,
IIF(Pos('%', value) > 0, REG_EXPAND_SZ, REG_SZ),
PChar(value), (Length(value) + 1) * SizeOf(Char));
end
else
begin
ExitCode := RegDeleteValue(hk, PChar(varName));
end;
end;
end
else if (command = '2') then
begin
if debugEnabled then
MessageBox(0, PChar('From: ' + varName + ' To: ' + varNewName), 'Rename', 0);
ExitCode := RegQueryValueEx(hk, PChar(varName), nil, @typ, nil, @valueLen);
if (ExitCode = ERROR_SUCCESS) then
begin
if (typ in [REG_SZ, REG_EXPAND_SZ]) then
begin
SetLength(value, valueLen div 2);
RegQueryValueEx(hk, PChar(value), nil, nil, @value[1], @valueLen);
value := Trim(value);
ExitCode := RegDeleteValue(hk, PChar(varName));
if (ExitCode = ERROR_SUCCESS) then
begin
ExitCode :=
RegSetValueEx(
hk, PChar(varNewName), 0,
IIF(Pos('%', value) > 0, REG_EXPAND_SZ, REG_SZ),
PChar(value), (Length(value) + 1) * SizeOf(Char));
end;
end;
end
end
else
ExitCode := ERROR_INVALID_PARAMETER;
if (debugEnabled) then
begin
if (ExitCode <> 0) then
MessageBox(0, PChar('Error: ' + IntToStr(ExitCode)), nil, 0)
else
MessageBox(0, 'Success!', nil, 0);
end;
RegCloseKey(hk);
end;
end;
code := ExitCode;
WriteFile(pipe, code, SizeOf(code), br, nil);
CloseHandle(pipe);
end
else
begin
MessageBox(0, 'Gate not found', nil, 0);
ExitCode := ERROR_FILE_NOT_FOUND;
end;
end.