Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


May 01, 2008

Securing the Windows Search Path

Lock it down with a script so an attacker can't escalate privileges
RSS
View this exclusive article with VIP access -- click here to join | See More Security Articles Here | Reprints | Or sign up for our VIP Monthly Pass!

Download the Code Here

Executive Summary:
When using the search path to resolve a command name, Windows attempts to match the command name to file-based commands in the folders in the path. If you don't specify a file extension for the command, Windows Explorer automatically searches for files with the extensions .com, .exe, .bat, or .cmd. This search process is useful but has some quirks. Applications—particularly older ones—might add their own directories to the search path, possibly ahead of Windows' intended first search folders. Malicious coders might also use pathext to make Windows identify common command names such as regedit with their application files. Tools such as Windows Vista's User Access Control (UAC) don't solve the search problem. If you're performing an administrative task and have already elevated your privileges in a subshell, invasive code will run with no special prompting. However, on systems that don't need significant command-search customization, you can do some tweaking to make it very difficult for an attacker to use command search to escalate privileges. The tweaking simply involves locking down the search path wherever possible, modifying and locking down the pathext variable, and maintaining security for the folders used in the search process.

The Windows search path represents a significant potential vulnerability on many systems. I'll explain why, then show you how you can minimize the vulnerability by identifying machines that don't need modified command search and locking them down.

Understanding the Search Path
When you run a command in Windows by specifying simply the name of the application—not its complete path—Windows has to search for the application. Windows Explorer, cmd.exe, and Windows PowerShell all have their own methods of attempting to resolve names before performing a search, but eventually they fall back on using the process environment to search for the command. Other shells might use the path and pathext variables to find the command.

The path variable is merely a list of complete paths to particular folders, each separated by a semicolon. When you open a command-prompt window and enter the command path, you get a list that looks like this on a freshly installed Windows system:

PATH=C:\Windows\system32; C:\Windows;C:\Windows\System32\Wbem 

When using the search path to resolve a command name, Windows attempts to match the command name to file-based commands in each of these folders in turn. If you don't specify a file extension for the command, Windows Explorer automatically searches for files with the extensions .com, .exe, .bat, or .cmd.

The cmd.exe shell instead uses the set of extensions in the environment variable pathext. The default value for pathext on a freshly installed Windows system is .com; .exe; .bat; .cmd; .vbs; .vbe; .js; .jse; .wsf; .wsh; .msc. So, for example, if cmd.exe is searching for a command named foo, cmd.exe first checks for C:\Windows\system32\foo.com; then C:\Windows\system32\foo.exe; and so on through C:\Windows\system32\foo.msc. If cmd.exe doesn't find the command, the same process continues with the folder C:\Windows and then C:\Windows\System32\Wbem.

This search process is useful but has some quirks. Applications— particularly older ones—might add their own directories to the search path, possibly ahead of Windows' intended first search folders. Malicious coders might also use pathext to make Windows identify common command names such as regedit with their application files. For example, Windows identifies a file named regedit.com in the System32 folder as the command regedit because the .com extension has precedence over .exe. Even legitimate applications can cause unintended complications due to collisions in application or DLL names.

Tools such as Windows Vista's User Access Control (UAC) don't solve the search problem. If you're performing an administrative task and have already elevated your privileges in a subshell, invasive code will run with no special prompting.

In short, if someone is able to compromise your system by putting malicious files on it, the Windows search process offers the attacker several attractive paths for escalating the compromise into system control. However, on systems that don't need significant command-search customization, you can do some tweaking to make it very difficult for an attacker to use command search to escalate privileges. The tweaking simply involves locking down the search path wherever possible, modifying and locking down the pathext variable, and maintaining security for the folders used in the search process.

Finding Systems You Can Lock Down
The first step is to determine the current details of the command-search process on client systems. The simplest general technique is to collect information about the path and pathext variables on every system on the network.

