2

I'm wondering if it's possible to convert batch files to executables using C++? I have plenty of batch files here and I would like to convert them to executables (mainly to obfuscate the code). I understand that there are 3rd party tools that can do this but I was thinking that this would be a good opportunity for a programming project.

I'm not sure where to start. Do I need to code some sort of parser or something?

Coding District
  • 11,901
  • 4
  • 26
  • 30

6 Answers6

5

You don't just need a parser, you need to write a compiler that accepts .BAT or .CMD files as it's input and outputs C++ as its "machine code". I would class this as a "hard to very hard" project (mainly because of the weirdo syntax and semantics of the input language) but if you want to go for it, the definitive SO question on compiler writing is here.

Community
  • 1
  • 1
3

Create an executable which stores the batch file as a resource. Then on execution, grab the batch file from the resource, write it to the disk, and execute it. Afterwards, delete it.

This way, you could even have multiple batch files in one executable and trigger the right one through a command line switch.

This is a typical problem that you can either solve the long, really hard way (see the suggestion on writing a compiler for the batch file), or make a nice fast solution in less than a day that does exactly what you need.

Alex
  • 75,813
  • 86
  • 255
  • 348
  • This is not entirely secure from obfuscation point-of-view. The batch file will be available on the disk, even if it's momentarily. – TFM Aug 08 '09 at 21:54
  • obfuscation is never "entirely secure" anyway -- disassembly &c will defeat it. this quick and dirty solution has good ROI -- small R(eturn), but even smaller I(nvestment)!-) – Alex Martelli Aug 08 '09 at 22:14
  • You can obfuscate the source by encrypting it; that won't defeat a really determined hacker (who will grab the source out of memory after you've decrypted it), but it will stop anyone from extracting the source with such tools as the `strings` program. – Adam Rosenfield Aug 08 '09 at 22:34
  • 3
    Guys, we're talking about batch files here. I don't think this is national security related. The guy wants to simply make sure nobody can easily read/modify the batch file. If this was high security it wouldn't be in batch files to begin with. – Alex Aug 08 '09 at 22:38
1

You could open the batch files with notepad and copy line by line then paste it in a C++ .cpp file like this and use the system function:

.cpp

int main()
{
   system("Your command here");
}

Not sure if this is what you want but it would in the end create an executable file but could take some time depending on the size of the batch file.

Partial
  • 9,529
  • 12
  • 42
  • 57
0

As Alex said, the easiest way to compile a batch file is storing it in some local storage area in the executable, and at runtime, extracting, executing, and removing it. Using a resource file is a good idea, because then you can just compile a stub and merely copy it and replace the contents of the resource whenever you need a new exe. While anon's solution is definitely plausible, it seems like it'd be way too much work than is actually needed in this situation.

Anyways, I actually got bored a few months back and wrote something to this effect, which can be found here. It's nothing special, but should show you what you need to do.

Charles Grunwald
  • 1,441
  • 18
  • 22
0

How about this ? I'm not sure it will obfuscate it much, but at least you don't have to write your own parser which might take a while :)

nmuntz
  • 1,158
  • 2
  • 11
  • 22
0

Well there is a program called Advanced BAT to EXE Converter (look on Google) which makes it a ton easier (if I was on my laptop, then I would know the exact name).