added logger

triing to add logger and some queue features (main loop can send and retrieve items form queue based on schedule and communication with dekstop interface)
This commit is contained in:
jakani24
2023-12-23 20:15:44 +01:00
parent 45f8ad413f
commit a7e323618c
92 changed files with 966 additions and 16 deletions

View File

@@ -4,11 +4,15 @@
#include <curl/curl.h>
#include <openssl/md5.h>
//#include <yara.h>
#include "app_ctrl.h"
#include "md5hash.h"
#include "connect.h"
#include "scan.h"
#include "app_ctrl.h"
#include "queue_ctrl.h"
#include "well_known.h"
#include "local_com.h"
#include "local_schedule.h"
#include "log.h"
int main() {
printf("welcome to the jakach security tool main thread\n");
//main thread:
@@ -19,9 +23,28 @@ int main() {
*/
while (!app_stop()) {
//run all the tasks described above
//check for tasks in com
//check for scheduled tasks
//execute tasks
//check_for_com_tasks(MAIN_COM,MAIN_COM_PATH);
log(LOGLEVEL::INFO,"test");
printf("check_from_com:%d\n",check_for_com_tasks(MAIN_COM, MAIN_COM_PATH));
printf("check_from_task:%d\n", check_for_sched_tasks(SCHED,SCHED_PATH));
//unlock_task("tsk1"); else it will only be executed once. but this function has to be called at the end of the task. else it will nvr be executed again. this would be bad :(
Sleep(1000);
int queue_size=get_queue_size();
for (int i = 0; i < queue_size; i++) {
char* queue_entry = new char[300 * 2 + 5];
queue_entry[0] = '\0';
queue_pop(queue_entry);
printf("%s\n", queue_entry);
delete[] queue_entry;
}
printf("\n\n\n");
}

View File

@@ -110,6 +110,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions);CURL_STATICLIB</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>c:\Program Files\OpenSSL-Win64\include\;c:\vcpkg\vcpkg-2023.08.09\installed\x64-windows\include\</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -139,6 +140,9 @@
<ClCompile Include="app_ctrl.h" />
<ClCompile Include="client_backend.cpp" />
<ClCompile Include="connect.cpp" />
<ClCompile Include="local_com.cpp" />
<ClCompile Include="local_schedule.cpp" />
<ClCompile Include="log.cpp" />
<ClCompile Include="md5hash.cpp" />
<ClCompile Include="permissions.cpp" />
<ClCompile Include="queue _ctrl.cpp" />
@@ -146,10 +150,14 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="connect.h" />
<ClInclude Include="local_com.h" />
<ClInclude Include="local_schedule.h" />
<ClInclude Include="log.h" />
<ClInclude Include="md5hash.h" />
<ClInclude Include="permissions.h" />
<ClInclude Include="queue_ctrl.h" />
<ClInclude Include="scan.h" />
<ClInclude Include="well_known.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@@ -39,6 +39,15 @@
<ClCompile Include="queue _ctrl.cpp">
<Filter>Headerdateien</Filter>
</ClCompile>
<ClCompile Include="local_com.cpp">
<Filter>Headerdateien</Filter>
</ClCompile>
<ClCompile Include="local_schedule.cpp">
<Filter>Headerdateien</Filter>
</ClCompile>
<ClCompile Include="log.cpp">
<Filter>Headerdateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="md5hash.h">
@@ -56,5 +65,17 @@
<ClInclude Include="queue_ctrl.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="well_known.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="local_com.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="local_schedule.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="log.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,59 @@
#ifndef LOCAL_COM_CPP
#define LOCAL_COM_CPP
#include "local_com.h"
int check_for_com_tasks(const char* com_name, const char* com_path) {
//check for new tasks in com file and add them into the queue
FILE* fp=nullptr;
char* command=new char [300];
if ((fopen_s(&fp,com_path, "r")) != 0) {
//panic, create log entry, return 1;
//no com file found = no communication needed
delete[] command;
return 1;
}
else {
while (!feof(fp)) {
fscanf_s(fp, "%s", command, 295); // get the command
if (!feof(fp)) { //last line =\n we dont want to process that
//printf("%s\n", command);
//we scanned the command, now we will scan the path. If the process does not need a path the path will be nopath. (set from client)
//get a full path. => " = start, second "= end
char* path = new char[300];
path[0] = '\0';
//search for starting ", then loop until ending "
int cnt = 0;
int chr = 0;
fgetc(fp);
fgetc(fp);
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if(chr!='\"')
path[cnt] = chr;
path[cnt+1] = '\0';
cnt++;
}
//printf("%s\n", path);
//we now have the command and the path. Now we will add it to the queue
char* queue_entry = new char[300*2+5]; //to enshure we have enough space
queue_entry[0] = '\0';
strcpy_s(queue_entry,600, command); //copy the command
strcat_s(queue_entry, 600, ";"); //add a ; to seperate command and path
//printf("%d\n", strlen(path));
strcat_s(queue_entry, 600, path); //add the path
//printf("%s::%d\n",queue_entry,strlen(queue_entry));
queue_push(queue_entry);
//(queue_entry);
delete[] queue_entry;
delete[] path;
}
}
}
remove(com_path);
fclose(fp);
delete[] command;
return 0;
}
#endif // !LOCAL_COM_CPP

