Little Patch Program in C++

S

Son of Sam

Ok what does the program do:
opens a file binary, jumps to an specific offset (0x3529BC9) reads out
the string which is 7 bytes (7 chars) long (which starts at the
offset), then generates a random string 7 char long sting (string
s[7]).
Thats what I was able to do, but now I want to replace this offset by
this random string and write the patched file to back to the file. i
didnt know how do do it with vectors, also this file write operations
were not working the way i wanted it, heres my code:

Ok what does the program do:
opens a file, jumps to an specific offset reads out the string which is
7 bytes or 7 chars long, then generates a random string.
thats what i was able to do but now i want to replace this offset by
this random string and write the patched file to back to the file. i
didnt know how do do it with vectors, also this file write opertions
were not working the way i wanted it, heres my code:

#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>
#include <stdio.h>
#include <time.h>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, _TCHAR* argv[])
{
char _string[7];
char _filename[128];
int _offset = 0x3529BC;

if(argc == 1)
{
system("cls");
cout << "\nUsage: patch.exe
[path/filename]\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "Enter path and filename to your
FlashpointResistance.exe\n\n";
cout << "(eg. C:/Desktop/ofp.exe): ";
cin >> _filename;
}
else
{
strcpy(_filename, argv[1]);
}

ifstream ofp;
ofp.open(_filename, ios_base::in | ios::binary);

if(!ofp.is_open())
{
system("cls");
cout << "\nResults:\n";
cout << " File \"" << _filename << "\" not found.\n\n";
}
else
{
system("cls");
//##### Create the "random" 7 bytes long string ######
ostringstream os;
os << hex << time(0);
string s = os.str();
s.resize(7);

//###### Read the string[7] at offset 0x3529BC ######
ofp.seekg(_offset, ios::beg);
ofp.read(_string, 7);
ofp.close();

ostringstream os2;
os2 << _string;
string s2 = os2.str();
s2.resize(7);

//##### Prints the results out ##########
cout << "\nFilename: \t\t" << _filename << endl;
cout << "Random Filestring: \t" << s << endl;
cout << "Existing String: \t" << s2 << endl;
//#####Here starts the part where the string is to be replaced and
the file to be saved (overwritten) #####
ifstream ofp;

// ###### here i tryed it somehow with vectors but i have no idead
how #######
std::vector<char> v(0x3529BC);
ofp.read(&v[0],0x3529BC);


}

return 0;
}
 
L

Larry I Smith

Son said:
Ok what does the program do:
opens a file binary, jumps to an specific offset (0x3529BC9) reads out
the string which is 7 bytes (7 chars) long (which starts at the
offset), then generates a random string 7 char long sting (string
s[7]).
Thats what I was able to do, but now I want to replace this offset by
this random string and write the patched file to back to the file. i
didnt know how do do it with vectors, also this file write operations
were not working the way i wanted it, heres my code:

Ok what does the program do:
opens a file, jumps to an specific offset reads out the string which is
7 bytes or 7 chars long, then generates a random string.
thats what i was able to do but now i want to replace this offset by
this random string and write the patched file to back to the file. i
didnt know how do do it with vectors, also this file write opertions
were not working the way i wanted it, heres my code:

