Execute a bat file when an image is viewed - image

I want to hide a bat file or its commands in a image file so when the image is opened the commands in the bat file is executed.

Assuming you're speaking specifically for the windows platform (what with "batch" files), you can't.
Batch files are parsed, not compiled, so an image file with batch in it would still be read as an image file.
I really can't sum it up more clearly than things just don't work like this.
However, assuming you're doing things ethically, you can re-associate, say, a .jpg extension to be opened with the command prompt and put your batch script in a text file with a .jpg extension.
Just a final note: Questions like these are often looked poorly upon by the StackOverflow members simply because you're hardly ever trying to achieve something like this with good intentions. I won't be the first to flame you, but I definitely won't be the last.

There are a number of ways in which a single file can contain two distinct streams of data which are independent of, and don't interfere with, each other. One commonly cited example of this is how it is possible to append a .zip file to a .jpg file and still have the combined file function as both types of files, depending on what sort of application is opening it. This trick takes advantage of the specific way jpeg and zip files are structured. These sorts of files are akin to polyglot program.
Another common method of embedding unrelated information into a file is steganography, which is the concealment of messages within mundane objects (like an image file.)
You might also consider using an Alternate Data Stream, though in such a case the batch file would arguably not be embedded in the image.
Once you have your batch file embedded, by whatever means, you must then find a way to cause the batch file to be run instead of--or in addition to--whichever image-viewing application the user has configured.
It can be safe to assume that the average user does not expect a file ending in .jpg (for example) to open a cmd.exe window and start executing unknown instructions. Windows Explorer, the application which under most circumstances decides what program to run when a certain file type is double clicked, has sane defaults (i.e. a .jpg file gets opened with an image viewing application) though these can be changed (though users generally don't change these sane defaults to insane customizations like cmd.exe.)
The simplest method would be to write your own handler program and cause it to become the default handler for image file types. I believe that all handler applications must be compiled PE application or library files (.exe, .dll, .scr, etc.) so another batch file won't work here. Your custom handler must reverse the embedding to retrieve the embedded batch file and then execute it with cmd.exe.
But if you've already managed to supplant the default handler with your own code then the embedded batch file is moot since you've already got your code running.
I have to echo what #Di-0xide said about this question. If you're trying to write a legitimate application and this is part of your design plan, then you need to go back to the drawing board because no legitimate application should ever do this sort of thing.

Although there are no doubt many more malace reasons why someone would want to embed a batch file within an image,
and have it execute when the image is opened. I can see at least one legitimate reason for wanting to perform the task.
What if you have a family computer, shared with your 2 kids, the wife, and yourself. Lets say you have some files you don't
want your children to come across, or a directory full of journal entries. It would be pretty neat if a simple batch file that would
change the attributes settings of the directory, containing the sensitive files. This way a parent could rest assure that their
kids wont stumble upon dads nudie pics. LOL
IE: Lets say someone wrote 2 simple batch files s.bat(to show the directory) and h.bat(to hide the directory)
hide.bat
#echo off
REM Show the directory containing sensitive info:
attrib +a +s +h +r C:\path\to\the\directory\to\hide
REM Hide this batch file(or image containing the batch file):
attrib +a +s +h +r C:\path\to\the\batch\files\hide.bat
REM Show a batch file(or image containing the batch file) that will allow for showing the directory:
attrib -a -s -h -r C:\path\to\the\batch\files\show.bat
show.bat
#echo off
REM Show the directory containing sensitive info:
attrib -a -s -h -r C:\path\to\the\directory\to\hide
REM Show the batch file(or image containing the batch file) that will allow user to hide directory:
attrib -a -s -h -r C:\path\to\the\batch\files\h.bat
REM Hide this batch file(or image containing the batch file):
attrib +a +s +h +r C:\path\to\the\batch\files\s.bat
I think it would work better having images that would execute the show.bat and hide.bat batch files, rather than just changing the icon on each batch file.
Perhaps writing a handler program that would change the attributes of the directory then open the image and associate the handler to some unused extension, would work.
Just a thought.

Simplest way to do this is to just create your batch file as normal and then create a desktop shortcut of the same batch file. Finally right-click on the shortcut and change the icon to what ever image you want. Once all changes are made then anytime the image is opened, the batch file will run.

This is easy just use winrar archive to embed bat file in image. This will run bat file before image or you can change settings to run image file before bat.
Follow this tutorial to do so:
https://www.youtube.com/watch?v=ytvKVYYrqEw

Related

Can a batch file write to a hidden notepad file?

Some context:
I have an Excel Macro (.xlsm) file I've created for my company, which I'm creating a batch file for that copies it from a Network folder to a local folder for each computer/user profile that clicks the batch file. What I'm looking to do is for every user that runs the batch file, I want to append their name to a hidden notepad file in the same network folder of the original macro file. That way, whenever I revise/fix/add features to the macro, I'll have a list of all the users that are using the macro file, so I can send an email letting them know of the updates.
Note: I know how to obtain the username of the user that runs the files.
I know that you can write and append to Notepad files using a batch file, but I can't find a source that verifies whether it's possible to write to an existing hidden Notepad file. I came upon this post in my search, but that's using the C# language.
So, my question(s) are:
Can a batch file write/append to a hidden notepad file?
If so, would I use the same syntax/method that you would when writing to a visible (non-hidden) file?
If not, would could I go about this feat? Could I temporarily unhide a hidden file, append to it, then rehide it? Or should I go about it using a different method?
Is this a good method, or is there a more efficient/better way to accomplish this (keeping a list of users that have the macro file)?
You could use stream redirection operator >> and system variable with login as i recall %username%. Do echo %username%>>hiddenfile. txt

Can you run Windows batch file not in a command prompt?

My programming teacher is requiring that everyone participate in the science fair this year, and now they accept computer-related projects. So, knowing only one language (Windows batch), I asked if I could make some kind of text based game in batch code. She said that I can only write the code, but not execute it.
She said that if we're even caught on the command prompt, we apparently get in big trouble and never get access to a school computer for the rest of the time I'm at the high school, or something like that... Even if all the code contains is a bunch of echos and simple variables, she won't let me and my partner run it. She also said that we must work on the project during school in her class.
So to sum all that up, she's letting me write a game/program, but I'm not allowed to test it to make sure it works in school, which will be when I'm writing it most...
So is there any way to run/test a batch script with simple echo and set /p commands without the "dangerous, black command prompt" showing up?
I think she has no idea what she's talking about, because on the board, she wrote "back script" lol
A batch file runs like any other executable file by double-clicking the file within Windows. However, because a batch file runs in a command line it immediately exits when done, so you may only see a black box for a second.
If the batch file is closing too fast, or you want to read the output from the batch file you can edit the batch file and add a pause to the end of the file. The pause command waits for user input before continuing.
A .BAT file can be made into a .EXE or .COM file to keep the program's source hidden from a user. You could use the .BAT to .EXE converter file to convert the batch files to .EXE file. In this program you can point to the batch file you want to convert into an executable file, include additional files, change the icon of the file, and add file details.
For Information on Batch files, this is one of the best resource.
Try this:
#echo off
color F0
mode con lines=25 cols=60
cls
title "Notepad - MyProgram.bat"
set /P "name=What is your name? "
echo Hello %name%, glad to see you...
pause
Execute it via a double click or enter in files browser...

Batch Script that saves log files

I am currently using a program called USBDeviewer for auditing purposes. Basically what it does is it gets a list of USB devices that were used on a particular PC. The program can create a log file but you have to do it manually. The keyboard command to save a log file is ctrl+A to select all devices and then ctrl+S to save. After you select save you are prompted to enter a filename and location to save it.
I need a batch script that will automatically run this program and save the information on the log file to a folder automatically without any human intervention such as typing in the name (it needs a different name every time, perhaps the filename can include a different date every time).
This is a simple script but I am not familiar with batch scripting so any help would be appreciated.
Save Command-Line Options for usbdeview.exe
/stext Filename : Save the list of all USB devices into a regular text file.
#echo off
set Log=c:\temp\logfile.txt
usbdeview.exe /stext %Log%

Hide Vim backups (*~) in Windows Explorer

On Windows, I normally work with Total Commander, which can easily be configured to ignore these *.*~ and *~ completely. But occasionally when I switch to Windows Explorer, I get little bit confused with all that "unknown" files/.
Can I set up Vim so that for every backup it creates it will also set "hidden" attribute?
Or set up some nice workaround?
I know I can set up Vim to put these in other directory, but I'd like to avoid that, since IIUC, it could suffer from naming conflicts.
If the backup option is set, vim updates the backup file every time we write the file with :w. And every time, it creates a file which is not hidden even though you had forcibly hidden it previously! So we need to do something everytime we write the buffer into file.
You can do this on windows. In your _vimrc file ( generally found at C:\Program Files (x86)\Vim ), add this line
autocmd BufWritePost,FileWritePost * silent ! attrib +h <afile>~
Where,
attrib=Windows File attribute changinf command
<afile>= Name of the file being sourced
silent= Prevent an annoying command window from popping up and asking user to press a key
This will make sure that the backup file gets hidden with every write to file from buffer. Why every time? Cos vim creates a non-hidden file at every write!
But you have to live with a flashing black window ( command window where we are running attrib command ) every time you save your file, but worth the pain :)
On linux/unix systems you can add this in your .vimrc
autocmd BufWritePost,FileWritePost * silent ! mv <afile>~ .<afile>
Hope this helps all those trying to find how to hide vim backup files.
I wrote a plugin for this a while back called autohide. It works by setting the "hidden" attribute after write as suggested in Pavan's answer. By default it only does this for swap files, viminfo, and persistent undo files; you can make it only hide backup files by configuring let g:autohide_types='b' in your .vimrc, or add it to the default list instead with 'suvb' instead of just 'b'.
Benefits over the manual method in Pavan's answer include handling of additional file types, arbitrary file patterns (like dotfiles), and some error handling (especially related to slow network shares that don't allow setting attributes right away after creating a file).
I have this in my _gvimrc:
set nobackup
No backup files are generated in the first place.
However, the swap file (.*.swp) is still generated during editing (and deleted when you close Vim). So if your computer crashes, you can still recover your changes.

