C. Modify the string in the executable file to generate a new executable file.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.