Michael HowardSecure Windows Initiative
June 16, 2003
Summary: Michael Howard Outlines Some of The Security Changes Made to Microsoft Windows Server 2003 To Reduce Its Attack Surface, and Discusses How these Changes Impact The Software Developer. (9 Printed Pages)
Note The Security Changes Discussed in this Article Are in No Particular Order.
Is That Service You Rely ON Running?
One of the first things we did when developing Microsoft Windows Server ™ 2003 was to turn off less-commonly used services. If a service has a security bug, and it's not running by the default, then the exploit potential is dramatically reduced. The prime example is Internet Information Services (IIS). Many Web application applications build on IIS and their setup tools expect the Web server to be there-but guess what-by default it's not. You should determine your operating system dependencies, and then update your setup tool to inform the user if a dependency is not running. A good example is NetDDE. If your application uses NetDDE you'll need to (a) start the service because it's disabled by default or (b) migrate your application to a more modern Protocol Like, Say, Sockets!
IIS 6.0 Runs No User Code As System
By Default, All Worker Processes Run As a Network Service. Once Again, It's An in-Depth Defense Mechanism. Some Isapi Applications Perform The Following Steps:
Call RevertToself, Expecting The Resulting Process Identity to Be System. Do Some Tasks That Only System CAN Perform. Impersonate The User Again.
This scenario does not work on IIS 6.0 because step 2 fails since user code runs in much lower privilege than SYSTEM. So how do you fix this? The only truly secure way to fix it is to change the way your ISAPI works. Frankly, you should not process user's requests as SYSTEM, just in case your code is compromised. If you need to perform some request in elevated privilege, then create a service that runs with the required privileges and passes the request to the service over a pipe or socket ...................................................................................................................................................................
I already discussed this in detail in the March 2003 article Impersonation Issues, but it's worth repeating. In Windows Server 2003 impersonation is a privilege granted only to admins, service accounts and COM processes running as specific identities. If your application calls any impersonation function, including SetThreadToken, it may fail unless it has the SeImpersonatePrivilege privilege. In short, if your code does fail, the token returned to you will be an identify token, and not an impersonation function. Refer to my previous article for more information and remedies.
Creating Various Global Objects May Fail
Starting with Windows Server 2003, the creation of some global objects, such as file mapping will fail unless the calling process has the SeCreateGlobalPrivilege privilege enabled. Note that the privilege check is limited to the creating of said objects, and does not apply to opening existing Ones. for Example, The Following Code Will Fail on Windows Server 2003 Unless The Process Accent Has This Privilege:
Handle Hmap = CreateFilemapping (Invalid_Handle_Value, 0, Page_Readwrite, 0, 0xfff, "global // mymapping);
By default this privilege is assigned to all services and administrators. This privilege also applies when creating symbolic links in the object manager. The way around this is to either grant the account in question this privilege, or do not make the object name global. Of Course, The Latter May Not Be Possible.
DLL Search Order Has Changed
No longer is the current directory searched first when loading DLLs! This change was also made in Windows XP SP1. The default behavior now is to look in all the system locations first, then the current directory, and finally any user-defined paths. This will have an impact on your code if you install a DLL in the application's directory because Windows Server 2003 no longer loads the 'local' DLL if a DLL of the same name is in the system directory. A common example is if an application won ' T Run with a specific Version of A DLL, An Older Version IS Installed That Does Work In The Application Directory. This Scenario Will Fail in Windows Server 2003.
The reason this change was made was to mitigate some kinds of trojaning attacks. An attacker may be able to sneak a bad DLL into your application directory or a directory that has files associated with your application. The DLL search order change removes this attack vector.
The SetDllDirectory function, also available in Windows XP SP1, modifies the search path used to locate DLLs for the application and affects all subsequent calls to the LoadLibrary and LoadLibraryEx functions by the application.
Implications of security enhanced Internet Explorer
During the Windows Server 2003 security audit in December 2002, we asked the Internet Explorer team to evaluate the potential threats (I do not mean bugs or vulnerabilities-I mean avenues of attack and damage potential) when an administrator is browsing untrusted Internet-based Web sites from a computer acting as a domain controller. This led us to disable all mobile code capabilities in the browser including VBScript, JScript, ActiveX controls, Java, and .NET Framework components. The way the mobile code functionality is disabled is by setting very tight zone security policy and setting strict enhanced security settings. I'm not going to outline all the settings as they are available in the article Internet Explorer Enhanced Security Configuration. You should print this document out and read it a couple of times.So What Does this mean for you? That's a complex question, it depends on what you're doing. If you write Web Server code That Creates Web Pages Containing Mobile Code, T Hen The Client May Not Work Correctly. But this Is No Different from a security-conscious user disable, say, scripting.
In short, you should thoroughly test any application you use that relies on Internet Explorer technologies. If you find problems, the easiest fix is to change the browser settings to a more permissive setting, but I do not think that's necessarily the correct thing to DO for your customer, I would refomend you add the web server name to the trusted sites zone. please don't ask the administrator to loosen the settings unless there is a really, really good.
Secure root acls
We change the acls on the file system root to be much more secure Than Everyone (Full Control). The ACL IS NOW: Admin, Sys, Creator - Full Control
Everyone - Read / Execute
Users - Read / Execute
Create Folders / Append Data (This and Sub Folders)
Create Files / Write Data (Sub Folders)
If you want to understand why this change has been made, read Windows 2000 Default Permissions Could Allow Trojan Horse Program Essentially, this means that your application can not write to C:.. / Unless the user is an administrator So, unless you have good reason To Write to C: / don't do it.
Secure Share ACLS
. This is a favorite change of mine Shares are no longer Everyone: Full Control, but rather Everyone: Read This was changed as a direct consequence of the Nimda worm that wrote its payload to network shares on other computers Of course, if you.. .................................
Tighter Acls on Event Logs
We also tightened up the ACLs on the event logs to restrict what accounts can read and write to the logs Better still, the security of each log is configured locally through the values in the following registry key.:
HKEY_LOCAL_MACHINE / SYSTEM / CURRENTCONTROLSET / SERVICES / EVENTLOG
For Example, The Application Log Security Descriptor is configured THROUGH THE FOLLOWING Registry Value:
HKEY_LOCAL_MACHINE / SYSTEM / CURRENTCONTROLSET / Services / EVENTLOG / Application / Customsd
And The System Log Security Descriptor is configured through the folowing:
HKEY_LOCAL_MACHINE / SYSTEM / CURRENTCONTROLSET / SERVICES / EVENTLOG / SYSTEM / CUSTOMSD
The Security Descriptor for each log is specified by using Security Descriptor Definition Language (SDDL) syntax The following is an example from the Application event log:. O: BAG: SYD: (D ;; 0xf0007 ;;; AN) (D ;; 0xF0007 ;;; Bg) (a ;; 0xF0007 ;;; Sy) (A ;; 0x7 ;; BA)
(A ;; 0x7 ;;; SO) (a ;; 0x3 ;;; Iu) (a ;; 0x3 ;; Su) (a ;; 0x3 ;; S-1-5-3)
Quick, What is this ACL IN ENGLISH? You can read about sddl at http://msdn.microsoft.com/library/en-us/security/security/security_descriptor_string_format.asp. This String Means:
Entrymeaningo: Baobject Owner is Built-in Admin (ba) .g: Syprimary Group is system (SY) .d: this is a dacl, Rather Than an audit entry or sacl. (D ;; 0xf0007 ;;; an) de Nenonymous (AN) All Access. (D ;; 0xF0007 ;; (BG) DENY Built-in Guests (BG) All Access. (A ;; 0xf0005 ;; Sy) Allow System Read and Clear, Including delete, read_control, write_dac, And write_owner (indicated by the 0xf0000). (a ;; 0x7 ;; BA) Allow Built-in Admin Read, Write and Clear. (a ;; 0x7 ;; SO) Allow Server Operators Read, Write and Clear. A ;; 0x3 ;;; iu) allow intective users read and write. (A ;; 0x3 ;; ad) allow service account ion and write. (A ;; 0x3 ;;; S-1-5-3) allow Batch Accounts (S-1-5-3) Read and Write.
The Specific Event Log Access Mask Bits Are:
0x0001elf_logfile_readpermission to read log files. 0x0002elf_logfile_writepermission to write log files. 0x0004elf_logfile_clerPermission to Clear log files.
The only time you should see a failure in your application when writing to the event log is because of an ACL issue. Please do not relax the ACL too much. Add your own ACE to the SDDL string and then restart the Event Log service. For example, if your process runs under an account MyAccount, that has the SID S-1-5-21-853885456-2109860151-3743179773-1190, and you want the process to write to the Application log, simply add this string to the SDDL String in the registry: (a ;; 0x2 ;; S-1-5-21-853885456-2109860151-3743179773-1190)
Restrick Remote Execution of Console Applications
TO HELP MITIGATE BAD Guys Calling Into cmd.exe (AND Other Tools) Remotely, We Changed The ACL ON Some Critical Console Applications. Compare The Windows® 2000 ACL ON CMD.EXE with The ACL ON Windows Server 2003:
WINDOWS 2000
Administrators (Full Control)
SYSTEM (FULL Control)
Users (Read & Execute)
Windows Server 2003
Administrators (Full Control)
SYSTEM (FULL Control)
Interactive (Read & Execute)
Services (Read & Execute)
Now, this Probably Won't affect you, but you shop be aware of the acl change.
More restrictions on anonymous user
This should not affect too many applications, unless it makes anonymous connections to Windows Server 2003 computers to gather information. Essentially, we removed the Everyone SID out of the anonymous token. Now you must explicitly grant anonymous access through Policy settings. To do so, Load The Group Policy Object Snap-in, and Navigate To Computer Configuration -> Windows Settings -> Security Settings -> Local Policies -> Security Options.
In-Memory Encryption
Okay, this is hardly a security change that may cause your application to fail, but it's something you should be aware of, and frankly, it's one of my pet peeve features.We added two new functions to Windows Server 2003 that allow you to encrypt and decrypt secret data, such as passwords and cryptographic keys, in memory. This reduces the potential data exposure. The new functions are CryptProtectMemory and CryptUnprotectMemory. Note this functionality is also available in Windows 2000 SP3 and Windows XP using the RtlEncryptMemory and RtlDecryptMemory functions, but they have no associated import library. They are available as resources named SystemFunction040 and SystemFunction041 in Advapi32.dll. You must use the LoadLibrary and GetProcAddress functions to dynamically link to Advapi32.dll.
The Following C Class Code Shows How To Access The Functions from Windows 2000 and Windows XP:
#include "windows.h"
#include "strsafe.h"
#ifndef rtl_ENCRYPT_MEMORY_SIZE
# Define RTL_ENCRYPT_MEMORY_SIZE 16
# Define RTL_ENCRYPT_OPTION_SAME_PROCESS 0x00
# Define RTL_ENCRYPT_OPTION_CROSS_PROCESS 0x01
# Define RTL_ENCRYPT_OPTION_SAME_LOGON 0X02
#ENDIF
Class cmemcrypt {
PUBLIC:
Cmemcrypt () {
Char Szdir [MAX_PATH 1];
Uint cchdir = sizeof (szdir) / sizeof (SZDIR [0]);
IF (GetSystemDirectory (SZDIR, CCHDIR) == 0)
Throw getLastError ();
IF (Failed (StringCchcat (SZDIR, CCHDIR, "// advapi32.dll"))))
Throw strsafe_e_insuffect_buffer;
m_hmod = loadingLibrary (SZDIR);
IF (null == m_hmod)
Throw getLastError ();
M_PEM = (EM) (M_HMOD, "SystemFunction040");
M_PDM = (DM) (GetProcadDress (M_HMOD, "SystemFunction041)); if (! m_pem ||! m_pdm)
Throw getLastError ();
}
Virtual ~ cmemcrypt () {
IF (m_hmod)
Freelibrary (M_HMOD);
m_pem = m_pdm = null;
m_hmod = NULL;
}
DWORD ENCRYPT (PVOID PMEM, ULONG CBMEM, ULONG FLAGS) {
Return (M_PEM) (PMEM, CBMEM, FLAGS);
}
DWORD Decrypt (PVOID PMEM, ULONG CBMEM, ULONG FLAGS) {
Return (M_PDM) (PMEM, CBMEM, FLAGS);
}
Private:
HModule M_HMOD;
Typedef long (__stdcall * em) (pvoid, ulong, ulong);
Typedf long (__stdcall * dm) (PVOID, ULONG, ULONG);
EM M_PEM;
DM m_pdm;
}
// Example Test Code
INT Main (int Argc, char * argv []) {
CHAR P [16 1];
Memcpy (p, "0123456789123456", 16);
P [16] = 0;
CMemcrypt M;
IF (M.Encrypt (P, 16, 0))
M. Decrypt (p, 16, 0);
Return 0;
}
A good starting point for more information regarding these functions is http://msdn.microsoft.com/library/en-us/security/security/rtlencryptmemory.asp.
Summary
In short, you should actively test your applications on a clean install of Windows Server 2003 to verify if any of the security changes affect your application. You can troubleshoot many security failures using the event log. In the case of ACL issues this will involve setting An Audit Ace On The Resource In Question, And Auditing for Object Access. for Privilege Issues, Make Sure You're Auditing for Faled Privilege.
Spot The Security Flaw
A bunch of people worked out my last error-it was a simple SQL injection attack. You can piggyback other SQL statements in the Id parameter, and thanks to the wonders of string concatenation, end up with a SQL query that does more than you expected , such as deleting database files, adding new user accounts, gathering credit card details, and so on. The remedy is to use SQL parameters, not string concatenation.There are also a couple of other flaws people did not pick up on, including connecting as the sysadmin account. Since when did you need to be sysadmin to query a table? Also, the code embeds a difficult-to-guess password in the code. do not do that, ever! Finally, if the code fails for Some Reason, The Code in Exception Handler Tells The Attacker Exactly What Faled.
Okay, ONTO the next error. What's up with this c # code?
// restrict access to only non-file requests
// for example http: // is valid, file: // is not
IF (String.Compare ("File", 0, Url.toupper (), 0, 4)! = 0)
AccessSensitiveStuff (URL);
Else
Goaway (401);
I'll Give a hint in a week or su. You'll Probably Need IT!
Michael Howard is a Senior Security Program Manager in the Secure Windows Initiative group at Microsoft and is the coauthor of Writing Secure Code, now in its second edition, and the main author of Designing Secure Web-based Applications for Windows 2000. His main focus in Life is Making Sure People Design, Build, Test, and Document Nothing Short of A Secure System. His Favorite Line IS "One Person's Feature IS ANOTHER's Exploit."