Redirect default program to another program when a file opens in Windows OS

This is only under windows env.
As I know windows os identifies associated application of a particular file by file extension.
Like wise each file (binary) starting with corresponding symbols ("starting symbols"). For an example .JPG starts with ÿØÿà. Let say I open this .JPG file in a Hex editor or a Text editor and then I change that starting symbols into another file type. for an example I can change ÿØÿà to .Eߣ (.mkv). So when I double click on the .JPG the Windows Photo Viewer says there are some errors or similar message. So I need to get some information about the application that tries to open that kind of a file. If I can, I need to open that file using the application that associated with "starting symbols".
Briefly when I open .JPG I need to open a default video player .mkv files. But It may not work for this example. Because I changed only the "starting symbols" of my .JPG.
Please give me any idea to do this.
Thanks!
When you encrypt the file, give it a new extension. e.g. Picture.jpg becomes Picture.encrypted-jpg. You then register as the handler for encrypted-jpg, decrypt the file, then launch the normal jpg handler.
When the shell is asked to perform a verb on a file, the shell does not use the contents of the file to determine which app to pass it to. The file extension is what determines how the file will be treated.
You wish to use the contents of the file to influence which app processes a shell verb. In order to do so you would need to create a launcher app that reads the file header and then decides which app to pass the file on to. You would assign your launcher app as the handler app for all file extensions that you were interested in.
Although you could do this, it would be much easier just to set the file extension appropriately.
The proper way to do this sort of thing is to replace the files with reparse points.
The downside is that this involves writing a file system filter driver, i.e., an operating system extension, which is a whole level of trouble above and beyond ordinary application programming. (Since Windows already does file encryption, I doubt it would be worth the effort.)

Resources