-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.c
164 lines (148 loc) · 6.78 KB
/
code.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
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
#include <Windows.h>
#include <stdio.h>
#include <Psapi.h>
#include <LM.h>
typedef NTSYSAPI NTSTATUS(WINAPI* procRtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation);
#define STATUS_SUCCESS 0x00000000
int main() {
// Get the username
{
char userName[UNLEN + 1];
DWORD userNameLength = UNLEN + 1;
if (GetUserNameA(userName, &userNameLength)) {
printf("User: %s\n", userName);
}
else {
printf("[-] Error: %d\n", GetLastError());
}
}
// Get hostname
{
char hostName[UNLEN + 1];
DWORD hostNameLength = UNLEN + 1;
if (GetComputerNameExA(ComputerNameDnsFullyQualified, hostName, &hostNameLength)) {
printf("Hostname: %s\n", hostName);
}
else {
printf("[-] Error: %d\n", GetLastError());
}
}
// Get the OS version
{
RTL_OSVERSIONINFOW osversioninfow = { 0 };
HMODULE hntdll = GetModuleHandleA("ntdll.dll");
procRtlGetVersion myRtlGetVersion = (procRtlGetVersion)GetProcAddress(hntdll, "RtlGetVersion");
if (myRtlGetVersion(&osversioninfow) == STATUS_SUCCESS) {
printf("Windows Version: %lu.%lu\n", osversioninfow.dwMajorVersion, osversioninfow.dwMinorVersion);
printf("Windows Build: %lu\n", osversioninfow.dwBuildNumber);
}
else {
printf("[-] Error: %d\n", GetLastError());
}
}
// Get the current directory
{
char currentDir[MAX_PATH + 1];
DWORD currentDirLength = MAX_PATH + 1;
if (GetCurrentDirectoryA(currentDirLength, currentDir) != 0) {
printf("Current Directory: %s\n", currentDir);
}
else {
printf("[-] Error: %d\n", GetLastError());
}
}
// Get the system uptime
{
DWORD tickCount = (DWORD)GetTickCount64();
DWORD dcuptime = ((tickCount / 1000) / 60);
printf("Uptime: %lu minutes\n", dcuptime);
}
// Enumerate device drivers
{
LPVOID drivers[1024];
DWORD cbNeeded;
int cDrivers, i;
if (EnumDeviceDrivers(drivers, sizeof(drivers), &cbNeeded) && cbNeeded < sizeof(drivers)) {
CHAR szDriver[1024];
cDrivers = cbNeeded / sizeof(drivers[0]);
printf("[+] %d drivers found\n", cDrivers);
for (i = 0; i < cDrivers; i++) {
if (GetDeviceDriverBaseNameA(drivers[i], szDriver, sizeof(szDriver) / sizeof(szDriver[0]))) {
printf("[.] %s => %p\n", szDriver, drivers[i]);
}
else {
printf("[-] E: %d\n", GetLastError());
}
}
}
}
// Enumerate services
{
SC_HANDLE hRemoteSvcManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
if (hRemoteSvcManager == NULL) {
printf("[-] E: %d\n", GetLastError());
}
else {
DWORD BytesNeeded;
DWORD NumberOfServices;
DWORD ResumeHandleSize = 0;
DWORD ServiceStructSize;
LPENUM_SERVICE_STATUSA lpServiceEnumStruct;
INT errVal = EnumServicesStatusA(hRemoteSvcManager, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &BytesNeeded, &NumberOfServices, &ResumeHandleSize);
if ((errVal == ERROR_INVALID_HANDLE) || (errVal == ERROR_ACCESS_DENIED) || (errVal == ERROR_INVALID_PARAMETER)) {
printf("[-] E: %d\n", GetLastError());
}
else {
ServiceStructSize = BytesNeeded + sizeof(ENUM_SERVICE_STATUSA);
lpServiceEnumStruct = (LPENUM_SERVICE_STATUSA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ServiceStructSize);
if (lpServiceEnumStruct == NULL) {
printf("[-] E: %d\n", GetLastError());
}
else {
errVal = EnumServicesStatusA(hRemoteSvcManager, SERVICE_WIN32, SERVICE_STATE_ALL, lpServiceEnumStruct, ServiceStructSize, &BytesNeeded, &NumberOfServices, &ResumeHandleSize);
if ((errVal != 0)) {
printf("\n\n[+] Enumerating Services [%lu]\n", NumberOfServices);
for (int i = 0; i < (int)NumberOfServices; i++) {
SC_HANDLE hSvc = OpenServiceA(hRemoteSvcManager, lpServiceEnumStruct[i].lpServiceName, SERVICE_QUERY_CONFIG);
if (hSvc != NULL) {
LPQUERY_SERVICE_CONFIGA lpServiceConfig;
DWORD scbytesNeeded;
DWORD ScdwBytes;
QueryServiceConfigA(hSvc, NULL, 0, &scbytesNeeded);
ScdwBytes = scbytesNeeded;
lpServiceConfig = (LPQUERY_SERVICE_CONFIGA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ScdwBytes);
if (lpServiceConfig != NULL) {
errVal = QueryServiceConfigA(hSvc, lpServiceConfig, ScdwBytes, &scbytesNeeded);
if (errVal != 0) {
printf("[+] Display Name: %s\n", lpServiceEnumStruct[i].lpDisplayName);
printf("[.] Service Name: %s\n", lpServiceEnumStruct[i].lpServiceName);
printf("[.] Service State: %lu\n", lpServiceEnumStruct[i].ServiceStatus.dwCurrentState);
printf("[.] Service Path: %s\n", lpServiceConfig->lpBinaryPathName);
printf("[.] Service User: %s\n", lpServiceConfig->lpServiceStartName);
printf("[.] Service Type: %lu\n\n", lpServiceEnumStruct[i].ServiceStatus.dwServiceType);
HeapFree(GetProcessHeap(), 0, lpServiceConfig);
CloseServiceHandle(hSvc);
}
else {
printf("[-] E: %d\n", GetLastError());
HeapFree(GetProcessHeap(), 0, lpServiceConfig);
CloseServiceHandle(hSvc);
}
}
}
else {
printf("[-] E: %d\n", GetLastError());
}
}
}
else {
printf("[-] E: %d\n", GetLastError());
}
HeapFree(GetProcessHeap(), 0, lpServiceEnumStruct);
}
}
CloseServiceHandle(hRemoteSvcManager);
}
}
return 0;
}