View File

@@ -0,0 +1,7 @@
#pragma once
#include <iostream>
#include <cstdio>
#include "queue_ctrl.h"
#include "well_known.h"
int check_for_com_tasks(const char* com_name, const char* com_path);

View File

@@ -0,0 +1,174 @@
#ifndef LOCAL_SCHEDULE_CPP
#define LOCAL_SCHEDULE_CPP
#include "local_schedule.h"
#include "queue_ctrl.h"
/*
* To do:
* read scheduled tasks form file
* process time and date to determine if task should be executed
* add task to queue
*/
// Map to store task execution states
std::map<std::string, bool> task_states;
bool is_valid_field(const std::string& field, int value) {
if (field == "*") {
return true; // Wildcard matches any value
}
// Parse the field and check for matches
std::istringstream iss(field);
std::vector<std::string> tokens;
std::string token;
while (std::getline(iss, token, ',')) {
if (std::find(token.begin(), token.end(), '-') != token.end()) {
// Range of values
int start, end;
if (sscanf_s(token.c_str(), "%d-%d", &start, &end) == 2 && value >= start && value <= end) {
return true;
}
}
else {
// Single value
int singleValue;
if (sscanf_s(token.c_str(), "%d", &singleValue) == 1 && value == singleValue) {
return true;
}
}
}
return false;
}
bool is_task_due(const std::string& task_name, const std::string& cron_expression, const std::tm& current_time) {
std::istringstream iss(cron_expression);
std::vector<std::string> fields;
std::string field;
while (iss >> field) {
fields.push_back(field);
}
// Check if the 'fields' vector has at least 5 elements
if (fields.size() < 5) {
// Handle the case where the cron expression is not well-formed
std::cerr << "Error: Invalid cron expression." << std::endl;
return false;
}
// Check if the task should be executed
if (is_valid_field(fields[0], current_time.tm_min) &&
is_valid_field(fields[1], current_time.tm_hour) &&
is_valid_field(fields[2], current_time.tm_mday) &&
is_valid_field(fields[3], current_time.tm_mon + 1) &&
is_valid_field(fields[4], current_time.tm_wday + 1)) {
// Check if the task has already been executed in this minute
if (!task_states[task_name]) {
// Set the flag to indicate that the task has been executed
task_states[task_name] = true;
return true;
}
}
else {
// Reset the flag for a new minute
task_states[task_name] = false;
}
return false;
}
void unlock_task(const std::string& task_name) {
// Unlock the task by setting its state to false
task_states[task_name] = false;
}
int check_for_sched_tasks(const char* sched_name, const char* sched_path) {
FILE* fp = nullptr;
char* command = new char[300];
if ((fopen_s(&fp, sched_path, "r")) != 0) {
//panic, create log entry, return 1;
//no schedule file found. this is not normal
delete[] command;
return 1;
}
else {
while (!feof(fp)) {
//read date-time config. it starts with " and ends with "
char* datetime = new char[300];
datetime[0] = '\0';
//search for datetime, starting ", then loop until ending "
int cnt = 0;
int chr = 0;
chr = fgetc(fp);//read in the first ", or at least try it
//printf("%c\n", chr);
if (chr == '\"'){
chr = 0;
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if (chr != '\"')
datetime[cnt] = chr;
datetime[cnt + 1] = '\0';
cnt++;
//printf("scanning...\n");
}
}
//now we had datetime. we can scan the command and the path now
fscanf_s(fp, "%s", command, 295); // get the command
char* path = new char[300];
path[0] = '\0';
//search for datetime, starting ", then loop until ending "
cnt = 0;
chr = 0;
fgetc(fp); //get th ewhitespoace after the command
chr = fgetc(fp);//read in the first ", or at least try it
if (chr == '\"') {
chr = 0;
while (cnt < 295 && chr != '\"') {
chr = fgetc(fp); //get a char
if (chr != '\"')
path[cnt] = chr;
path[cnt + 1] = '\0';
cnt++;
}
}
//now get the taskname
char* taskname = new char[300];
taskname[0] = '\0';
fscanf_s(fp, "%s", taskname, 295); // get the taskname
//lets check if the command should be executed
//get the current time
std::time_t t = std::time(nullptr);
struct std::tm current_time;
localtime_s(&current_time, &t);
//printf("%s\n", datetime);
if (strcmp(datetime, "") != 0 && strcmp(command, "") != 0 && strcmp(path, "") != 0 && strcmp(taskname, "") != 0) {
if (is_task_due(taskname,datetime, current_time)) {
//printf("command:%s\n", command);
//printf("path:%s\n", path);
//now we can build up the command for the queue
char* queue_entry = new char[300 * 2 + 5]; //to enshure we have enough space
queue_entry[0] = '\0';
strcpy_s(queue_entry, 600, command); //copy the command
strcat_s(queue_entry, 600, ";"); //add a ; to seperate command and path
//printf("%d\n", strlen(path));
strcat_s(queue_entry, 600, path); //add the path
//printf("%s::%d\n",queue_entry,strlen(queue_entry));
queue_push(queue_entry);
delete[] queue_entry;
}
}
else {
//echo something was malformatted
}
delete[] datetime;
delete[] path;
delete[] taskname;
}
}
delete[] command;
fclose(fp);
return 0;
}
#endif // !LOCAL_SCHEDULE_CPP

