views:

213

answers:

4

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?

+3  A: 

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.

anon
A: 

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
+2  A: 

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
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
Alex Martelli
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
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
+1  A: 

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