-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckTCCRevisedSafeRegGetValueAPathEnvironmentVariable.c
89 lines (69 loc) · 3.65 KB
/
CheckTCCRevisedSafeRegGetValueAPathEnvironmentVariable.c
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
#pragma comment(lib, "advapi32") // No more Need for tcc -ladvapi32 RegGetValueA.c error: undefined symbol 'RegGetValueA'
#include <stdio.h> // Prevents a warning: warning: implicit declaration of function 'printf'
#include <windows.h> // Required, else an error: error: 'DWORD' undeclared
#include <stdlib.h> // Gives access to Malloc, And Prevents: warning: implicit declaration of function 'malloc'
// Calloc, warning: cast between pointer and integer of different size
// Realloc
/*
Required, else an error:
"error: undefined symbol 'RegGetValueA'"
Proper compilation method/tutorial
tcc -impdef advapi32.dll -o .\lib\advapi32.def
tcc -ladvapi32 RegGetValueAPathEnvironmentVariable.c
*/
/* References
pvData-split
https://www.geeksforgeeks.org/how-to-split-a-string-in-cc-python-and-java/
https://www.geeksforgeeks.org/how-to-append-a-character-to-a-string-in-c/
*/
void main(){
DWORD BufferSize = 0;
HKEY hkey = HKEY_LOCAL_MACHINE;
LPCSTR lpSubKey = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
LPCSTR lpValue = "Path";
DWORD dwFlags = RRF_RT_ANY;
LPDWORD pdwType = NULL;
PVOID pvData = NULL;
LPDWORD pcbData = &BufferSize;
// First, RegGetValueA retrieves pcbData, which contains size of pvData in bytes
RegGetValueA(hkey, lpSubKey, lpValue, dwFlags, pdwType, pvData, pcbData);
// Allocate Memory for pvData | Returns address for pvData to use
pvData = (int*)malloc(*pcbData);
// Second, RegGetValueA retrieves pvData, which contains path variable value
RegGetValueA(hkey, lpSubKey, lpValue, dwFlags, pdwType, pvData, pcbData);
puts("___________________[pvData]___________________");
puts(pvData);
puts("________________[pvData-split]________________");
puts("Not working properly, needs checking for directory and correct placement of \\\\");
puts("Not working properly, C:\\Program Files (x86) contains space, needs sanitation");
puts("Not true, everything works, the emptyString had too little space allocated");
puts("Not true, everything works, no need for checking if directory or not, executes properly even with double backslashes");
puts("C:\\ is lower priority than C:\\Windows directory,but higher than C:\\Users\\user\\Desktop");
puts("Make priority for executables by folder location");
char *token = strtok(pvData, ";");
while (token != NULL)
{
FILE *file;
char filename[50] = "\\tcc.exe";
char emptyString[90];
emptyString[0] = '\0';
strcat(emptyString, token);
strcat(emptyString, filename);
printf("Concatinated: ");
printf(emptyString);
printf("\n");
if (file = fopen(emptyString, "r")){
fclose(file);
printf("yes\n");
}
//printf("%s\n", token);
token = strtok(NULL, ";");
}
puts("___________________[Debug]____________________");
printf(" Retrieved information about array size: %i\n",*pcbData);
printf(" Actual size of the array: %i\n", strlen(pvData));
printf(" Manually Allocated bytes for the array: %zu", sizeof(pvData));
printf("\n (Since malloc was used, the dynamic Allocation makes the Manual Allocation results strange.)\n");
// https://stackoverflow.com/questions/37447198/dynamically-allocated-variable-size // https://stackoverflow.com/questions/37447198/dynamically-allocated-variable-size
free(pvData);
}