Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
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 GetExecutableDirectory ()
bool HasCommandLineFlag (const char *flag)
std::string GetBaseName (const std::string &path)
std::string FormatTime (const std::time_t &time)
std::string FormatSystemTime (const FILETIME &ft)
 The log's Time format: "2026/11/15 21:05:48.123", in the FILETIME's zone (UTC).

Function Documentation

◆ FileExists()

bool FileExists ( const char * filename)

Definition at line 34 of file utils.cpp.

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

◆ FormatSystemTime()

std::string FormatSystemTime ( const FILETIME & ft)

The log's Time format: "2026/11/15 21:05:48.123", in the FILETIME's zone (UTC).

Definition at line 109 of file utils.cpp.

109 {
110 SYSTEMTIME st;
111 FileTimeToSystemTime(&ft, &st);
112
113 std::ostringstream ss;
114 ss << std::setfill('0')
115 << st.wYear << '/'
116 << std::setw(2) << st.wMonth << '/'
117 << std::setw(2) << st.wDay << ' '
118 << std::setw(2) << st.wHour << ':'
119 << std::setw(2) << st.wMinute << ':'
120 << std::setw(2) << st.wSecond << '.'
121 << std::setw(3) << st.wMilliseconds;
122 return ss.str();
123}

◆ FormatTime()

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

Definition at line 99 of file utils.cpp.

99 {
100 std::tm timeinfo;
101 localtime_s(&timeinfo, &time);
102
103 char buffer[80];
104 std::strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", &timeinfo);
105
106 return std::string(buffer);
107}

Referenced by CleanUpProtobufMessage().

◆ GetBaseName()

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

Definition at line 94 of file utils.cpp.

94 {
95 const char* fileName = PathFindFileNameA(path.c_str());
96 return std::string(fileName);
97}

◆ GetExecutableDirectory()

std::string GetExecutableDirectory ( )

Definition at line 71 of file utils.cpp.

72{
73 char buffer[MAX_PATH];
74 DWORD length = GetModuleFileNameA(NULL, buffer, MAX_PATH);
75
76 if (length == 0 || length == MAX_PATH)
77 {
78 return "";
79 }
80
81 std::string path(buffer, length);
82 return path.substr(0, path.find_last_of("\\/"));
83}
#define MAX_PATH
Definition callbacks.h:7

References MAX_PATH.

Referenced by BuildContainerCommandLine(), PanoptesDatabase::InitializeDatabase(), StartContainers(), and WinMain().

◆ HasCommandLineFlag()

bool HasCommandLineFlag ( const char * flag)

Definition at line 85 of file utils.cpp.

85 {
86 for (int i = 1; i < __argc; i++) {
87 if (_stricmp(__argv[i], flag) == 0) {
88 return true;
89 }
90 }
91 return false;
92}

Referenced by WinMain().

◆ IsRunningAsAdmin()

bool IsRunningAsAdmin ( )

Definition at line 10 of file utils.cpp.

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

Referenced by InitializeShutdown(), and WinMain().

◆ ToLower()

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

Definition at line 63 of file utils.cpp.

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

◆ ToString()

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

Definition at line 51 of file utils.cpp.

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

Referenced by IsDetectorPeer(), ResolveProcessImage(), and RespondToDetection().