-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
169 lines (141 loc) · 6.06 KB
/
Copy pathmain.cpp
File metadata and controls
169 lines (141 loc) · 6.06 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
/**
* Enable CMD shell from current directory
* and some other security functions for to have
* @Ali 05/02/2020
*/
#include "Util.hpp"
#include "winapi.hpp"
#include <sddl.h>
#include <conio.h>
#include <iostream>
#include <assert.h>
BOOL static RegOpenKeyAlt(HKEY hKey, LPCWSTR lpSubKey, REGSAM samDesired, PHKEY phkResult);
int static EnableShellCMD(LPCWSTR key);
int wmain()
{
LPCWSTR Directories = TEXT("Directory\\shell\\cmd");
LPCWSTR Background = TEXT("Directory\\Background\\shell\\cmd");
std::wcout << "\n<<<< Setting CMD Shell Extension For Directory >>>>\n"
<< std::endl;
if (EnableShellCMD(Directories))
OUT_ERR("EnableShellCMD failed on directory", -1)
std::wcout << std::endl
<< std::endl;
std::wcout << "<<<< Setting CMD Shell Extension For Backgrounds >>>>\n"
<< std::endl;
if (EnableShellCMD(Background))
OUT_ERR("EnableShellCMD failed on background", -1)
std::wcout << std::endl
<< std::endl;
std::wcout << "CMD is available on every directory on this machine\n"
<< std::endl;
return EXIT_SUCCESS;
}
int EnableShellCMD(LPCWSTR key)
{
HKEY RegKey;
DWORD dwErr;
LSTATUS r;
// Allowing current process taking ownership of objects
OUT_MSG("Checking for SeTakeOwnershipName privilege")
if (!(IsPrivilegeEnabled(NULL, SE_TAKE_OWNERSHIP_NAME) || SetPrivilege(SE_TAKE_OWNERSHIP_NAME)))
{
dwErr = GetLastError();
OUT_ERR("Failed to enable SeTakeOwnershipName privilege", dwErr)
return dwErr;
}
/* Open Registry Key */
OUT_MSG("Trying to open Registry key with READ_CONTROL, KEY_QUERY_VALUE, WRITE_OWNER")
RegOpenKeyAlt(HKEY_CLASSES_ROOT, key, KEY_QUERY_VALUE | READ_CONTROL | WRITE_OWNER, &RegKey);
DWORD pvData_ShowValue;
DWORD pvData_HideValue;
DWORD dwSize = sizeof(DWORD);
std::wcout << std::endl;
OUT_MSG("Reading 'ShowBasedOnVelocityId' value")
RegGetValue(RegKey, NULL, (LPCWSTR)TEXT("ShowBasedOnVelocityId"), RRF_RT_REG_DWORD | RRF_ZEROONFAILURE, NULL, (PVOID)&pvData_ShowValue, &dwSize);
std::wcout << "\tShowBasedOnVelocityId: " << pvData_ShowValue << std::endl;
OUT_MSG("Reading 'HideBasedOnVelocityId' value")
RegGetValue(RegKey, NULL, (LPCWSTR)TEXT("HideBasedOnVelocityId"), RRF_RT_REG_DWORD | RRF_ZEROONFAILURE, NULL, (PVOID)&pvData_HideValue, &dwSize);
std::wcout << "\tHideBasedOnVelocityId: " << pvData_HideValue << std::endl;
/* if HideBasedOnVelocityId */
if (pvData_HideValue)
{
/* **** Change Permissions **** */
/* Getting ownership of the registry key */
PTOKEN_OWNER pTokenOwner = nullptr;
HANDLE hProcessToken;
LPWSTR StringSid;
/* Get current process token owner */
hProcessToken = GetCurrentProcessToken();
GetTokenOwner(hProcessToken, &pTokenOwner);
/* Check Current Owner of Key */
OUT_MSG("Checking current owner of key")
if (!RegGetKeyOwner(RegKey, pTokenOwner))
{
OUT_MSG("Setting current process owner as the owner of the registry key")
if (RegSetKeyOwner(RegKey, pTokenOwner))
{
dwErr = GetLastError();
OUT_ERR("Setting registry key ownership failed", dwErr)
RegCloseKey(RegKey);
return dwErr;
}
}
ConvertSidToStringSid(pTokenOwner->Owner, &StringSid);
std::wcout << TEXT("\tCurrent process owner: ") << StringSid << std::endl;
LocalFree(StringSid);
StringSid = NULL;
/* Reopen the RegKey with WRITE_DAC */
RegCloseKey(RegKey);
OUT_MSG("Reopening Registry key with READ_CONTROL and WRITE_DAC")
RegOpenKeyAlt(HKEY_CLASSES_ROOT, key, READ_CONTROL | WRITE_DAC, &RegKey);
/* Setting ACE of registry key to Full Control */
if (!RegSetKeyACE(RegKey, pTokenOwner->Owner))
{
HeapFree(GetProcessHeap(), 0, (LPVOID)pTokenOwner);
RegCloseKey(RegKey);
return EXIT_FAILURE;
}
HeapFree(GetProcessHeap(), 0, (LPVOID)pTokenOwner);
/* Change Value Name */
/* Reopen RegKey with KEY_SET_VALUE */
RegCloseKey(RegKey);
OUT_MSG("Reopening Registry key with KEY_SET_VALUE")
RegOpenKeyAlt(HKEY_CLASSES_ROOT, key, READ_CONTROL | KEY_SET_VALUE, &RegKey);
std::wcout << std::endl;
OUT_MSG("Setting new value ShowBasedOnVelocityId")
RegSetValueEx(RegKey, (LPCWSTR)TEXT("ShowBasedOnVelocityId"), 0, REG_DWORD, (BYTE *)&pvData_HideValue, sizeof(pvData_HideValue));
OUT_MSG("Deleting old value HideBasedOnVelocityId")
RegDeleteValue(RegKey, (LPCWSTR)TEXT("HideBasedOnVelocityId"));
/* **** Change Permissions Back **** */
RegCloseKey(RegKey);
OUT_MSG("Reopening Registry key with WRITE_OWNER")
RegOpenKeyAlt(HKEY_CLASSES_ROOT, key, READ_CONTROL | WRITE_OWNER, &RegKey);
RegSetKeyDefaults(RegKey);
}
std::wcout << TEXT("\nRegistry is configured correctly on current key.\nYou can open cmd.exe shell from every directory!") << std::endl;
/* Exit */
OUT_MSG("Closing registry key")
RegCloseKey(RegKey);
return EXIT_SUCCESS;
}
/* return the permissions - removing the setvalue and the ownership - maybe */
BOOL BackToNormal(HKEY hKey)
{
return TRUE;
}
/* Cleaner way to open registry keys; just because */
BOOL static RegOpenKeyAlt(HKEY hKey, LPCWSTR lpSubKey, REGSAM samDesired, PHKEY phkResult)
{
LSTATUS r;
if ((r = RegOpenKeyEx(hKey, lpSubKey, 0, samDesired, phkResult)))
{
OUT_ERR("Failed to open the requested key", r)
if (r == ERROR_ACCESS_DENIED)
{
OUT_ERR("Run the process again as administrator", r)
return r;
}
}
return TRUE;
}