1/* 2anonymizer.cpp - Overwrites string table for a function name. 3 4Copyright (C) 2017 by Martin Bickel 5Copyright (C) 2020 by Serge Lamikhov-Center 6 7Permission is hereby granted, free of charge, to any person obtaining a copy 8of this software and associated documentation files (the "Software"), to deal 9in the Software without restriction, including without limitation the rights 10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11copies of the Software, and to permit persons to whom the Software is 12furnished to do so, subject to the following conditions: 13 14The above copyright notice and this permission notice shall be included in 15all copies or substantial portions of the Software. 16 17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23THE SOFTWARE. 24*/ 25 26/* 27To run the example, you may use the following script: 28 29#!/usr/bin/bash 30 31make 32cp anonymizer temp.elf 33readelf -a temp.elf > before.txt 34./anonymizer temp.elf 35readelf -a temp.elf > after.txt 36diff before.txt after.txt 37 38*/ 39 40#ifdef _MSC_VER 41#define _SCL_SECURE_NO_WARNINGS 42#define ELFIO_NO_INTTYPES 43#endif 44 45#include <string> 46#include <iostream> 47#include <fstream> 48#include <elfio/elfio.hpp> 49 50using namespace ELFIO; 51 52void overwrite_data( const std::string& filename, 53 Elf64_Off offset, 54 const std::string& str ) 55{ 56 std::ofstream file( filename, 57 std::ios::in | std::ios::out | std::ios::binary ); 58 if ( !file ) 59 throw "Error opening file" + filename; 60 std::string data( str.length(), '-' ); 61 file.seekp( (std::streampos)offset ); 62 file.write( data.c_str(), data.length() + 1 ); 63} 64 65void process_string_table( const section* s, const std::string& filename ) 66{ 67 std::cout << "Info: processing string table section" << std::endl; 68 size_t index = 1; 69 while ( index < s->get_size() ) { 70 auto str = std::string( s->get_data() + index ); 71 // For the example purpose, we rename main function name only 72 if ( str == "main" ) 73 overwrite_data( filename, s->get_offset() + index, str ); 74 index += str.length() + 1; 75 } 76} 77 78int main( int argc, char** argv ) 79{ 80 if ( argc != 2 ) { 81 std::cout << "Usage: anonymizer <file_name>\n"; 82 return 1; 83 } 84 85 std::string filename = argv[1]; 86 87 elfio reader; 88 89 if ( !reader.load( filename ) ) { 90 std::cerr << "File " << filename 91 << " is not found or it is not an ELF file\n"; 92 return 1; 93 } 94 95 for ( const auto& section : reader.sections ) { 96 if ( section->get_type() == SHT_STRTAB && 97 std::string( section->get_name() ) == std::string( ".strtab" ) ) { 98 process_string_table( section.get(), filename ); 99 } 100 } 101 return 0; 102} 103