some small updates

This commit is contained in:
jakani24
2024-01-14 12:54:07 +01:00
parent 343720365a
commit f1ca006998
36 changed files with 128 additions and 99 deletions

1
.gitignore vendored
View File

@@ -31,3 +31,4 @@
*.out
*.app
*.ipch
*.IPCH

View File

@@ -161,10 +161,10 @@ void process_changes(const FILE_NOTIFY_INFORMATION* pInfo) {
return;
}
else {
std::thread scan_thread(action_scanfile_t, filename_str);
std::thread scan_thread(scan_file_t, filename_str);
scan_thread.detach();
}
log(LOGLEVEL::INFO, "[process_changes()]: File change: ", filename_str.c_str(), " while monitoring directory for changes");
//log(LOGLEVEL::INFO, "[process_changes()]: File change: ", filename_str.c_str(), " while monitoring directory for changes");
}
/* this was the old algorithm. it was slower and used up more resources, because it used a database to track which files have been modified instead of using the windows internal functions
void monitor_directory(LPCSTR directory) {

View File

@@ -150,6 +150,7 @@
<ClCompile Include="scan.cpp" />
<ClCompile Include="settings.cpp" />
<ClCompile Include="thread_ctrl.cpp" />
<ClCompile Include="update.cpp" />
<ClCompile Include="virus_ctrl.cpp" />
</ItemGroup>
<ItemGroup>
@@ -165,6 +166,7 @@
<ClInclude Include="scan.h" />
<ClInclude Include="settings.h" />
<ClInclude Include="thread_ctrl.h" />
<ClInclude Include="update.h" />
<ClInclude Include="virus_ctrl.h" />
<ClInclude Include="well_known.h" />
</ItemGroup>

View File

@@ -60,6 +60,9 @@
<ClCompile Include="settings.cpp">
<Filter>Headerdateien</Filter>
</ClCompile>
<ClCompile Include="update.cpp">
<Filter>Headerdateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="md5hash.h">
@@ -104,6 +107,9 @@
<ClInclude Include="resource.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="update.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="client_backend.rc">

View File

@@ -41,7 +41,7 @@ size_t write_callback(void* contents, size_t size, size_t nmemb, void* userp) {
return totalSize;
}
int download_file_from_srv(const char* url, const char* outputFileName) {
int download_file_from_srv(const char* url, const char* output_file_path) {
//use curl to download a file from a server
CURL* curl;
@@ -57,7 +57,7 @@ int download_file_from_srv(const char* url, const char* outputFileName) {
curl_easy_setopt(curl, CURLOPT_URL, url);
// Create a file to write the downloaded data
output_file = fopen(outputFileName, "wb");
output_file = fopen(output_file_path, "wb");
if (!output_file) {
curl_easy_cleanup(curl);
return 1;

View File

@@ -64,7 +64,7 @@ bool is_task_due(const std::string& task_name, const std::string& cron_expressio
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
// Check if the task has already been executed
if (!task_states[task_name]) {
// Set the flag to indicate that the task has been executed
task_states[task_name] = true;
@@ -72,7 +72,7 @@ bool is_task_due(const std::string& task_name, const std::string& cron_expressio
}
}
else {
// Reset the flag for a new minute
// Reset the flag
task_states[task_name] = false;
}

View File

@@ -19,46 +19,6 @@ std::string get_loglevel(LOGLEVEL level) {
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

View File

@@ -24,12 +24,17 @@ void log(LOGLEVEL level, const std::string& message, Args&&... args) {
localtime_s(&tm, &now);
int error = 0;
std::ostringstream logStream;
std::ostringstream to_srv;
to_srv << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << ";" << prefix << ";" << message;
logStream << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << " " << prefix << " " << message;
if constexpr (sizeof...(args) > 0) {
((logStream << ' ' << std::forward<Args>(args)), ...);
((to_srv << ' ' << std::forward<Args>(args)), ...);
}
logStream << std::endl;
to_srv << std::endl;
std::string logString = logStream.str();
std::string to_srv_string = to_srv.str();
printf("info from logger: %s", logString.c_str());
// Open the file based on log level
FILE* fp;
@@ -86,6 +91,10 @@ void log(LOGLEVEL level, const std::string& message, Args&&... args) {
fprintf_s(fp, "%s", logString.c_str());
fclose(fp);
}
if (fopen_s(&fp, SRV_LOGFILE, "a") == 0) {
fprintf_s(fp, "%s", to_srv_string.c_str());
fclose(fp);
}
}
}

View File

@@ -12,6 +12,7 @@
#include "well_known.h"
#include "log.h"
#include "virus_ctrl.h"
#include "app_ctrl.h"
#ifndef SCAN_CPP
#define SCAN_CPP
std::unordered_map<std::string, HANDLE> fileHandles;
@@ -121,7 +122,7 @@ bool file_exists(const std::string& filePath) {
}
//this is the main function to scan folders. it will then start multuiple threads based on the number of cores / settings
void scan_folder_recursive(const std::string& directory) {
void scan_folder(const std::string& directory) {
std::string search_path = directory + "\\*.*";
WIN32_FIND_DATA find_file_data;
HANDLE hFind = FindFirstFile(search_path.c_str(), &find_file_data);
@@ -140,7 +141,7 @@ void scan_folder_recursive(const std::string& directory) {
const std::string full_path = directory + "\\" + find_file_data.cFileName;
if (find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// If it's a directory, recurse into it
scan_folder_recursive(full_path);
scan_folder(full_path);
}
else {
//action scanfile_t will start the trheads for scanning the hashes
@@ -151,7 +152,7 @@ void scan_folder_recursive(const std::string& directory) {
Sleep(10);
}
num_threads++;
std::thread scan_thread(action_scanfile_t, full_path);
std::thread scan_thread(scan_file_t, full_path);
scan_thread.detach();
cnt++;
@@ -169,10 +170,12 @@ void scan_folder_recursive(const std::string& directory) {
//for singlethreaded scans
void action_scanfile(const char*filepath) {
thread_init();
char* db_path = new char[300];
//log(LOGLEVEL::INFO, "[action_scanfile_t()]: Scanning file: ", filepath);
if (strlen(filepath) == 0 or strcmp("", filepath) == 0 or file_exists(filepath) == false) {
thread_shutdown();
return; //no filepath given or file not accessible
}
else {
@@ -184,35 +187,15 @@ void action_scanfile(const char*filepath) {
delete[] hash;
}
delete[] db_path;
thread_shutdown();
}
void action_scanfolder(const char* folderpath) {
thread_init();
scan_folder(folderpath);
thread_shutdown();
}
/*
void action_scanfile_t( const char*filepath) {
char* db_path = new char[300];
int max_threads = std::thread::hardware_concurrency();
//log(LOGLEVEL::INFO, "[action_scanfile_t()]: Scanning file: ", filepath);
if (strlen(filepath) == 0 or strcmp("", filepath) == 0 or file_exists(filepath) == false) {
return; //no filepath given or file not accessible
}
else {
char* hash = new char[300];
hash[0] = '\0';
hash = md5_file_t(filepath);
sprintf_s(db_path, 295, "%s\\%c%c.jdbf", DB_DIR, hash[0], hash[1]);
while (num_threads >= max_threads) {
Sleep(10);
}
num_threads++;
std::thread search_thread(search_hash,db_path, hash, filepath);
search_thread.detach();
std::this_thread::sleep_for(std::chrono::microseconds(50));
delete[] hash;
}
delete[] db_path;
}*/
void action_scanfile_t(const std::string& filepath_) {
void scan_file_t(const std::string& filepath_) {
thread_local const std::string filepath (filepath_);
thread_local char* db_path = new char[300];
thread_local char*hash = new char[300];

View File

@@ -10,9 +10,10 @@
#include <future>
#include <vector>
#include <algorithm>
void scan_folder_recursive(const std::string& directory);
void scan_folder(const std::string& directory);
void action_scanfile(const char* filepath);
void action_scanfolder(const char* folderpath);
//void action_scanfile_t(const char* filepath);
void action_scanfile_t(const std::string& filepath_);
void scan_file_t(const std::string& filepath_);
void initialize(const std::string& folderPath);
void cleanup();

View File

@@ -4,6 +4,7 @@
#include "log.h"
#include "well_known.h"
#include "scan.h"
#include "app_ctrl.h"
void split(char* input,char*delimiter, char* out1, char* out2) {
//split a string at the delimiter. the delimiter only occurs once. so the first part is out1 and the second part is out2
int i = 0;
@@ -28,22 +29,26 @@ void split(char* input,char*delimiter, char* out1, char* out2) {
}
}
}
int start_thread(const char*command) {
char*out2= new char[100]; //for the command
char*out1 = new char[300]; //for the arguments
split((char*)command, (char*)";", (char*)out1, (char*)out2);
int start_thread(const char* command) {
if (can_run_thread()) {
char* out2 = new char[100]; //for the command
char* out1 = new char[300]; //for the arguments
split((char*)command, (char*)";", (char*)out1, (char*)out2);
//printf("out1: %s\n", out1);
//printf("out2: %s\n", out2);
//determine what should be executed
if(strcmp(out1,"scanfile")==0){
//start a new thread with the scanfile function
std::thread t1(action_scanfile, out2);
//printf("out1: %s\n", out1);
//printf("out2: %s\n", out2);
//determine what should be executed
if (strcmp(out1, "scanfile") == 0) {
//start a new thread with the scanfile function
std::thread t1(action_scanfile, out2);
}
else if (strcmp(out1, "scanfolder") == 0) {
//start a new thread with the scanfolder function
std::thread t1(action_scanfolder, out2);
}
delete[] out1;
delete[] out2;
}
delete[] out1;
delete[] out2;
return 0;
}

View File

@@ -0,0 +1,58 @@
#ifndef UPDATE_CPP
#define UPDATE_CPP
#include "update.h"
#include "log.h"
#include "connect.h"
#include "settings.h"
int update_db(const std::string& folder_path) {
//download the databases from the server
for (char firstChar = '0'; firstChar <= 'f'; ++firstChar) {
for (char secondChar = '0'; secondChar <= 'f'; ++secondChar) {
// Ensure that the characters are valid hexadecimal digits
if (!std::isxdigit(firstChar) || !std::isxdigit(secondChar) or std::isupper(firstChar) or std::isupper(secondChar)) {
continue;
}
// Create the filename based on the naming convention
std::string file_path = folder_path + "\\" + firstChar + secondChar + ".jdbf";
std::string file_name = firstChar + secondChar + ".jdbf";
//create the strings to download the files
char*url=new char[300];
char*output_path=new char[300];
get_setting("server:server_url", url);
strcat_s(url, 295,"/database/");
strcat_s(url, 295,file_name.c_str() );
strcpy_s(output_path, 295, file_path.c_str());
int res = download_file_from_srv(url, output_path);
if (res != 0) {
log(LOGLEVEL::ERR, "[update_db()]: Error downloading database file from server", url);
return 1;
}
delete[] url;
delete[] output_path;
}
}
return 0;
}
int update_settings(const std::string& folder_path) {
//create the strings to download the files
char* url = new char[300];
get_setting("server:server_url", url);
strcat_s(url, 295, "/database/");
strcat_s(url, 295, "settings_db.txt");
int res = download_file_from_srv(url, SETTINGS_DB);
if (res != 0) {
log(LOGLEVEL::ERR, "[update_db()]: Error downloading settings database file from server", url);
return 1;
}
delete[] url;
return 0;
}
#endif

View File

@@ -0,0 +1,7 @@
#pragma once
#ifndef UPDATE_H
#define UPDATE_H
#include <curl/curl.h>
int update_db(const std::string& folder_path);
int update_settings(const std::string& folder_path);
#endif

View File

@@ -16,6 +16,7 @@
#define ERRORFILE "C:\\Program Files\\cyberhex\\secure\\log\\error.txt"
#define VIRUSFILE "C:\\Program Files\\cyberhex\\secure\\log\\virus.txt"
#define RISKFILE "C:\\Program Files\\cyberhex\\secure\\log\\risk.txt"
#define SRV_LOGFILE "C:\\Program Files\\cyberhex\\secure\\log\\srv_log.txt"
#define FOLDER_DATABASE_DIR "C:\\Program Files\\cyberhex\\secure\\database\\folder"

View File

@@ -1,8 +1,3 @@
 Quellen werden auf Modulabhängigkeiten überprüft...
client_backend.cpp
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\client_backend.cpp(70,24): warning C4244: "Argument": Konvertierung von "_Rep" in "DWORD", möglicher Datenverlust
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\client_backend.cpp(70,24): warning C4244: with
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\client_backend.cpp(70,24): warning C4244: [
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\client_backend.cpp(70,24): warning C4244: _Rep=__int64
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\client_backend.cpp(70,24): warning C4244: ]
update.cpp
client_backend.vcxproj -> C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\client_backend.exe

View File

@@ -12,4 +12,5 @@ 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\scan.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\scan.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\settings.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\settings.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\thread_ctrl.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\thread_ctrl.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\update.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\update.obj
C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\virus_ctrl.cpp;C:\Users\janis\Documents\Projekte_mit_c\ma\ma\src\client_backend\x64\Debug\virus_ctrl.obj