#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>
#include <stdio.h>
#include <time.h>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, _TCHAR* argv[])
{
char _string[7];
char _filename[128];
int _offset = 0x3529BC;

if(argc == 1)
{
system("cls");
cout << "\nUsage: patch.exe
[path/filename]\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "Enter path and filename to your
FlashpointResistance.exe\n\n";
cout << "(eg. C:/Desktop/ofp.exe): ";
cin >> _filename;
}
else
{
strcpy(_filename, argv[1]);
}

//ifstream ofp;
//ofp.open(_filename, ios_base::in | ios::binary);

fstream ofp;
ofp.open(_filename, ios_base::in |ios_base::eek:ut | ios::binary);

if(!ofp.is_open())
{
system("cls");
cout << "\nResults:\n";
cout << " File \"" << _filename << "\" not found.\n\n";
}
else
{
system("cls");
//##### Create the "random" 7 bytes long string ######
ostringstream os;
os << hex << time(0);
string s = os.str();
s.resize(7);

//###### Read the string[7] at offset 0x3529BC ######

//ofp.seekg(_offset, ios::beg);
//ofp.read(_string, 7);
//ofp.close();

ofp.pubseekoff(_offset, ios_base::beg,
ios_base::in | ios_base::eek:ut);
ofp.read(_string, 7);

ostringstream os2;
os2 << _string;
string s2 = os2.str();
s2.resize(7);

//##### Prints the results out ##########
cout << "\nFilename: \t\t" << _filename << endl;
cout << "Random Filestring: \t" << s << endl;
cout << "Existing String: \t" << s2 << endl;
//#####Here starts the part where the string is to be replaced and
the file to be saved (overwritten) #####
//ifstream ofp;

// ###### here i tryed it somehow with vectors but i have no idead
how #######
//std::vector<char> v(0x3529BC);
//ofp.read(&v[0],0x3529BC);

ofp.pubseekoff(_offset, ios_base::beg,
ios_base::in | ios_base::eek:ut);
ofp.write(s.c_str(), 7);
ofp.close();
 
S

Son of Sam

Hi thanks for this code, my compiler says: error C2039: 'pubseekoff' :
is not a member of 'std::basic_fstream<_Elem,_Traits>'

I use msvs c++ (2003) as compiler.

Heres the code:

#pragma once

#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>
#include <stdio.h>
#include <time.h>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, _TCHAR* argv[])
{
char _string[7];
char _filename[128];
int _offset = 0x3529BC;

if(argc == 1)
{
system("cls");
cout << "\nUsage: patch.exe
[path/filename]\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "Enter path and filename to your
FlashpointResistance.exe\n\n";
cout << "(eg. C:/Desktop/ofp.exe): ";
cin >> _filename;
}
else
{
strcpy(_filename, argv[1]);
}

//ifstream ofp;
//ofp.open(_filename, ios_base::in | ios::binary);

fstream ofp;
ofp.open(_filename, ios_base::in |ios_base::eek:ut | ios::binary);

if(!ofp.is_open())
{
system("cls");
cout << "\nResults:\n";
cout << " File \"" << _filename << "\" not found.\n\n";
}
else
{
system("cls");
//##### Create the "random" 7 bytes long string ######
ostringstream os;
os << hex << time(0);
string s = os.str();
s.resize(7);

//###### Read the string[7] at offset 0x3529BC ######
//ofp.seekg(_offset, ios::beg);
//ofp.read(_string, 7);
//ofp.close();
ofp.pubseekoff(_offset, ios_base::beg, ios_base::in |
ios_base::eek:ut);
ofp.read(_string, 7);

ostringstream os2;
os2 << _string;
string s2 = os2.str();
s2.resize(7);

cout << "\nFilename: \t\t" << _filename << endl;
cout << "Random Filestring: \t" << s << endl;
cout << "Existing String: \t" << s2 << endl;
//##############################################################
//ifstream ofp;
//std::vector<char> v(0x3529BC);
//ofp.read(&v[0],0x3529BC);
ofp.pubseekoff(_offset, ios_base::beg,
ios_base::in | ios_base::eek:ut);
ofp.write(s.c_str(), 7);
ofp.close();

}

return 0;
}
 
A

Alf P. Steinbach

* Son of Sam:
Ok what does the program do:
opens a [FlashpointResistance] file binary, jumps to an specific
offset (0x3529BC9) reads out the string which is 7 bytes (7 chars)
long (which starts at the offset), then generates a random string
7 char long sting (string > s[7]).

<ot>If this patch is in order to defeat some copy protection
scheme you'll soon have a horde of very angry very competent Russian
hackers displeased with you -- which would please _me_. :) </ot>
 
S

Son of Sam

If this patch is in order to defeat some copy protection scheme
Nope, copyprotection of the FlashpointResistance.exe for exmple comes
without a copyprotection ;)

Thanks for the code Larry I Smith, heres the finished code:

#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>
#include <stdio.h>
#include <time.h>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <vector>

using namespace std;
[...]
fstream ofp;
ofp.open(_filename, ios_base::in |ios_base::eek:ut | ios::binary);

if(!ofp.is_open())
{
system("cls");
cout << "\nResults:\n";
cout << " File \"" << _filename << "\" not found.\n\n";
}
else
{
system("cls");
//##### Create the "random" 7 bytes long string ######
ostringstream os;
os << hex << time(0);
string s = os.str();
s.resize(7);

//###### Read the string[7] at offset 0x3529BC ######
ofp.pubseekoff(_offset, ios_base::beg, ios_base::in |
ios_base::eek:ut);
ofp.read(_string, 7);

ostringstream os2;
os2 << _string;
string s2 = os2.str();
s2.resize(7);

cout << "\nFilename: \t\t" << _filename << endl;
cout << "Random Filestring: \t" << s << endl;
cout << "Existing String: \t" << s2 << endl;
//##############################################################
ofp.pubseekoff(_offset, ios_base::beg,
ios_base::in | ios_base::eek:ut);
ofp.write(s.c_str(), 7);
ofp.close();

}

....but my compiler gives me following error message: error C2039:
'pubseekoff' : is not a member of 'std::basic_fstream<_Elem,_Traits>'

How can I fix that?
 
L

Larry I Smith

Son said:
Hi thanks for this code, my compiler says: error C2039: 'pubseekoff' :
is not a member of 'std::basic_fstream<_Elem,_Traits>'

I use msvs c++ (2003) as compiler.

Heres the code:

[snip]


Try looking in the manual. Read the docs on streams.

It should be:

ofp.rdbuf()->pubseekoff(_offset, ios_base::beg,
ios_base::in |ios_base::eek:ut);

Larry
 
L

Larry I Smith

Son said:
If this patch is in order to defeat some copy protection scheme
Nope, copyprotection of the FlashpointResistance.exe for exmple comes
without a copyprotection ;)

[snip]


...but my compiler gives me following error message: error C2039:
'pubseekoff' : is not a member of 'std::basic_fstream<_Elem,_Traits>'

How can I fix that?

Try looking in the manual. Read the docs on streams.

It should be:

ofp.rdbuf()->pubseekoff(_offset, ios_base::beg,
ios_base::in |ios_base::eek:ut);

Larry
 
A

Andrew Cox

pubseekoff is a member of basic_streambuf which is not derived from when
forming the fstream class. Try using seekp (for write position) and
seekg (for read position) instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top