165042b18Sopenharmony_ci/* 265042b18Sopenharmony_cianonymizer.cpp - Overwrites string table for a function name. 365042b18Sopenharmony_ci 465042b18Sopenharmony_ciCopyright (C) 2017 by Martin Bickel 565042b18Sopenharmony_ciCopyright (C) 2020 by Serge Lamikhov-Center 665042b18Sopenharmony_ci 765042b18Sopenharmony_ciPermission is hereby granted, free of charge, to any person obtaining a copy 865042b18Sopenharmony_ciof this software and associated documentation files (the "Software"), to deal 965042b18Sopenharmony_ciin the Software without restriction, including without limitation the rights 1065042b18Sopenharmony_cito use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1165042b18Sopenharmony_cicopies of the Software, and to permit persons to whom the Software is 1265042b18Sopenharmony_cifurnished to do so, subject to the following conditions: 1365042b18Sopenharmony_ci 1465042b18Sopenharmony_ciThe above copyright notice and this permission notice shall be included in 1565042b18Sopenharmony_ciall copies or substantial portions of the Software. 1665042b18Sopenharmony_ci 1765042b18Sopenharmony_ciTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1865042b18Sopenharmony_ciIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1965042b18Sopenharmony_ciFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2065042b18Sopenharmony_ciAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2165042b18Sopenharmony_ciLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2265042b18Sopenharmony_ciOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2365042b18Sopenharmony_ciTHE SOFTWARE. 2465042b18Sopenharmony_ci*/ 2565042b18Sopenharmony_ci 2665042b18Sopenharmony_ci/* 2765042b18Sopenharmony_ciTo run the example, you may use the following script: 2865042b18Sopenharmony_ci 2965042b18Sopenharmony_ci#!/usr/bin/bash 3065042b18Sopenharmony_ci 3165042b18Sopenharmony_cimake 3265042b18Sopenharmony_cicp anonymizer temp.elf 3365042b18Sopenharmony_cireadelf -a temp.elf > before.txt 3465042b18Sopenharmony_ci./anonymizer temp.elf 3565042b18Sopenharmony_cireadelf -a temp.elf > after.txt 3665042b18Sopenharmony_cidiff before.txt after.txt 3765042b18Sopenharmony_ci 3865042b18Sopenharmony_ci*/ 3965042b18Sopenharmony_ci 4065042b18Sopenharmony_ci#ifdef _MSC_VER 4165042b18Sopenharmony_ci#define _SCL_SECURE_NO_WARNINGS 4265042b18Sopenharmony_ci#define ELFIO_NO_INTTYPES 4365042b18Sopenharmony_ci#endif 4465042b18Sopenharmony_ci 4565042b18Sopenharmony_ci#include <string> 4665042b18Sopenharmony_ci#include <iostream> 4765042b18Sopenharmony_ci#include <fstream> 4865042b18Sopenharmony_ci#include <elfio/elfio.hpp> 4965042b18Sopenharmony_ci 5065042b18Sopenharmony_ciusing namespace ELFIO; 5165042b18Sopenharmony_ci 5265042b18Sopenharmony_civoid overwrite_data( const std::string& filename, 5365042b18Sopenharmony_ci Elf64_Off offset, 5465042b18Sopenharmony_ci const std::string& str ) 5565042b18Sopenharmony_ci{ 5665042b18Sopenharmony_ci std::ofstream file( filename, 5765042b18Sopenharmony_ci std::ios::in | std::ios::out | std::ios::binary ); 5865042b18Sopenharmony_ci if ( !file ) 5965042b18Sopenharmony_ci throw "Error opening file" + filename; 6065042b18Sopenharmony_ci std::string data( str.length(), '-' ); 6165042b18Sopenharmony_ci file.seekp( (std::streampos)offset ); 6265042b18Sopenharmony_ci file.write( data.c_str(), data.length() + 1 ); 6365042b18Sopenharmony_ci} 6465042b18Sopenharmony_ci 6565042b18Sopenharmony_civoid process_string_table( const section* s, const std::string& filename ) 6665042b18Sopenharmony_ci{ 6765042b18Sopenharmony_ci std::cout << "Info: processing string table section" << std::endl; 6865042b18Sopenharmony_ci size_t index = 1; 6965042b18Sopenharmony_ci while ( index < s->get_size() ) { 7065042b18Sopenharmony_ci auto str = std::string( s->get_data() + index ); 7165042b18Sopenharmony_ci // For the example purpose, we rename main function name only 7265042b18Sopenharmony_ci if ( str == "main" ) 7365042b18Sopenharmony_ci overwrite_data( filename, s->get_offset() + index, str ); 7465042b18Sopenharmony_ci index += str.length() + 1; 7565042b18Sopenharmony_ci } 7665042b18Sopenharmony_ci} 7765042b18Sopenharmony_ci 7865042b18Sopenharmony_ciint main( int argc, char** argv ) 7965042b18Sopenharmony_ci{ 8065042b18Sopenharmony_ci if ( argc != 2 ) { 8165042b18Sopenharmony_ci std::cout << "Usage: anonymizer <file_name>\n"; 8265042b18Sopenharmony_ci return 1; 8365042b18Sopenharmony_ci } 8465042b18Sopenharmony_ci 8565042b18Sopenharmony_ci std::string filename = argv[1]; 8665042b18Sopenharmony_ci 8765042b18Sopenharmony_ci elfio reader; 8865042b18Sopenharmony_ci 8965042b18Sopenharmony_ci if ( !reader.load( filename ) ) { 9065042b18Sopenharmony_ci std::cerr << "File " << filename 9165042b18Sopenharmony_ci << " is not found or it is not an ELF file\n"; 9265042b18Sopenharmony_ci return 1; 9365042b18Sopenharmony_ci } 9465042b18Sopenharmony_ci 9565042b18Sopenharmony_ci for ( const auto& section : reader.sections ) { 9665042b18Sopenharmony_ci if ( section->get_type() == SHT_STRTAB && 9765042b18Sopenharmony_ci std::string( section->get_name() ) == std::string( ".strtab" ) ) { 9865042b18Sopenharmony_ci process_string_table( section.get(), filename ); 9965042b18Sopenharmony_ci } 10065042b18Sopenharmony_ci } 10165042b18Sopenharmony_ci return 0; 10265042b18Sopenharmony_ci} 103