This technology is often used in remote control software, and we already have a remote control software server. We use the client configuration of the remote control software to generate our own server software to update the port and IP address of the server.
See the source code below
# Include <stdio. h>
# Include <windows. h>
Int findstr (char * deststr, char * srcstr, int deststrlen, int srcstrlen );
Void replacestr (char * deststr, char * srcstr, int beginpoint );
Int main ()
{
File * preadfile;
File * poutfile;
Char * pfilebuf;
// Open the source program to be modified
If (preadfile = fopen ("../custom/testpe.exe", "rb") = NULL)
{
Printf ("It's failure to open the readable file \ n ");
Return-1;
}
// Source code to be generated
If (poutfile = fopen ("../custom/testpe1.exe", "WB") = NULL)
{
Printf ("It's failure to open the writable file \ n ");
Return-1;
}
Fseek (preadfile, 0l, seek_end );
Int filelen = ftell (preadfile );
Pfilebuf = (char *) malloc (filelen + 1 );
If (pfilebuf = NULL)
{
Fclose (preadfile );
Return-1;
}
Fseek (preadfile, 0l, seek_set );
Fread (pfilebuf, filelen, sizeof (char), preadfile );
Pfilebuf [filelen] = '\ 0 ';
// The string to be modified
Char * modifystr = "bbbbbbbbbbb ";
// String in the source program
Char * findstr = "aaaaaaaaaaaaa ";
Int beginpoint;
// Find the starting position of the string to be searched in the source array.
Beginpoint = findstr (pfilebuf, findstr, filelen, 0 );
If (beginpoint =-1)
{
Printf ("It's failure to find the string \ n ");
Return-1;
}
// Replace our string
Replacestr (pfilebuf, modifystr, beginpoint );
// Generate the modified source program
Fwrite (pfilebuf, filelen, sizeof (char), poutfile );
Fclose (preadfile );
Fclose (poutfile );
If (pfilebuf! = NULL)
{
Free (pfilebuf );
Pfilebuf = NULL;
}
Return 0;
}
Int findstr (char * deststr, char * srcstr, int deststrlen, int srcstrlen)
{
Int I, j, findstrlen;
If (srcstrlen = 0)
{
Findstrlen = strlen (srcstr );
}
Else
{
Findstrlen = srcstrlen;
}
For (I = 0; I <deststrlen; I ++)
{
For (j = 0; j <findstrlen; j ++)
{
If (deststr [I + J]! = Srcstr [J])
{
Break;
}
}
If (j = findstrlen)
{
Return I;
}
}
Return-1;
}
Void replacestr (char * deststr, char * srcstr, int beginpoint)
{
Int srcstrlen, I;
Srcstrlen = strlen (srcstr );
For (I = 0; I <srcstrlen; I ++)
{
Deststr [beginpoint + I] = srcstr [I];
}
Deststr [beginpoint + srcstrlen] = '\ 0 ';
}
C. Modify the string in the executable file to generate a new executable file.