____________________________________________________________ GUIDE TO (mostly) HARMLESS HACKING Microsoft-only version Number 2 Hacking with Win95/NT: Batch File Programming ____________________________________________________________ by KeyDet89 I figured that, after all of the Happy Hacker Digests and Guides To (mostly) Harmless Hacking that have dealt with shell programming, something for Win95 and NT needed to be addressed. *************************************************** Newbie Note: A good resource for batch files is to go to you local library and look up back issues of PC Magazine. The older editions, even those published after Win95 came out, list several useful batch files that you can use or modify for your own use. WindowsNT Magazine has had several good articles on scripting languages for NT. Other excellent sources of information include books from the library and used bookstores. Also, visit the alt.msdos.batch newsgroup. If you can't access newsgroups from your ISP, use your browser to visit DejaNews at http://www.dejanews.com. Or visit any of the sites listed at the end of this file. *************************************************** Let's get started. First, keep in mind that batch files are just series of programs that you would like executed. The files are made up of commands that already reside on your machine. If you have several commands that type over and over, each with specific switches and parameters, you may want to put them into a batch file. Batch files can also be used the way aliases are on Un*x...written properly, you can launch a program from the command line without having to type it out each time. A word about editors: You already have two suitable text editors on your computer...DOS edit, and Notepad. Edit is fairly simple to use, but you may not be used to it. Notepad is also easy to use, just remember to save your files with the correct ".bat" extension so that they will be recognized as batch files by the command processor. *************************************************** Newbie Note: Most of the commands that you will want to run from batch files will be command line programs. Check out your windows\command directory to see what is available. Don't hesitate to look for DOS or Win95 command line programs on the Internet. *************************************************** STEP 1 -- "Hello, World" The first program we will write is the obligatory "Hello World" program that accompanies every programming language. Simply save a file called "hello.bat" with the following text: echo Hello, World! Now, if you type "hello" at the prompt, you will see the "echo" line printed at the prompt, then the line "Hello, World!" on the line below it. To suppress the commands at the prompt, add the line: @echo off as the first line of the file. Now rerun the file. STEP 2 -- Arguments Now, let's personalize our program a bit. Change the second line to: echo Hello, %1 and run the program by typing: c:\>hello Johnny Now we've added arguments, as denoted by the "%1". This refers to the first argument that is sent to the file. We can send multiple arguments. To demonstrate, open a file called "args.bat" and type in the following lines: @echo off echo ARGS: %1 %2 %3 %4 echo REV: %4 %3 %2 %1 echo MIXED: %2 %1 %4 %3 Now run the file with at least two arguments (you can use more and examine the output): c:\>args hello steve dog rain STEP 3 -- Redirection When programming in most languages, there are three references that you need to be aware of: standard input (STDIN - usually the keyboard), standard output (STDOUT - the screen), and standard error (STDERR - also the screen). However, you may not want the output to go to the screen, you may want it stored in a file. Well, we can do this fairly easily with something called redirection. How does this work? Well, when you type in something like: c:\>attrib /? to find out what the attrib (attribute) command does, and how it is used, you see a lot of information on the screen. Try doing the same thing with the "net" command under Win95, and the info seems to disappear off the screen! So to send the output of the command to a file, simply add the greater-than sign and a file name to the command: c:\>net /? > net.txt If you want to add or append information to an already existing file, use the double-greater-than symbol: c:\>net view /? >> net.txt **************************************************** Cool Trick To Try: I won't be covering HTML programming here, but here is something to try. There are basic commands that every web page has...these are the tags that are that are used to designate the header, title, change colors or fonts, etc. Write a batch file that takes the output of series of commands, such as "net", "net view", "net use", "net user", and "net time" and puts them in an HTML file. That way, you can post it on the web. You may even go so far as to include links to examples, etc. **************************************************** STEP 4 -- Autoexec.bat Now is a good time for a word on the King of All Batch Files, the autoexec.bat file. This is the file that is used by DOS at boot up, and exists for DOS and Win95 (Win3.1 runs on top of DOS, and is called from the autoexec.bat file). Use the autoexec.bat file "to set the characteristics of your devices, customize the information that MS-DOS displays, and start memory-resident programs and other applications" (from the MS-DOS User Guide). Really? Well, given that, you can do all sorts of interesting things with this file...or any other batch file. Note on NT: NT does not use the autoexec.bat file, but there is a registry key that when set, will enable the parsing of the autoexec.bat file, reportedly for environment variables. Gee, I wonder what else it will do... ***************************************************** Evil Genius Tip: Take a look at the prompt command by typing: c:\>prompt /? Play around with different settings. Typing the command to change the prompt at the current command prompt will change it for that session...adding the command to the autoexec.bat file will change if for all sessions. ***************************************************** STEP 5 -- Aliases You'll notice that when you type: c:\>notepad somefile.txt Notepad opens with the file, and in the DOS window, you get you command prompt back. So if you want a quick way to open the text files, create a small batch file called "np.bat", with the lines: @echo off notepad %1 Now all you have to do is type "np" and the file name. **************************************************** Evil Genius Trick: Here's a handy little way to create a mini-syslog daemon of your very own...or someone elses. Create a file called "file.log", or whatever, on the target computer, in the Windows directory. HINT: Investigate the "attrib" command, paying particular attention to the "h" option. Now, create a batch file that will make entries to file.log. You might want to have something printed, or just the file that was opened. Next, click Start -> Help, and type in "associating" and display the help on "file types with programs". Change the associations for ".txt" files to point to your batch file, and make sure that the last line reads: notepad %1 If your friend uses Microsoft Word a lot, make the appropriate changes there, too. **************************************************** **************************************************** Neat Trick Tip: If you like the Un*x commands, but don't want to fool with downloading them, write you own. Create a batch file called "ls.bat" and use the "dir" commands to customize the display. Start with: @echo off dir %1 Make sure to see what switches are available for the dir command... **************************************************** STEP 6 -- Information Gathering There are several commands that can be used to gather information, especially on a networked computer. These commands can be used to gather information for diagnostic purposes, as well as being used for other insidious purposes (no Evil Genius Tips here, I'll leave it up to your imagination). Start by running the following commands on your machine while connected to a network or to the Internet: nbtstat -c nbtstat -n netstat -an net user (NT only) net use/config/time/view arp -a When you begin to see the type of information that is available, tailor the commands to your needs, and put them in a batch file, redirecting the output to a log file of some sort. STEP 7 -- More Stuff I have gathered together some sites that provide more detailed information on batch file programming. These sources range all the way from examples to tutorials to post-graduate theses...so take a look... BATCH FILE PROGRAMMING SITES ftp://garbo.uwasa.fi/pc/ts/tsbat53.zip http://gearbox.maem.umr.edu/~batch/ http://www.nc5.infi.net/~wtnewton/batch/index.html http://purl.oclc.org/net/dirk/batcoll.all http://purl.oclc.org/net/dirk/batvirus.all http://www.deltaelectronics.com/tglbatch/ ___________________________________________________________ To subscribe to Happy Hacker and receive the Guides to (mostly) Harmless Hacking, please email hacker@techbroker.com with message "subscribe happy-hacker" in the body of your message. Copyright 1998 KeyDet89 . You may forward or post this GUIDE TO (mostly) HARMLESS HACKING on your Web site as long as you leave this notice at the end. ___________________________________________________________ Carolyn Meinel M/B Research -- The Technology Brokers http://techbroker.com