Windows
See:
Originally written for and copyright by Information Security Magazine, February 2002 " Windows Security Scripting."
Note: Under DOS & Win9x/ME some of the simple STDOUT redirection and pipes work, but none of the advanced STDERR or multiple command methods are supported.
| FD | Description |
|---|---|
| 1 | STDOUT |
| 2 | STDERR |
| 3+ | Additional files as opened by the process |
| Command | Description |
|---|---|
cmd1 | cmd1 |
Pipe STDOUT of cmd1 into STDIN of cmd2 |
\> file |
Direct STDOUT to file, overwriting existing contents |
\>\> file |
Direct STDOUT to file, appending to existing contents |
| `> | file` |
2\> file |
Direct STDERR to file, overwriting existing contents |
2\>\> file |
Direct STDERR to file, appending to existing contents |
\< file |
Get STDIN from file |
2\>&1 |
Direct STDERR to the same place as STDOUT |
\>& file |
Direct both STDOUT and STDERR to file |
2\>& |
Duplicate STDOUT to STDERR |
echo 'foo' \>&2 |
Send output to STDERR instead of STDOUT |
/dev/null is equivalent to Windows NUL. Windows NUL is not case
sensitive.^ is the meta-character escape in DOS/Windows, so it may sometimes
be necessary to use ^| (e.g. when using egrep in a batch file). You
may use ^^ for a literal ^.| Command | Description |
|---|---|
dir c:\*.* > myls.txt |
Redirect output of ls into myls.txt, overwriting or creating myls.txt if necessary |
dir c:\winnt\*.* >> myls.txt |
Append more output of ls into myls.txt |
noisy_cmd > NUL |
Make STDOUT output from noisy_cmd go away |
noisy_cmd 2> NUL |
Make STDERR output from noisy_cmd go away |
noisy_cmd > NUL 2>&1 |
Make ALL output from noisy_cmd go away |
noisy_cmd 2> NUL 1>&2 |
Make ALL output from noisy_cmd go away |
noisy_cmd | more |
Pipe noisy_cmd STDOUT into more (or less or whatever) |
noisy_cmd 2>&1 | more |
Pipe noisy_cmd STDOUT and STDERR into more (this is great for those “net” commands that scroll off the screen when you try to get help) |
echo some message 1>&2 |
Use the echo command to send output to STDERR (it usually goes to STDOUT). |
Use parentheses to nest as needed.
| Command | Description |
|---|---|
cmd1 & cmd2 |
Run cmd1, then run cmd2 |
cmd1 ; cmd2 |
Run cmd1, then run cmd2 (UNIX only) |
cmd1 && cmd2 |
Run cmd1. If it finishes successfully then run cmd2 |
cmd1 || cmd2 |
Run cmd1. If it fails then run cmd2 |
This content is obsolete, but I am leaving it here as a historical reference.
Does your computer ever do any of the following, “just for the heck of it,” with no rhyme or reason? Does it do it a lot? More than once or twice a day?
If so, this page may be able to help. It is intended for novice users, so hopefully everything will be clear. Let me know at JPATjpsdomainDOTorg if it’s not.
This page is mostly oriented towards the Win9x code-base, which includes Windows 95, 98 and ME. Some of the material is applicable to Windows NT/2000/XP (such as clearing the temp directory) and some is not. This is indicated in square brackets after each step.
Some of the methods and techniques I talk about can be dangerous to your data! Use this page at your own risk.
Make backups of your important data. I strongly recommend purchasing and using a modern tape drive. They cost between $100 to $500 for typical retail/consumer oriented units. Some of them even plug right into your printer port, so you don’t even have to open up the computer to install it, and they may be used on more than one computer. Another solution is a CD Burner. They will not be able to back up the entire system, but your data files will easily fit onto a CD-Rewritable or CD-R disk.
To be added.
To be added.
Thanks to Leo for this great info.
Ever try to upgrade hardware on a Windows 2000 or XP system, especially the motherboard or hard drive controller? Bet you regretted it, didn’t you? Well, here are some MS TechNet articles that might help. Read the disclaimer!
The Windows TCP/IP networking stack is not the most stable thing in the world. In particular, there is a ability for third-party applications to tie themselves into the stack for various reasons. The advisability of allowing this is questionable at best, but there it is. One of the possible results of this “feature” is that Windows networking can sometimes simply die. Often there is no remedy but reinstalling the entire system from scratch.
However, since this is a known issue and such a giant pain there are a couple of tools available to try and help. In particular, LSP-Fix “is a free utility to repair [problems in] Layered Service Provider or LSP, a piece of software that can be inserted into the Windows TCP/IP handler like a link in a chain. However, due to bugs in the LSP software or deletion of the software, this chain can get broken, rendering the user unable to access the Internet.”
There are programs out there that spy on you (spyware), monitor keystrokes, bother you with advertisements (adware) and all kinds of other things. They are often collectively known as “crapware” and they are BAD. Many add-on tool or search bars for IE are crapware! Many of the symptoms above can be cause by crapware, so it’s definitely worth it to look into. I recommend reading the following links, then installing and using the free Ad-aware and Spybot programs.
I call these techniques “voodoo” because it is often not clear that they a) should work or b) why they work. They just do. This page is mostly oriented towards the Win9x code-base, which includes Windows 95, 98 and ME. Some of the material is applicable to Windows NT/2000/XP (such as clearing the temp directory) and some is not. This is indicated in square brackets after each step.
I’ve created a bootdisk that will automate some of these steps for you, if you are using Win9x/ME. See the Boot Disk section below.
ScanDisk and/or Chkdsk (technically, ScanDisk
is preferable, but I’ve had some luck with ChkDsk where ScanDisk
failed). [All]Finally, the worst case is that you have to back up your data, reformat your hard drive, and start over.
Skip this step if you are using my boot disk.
This applies to Win9x/ME only. In NT, you can’t really do this at all, especially if you are using NTFS (which you should be). (Technically, that’s not true – there are ways. But they are far too complicated for this page.) Win2K has something similar, but you probably don’t need or want to mess with it.
If you don’t get the “Starting Windows 9x.” or the menu, there are a couple of reasons why that could be. Lots of computers have annoying manufacturer logos that cover this stuff. Try hitting the ESC key when the logo comes up. Other “F” keys sometimes work too.
You can also going to Start, Shutdown, “Restart in MS-DOS Mode,” which will do pretty much the same thing. You type “exit” to end MS-DOS mode and return to windows.
If you get the “Starting Windows 9x.” but pressing “F” keys does not work, your c:\MS-DOS.sys file probably has BootKeys=0, BootMulti=0 or something else screwy. Try rebooting and holding down one of the CTRL keys. If that doesn’t work you have to edit c:\MS-DOS.sys, which is a bit of a pain to correct that. See MS Technet “ Contents of the Windows Msdos.sys File [Q118579]” for details and instructions on how to fix it.
Read the disclaimer!
Skip this step if you are using my boot disk. Also, see my
Sweepup.bat script in Favorite Utilities, Tools, Software for Windows
.
The “temp” directory is used to temporary files. Just about every program uses temp files for something. The operating system uses then, your applications like Word or Excel use them, and utilities use them. What is supposed to happen is that when the program either shuts down, or no longer needs the temp file – it is deleted. In practice this does not always work. So over time you can build up amazing numbers of junk temp files. I’ve seen computers with temp files dating back two and three years, wasting hundreds of megs of disk space.
Of course, you can’t just randomly delete everything in the temp directory. No. That would be too easy! The problem is that some of the stuff in there might actually be in use and if you delete it, something will break. Usually the system will not let you delete something that is in use, but you can’t depend on that. There are two ways around this. The first and best is to boot to DOS, which guarantees that nothing is currently in use, then clear it. The second way is to reboot, and clean out the temp directory before anything has a chance to get started. The problem with that is that many people have all kinds of stuff running in the background on startup, and those programs may open temp files right away (see unnecessary services).
Do this after using the boot disk.
ScanDisk and Chkdsk (Check Disk) perform more or less the same function. The difference is that Chkdsk is the old, DOS, command line tool, which does not really know about long file names. ScanDisk is the more modern graphical tool that can deal with a few more problems than Chkdsk. There is also command line (non GUI) version of ScanDisk.
Read the disclaimer! If the power goes out while defrag is running, you can lose your data!
Do this after using the boot disk.
NT does not come with a defragger, so you are out of luck. Win9x and Win2k do have them. You want to run it from the GUI (Graphical User Interface) not the command line or DOS prompt.
Do this after using the boot disk.
This is easy to say, but hard to do. There are so many different tools and configurations out there that it’s impossible to say what is really needed or not. However, if your system tray (the little area next to the clock) has more than 3-8 items in it, you probably have too many and could get rid of some. Also, some incredibly annoying applications, such as Netscape (which I otherwise like) install crap you probably don’t want or need in the system tray – without asking! For example, Real Player and AIM (AOL Instant Messenger) are often installed. Quicken is another one that runs a bunch of crap most people don’t use.
I can’t even really tell you how to get rid of them, as they are all different. However, I can tell you where to look.
If you have Win98 (or I assume ME, but I don’t know for sure), go to Start, Programs, Accessories, System Tools, System Information, then get the Tools, System Configuration Utility. Better yet, go to Start, Run and type “msconfig” (without the quotes) and hit enter. This gets you to the same place, a lot faster.
Anyway, the System Configuration Utility has a very cool “Startup” tab. This shows you all the stuff that starts up with your PC. You can go in there and un-check things, then reboot and make sure everything still works. You should check all the other tabs, but unless you see something really obvious (like drivers for an old hardware device you no longer have) you should probably not touch them too much.
Go to Start, Settings, Control Panel, Services. See what is started, and what is automatic. Try stopping things that you know you don’t need. For example, you might be running a web server, FTP server, etc. without even knowing it. This is a large security risk as well. If you can stop services with no ill effects, you must change the startup type to manual (which will still allow the service to be started under some circumstances) or disabled (the service is now toast) or they will be restarted the next time you reboot.
Go to Start, Settings, Control Panel, Administrative Tools, Services. See above NT section for the rest of the details.
Consider these after trying the above and/or using the boot disk.
This boot disk will work for Win9x/ME. It might work for NT/2000/XP, if you are using the FAT file-system (which you shouldn’t be). This disk uses only FREE programs – from the FREEDOS Project! Download the Boot Disk.
This boot disk will clean out the TEMP directory and some other junk files, then try to run a command line ScanDisk. Since you are booting from the disk, you do not have to mess with DOS mode – you’re already where you need to be.
It will also clean up some other files that I didn’t mention above. If any of the following exist, they will be deleted:
This content is obsolete, but I am leaving it here as a historical reference.
This content is obsolete, but I am leaving it here as a historical reference.
As far as I know, all of these tools are free unless otherwise noted.
Use any ZIP program, such as WinZip or InfoZip’s FREE Unzip to extract the ZIP files.
While the programs above are ports of various UNIX tools, the next four items are complete UNIX environments for the PC. DJGPP is older, and has not been updated too much (not that it needs it). DJ Delorie went to work on the Cygwin project after he wrote DJGPP. Both environments are very cool! I don’t know too much about the other two, and have never used them.
Finally, some stuff from Microsoft.
less these days!
I used
v7.7a because it works
with network drives, but there are others, including shareware
versions such as
v9.3a.
If you want something similar to list, but for UNIX, try “less” or
“pilot” which is the browser built into
Pine. “Pico,” the Pine editor,
is also good for people who don’t like the typical UNIX editors.
Pine is free (but not GPL).This content is obsolete, but I am leaving it here as a historical reference.
This content is old! It’s still useful, but it’s old, and there may be bit rot, newer/better tools or ways to do things. Sanity check and do your research.
This article is translated to Serbo-Croatian by WHGeeks. Thanks!
I plan to move much of this to https://github.com/vossenjp/ at some point…
The term “shell script” comes from UNIX, the DOS term is “batch files.” UNIX shell scripts are very powerful and flexible, they are essentially programming languages unto themselves. Windows or more rightfully DOS batch files are a pale imitation. However, sometimes you need to write something that will just work on any plain old out-of-the-box Windows install someone has–without adding all kinds of other tools.
Before getting too deep into this topic, consider if there is another tool you might use. Here is a list of tools, all of which are far more powerful, flexible and are probably easier to use than batch files:
If you follow Windows at all you will be aware that Powershell is Microsoft’s new command line tool, and that you will be required to use it more and more with newer Windows versions. That is a Good Thing, in my opinion, and it only took them about 20 years to realize, but that is not covered here! I don’t really do Windows anymore and I have not bothered to learn Powershell, which reminds me unpleasantly of Java’s verbose ugliness. So this page is somewhat historical, though most everything should work to at least Win7.
If you are interested in current Windows command line scripting and Powershell (and if you like Windows you should be), there are any number of other resources and books that will help. These are probably good but I haven’t read them:
Did you know that Window’s cmd.exe has file and directory name
completion, like UNIX shells? It does, and that can be amazingly useful.
But in most versions of Windows it’s not turned on by default. (I
believe it may be on in Windows 2003, but can’t swear to it.)
To enable file and directory name completion under Windows, download
this registry file and remove the
.txt, then double-click on it and answer yes to the question about
importing into the Registry. If you can’t download for some reason you
can copy the text below into a new file and import it, or just open
regedit, navigate to the key, and change the values for CompletionChar
and PathCompletionChar to 9. One you’ve done that, open a new command
prompt and type dir c:\win then hit the TAB key and watch what
happens. Of course the up arrow and other command line editing functions
will still work as always.
OK, if you are still going to go through with this, the first thing you need is Tim Hill’s Windows NT Shell Scripting, otherwise you don’t have a chance. For Windows 9x/ME, you are still toast, but for NT/2000 this book is really great. It’s the only way you can navigate the bazaar, inconsistent, contradictory and often asinine “scripting” language built into cmd.exe.
Using material from that book, plus my own almost 20 years experience with DOS batch files, I still had a hell of a time writing the following script. All it does is give you some basic file information (similar to UNIX stat) and tell you if a file will fit on a floppy disk.
Clicking on the name of a script will open that script in a new window.
Another of the many lacking tools is a simple “sleep” command, but you can easily fake that using the “ping” command of all things. The following will “sleep” for about 5 seconds, give or take:
Obviously you adjust the 5 as needed for the number of seconds. You can even write a trivial “sleep” function in your scripts:
This is trivially easy in UNIX. You want to copy a log file to a dated name? “cp mylog `date ‘+%Y-%m-%d’`-mylog” will copy mylog to 2002-11-27 (as of this writing). What could be easier? But in Windows, it sucks.
There are two basic ways to approach this, both with advantages and disadvantages. The native way is the “for” and “date /t” commands under NT/2000/XP. These do NOT work under Windows 9x and they do not consistently use 2 digit time fields, which totally screws you up if you need the time. The second way is to use the UNIX date command, then do whatever you please. This is very flexible, but requires you to download and have the executable ( date) handy. You will also want to rename it (I use udate.exe) so you don’t conflict with the built-in date command.
UPDATE (2012-07-26): All versions of Unix “date” commands that I have tested under both WinXP and Win7 have a bug that causes them to skip skip Mar-11 and/or Apr-04! That’s pretty annoying but has never been fixed as far as I know. Since it affects both tools I’ve tested (UnxUtils and GNU Win32), I suspect the Windows strftime lib is the problem. But I can’t prove it. And someone else replied to the bug he could not reproduce the problem. So I’d say it’s something I’m doing, but I find it off that both the WinXP I’ve been using forever and a much newer Win7 do the same thing.
UPDATE (2003-06-07): Here is a third way that’s trivial! It seems there are built-in but undocumented environment variables %time% and %date% in Windows 2000. I have not tested other platforms (let me know if you do). Due to the format, you can’t easily use the date in file copy operations (for example), but the time should be OK. And it’s by far the easiest option if you are just going to display (writing to a log file or something).
UPDATE (2006-05-11): Here is a another trivial way! Thanks to Richard Blake (RBlake {at} nea {DOT} org) for this great hack. In addition to the above %time% and %date% variables, there is a %VAR:offset,len% construct documented for the SET command, which works elsewhere. As above, the use of a two digit time code can mess you up, but for just the date it will work very well. Code to deal with non zero padded hours is left as an exercise for the reader.
This RedmondMag.com Backup Basics in Windows Server 2008 R2 article expands on the same method, but they are not portable because they depend on how your system time is displayed, and that will vary from machine to machine based on locale and user preference. For example, I loath any date/time format except for ISO8601 so I have my Windows formats set as close to that as possible, which then breaks the assumptions in the first block:
There are various tools like ask.exe and choice.exe that allow you to get input. There there’s an even easier, although undocumented, way: set /P. As in:
That prompts the user with “Your Prompt Here!” and puts whatever they type into %MyAnswer%. Very cool.
Except for FindZero.bat, all of these batch files will work under DOS, or any Windows.
Automatically Download a file using FTP (not secure!).
Format Floppy with no user prompts.
Requires ANSI.sys, included with DOS & Windows, or PC Magazine’s free AnsiCom.
Requires Microsoft Networking to be installed and active, and the DOS find command. If you have a UNIX find command in the path, you’ll probably get a “No such file or directory” error.
Something else that is taken for granted on UNIX is the ability to send e-mail from the command line or a script. As usual, windows makes this a challenge. There are a few free and commercial solutions for this, including but not limited to the following list (I’ve only ever used Blat):
I used to use MS FrontPage to maintain this site (don’t ask me why). My old ISP did not support FrontPage or its extensions, for excellent security reasons. Using FrontPage to create pages, then uploading them to a hosted site is a gigantic pain in the ass because of the way FrontPage keeps all of its proprietary information in various “_VTI_CNF” and other subdirectories. So simply zipping up the directories and dumping them onto a host is not ideal. So I came up with the following solution.
The old code I posted to a Netaxs news group was WRONG in places! THIS stuff works.
I don’t use this any more, I use Hugo and Relearn.
Open issues are:
This article is translated to Serbo-Croatian by WHGeeks. Thanks!
This content is old! It’s still useful, but it’s old, and there may be bit rot, newer/better tools or ways to do things. Sanity check and do your research.
This content is obsolete, but I am leaving it here as a historical reference.
Welcome to the Windows port of Logcheck (now called LogSentry), the famous UNIX log processing tool. Psionic was bought by Cisco who has moved the cool Abacus tools, including LogSentry, to http://sourceforge.net/projects/sentrytools/
As you probably know if you are bothering to read this, LogSentry helps spot problems and security violations in your logfiles automatically and will send the results to you in e-mail. However, it can only work with what it’s given. I personally find the Windows Event Logs to be verbose, yet un-informative. So when you get e-mailed messages with three or 4 lines on arcane gibberish, remember that it’s the same information as you would see in the Event Log, except it’s in a slightly different format, and you are actually SEEING it! (Of course, you would have reviewed the Event Logs anyway, right?) :-)
The beginning of each log entry contains the name of the Event Log, the date, and the time, like: “DIR,7/20/2001 11:52:12”. this is followed by the event details. The three letter codes for the Event Logs are:
It will help to look for those codes to isolate one event from the next, since your mailer will probably wrap the lines.
Also, the MS API for the Event Logs will return the application log if the Event Log being asked for does not exist. Once it returns the handle, there isn’t any way that DumpEvt can tell which log is being read. For example, if you dump RPL on an NT box, or DNS on W2k Pro it dumps the APP log instead. This causes duplicate entries in the capture file. The only work-around right now is to edit wrapper.cmd and REM out the Event Logs that do not exist on that machine.
While the actual logcheck.sh script could have been ported to the CMD.EXE shell, I thought it was much more efficient and effective to make as few changes to that as possible, and instead create a “wrapper” program to translate “Windows” into “UNIX.”
I gave the wrapper the highly imaginative and interesting name of “wrapper.cmd.” Note that it only runs under Windows NT and 2000 (and probably XP though I have not tested that). In short, logcheck is pointless without logs, which means the Event Logs. Since the Win9x series doesn’t have those…
See the “Tools-Readme.txt” for details about what tools are needed.
I struggled with the default directory location for a while. I *hate* programs that put themselves in the %SYSTEMROOT% (e.g. c:\winnt) directory. However, I wanted to be a little obscure, so I thought c:\etc was a little too obvious. And I wanted to be reliable, so “c:\Program Files” or “c:\Documents and Settings” were both too long, and had annoying spaces. C:\Progra~1 and c:\Docume~1 are not 100% reliable. So %SYSTEMROOT%\etc it is… Of course, you can change that if you want.
Note the word “secured” above. YOU need to set proper permissions on those directories so that the account under which logcheck runs as the proper access, but “Everyone” else does not. See the INSTALL-Windows.txt file for more details.
At the heart of logcheck are two basic things, grep and mail. If they fail, so will logcheck. While in the UNIX world you can assume the ability to send mail from the command line, in the Windows world you can’t. So make sure Blat is working. Again, see the INSTALL-Windows.txt file for more details.
This software is released under the GPLv2. See the included LICENSE file.
First, to Marcus J. Ranum and Fred Avolio for writing the deceptively simple yet brilliant original frequentcheck.sh for TIS Gauntlet.
Second, to Craig Rowland for applying the idea to system logs.
Third, to all the people who wrote or ported the tools I needed to get logcheck to run under Windows, notably K. M. Syring, the guys at Somarsoft, and Tim Charron.
Fourth, to G.P. and I.P. who sent me logs to test to help me tune the keyword files, and answered various other questions.
I should also mention the guys at Bastille Linux, whos style I’ve copied here a bit in the Setup program “interview.”
Finally, a few notes about other tools or resources that may be of interest, if you’ve managed to read this far.
I’ve found the following book to be essential in other Windows scripting projects, and it proved helpful in this project as well. For anyone who thinks you couldn’t possibly find enough material in the old DOS batch file language to write a book about it – that’s what I thought when I saw it. Check it out! While not nearly as powerful, flexible or easy to use (though some would argue that last) as UNIX shells, Windows’s CMD.EXE is actually a lot more powerful than you think. Forget about Command.com though…
Windows NT Shell Scripting, by Tim Hill New Riders Publishing, Paperback, Published April 1998, 377 pages, ISBN 1578700477
This content is obsolete, but I am leaving it here as a historical reference.