1 /*
2 anonymizer.cpp - Overwrites string table for a function name.
3
4 Copyright (C) 2017 by Martin Bickel
5 Copyright (C) 2020 by Serge Lamikhov-Center
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25
26 /*
27 To run the example, you may use the following script:
28
29 #!/usr/bin/bash
30
31 make
32 cp anonymizer temp.elf
33 readelf -a temp.elf > before.txt
34 ./anonymizer temp.elf
35 readelf -a temp.elf > after.txt
36 diff 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
50 using namespace ELFIO;
51
overwrite_data(const std::string & filename,Elf64_Off offset,const std::string & str)52 void 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
process_string_table(const section * s,const std::string & filename)65 void 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
main(int argc,char ** argv)78 int 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