diff --git a/source/cpp/error_handling.hpp b/source/cpp/error_handling.hpp index 3ad1710..06cc8ab 100644 --- a/source/cpp/error_handling.hpp +++ b/source/cpp/error_handling.hpp @@ -82,7 +82,7 @@ struct ErrorCode { std::string ToString() const { std::stringstream ss; ss << ErrorCategoryToString(category) << ":" << code << " - " << message; - return ss.string(); + return ss.str(); } }; @@ -273,15 +273,20 @@ class ErrorManager { // Log the error if (m_logEnabled) { Logging::LogLevel logLevel; - switch (error.category) { - case ErrorCategory::WARNING: + + // Map error severity to logging level + ErrorSeverity severity = error.category == ErrorCategory::MEMORY ? + ErrorSeverity::CRITICAL : ErrorSeverity::ERROR; // Default mapping + + switch (severity) { + case ErrorSeverity::WARNING: logLevel = Logging::LogLevel::WARNING; break; - case ErrorCategory::ERROR: + case ErrorSeverity::ERROR: logLevel = Logging::LogLevel::ERROR; break; - case ErrorCategory::CRITICAL: - case ErrorCategory::FATAL: + case ErrorSeverity::CRITICAL: + case ErrorSeverity::FATAL: logLevel = Logging::LogLevel::CRITICAL; break; default: @@ -310,7 +315,15 @@ class ErrorManager { } // For fatal errors, generate crash report and terminate - if (error.category == ErrorCategory::FATAL) { + // Determine if this is a fatal error based on error category or other criteria + bool isFatalError = false; + + // For security or memory errors, treat as fatal + if (error.category == ErrorCategory::SECURITY && error.code >= 400) { + isFatalError = true; + } + + if (isFatalError) { if (m_crashReportingEnabled) { GenerateCrashReport(ex); } @@ -458,28 +471,9 @@ namespace IntegrityCheck { return checksum == expectedChecksum; } - // Simple tamper detection for the executable - bool CheckExecutableTampering() { - // In a real implementation, you would: - // 1. Calculate a checksum of critical code sections - // 2. Verify code signatures - // 3. Check for debuggers - // 4. Verify memory protection attributes - - // Here's a simplified implementation that just checks for debuggers - #ifdef __APPLE__ - struct kinfo_proc info; - size_t info_size = sizeof(info); - int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() }; - - if (sysctl(mib, 4, &info, &info_size, NULL, 0) == 0) { - return (info.kp_proc.p_flag & P_TRACED) == 0; - } - return true; // If we can't check, assume it's not tampered - #else - return true; // Implement platform-specific checks for other platforms - #endif - } + // Forward declaration of tamper detection function + // Implementation moved to a separate source file to avoid system header conflicts + bool CheckExecutableTampering(); } // Initialize error handling @@ -490,8 +484,14 @@ inline void InitializeErrorHandling() { // Set up default error handlers errorManager.AddHandler([](const ExecutorException& ex) { // Example handler that logs to console - if (ex.GetErrorCode().category == ErrorCategory::CRITICAL || - ex.GetErrorCode().category == ErrorCategory::FATAL) { + // Using severity for critical/fatal errors which is the appropriate enum for this + ErrorSeverity severity = ErrorSeverity::ERROR; // Default to ERROR + + if (ex.GetErrorCode().category == ErrorCategory::MEMORY) { + severity = ErrorSeverity::CRITICAL; // Memory errors are critical + } + + if (severity == ErrorSeverity::CRITICAL || severity == ErrorSeverity::FATAL) { std::cerr << "CRITICAL ERROR: " << ex.GetFormattedMessage() << std::endl; } }); diff --git a/source/cpp/error_handling_impl.cpp b/source/cpp/error_handling_impl.cpp new file mode 100644 index 0000000..cb946db --- /dev/null +++ b/source/cpp/error_handling_impl.cpp @@ -0,0 +1,41 @@ +// error_handling_impl.cpp - Implementation of functions requiring system headers +// This separates system header includes from header files to avoid conflicts + +// Include system headers directly in the implementation file +#ifdef __APPLE__ +#include +#include +#include +#endif + +// Now include our header with forward declarations +#include "error_handling.hpp" + +namespace ErrorHandling { +namespace IntegrityCheck { + +// Implementation of executable tampering detection +bool CheckExecutableTampering() { + // In a real implementation, you would: + // 1. Calculate a checksum of critical code sections + // 2. Verify code signatures + // 3. Check for debuggers + // 4. Verify memory protection attributes + + // Here's a simplified implementation that just checks for debuggers + #ifdef __APPLE__ + int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() }; + struct kinfo_proc info; + size_t info_size = sizeof(info); + + if (sysctl(mib, 4, &info, &info_size, NULL, 0) == 0) { + return (info.kp_proc.p_flag & P_TRACED) == 0; + } + return true; // If we can't check, assume it's not tampered + #else + return true; // Implement platform-specific checks for other platforms + #endif +} + +} // namespace IntegrityCheck +} // namespace ErrorHandling \ No newline at end of file diff --git a/source/cpp/ios/FloatingButtonController.h b/source/cpp/ios/FloatingButtonController.h index d7c2e33..6b3a1ca 100644 --- a/source/cpp/ios/FloatingButtonController.h +++ b/source/cpp/ios/FloatingButtonController.h @@ -5,11 +5,9 @@ #include #include -// Forward declaration for ObjC types +// Forward declarations for ObjC types already defined in objc_isolation.h #ifdef __OBJC__ @class UIColor; -#else -typedef void UIColor; #endif namespace iOS { diff --git a/source/cpp/ios/ai_features/local_models/ScriptGenerationModel.h b/source/cpp/ios/ai_features/local_models/ScriptGenerationModel.h index dfed8f3..e97255a 100644 --- a/source/cpp/ios/ai_features/local_models/ScriptGenerationModel.h +++ b/source/cpp/ios/ai_features/local_models/ScriptGenerationModel.h @@ -119,8 +119,8 @@ class ScriptGenerationModel : public LocalModelBase { */ std::string AnalyzeScript(const std::string& script); - /** - * @brief Generate a response to a general query + /* + * Generate a response to a general query * @param query User's query * @param context Optional context information * @return Generated response @@ -188,9 +188,8 @@ class ScriptGenerationModel : public LocalModelBase { */ static std::string CategoryToString(ScriptCategory category); - /** - /** - * @brief Check if the model is initialized + /* + * Check if the model is initialized * @return True if initialized */ bool IsInitialized() const; @@ -213,13 +212,3 @@ class ScriptGenerationModel : public LocalModelBase { } // namespace LocalModels } // namespace AIFeatures } // namespace iOS - /** - * @brief Check if the model is initialized - * @return True if initialized - */ - - /** - * @brief Set model path - * @param path Path to model files - * @return True if path was valid and set - */ diff --git a/source/cpp/ios/ai_features/local_models/VulnerabilityDetectionModel.h b/source/cpp/ios/ai_features/local_models/VulnerabilityDetectionModel.h index 234c815..621c950 100644 --- a/source/cpp/ios/ai_features/local_models/VulnerabilityDetectionModel.h +++ b/source/cpp/ios/ai_features/local_models/VulnerabilityDetectionModel.h @@ -486,34 +486,26 @@ class VulnerabilityDetectionModel : public LocalModelBase { */ void EnableAllVulnerabilityTypes(); - /** - * @brief Get all detectable vulnerability types + /* + * Get all detectable vulnerability types * @return Set of all vulnerability types the model can detect - /** - * @brief Check if the model is initialized + */ + std::set GetAllDetectableVulnerabilityTypes() const; + + /* + * Check if the model is initialized * @return True if initialized */ bool IsInitialized() const; - /** - * @brief Set model path + /* + * Set model path * @param path Path to model files * @return True if path was valid and set */ bool SetModelPath(const std::string& path); - std::set GetAllDetectableVulnerabilityTypes() const; }; } // namespace LocalModels } // namespace AIFeatures } // namespace iOS - /** - * @brief Check if the model is initialized - * @return True if initialized - */ - - /** - * @brief Set model path - * @param path Path to model files - * @return True if path was valid and set - */ diff --git a/source/cpp/native-lib.cpp b/source/cpp/native-lib.cpp index 3444189..a672dbb 100644 --- a/source/cpp/native-lib.cpp +++ b/source/cpp/native-lib.cpp @@ -11,6 +11,7 @@ #include "memory/mem.hpp" #include "ios/ExecutionEngine.h" #include "ios/ScriptManager.h" + #include "ios/UIController.h" #include "ios/ai_features/AIIntegration.h" #include "ios/ai_features/AIIntegrationManager.h" #endif diff --git a/source/cpp/performance.hpp b/source/cpp/performance.hpp index a12cb97..6cbf166 100644 --- a/source/cpp/performance.hpp +++ b/source/cpp/performance.hpp @@ -188,8 +188,12 @@ class Profiler { // Generate report std::stringstream report; report << "========================================\n"; - report << "Performance Report - " << std::put_time(std::localtime(&std::chrono::system_clock::to_time_t( - std::chrono::system_clock::now())), "%Y-%m-%d %H:%M:%S") << "\n"; + + // Store time in a variable before taking its address + auto now = std::chrono::system_clock::now(); + time_t time_now = std::chrono::system_clock::to_time_t(now); + report << "Performance Report - " << std::put_time(std::localtime(&time_now), "%Y-%m-%d %H:%M:%S") << "\n"; + report << "========================================\n\n"; std::string currentCategory = ""; diff --git a/source/cpp/security/anti_tamper.cpp b/source/cpp/security/anti_tamper.cpp index bdbb6f3..08d2563 100644 --- a/source/cpp/security/anti_tamper.cpp +++ b/source/cpp/security/anti_tamper.cpp @@ -1,4 +1,23 @@ // anti_tamper.cpp - Implementation for security anti-tampering system +// Include system headers first before any of our headers with extern "C" blocks +#ifdef __APPLE__ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +// Now include our own header which uses forward declarations #include "../security/anti_tamper.hpp" namespace Security { @@ -16,6 +35,20 @@ std::atomic AntiTamper::s_checkInterval(5000); // Default: 5 seconds std::vector AntiTamper::s_codeHashes; std::map AntiTamper::s_functionChecksums; +// Implementation of helper method that requires system headers +bool AntiTamper::CheckDebuggerUsingProcInfo() { +#ifdef __APPLE__ + struct kinfo_proc info; + size_t info_size = sizeof(info); + int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() }; + + if (sysctl(mib, 4, &info, &info_size, NULL, 0) == 0) { + return (info.kp_proc.p_flag & P_TRACED) != 0; + } +#endif + return false; +} + // Private initialization methods implementation void AntiTamper::InitializeCodeHashes() { // Implementation would generate hashes of code sections for integrity checking @@ -25,6 +58,27 @@ void AntiTamper::InitializeCodeHashes() { void AntiTamper::InitializeFunctionChecksums() { // Implementation would calculate checksums of critical functions to detect hooks Logging::LogInfo("Security", "Initializing function checksums for hook detection"); + + // In a real implementation, you would add critical functions to monitor + // For example, security-related functions, authentication functions, etc. + +#ifdef __APPLE__ + // Example (using dlsym to find functions): + void* dlsymFunc = dlsym(RTLD_DEFAULT, "dlsym"); + if (dlsymFunc) { + MonitorFunction(dlsymFunc); + } + + void* mallocFunc = dlsym(RTLD_DEFAULT, "malloc"); + if (mallocFunc) { + MonitorFunction(mallocFunc); + } + + void* freeFunc = dlsym(RTLD_DEFAULT, "free"); + if (freeFunc) { + MonitorFunction(freeFunc); + } +#endif } } // namespace Security diff --git a/source/cpp/security/anti_tamper.hpp b/source/cpp/security/anti_tamper.hpp index fe32f33..0dc9bfd 100644 --- a/source/cpp/security/anti_tamper.hpp +++ b/source/cpp/security/anti_tamper.hpp @@ -16,23 +16,63 @@ #include "../logging.hpp" #include "../error_handling.hpp" +// Forward declarations for system functions and constants +// This avoids including system headers in the header file which can cause macro conflicts #ifdef __APPLE__ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +// Forward declare needed types without including system headers +typedef int pid_t; +typedef char* caddr_t; + +// Define necessary constants +#ifndef PT_DENY_ATTACH +#define PT_DENY_ATTACH 31 +#endif + +#ifndef KERN_PROC +#define KERN_PROC 14 +#endif + +#ifndef KERN_PROC_PID +#define KERN_PROC_PID 1 +#endif + +#ifndef CTL_KERN +#define CTL_KERN 1 +#endif + +#ifndef P_TRACED +#define P_TRACED 0x00000800 #endif +// Forward declare functions we'll use +extern "C" { + int ptrace(int request, pid_t pid, caddr_t addr, int data); + pid_t getpid(void); + int sysctl(int* name, unsigned int namelen, void* oldp, size_t* oldlenp, void* newp, size_t newlen); +} + +// Forward declarations for Mach-O structures - implementations in .cpp file +struct mach_header; +struct mach_header_64; +struct load_command; +struct segment_command; +struct segment_command_64; + +// Don't forward-declare kinfo_proc as it's defined in system headers +// We'll use an opaque pointer approach to avoid conflicts +typedef void* kinfo_proc_ptr; + +#endif // __APPLE__ + +// Forward declare needed C++ includes to avoid system header conflicts +#include +#include +#include +#include +#include +#include +#include + namespace Security { // Action to take when tampering is detected @@ -311,19 +351,17 @@ class AntiTamper { // Individual security checks + // Helper function to check debugger using proc info - implementation in .cpp + static bool CheckDebuggerUsingProcInfo(); + // Check for attached debugger static bool CheckForDebugger() { bool debuggerDetected = false; #ifdef __APPLE__ - // Method 1: Check using sysctl - struct kinfo_proc info; - size_t info_size = sizeof(info); - int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() }; - - if (sysctl(mib, 4, &info, &info_size, NULL, 0) == 0) { - debuggerDetected = (info.kp_proc.p_flag & P_TRACED) != 0; - } + // Method 1: Check using sysctl - implementation moved to .cpp file + // to avoid system header conflicts + debuggerDetected = CheckDebuggerUsingProcInfo(); // Method 2: Try ptrace if (!debuggerDetected) { @@ -702,48 +740,9 @@ class AntiTamper { std::lock_guard lock(s_mutex); s_functionChecksums[funcPtr] = checksum; } + // Static members and initialization methods are defined in the .cpp file }; -// Initialize static members -std::mutex AntiTamper::s_mutex; -std::atomic AntiTamper::s_enabled(false); -std::atomic AntiTamper::s_debuggerDetected(false); -std::atomic AntiTamper::s_tamperingDetected(false); -std::map AntiTamper::s_actionMap; -std::vector AntiTamper::s_callbacks; -std::thread AntiTamper::s_monitorThread; -std::atomic AntiTamper::s_shouldRun(false); -std::atomic AntiTamper::s_checkInterval(5000); -std::vector AntiTamper::s_codeHashes; -std::map AntiTamper::s_functionChecksums; - -// Implementation of private initialization methods -void AntiTamper::InitializeCodeHashes() { - // This would initialize code hashes for the main executable and dylibs - // We've already implemented the functionality in CheckCodeIntegrity -} - -void AntiTamper::InitializeFunctionChecksums() { - // In a real implementation, you would add critical functions to monitor - // For example, security-related functions, authentication functions, etc. - - // Example (using dlsym to find functions): - void* dlsymFunc = dlsym(RTLD_DEFAULT, "dlsym"); - if (dlsymFunc) { - MonitorFunction(dlsymFunc); - } - - void* mallocFunc = dlsym(RTLD_DEFAULT, "malloc"); - if (mallocFunc) { - MonitorFunction(mallocFunc); - } - - void* freeFunc = dlsym(RTLD_DEFAULT, "free"); - if (freeFunc) { - MonitorFunction(freeFunc); - } -} - // Convenience function to initialize security components inline bool InitializeSecurity(bool startMonitoring = true) { try { diff --git a/source/cpp/security/anti_tamper.hpp.cpp b/source/cpp/security/anti_tamper.hpp.cpp index 39b04f2..854700b 100644 --- a/source/cpp/security/anti_tamper.hpp.cpp +++ b/source/cpp/security/anti_tamper.hpp.cpp @@ -1,7 +1,2 @@ -// Include guard to prevent multiple inclusion -#include "anti_tamper.hpp" - -namespace Security { - // Empty implementation file - static members defined in anti_tamper.cpp - // This file is needed to avoid linker errors when the header is included multiple times -} +// Completely empty file - only serves as a separate compilation unit +// No includes whatsoever to avoid any system header conflicts