Archive for the ‘Command line’ Category

Changing the current directory to a UNC path in DOS

Monday, February 18th, 2008

A useful tip in DOS and for those batch files is how to change the current directory to a network drive. You can use the NET USE command to map a network drive, but that requires knowing an available drive letter… The commands PUSHD and POPD allow you to immediately jump to the UNC path, temporarily mapping the first available drive letter. Use the command as follows:

PUSHD \\SERVERNAME\SHARE

The current directory will be changed to the route directory of a temporarily mapped network drive pointing at the share mentioned in the command. Use POPD on its own to unmap the drive and return to wherever you were before the PUSHD command was used. If you used PUSHD again before POPD then you begin to stack the drive mappings, and you simply POPD for each one of them when you’re done. Run PUSHD without any parameters to see the current details of the stack.

See Microsoft’s support site for the PUSHD command here.

Launching Windows Explorer from an Admin Command Prompt

Friday, November 16th, 2007

This one just came up at work. You started command prompt running under an administrator account using runas and you want to invoke Windows Explorer with these elevated privileges. I was sure I’d done this before using explorer.exe but this didn’t seem to be working. After a quick search I found that start "." works, and you can replace the "." with whatever path you want Windows Explorer to open with.

Resize images from the command line

Monday, August 20th, 2007

The other day I needed to resize a few dozen images. I was using someone elses laptop and as I didn’t have Photoshop to hand I had to improvise. I had already downloaded GIMP and used it (for the first time) to do some simple cropping and cloning, and so decided to look into creating a script in much the same way as I would have used an action in PS. After a bit of reading online I discovered that the performance of GIMP scripting in Windows was less than ideal and came across another Wordpress blog that mentioned ImageMagick, which is free and allows image manipulation from the command line.

The mentioned blog entry contained a batch operation, which will convert all images in a specified directory. I have improved this command for use with files that contain spaces (as mine did).

for /f "tokens=*" delims= " %i in ('dir /b *.jpg') do convert "%i" -resize 50% "%i"

Note that if you are using the above in a batch file, variable names are indicated using double percent symbols (%%) instead of just one.

Starting Control Panels applications with admin rights

Tuesday, August 14th, 2007

If you’re a frequent user of the runas command in Windows - which allows you to run an application as another user without needed to log on interactively - then you might find these following tips handy.

In most cases the second user has increased privileges, for example you might run a program installer under an admin account because the standard user does not have rights to install new applications. I therefore find it handy to have a shortcut to run a command line (cmd) with admin rights, so that anything I run from that command line will also have the elevated rights. I would also reccommend modifying the colours of the text/background of the cmd sessions so you can immediately see when you’re running as admin. Obviously this can be just as ‘dangerous’ as logging into the machine as admin, so you should take care.

Another really handy tip is to become familar with the names of the Control Panel applications so you can quickly run these with the increased rights. The syntax of the runas command is below, followed by a list of common Control Panel (cpl) application names. A full list of these can be found here.

runas /user:<user> "control <appname>"

Replace <user> with the administrator account username and <appname> with one of the following:

  • Add/Remove Programs: appwiz.cpl
  • Display Properties: desk.cpl
  • Firewall Properties: firewall.cpl
  • Internet Options: inetcpl.cpl
  • Network Connections: ncpa.cpl
  • Regional and Language Options: intl.cpl
  • System Properties: sysdm.cpl

Avoid corporate proxy servers

Monday, August 6th, 2007

If like me you work for a company where a proxy server filters the kind of site you visit, you may find it useful to set up a local proxy through a trusted SSH server. In my work I often need to look at security sites, and read about the latest hacking techniques - but I’m almost always blocked because the site could be used for evil purposes. Alternatively, you can use this if you are using a wireless connection, and are concerned about someone hacking your gmail account. It makes perfect sense, but does require a SSH server you can trust.

Download OpenSSH and run the following:

ssh -D 8080 -f -C -q -N -p 22 username@my.server.com

Now configure your browser to use a socks proxy of 127.0.0.1 on port 8080.

All credit goes to Security Monkey with this post about Black Hat 2007.

Oh and I accept no responsibility for misuse.