View File

@@ -0,0 +1,12 @@
#pragma once
#ifndef LOCAL_SCHEDULE_H
#define LOCAL_SCHEDULE_H
#include <iostream>
#include <ctime>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
int check_for_sched_tasks(const char* sched_name, const char* sched_path);
void unlock_task(const std::string& task_name);
#endif // !LOCAL_SCHEDULE_H

View File

@@ -0,0 +1,59 @@
#ifndef LOG_CPP
#define LOG_CPP
#include "log.h"
std::string get_loglevel(LOGLEVEL level) {
switch (level) {
case LOGLEVEL::INFO:
return "INFO";
case LOGLEVEL::WARN:
return "WARNING";
case LOGLEVEL::ERR:
return "ERROR";
default:
return "UNKNOWN";
}
}
template <typename... Args>
void log(LOGLEVEL level, const std::string& message, Args&&... args) {
std::string prefix = get_loglevel(level);
std::time_t now = std::time(nullptr);
std::tm tm = *std::localtime(&now);
std::ostringstream logStream;
logStream << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << " " << prefix << message;
if constexpr (sizeof...(args) > 0) {
((logStream << ' ' << std::forward<Args>(args)), ...);
}
logStream << std::endl;
std::string logString = logStream.str();
// Open the file based on log level
std::ofstream logFile;
switch (level) {
case LOGLEVEL::INFO:
logFile.open(INFOFILE, std::ios_base::app);
break;
case LOGLEVEL::WARNING:
logFile.open(WARNFILE, std::ios_base::app);
break;
case LOGLEVEL::ERROR:
logFile.open(ERRORFILE, std::ios_base::app);
break;
}
// Write the log to the file
if (logFile.is_open()) {
logFile << logString.c_str();
logFile.close();
}
//write the log to the general file
logFile.open(LOGFILE, std::ios_base::app);
if (logFile.is_open()) {
logFile << logString.c_str();
logFile.close();
}
}
#endif

21
src/client_backend/log.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef LOG_H
#define LOG_H
#include <iostream>
#include <ctime>
#include <iomanip>
#include <sstream>
#include "well_known.h"
enum class LOGLEVEL {
INFO,
WARN,
ERR
};
std::string get_loglevel(LOGLEVEL level);
template <typename... Args>
void log(LOGLEVEL level, const std::string& message, Args&&... args);
#endif // LOGGER_H

View File

@@ -29,7 +29,7 @@ BOOL create_file_protection(SECURITY_ATTRIBUTES* pSA)
// Administrators are allowed full control.
// Modify these values as needed to generate the proper
// DACL for your application.
TCHAR* szSD = TEXT("D:")
LPCSTR szSD = TEXT("D:")
TEXT("(D;OICI;GA;;;BG)") // Deny access to guest users
TEXT("(D;OICI;GA;;;AN)") // Deny access to unauthenticated users
//TEXT("(D;OICI;GA;;;AU)") // Deny access to authenticated users do not execute else not even admins have rights anymore :(

View File

@@ -0,0 +1,14 @@
#pragma once
#define MAIN_COM "main_com.txt"
#define MAIN_COM_PATH "C:\\Program Files\\cyberhex\\com\\main_com.txt"
#define SECURE_COM "secure_com.txt"
#define SECURE_COM_PATH "C:\\Program Files\\cyberhex\\secure\\com\\secure_com.txt"
#define SCHED "sched.txt"
#define SCHED_PATH "C:\\Program Files\\cyberhex\\secure\\settings\\sched.txt"
#define LOGFILE "C:\\Program Files\\cyberhex\\secure\\log\\log.txt"
#define INFOFILE "C:\\Program Files\\cyberhex\\secure\\log\\info.txt"
#define WARNFILE "C:\\Program Files\\cyberhex\\secure\\log\\warn.txt"
#define ERRORFILE "C:\\Program Files\\cyberhex\\secure\\log\\error.txt"

Binary file not shown.

View File

@@ -0,0 +1,10 @@
{
"Version": "1.2",
"Data": {
"Source": "c:\\users\\janis\\documents\\projekte_mit_c\\ma\\ma\\src\\client_backend\\app_ctrl.h",
"ProvidedModule": "",
"Includes": [],
"ImportedModules": [],
"ImportedHeaderUnits": []
}
}

View File

@@ -0,0 +1,22 @@
{
"version": 1,
"revision": 0,
"rules": [
{
"primary-output": "x64\\Debug\\app_ctrl.h.obj",
"outputs": [
"C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\x64\\Debug\\vc143.pdb",
"x64\\Debug\\",
"C:\\Users\\janis\\Documents\\Projekte_mit_c\\ma\\ma\\src\\client_backend\\x64\\Debug\\app_ctrl.h.ifc"
],
"provides": [
{
"logical-name": "app_ctrl.h",
"source-path": "c:\\users\\janis\\documents\\projekte_mit_c\\ma\\ma\\src\\client_backend\\app_ctrl.h",
"lookup-method": "absolute"
}
],
"requires": []
}
]
}

View File

@@ -0,0 +1,10 @@
{
"Version": "1.2",
"Data": {
"Source": "c:\\users\\janis\\documents\\projekte_mit_c\\ma\\ma\\src\\client_backend\\app_ctrl.h",
"ProvidedModule": "",
"Includes": [],
"ImportedModules": [],
"ImportedHeaderUnits": []
}
}

View File

@@ -0,0 +1,9 @@
{
"Version": "1.1",
"Data": {
"Source": "c:\\users\\janis\\documents\\projekte_mit_c\\ma\\ma\\src\\client_backend\\app_ctrl.h",
"ProvidedModule": "",
"ImportedModules": [],
"ImportedHeaderUnits": []
}
}

View File

@@ -1,2 +1,6 @@
 client_backend.cpp
client_backend.vcxproj -> C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\client_backend.exe
 Quellen werden auf Modulabhängigkeiten überprüft...
client_backend.cpp
log.cpp
Code wird generiert...
client_backend.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""void __cdecl log<>(enum LOGLEVEL,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??$log@$$V@@YAXW4LOGLEVEL@@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)" in Funktion "main".
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\client_backend.exe : fatal error LNK1120: 1 nicht aufgelöste Externe

View File

@@ -1,7 +1,10 @@
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\app_ctrl.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\app_ctrl.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\app_ctrl.h;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\app_ctrl.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\app_ctrl.h;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\app_ctrl.h.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\client_backend.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\client_backend.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\connect.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\connect.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\local_com.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\local_com.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\local_schedule.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\local_schedule.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\log.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\log.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\md5hash.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\md5hash.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\permissions.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\permissions.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\queue _ctrl.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\queue _ctrl.obj

View File

@@ -1,2 +1,2 @@
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.37.32822:TargetPlatformVersion=10.0.22621.0:VcpkgTriplet=x64-windows:
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.38.33130:TargetPlatformVersion=10.0.22621.0:VcpkgTriplet=x64-windows:
Debug|x64|C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\|

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@@ -0,0 +1 @@
<EFBFBD><EFBFBD>