The Ultimate Guide To Batch File Programming PART- 9



PIPING:
Piping is a feature which combines both Input and Output Redirection. It uses the Pipe operator, which is the | symbol. This command captures the Output of one command and sends it as the Input of the other command. Say for example, when you give the command del *.* then you need to confirm that you mean to delete all files by pressing y. Instead we can simply do the same without any User Interaction by giving the command:

c:\windows> echo y | del *.*

This command is pretty self explanatory, y is sent to the command del *.*

Batch File Programming can be very easy and quite useful. The only thing that one needs to be able to become a Batch File Programming nerd, is adequate knowledge of DOS commands. I suggest you surf the net or get a book on DOS commands and really lick the pages off the book, only then can you become an expert.

batch-file-programming 9

Making your own Syslog Daemon:

We can easily combine the power of batch file programs and the customizable Windows Interface to make our own small but efficient System Logging Daemon.
Basically this Syslog Daemon can keep a track of the files opened(any kind of files), the time at which the files were opened also actually post the log of the User's activities on to the web, so that the System Administrator can keep a eye on things.

Simply follow the following steps to make the daemon-:

NOTE: In the following example, I am making a syslog daemon which keeps an eye on what text files were opened by the User. You can easily change what files you want it to keep an eye on by simply following the same steps.

1. ASSOCIATING THE FILES TO BE MONITORED TO THE LOGGER

Actually this step is not the first, but being the easiest, I have mentioned it earlier. The first thing to do is to associate the text files(*.txt) files to our batch file which contains the code to log the User's activities. You can of course keep an eye on other files as well, the procedure is almost similar. Anyway, we associate .txt files to our batch program so that each time a .txt file is opened, the batch file is also executed. To do this, we need to change the File Associations of .txt files.

For more information on Changing File Associations, refer to the Windows Help Files, simply type Associations and search. Anyway to change the associations of .txt files and to point them to our batch file, simply do the below:

Locate any .txt file on your system, select it(click once) and Press the SHIFT key. Keeping the SHIFT key pressed, right click on the .txt file to bring up the OPEN WITH... option. Clicking on the OPEN WITH... option will bring up OPEN WITH dialog box. Now click on the OTHER button and locate the batch file program which contains the logging code and click on OPEN and OK. Now each time a .txt file is opened, the batch file is also executed, hence logging all interactions of the User with .txt files.

2. Creating the Log File

Now you need to create a text file, which actually will act like a log file and will log the activities of the User. This log file will contain the filename and the time at which the .txt file was opened. Create a new blank text file in the same directory as the batch file. Now change the attributes of this log file and make it hidden by changing it's attributes by issuing the ATTRIB command.

C:\windows>attrib xyz.txt +h

This will ensure that a lamer will not know as to where the log file is located.

3. CODING THE LOGGING BATCH FILE

The coding of the actual batch file which will log the User's activities and post it on the web is quite simple. If you have read this tutorial properly till now, then you would easily be able to understand it, although I still have inserted comments for novices.

echo %1 >> xyz.txt /* Send the file name of the file opened to the log file, xyz.txt */
notepad %1 /* Launch Notepad so that the lamer does not know something is wrong. */


This logging file will only log the filename of the text file which was opened by the unsuspecting lamer, say you want to also log the time at which a particular file was opened, then you simply make use of the 'time' command. The only thing that one needs to keep in mind is that after giving the TIME command , we need to press enter too, which in turn has to entered in the batch file too.Say you, who are the system administrator does not have physical access or have gone on a business trip, but have access to the net and need to keep in touch with the server log file, then you easily link the log file to a HTML file and easily view it on the click of a button. You could also make this part of the site password protected or even better form a public security watch contest where the person who spots something fishy wins a prize or something, anyway the linking can easily be done by creating an .htm or. html file and inserting the following snippet of code:

<html>
<title> Server Logs</title>
<body>
<a href="xyz.txt>Click here to read the Server Logs</a>
</body>
</html>


That was an example of the easiest HTML page one could create.

Another enhancement that one could make is to prevent the opening of a particular file. Say if you want to prevent the user from launching abc.txt then you would need to insert an IF conditional statement.

IF "%1" == "filename.extension" ECHO Error Message Here

4. Enhancing the logging Batch file to escape the eyes of the Lamer.

To enhance the functioning of our logging daemon, we need to first know it's normal functioning.Normally, if you have followed the above steps properly, then each time a .txt file is opened, the batch file is launched(in a new window, which is maximized) and which in turn launches Notepad. Once the filename and time have been logged, the batch file Window does not close automatically and the User has to exit from the Window manually. So maybe someone even remotely intelligent will suspect something fishy. We can configure our batch file to work minimized and to close itself after the logging process has been completed. To do this simply follow the following steps-:

a) Right Click on the Batch File.
b) Click on properties from the Pop up menu.
c) In the Program tab click on the Close on Exit option.
d) Under the same tab, under the RUN Input box select Minimized.
e) Click on Apply and voila the batch file is now more intelligent

This was just an example of a simple batch file program. You can easily create a more intelligent and more useful program using batch code.

ABOUT THE AUTHOR
AbhiShek SinGh
Founder of 'TheHackingArticles'. Cyber Security Analyst, Cyber Security Researcher, and Software Engineer. Follow 'AbhiShek SinGh' on Facebook , Twitter or Google+ or via Email

Subscribe to stay up to date