Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1#include "utils.h"
2#include <algorithm>
3#include <Shlwapi.h>
4#include <stdexcept>
5#include <vector>
6#include <sstream>
7
8
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}
32
33bool FileExists(const char* filename) {
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}
49
50std::string ToString(const std::wstring& wstr)
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}
61
62std::string ToLower(std::string str) {
63 std::transform(str.begin(), str.end(), str.begin(),
64 [](unsigned char c) { return std::tolower(c); });
65 return str;
66}
67
68std::string GetCurrentPath()
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}
81
82std::string GetBaseName(const std::string& path) {
83 const char* fileName = PathFindFileNameA(path.c_str());
84 return std::string(fileName);
85}
86
87std::string FormatTime(const std::time_t& time) {
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}
#define MAX_PATH
Definition callbacks.h:6
int BOOL
Definition inject.h:3
unsigned long DWORD
Definition inject.h:2
bool FileExists(const char *filename)
Definition utils.cpp:33
std::string GetCurrentPath()
Definition utils.cpp:68
std::string GetBaseName(const std::string &path)
Definition utils.cpp:82
bool IsRunningAsAdmin()
Definition utils.cpp:9
std::string FormatTime(const std::time_t &time)
Definition utils.cpp:87
std::string ToLower(std::string str)
Definition utils.cpp:62
std::string ToString(const std::wstring &wstr)
Definition utils.cpp:50