{"id":27154976,"url":"https://github.com/maximilianfeldthusen/tamperprotect","last_synced_at":"2026-04-01T17:02:25.277Z","repository":{"id":282509126,"uuid":"948829454","full_name":"maximilianfeldthusen/TamperProtect","owner":"maximilianfeldthusen","description":"This C++ code is a simple program designed to compute the SHA256 hash of its own executable file and compare it against a known good hash.","archived":false,"fork":false,"pushed_at":"2025-04-24T16:40:17.000Z","size":13,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"TFD","last_synced_at":"2026-03-27T23:42:23.542Z","etag":null,"topics":["cpp","protection","tampering"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maximilianfeldthusen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-03-15T03:44:13.000Z","updated_at":"2025-04-24T16:40:21.000Z","dependencies_parsed_at":"2025-06-14T04:08:04.127Z","dependency_job_id":null,"html_url":"https://github.com/maximilianfeldthusen/TamperProtect","commit_stats":null,"previous_names":["maximilianfeldthusen/tamperprotect"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/maximilianfeldthusen/TamperProtect","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2FTamperProtect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2FTamperProtect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2FTamperProtect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2FTamperProtect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maximilianfeldthusen","download_url":"https://codeload.github.com/maximilianfeldthusen/TamperProtect/tar.gz/refs/heads/TFD","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2FTamperProtect/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31290538,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-01T13:12:26.723Z","status":"ssl_error","status_checked_at":"2026-04-01T13:12:25.102Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","protection","tampering"],"created_at":"2025-04-08T18:56:41.172Z","updated_at":"2026-04-01T17:02:25.256Z","avatar_url":"https://github.com/maximilianfeldthusen.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Documentation\n\n### TamperProtect\n\nThis C++ code is a simple program designed to compute the SHA256 hash of its own executable file and compare it against a known good hash. This is often used as a tamper detection mechanism to ensure that the executable has not been altered in any way. Below, I'll break down the code step by step.\n\n### Code Breakdown\n\n1. **Includes and Namespace**:\n    ```cpp\n    #include \u003ciostream\u003e\n    #include \u003cfstream\u003e\n    #include \u003csstream\u003e\n    #include \u003ciomanip\u003e\n    #include \u003cvector\u003e\n    #include \u003copenssl/sha.h\u003e\n    #include \u003cstdexcept\u003e\n    #include \u003cstring\u003e\n    #include \u003cfilesystem\u003e\n\n    namespace fs = std::filesystem;\n    ```\n   - The code includes necessary headers for input/output operations, file handling, string manipulation, hashing with OpenSSL, exception handling, and filesystem operations. \n   - The `filesystem` library is aliased as `fs` for convenience.\n\n2. **SHA256 Calculation Function**:\n    ```cpp\n    std::string calculate_sha256(const std::string \u0026file_path) {\n        unsigned char hash[SHA256_DIGEST_LENGTH];\n        SHA256_CTX sha256;\n        SHA256_Init(\u0026sha256);\n\n        std::ifstream file(file_path, std::ios::binary);\n        if (!file.is_open()) {\n            throw std::runtime_error(\"Failed to open file: \" + file_path);\n        }\n\n        char buffer[4096];\n        while (file.read(buffer, sizeof(buffer))) {\n            SHA256_Update(\u0026sha256, buffer, file.gcount());\n        }\n        // Handle the last chunk\n        if (file.gcount() \u003e 0) {\n            SHA256_Update(\u0026sha256, buffer, file.gcount());\n        }\n        SHA256_Final(hash, \u0026sha256);\n\n        std::ostringstream oss;\n        for (const auto \u0026byte : hash) {\n            oss \u003c\u003c std::setw(2) \u003c\u003c std::setfill('0') \u003c\u003c std::hex \u003c\u003c static_cast\u003cunsigned int\u003e(byte);\n        }\n        return oss.str();\n    }\n    ```\n   - This function reads a file specified by `file_path` in binary mode and computes its SHA256 hash.\n   - It initializes the SHA256 context, reads the file in chunks (to handle large files), and updates the hash accordingly.\n   - After reading the file, it finalizes the hash and converts the raw hash bytes into a hexadecimal string.\n\n3. **Main Function**:\n    ```cpp\n    int main(int argc, char* argv[]) {\n        const std::string known_good_hash = \"your_known_good_hash_here\"; // Replace with your known good hash\n\n        std::string executable_path = fs::absolute(argv[0]).string(); // Get the absolute path of the current executable\n\n        try {\n            std::string current_hash = calculate_sha256(executable_path);\n\n            if (current_hash == known_good_hash) {\n                std::cout \u003c\u003c \"No tampering detected.\" \u003c\u003c std::endl;\n            } else {\n                std::cout \u003c\u003c \"Tampering detected!\" \u003c\u003c std::endl;\n            }\n        } catch (const std::exception \u0026e) {\n            std::cerr \u003c\u003c \"Error: \" \u003c\u003c e.what() \u003c\u003c std::endl;\n        }\n\n        return 0;\n    }\n    ```\n   - The main function sets a placeholder for the known good hash, which should be replaced with the actual SHA256 hash of the untouched executable.\n   - It retrieves the absolute path of the currently running executable using `argv[0]`, which contains the path of the program.\n   - It calls the `calculate_sha256` function to compute the current hash of the executable.\n   - Finally, it compares the computed hash with the known good hash and outputs whether tampering has been detected or not.\n   - Any exceptions thrown during the hash calculation (such as file opening errors) are caught and printed.\n\n### Key Concepts\n- **SHA256 Hashing**: A cryptographic hash function that produces a fixed-size (256-bit) hash value from variable-sized input data.\n- **File I/O**: Reading the executable file in binary mode to ensure accurate hashing.\n- **Exception Handling**: Using try-catch blocks to handle potential errors in file operations.\n- **Filesystem Operations**: Using the filesystem library to work with file paths, making the program more robust and portable.\n\n### Usage\nTo use this program, you would compile it, run it, and provide the known good hash of the executable. The program then checks if its own binary has been altered by comparing its computed hash against the known good hash value. If they match, the executable is considered unaltered; if not, it signals potential tampering.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximilianfeldthusen%2Ftamperprotect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaximilianfeldthusen%2Ftamperprotect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximilianfeldthusen%2Ftamperprotect/lists"}