Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Functions
utils.h File Reference
#include <Windows.h>
#include <string>
#include <ctime>

Go to the source code of this file.

Functions

bool IsRunningAsAdmin ()
 
bool FileExists (const char *filename)
 
std::string ToString (const std::wstring &wstr)
 
std::string ToLower (std::string str)
 
std::string GetCurrentPath ()
 
std::string GetBaseName (const std::string &path)
 
std::string FormatTime (const std::time_t &time)
 

Function Documentation

◆ FileExists()

bool FileExists ( const char *  filename)

Definition at line 33 of file utils.cpp.

33 {
34 HANDLE hFile = CreateFileA(filename,
35 GENERIC_READ, // Open for reading
36 FILE_SHARE_READ, // Share for reading
37 NULL, // Default security
38 OPEN_EXISTING, // Open only if exists
39 FILE_ATTRIBUTE_NORMAL, // Normal file
40 NULL); // No template
41
42 if (hFile == INVALID_HANDLE_VALUE) {
43 return false; // File does not exist
44 }
45
46 CloseHandle(hFile);
47 return true; // File exists
48}

◆ FormatTime()

std::string FormatTime ( const std::time_t &  time)

Definition at line 87 of file utils.cpp.

87 {
88 std::tm timeinfo;
89 localtime_s(&timeinfo, &time);
90
91 char buffer[80];
92 std::strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", &timeinfo);
93
94 return std::string(buffer);
95}

Referenced by CleanUpProtobufMessage().

◆ GetBaseName()

std::string GetBaseName ( const std::string &  path)

Definition at line 82 of file utils.cpp.

82 {
83 const char* fileName = PathFindFileNameA(path.c_str());
84 return std::string(fileName);
85}

◆ GetCurrentPath()

std::string GetCurrentPath ( )

Definition at line 68 of file utils.cpp.

69{
70 char buffer[MAX_PATH];
71 DWORD length = GetCurrentDirectoryA(MAX_PATH, buffer);
72
73 if (length == 0)
74 {
75 // Handle error - you might want to throw an exception or return an error code
76 return "";
77 }
78
79 return std::string(buffer);
80}
#define MAX_PATH
Definition callbacks.h:6
unsigned long DWORD
Definition inject.h:2

References MAX_PATH.

Referenced by BuildContainerCommandLine(), and WinMain().

◆ IsRunningAsAdmin()

bool IsRunningAsAdmin ( )

Definition at line 9 of file utils.cpp.

9 {
10 BOOL isAdmin = FALSE;
11 PSID adminGroup = NULL;
12
13 // Allocate and initialize a SID of the administrators group.
14 SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
15 if (AllocateAndInitializeSid(
16 &NtAuthority,
17 2,
18 SECURITY_BUILTIN_DOMAIN_RID,
19 DOMAIN_ALIAS_RID_ADMINS,
20 0, 0, 0, 0, 0, 0,
21 &adminGroup)) {
22 // Check whether the SID of administrators group is enabled in
23 // the primary access token of the process.
24 if (!CheckTokenMembership(NULL, adminGroup, &isAdmin)) {
25 isAdmin = FALSE;
26 }
27 FreeSid(adminGroup);
28 }
29
30 return isAdmin != FALSE;
31}
int BOOL
Definition inject.h:3

Referenced by WinMain().

◆ ToLower()

std::string ToLower ( std::string  str)

Definition at line 62 of file utils.cpp.

62 {
63 std::transform(str.begin(), str.end(), str.begin(),
64 [](unsigned char c) { return std::tolower(c); });
65 return str;
66}

◆ ToString()

std::string ToString ( const std::wstring &  wstr)

Definition at line 50 of file utils.cpp.

51{
52 if (wstr.empty())
53 {
54 return std::string();
55 }
56 int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
57 std::string str(size_needed, 0);
58 WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &str[0], size_needed, NULL, NULL);
59 return str;
60}

Referenced by DisplayEventInfo().