Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a constant variable without rebuilding C++

I've developed a c++ project with visual studio 2015. The output of my project is a single executable that must have a unique ID for every client and this ID must be accessible inside the code. A simple approach is to just define a constant variable inside the code and change its value for every client and build it many times but I have a Linux server and I'm not sure I can build it simply because I've used many Winapi libraries. I was thinking that maybe there is another way to change or add some constant value to the output like manipulating the executable. For example:

#include <string>
#include <iostream>
#include <Windows.h>


const std::string ID = "some unique ID";

int main() {
    std::cout << "Your ID: " << ID << std::endl;
    getchar();
    return(0);
}
like image 202
Masoud Rahimi Avatar asked May 23 '18 03:05

Masoud Rahimi


2 Answers

It seems that there are only two approaches. One is just building the project inside a Linux environment which is a better method but must be used some tools like Mono XBuild link here. Another option which may be simpler is just open the binary file and manipulate the specific string. As @aloMalbarez comment Here is a simple script based on this. Suppose this example: (I used 50 ms as a fixed length for my ID)

#include <string>
#include <iostream>
#include <Windows.h>

#define ID "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"


using namespace std;

int main() {
    cout << "Your ID: " << ID << "\nlen:" << strlen(ID) <<  endl;
    getchar();
    return(0);
}

After generating the executable use the following script to create output. I'm not a Linux guy so you can help me improve this. ./build.sh input.exe output.exe "myfixedID"

#!/bin/bash
# build.sh input_file output_file <ID>


input_file=$1
output_file=$2
ID=$3


if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
  echo "wrong parameters"
  echo "build.sh input_file output_file <ID>"
  exit 1
fi

# use fixed string (ID) in the source file
# this creates 50 of "m"s
search_value=$(printf 'm%.0s' {1..50})

extension=".back"
temp_file="$input_file$extension"
tmpstring_file="./tmp"
null_termin='\0'


echo "copying the original file..."
yes | cp -rf $input_file $temp_file

address=$(strings -t d $temp_file | grep $search_value | grep -o '[0-9]*')

echo "Address:"
echo $address
if ! [[ $address =~ ^[0-9]+$ ]]; then
  echo "cannot find valid ID in executable"
  echo "removing temps"
  rm $temp_file
  exit 1
fi


# make the tempstring file
printf "$ID$null_termin" > $tmpstring_file

dd if=$tmpstring_file of=$temp_file obs=1 seek=$address conv=notrunc

echo "make new file"
yes | cp -rf $temp_file $output_file

echo "removing temps"

rm $temp_file $tmpstring_file

echo "Done!"
like image 137
Masoud Rahimi Avatar answered Sep 30 '22 06:09

Masoud Rahimi


In init function of your program. Generate a unique id based of SHA-1 hash of current time, IP address, username (same more). you can do whatever you want to do in that program afterward (i.e save in database). Will that work ?

like image 32
code707 Avatar answered Sep 30 '22 07:09

code707