If you have familiar registry auditing tools available, you can use them to check the path and pathext value names in the Environment registry subkey, located in the registry path HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment. If you don't have such tools available, you can use the CheckCommandSearch script, one of several scripts that I've included with this article. To get the scripts and the downloadable general Group Policy template I created, click the Download the Code Here button at the top of this page (http://www.securityprovip.com/articles/articleid/98761/98761.html). To use the CheckCommandSearch script, you need to be able to access Windows Management Instrumentation (WMI) remotely on the machines you wish to audit.

The CheckCommandSearch.cmd script is a wrapper for CheckCommandSearch.vbs that ensures that CheckCommandSearch.vbs runs correctly within a command shell. Simply save the scripts to a folder. Create a file containing the names of computers you wish to check remotely, one name per line, and save it. Assuming you've saved the computer listing as computers.txt in the same folder as the scripts, just open a command-prompt window, change to the directory where you saved the scripts, and use this command:

CheckCommandSearch < computers.txt 

You can redirect output from the command to a file named CommandSearch.txt like this:

CheckCommandSearch < computers.txt > 
CommandSearch.txt

If the script can’t connect to a remote system, you'll see an error message similar to the following onscreen (even if you redirected output to a file): SomeServer: Error: could not connect to remote system.

CheckCommandSearch remotely checks the path and pathext values on each system. If it gets values back, it removes elements that match the defaults for path and pathext, then concatenates any leftover values and emits them on a single line, separated by a tab. If you checked a remote system named Server1 that had both PowerShell and Perl installed, for example, you would see output that looks similar to that shown in Figure 1. Values won’t line up cleanly on the screen, but a spreadsheet program such as Microsoft Excel can easily import the tab-delimited values into a readable table.

Besides quickly collecting data for several systems, one advantage of this script is that by removing the default search folders and searched extensions, you can more easily see what is unique to your LAN. You'll see that computers with standard search paths and extensions show no values next to their names. You'll be able to lock down the path variable of computers with no search path elements shown, with no loss in functionality, and you'll be able to lock down pathext on computers with no pathext elements. It's also worthwhile to investigate the machines that do show some unique elements; in many cases, the custom path or pathext elements might play no useful role.

Creating and Implementing Standard GPOs
The next step is to implement a standard set of policies for machines that need similar command-search configurations. The simplest solution for mass deployment is a custom Group Policy template providing drop-down item lists for each discrete configuration you need to apply to particular groups.

The downloadable Command Search Policy.adm file is a general Group Policy template that provides preconfigured policies for command search that you can apply to your LAN. It includes two possible choices for preconfigured path variables that you can apply to systems. The default Windows search path is the default and will show up in the Group Policy Editor (GPE) with the name Default Path. There’s also a custom configuration named IT Support that includes support for systems that have Windows PowerShell installed. Each configuration consists of the keyword NAME followed by the name that will be displayed in GPE for the policy; the optional keyword VALUE followed by the actual value that this option sets in the registry; and finally by the optional DEFAULT keyword indicating the default option that will be selected if this policy is enabled.

The template also includes options for enforced pathext values. The default choice in this template is a pathext value of .exe; .com; .bat; .cmd; .vbs; .js; .wsf; .wsh; .msc. This default differs in two minor ways from the Windows defaults. The first is that it assigns the .exe extension a higher priority than the .com extension. Since .exe has higher precedence, if you invoke regedit by name from a command prompt, a spoofing application named regedit.com in the same folder as regedit.exe will no longer be able to intercept the command. The second change is that I removed the .vbe and .jse extensions used for encoded VBScript and JScript files. Standard Windows tools don’t use the .vbe and .jse extensions, and administrators only rarely use them. These extensions have occasionally been popular for malicious scripts because the encoding makes it impossible to read a script without running a decoder on it first. Note, however, that I'm not turning off these extensions because they're particularly vulnerable to exploitation—they're no more vulnerable than any other extension. Removing the extensions is analogous to disabling an unused service or user account on a system.

Using Security Templates for Folders in the Search Path
The final step to improving command-search security is to ensure security on folders in the search path. The simplest way to secure the standard search path folders is to reapply default secure workstation and server security templates to systems you administer; this locks down the Windows directory and its subdirectories, preventing non-privileged accounts from saving files in those locations.

If you want to protect another folder in user paths, you need to create your own custom security template. If you haven't done this before, you might want to look at "Security Templates Define and Enforce the Rules" (http://windowsitpro.com/article/articleid/23375/security-templates-define-and-enforce-the-rules.html). In general, full control over folders in the search path should belong only to administrators, the local system account, and creator/owners. Other users will typically need read/execute/traversal permissions. After you create the template and apply it to a test system, make sure you log on as a user and test all applications that are affected by file system permissions you've restricted.

You can, of course, use security templates on systems on which you aren’t controlling the path and pathext variables with Group Policy. Using security templates lets you provide a measure of protection for specialty applications that might appear on only a few systems on your network, where path search needs more complexity than you can easily control with Group Policy. Although it's a little messy, you can even use a single generic security template to force a lockdown on all folders that appear in paths on any machines on your network. For example, if you secure C:\Python and C:\Tcl in a template and then apply the template to machines on which one or both of those folders is missing, you'll simply get warnings logged about the missing folders.

Understanding Search Path Control Effects and Limitations
Controlling the search path isn’t a front-line security measure. Instead, it helps prevent privilege escalation if a malicious user does successfully gain access to your network. Intercepting a trusted application using path search is an attractive strategy for newer exploits trying to get past UAC. Tighter path control not only provides you a second line of defense against such external threats but also makes malicious hacks by trusted internal users more difficult.

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Learning Path To Learn More About Scripts
"How to Get Started Writing Scripts"


To Learn More About VBScript
"Understanding VBScript"


To Learn More About Creating Security Templates
"Security Templates Define and Enforce the Rules"


Top Viewed ArticlesView all articles
Microsoft Misses Windows Mobile Sales Target

The warning signs were there. After boldly proclaiming that it would sell "more than" 20 million licenses to its Windows Mobile operating system by the end of its fiscal year on June 30, Microsoft later scaled that prediction back to "nearly" 20 million ...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

Microsoft: Midori is Not a Future Windows

As I've written previously here and mentioned in the "Windows Weekly" podcast, the oft-hyped-of-late "Midori" project that Microsoft is currently working on is not designed as an update to its current family of Windows operating systems. Midori has been ...


Related Articles Preventing Office from Opening Certain File Types

A Tool Harness

6 Steps to Secure Shared Computers

Combining LogParser and Sed

Security Whitepapers Anti-Virus Is Dead: The Advent of the Graylist Approach to Computer Protection

Getting the Job Done: Comparing Approaches for Desktop Software Lockdown

Instant Messaging, VoIP, P2P, and games in the workplace: How to take back control

Related Events Check out our list of Free Email Newsletters!

Security eBooks Spam Fighting and Email Security for the 21st Century

Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

Related Security Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

WinConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

Deploying SharePoint! In-Person Event Series – 8 Cities
Discover best practices and tips for deploying the perfect SharePoint infrastructure. Early Bird Price of $99 through Aug 29th.

Find a new job now on the all new IT Job Hound!
Search jobs, post your resume, and set up job e-mail alerts!

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Top Tools for Virtualization Disaster Recovery & Replication
View this web seminar on August 14th to learn about two tools that will result in faster backup and restore with P2V disaster recovery.

SharePointConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

VMworld 2008 - Sign Up Today!
Join your peers on September 15-18 at The Venetian Hotel in Las Vegas as VMware hosts VMworld 2008, the leading Virtualization event.



Microsoft® Tech•Ed EMEA 2008 IT Professionals
Advance your thinking with new ideas and practical real-world solutions at Microsoft’s FIVE day technical infrastructure conference 3-7 Nov., 2008. Register before 26 September 2008 to save €300.

What’s up with your network? Find out with ipMonitor
Availability monitoring for servers, applications and networks – FREE trial

Agent-less Remote Backup Service, Free 30 Day Trial
Award winning remote backup service at a competitive price with no min GB/month. Sign up Now!

Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.

Are You Really Compliant with Software Regulations?
View this web seminar that will help you with compliance best practices and check out a management solution to assure that you won’t be in jeopardy of an audit.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound
IT Library Technical Resources Directory Connected Home Windows Excavator SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing