SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Secure Programming
in C#
Siddharth Bezalwar
@be_siddharth
siddharth.bezalwar@gmail.com
Agenda
● Common mistakes(Insecure coding practice).
● Illustrations based on OWASP Top 10 Web
vulnerabilities.
● Secure code practices.
Secure Coding?
Developing practice to guard against the
accidental introduction of vulnerabilities.
Quick Look
C #
– Simple, modern, general-purpose, object-oriented
programming language.
– Developed by Microsoft within its .NET initiative
led by Anders Hejlsberg.
– Very much based on C and C++ programming
language
Vulnerabilities
OWASP Top 10 2013 Vulnerabilities
– A1-Injection(SQL Injection)
– A2- Broken Authentication And Session Mgt.
(Password Storage)
– A3-Cross-site scripting
– A5-Security Misconfiguration
– A8-CSRF
A1-Injection(SQL Injection)
SQL injection is a code injection technique,
used to attack data-driven applications, in
which nefarious SQL statements are inserted
into an entry field for execution (e.g. to dump
the database contents to the attacker).
Vulnerable Code
Normal input:
SELECT * FROM ProductDB where id =' 1 ' AND name=' XYZ ' and cost=' 123 ';
Malicious input('or'='1'='1):
SELECT * FROM ProductDB where id=' 1’or'1’='1 ' AND name = ' XYZ 'or'1'='1 ' AND
cost =' 123'or'1'='1 ';
Incorrect Mitigation
● Client side validations.
● Blacklisting of SQL keywords
● Checking number of rows returned.
Secure Code
Parameterized sql query and it’s working:
• Parameters i.e. user inputs are never inserted directly into the
statement.
• A system stored procedure called sp_executesql is called with
given SQL statement and parameters.
• Parameters are treated as data instead of parsing out as a SQL
statement string.
Leaks or flaws in the authentication or session
management functions (e.g., exposed
accounts, passwords, session IDs) to
impersonate users. Developers frequently
build custom authentication and session
management schemes, but building these
correctly is hard.
A2- Broken Authentication and
Session Management.
Secure Implementation
● Do not store passwords in plain text.
● Don't attempt to implement your own hashing schemes, use strong and valid,
time proven and tested cryptography algorithms such as ASP.NET's Identity (be
aware of the low 1000 iteration count).
● For scenario's where implementation is required, use a unique salt with a high
level of entropy with each password hash. Hash with a valid hashing algorithm
such as PBKDF2 and Bcrypt with a high level of hashing rounds.
● https://cmatskas.com/-net-password-hashing-using-pbkdf2/
Password Storage:
Wacky Hash Functions
● md5(sha1(password))
● md5(md5(salt) + md5(password))
● sha1(sha1(password))
● sha1(str_rot13(password + salt))
● md5(sha1(md5(md5(password) +
sha1(password)) + md5(password)))
A3-Cross-site Scripting
Cross-site scripting (XSS) is a type of
computer security vulnerability typically found
in web applications. XSS enables attackers to
inject client-side scripts into web pages viewed
by other users. A cross-site scripting
vulnerability may be used by attackers to
bypass access controls such as the same-
origin policy.
Vuln. Code (Reflected)
• Sanitization(encoding) of user input is missing.
• User’s input is included in web page and treated as code
by the victim’s browser.
User Input:
<script>alert(‘Hacked’)</script>
Secure Implementation
● ValidateRequest="true"
– rejects the input because it includes potentially dangerous HTML characters.
On .aspx file
<%@ Page Language="C#" ValidateRequest="true" %>
● Encode HTML Output
– Server.HmlEncode(HttpServerUtility)
– HttpUtility.HtmlEncode
● Encode URL Output
– Server.UrlEncode(HttpServerUtility)
– HttpUtility.UrlEncode
Secure Implementation contd.
To safely allow restricted HTML input
● Disable ASP.NET request validation by the adding the
ValidateRequest="false" attribute to the @ Page directive.
● Encode the string input with the HtmlEncode method.
● Use a StringBuilder and call its Replace method to selectively
remove the encoding on the HTML elements that you want to permit
Secure Implementation Contd.
HTML-encoding of user input.
Vuln. Code (DOM)
HTMLcontent is set without validation and sanitization.
Secure Code
Creates text node and appends it to the DOM element.
• HTML escape then JavaScript escape in HTML subcontext.
<%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%>
• URL escape then JavaScript escape in URL attribute subcontext.
<%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%>
• JavaScript escape in HTML and CSS attribute context.
For HTML attribute ,escape the untrusted input and then set the attribute of DOM
element.
<%=Encoder.encodeForJS(untrustedData)%>
For CSS attribute
document.body.style.backgroundImage = "url(<
%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%>)"
Secure Implementation
Use ESAPI ( https://www.owasp.org/index.php/ESAPI )
A5-Security Misconfiguration
Security Misconfiguration arises when Security
settings are defined, implemented, and
maintained as defaults. Good security requires
a secure configuration defined and deployed
for the application, web server, database
server, and platform. It is equally important to
have the software up to date.
Web.Config File
Debug settings:
<compilation debug="false" targetFramework="4.5"/>
Request Processing:
<httpRuntime enableVersionHeader="false" requestValidationMode="4.0" />
Cookie Settings:
<httpCookies httpOnlyCookies="true" requireSSL="true" lockItem="true"/>
Trace Settings:
<trace enabled="false"/>
Web Application settings (<system.web>)
Directory Browsing Setting:
<directoryBrowse enabled="false"/>
Web server settings (<system.webServer>)
Custom Header Setting:
The <customHeaders> element of the <httpProtocol> element specifies
custom HTTP headers
Web.Config File
<httpProtocol>
<customHeaders>
<remove name="Access-Control-Allow-Origin"/>
<add name="Access-Control-Allow-Origin" value="http://domain.com"/>
<remove name="X-Powered-By"/>
<remove name="X-Frame-Options"/>
<add name="X-Frame-Options" value="SAMEORIGIN"/>
<remove name="X-Content-Type-Options"/>
<add name="X-Content-Type-Options" value="nosniff"/>
<remove name="X-XSS-Protection"/>
<add name="X-XSS-Protection" value="1; mode=block"/>
<remove name="X-Strict-Transport-Security"/>
<add name="X-Strict-Transport-Security" value="max-age=15768000; includeSubDomains"/>
<remove name="X-Content-Security-Policy"/>
<add name="X-Content-Security-Policy" value="default-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline
fonts.googleapis.com; font-src 'self' fonts.gstatic.com;"/>
<remove name="X-WebKit-CSP"/>
<add name="X-WebKit-CSP" value="default-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'
fonts.googleapis.com; font-src 'self' fonts.gstatic.com;"/>
</customHeaders>
</httpProtocol>
A8- Cross-site request forgery
● Cross-Site Request Forgery (CSRF) is an
attack that forces an end user to execute
unwanted actions on a web application in
which they're currently authenticated. CSRF
attacks specifically target state-changing
requests, not theft of data, since the attacker
has no way to see the response to the forged
request.
Wrong Assumptions
● Assuming that SSL/TLS will thwart CSRF attacks
just because the cookie is marked "Secure"
and/or "HTTPOnly"
● Referer header verification as the only protection
● Any CSRF protection is null and void given the
presence of XSS
● Cookie double-submission when the cookie
utilized is the session cookie.
Secure Implementation
Use Anti-Forgery Tokens
1.Generate the security token (or grab it from the session state) and send the token as a
session cookie (again, managed in the session state, unique per session) as well as within
a hidden value in each form.
2.Once the user submits the form, validate the token stored in the session state against
the token included in the submitted form value. On failure, disregard form.
Rendering token as a hidden field on aspx page.
Secure Implementation
Secure Implementation
Method for generating random token and response handling
Secure Implementation
Generating token and saving it in session
Secure Implementation
Validating token received from request against the token saved in session
state
Thank you!!!

Weitere ähnliche Inhalte

Was ist angesagt?

Security Code Review 101
Security Code Review 101Security Code Review 101
Security Code Review 101Paul Ionescu
 
OWASP Top 10 Web Application Vulnerabilities
OWASP Top 10 Web Application VulnerabilitiesOWASP Top 10 Web Application Vulnerabilities
OWASP Top 10 Web Application VulnerabilitiesSoftware Guru
 
Web and Mobile Application Security
Web and Mobile Application SecurityWeb and Mobile Application Security
Web and Mobile Application SecurityPrateek Jain
 
Secure coding practices
Secure coding practicesSecure coding practices
Secure coding practicesScott Hurrey
 
Applications secure by default
Applications secure by defaultApplications secure by default
Applications secure by defaultSecuRing
 
XSS - Do you know EVERYTHING?
XSS - Do you know EVERYTHING?XSS - Do you know EVERYTHING?
XSS - Do you know EVERYTHING?Yurii Bilyk
 
OWASP Top 10 2021 Presentation (Jul 2022)
OWASP Top 10 2021 Presentation (Jul 2022)OWASP Top 10 2021 Presentation (Jul 2022)
OWASP Top 10 2021 Presentation (Jul 2022)TzahiArabov
 
Security in CI/CD Pipelines: Tips for DevOps Engineers
Security in CI/CD Pipelines: Tips for DevOps EngineersSecurity in CI/CD Pipelines: Tips for DevOps Engineers
Security in CI/CD Pipelines: Tips for DevOps EngineersDevOps.com
 
Introduction to path traversal attack
Introduction to path traversal attackIntroduction to path traversal attack
Introduction to path traversal attackPrashant Hegde
 
OK Google, How Do I Red Team GSuite?
OK Google, How Do I Red Team GSuite?OK Google, How Do I Red Team GSuite?
OK Google, How Do I Red Team GSuite?Beau Bullock
 
OWASP API Security Top 10 - API World
OWASP API Security Top 10 - API WorldOWASP API Security Top 10 - API World
OWASP API Security Top 10 - API World42Crunch
 
Cross Site Scripting ( XSS)
Cross Site Scripting ( XSS)Cross Site Scripting ( XSS)
Cross Site Scripting ( XSS)Amit Tyagi
 
Security Code Review for .NET - Sherif Koussa (OWASP Ottawa)
Security Code Review for .NET - Sherif Koussa (OWASP Ottawa)Security Code Review for .NET - Sherif Koussa (OWASP Ottawa)
Security Code Review for .NET - Sherif Koussa (OWASP Ottawa)OWASP Ottawa
 
Secure coding guidelines
Secure coding guidelinesSecure coding guidelines
Secure coding guidelinesZakaria SMAHI
 
Dom based xss
Dom based xssDom based xss
Dom based xssLê Giáp
 
Polyglot payloads in practice by avlidienbrunn at HackPra
Polyglot payloads in practice by avlidienbrunn at HackPraPolyglot payloads in practice by avlidienbrunn at HackPra
Polyglot payloads in practice by avlidienbrunn at HackPraMathias Karlsson
 

Was ist angesagt? (20)

Security Code Review 101
Security Code Review 101Security Code Review 101
Security Code Review 101
 
OWASP Top 10 Web Application Vulnerabilities
OWASP Top 10 Web Application VulnerabilitiesOWASP Top 10 Web Application Vulnerabilities
OWASP Top 10 Web Application Vulnerabilities
 
Web and Mobile Application Security
Web and Mobile Application SecurityWeb and Mobile Application Security
Web and Mobile Application Security
 
Secure coding-guidelines
Secure coding-guidelinesSecure coding-guidelines
Secure coding-guidelines
 
Secure coding practices
Secure coding practicesSecure coding practices
Secure coding practices
 
Applications secure by default
Applications secure by defaultApplications secure by default
Applications secure by default
 
Secure PHP Coding
Secure PHP CodingSecure PHP Coding
Secure PHP Coding
 
XSS - Do you know EVERYTHING?
XSS - Do you know EVERYTHING?XSS - Do you know EVERYTHING?
XSS - Do you know EVERYTHING?
 
OWASP Top 10 2021 Presentation (Jul 2022)
OWASP Top 10 2021 Presentation (Jul 2022)OWASP Top 10 2021 Presentation (Jul 2022)
OWASP Top 10 2021 Presentation (Jul 2022)
 
Security in CI/CD Pipelines: Tips for DevOps Engineers
Security in CI/CD Pipelines: Tips for DevOps EngineersSecurity in CI/CD Pipelines: Tips for DevOps Engineers
Security in CI/CD Pipelines: Tips for DevOps Engineers
 
Kheirkhabarov24052017_phdays7
Kheirkhabarov24052017_phdays7Kheirkhabarov24052017_phdays7
Kheirkhabarov24052017_phdays7
 
Introduction to path traversal attack
Introduction to path traversal attackIntroduction to path traversal attack
Introduction to path traversal attack
 
OK Google, How Do I Red Team GSuite?
OK Google, How Do I Red Team GSuite?OK Google, How Do I Red Team GSuite?
OK Google, How Do I Red Team GSuite?
 
OWASP API Security Top 10 - API World
OWASP API Security Top 10 - API WorldOWASP API Security Top 10 - API World
OWASP API Security Top 10 - API World
 
Owasp Top 10 A1: Injection
Owasp Top 10 A1: InjectionOwasp Top 10 A1: Injection
Owasp Top 10 A1: Injection
 
Cross Site Scripting ( XSS)
Cross Site Scripting ( XSS)Cross Site Scripting ( XSS)
Cross Site Scripting ( XSS)
 
Security Code Review for .NET - Sherif Koussa (OWASP Ottawa)
Security Code Review for .NET - Sherif Koussa (OWASP Ottawa)Security Code Review for .NET - Sherif Koussa (OWASP Ottawa)
Security Code Review for .NET - Sherif Koussa (OWASP Ottawa)
 
Secure coding guidelines
Secure coding guidelinesSecure coding guidelines
Secure coding guidelines
 
Dom based xss
Dom based xssDom based xss
Dom based xss
 
Polyglot payloads in practice by avlidienbrunn at HackPra
Polyglot payloads in practice by avlidienbrunn at HackPraPolyglot payloads in practice by avlidienbrunn at HackPra
Polyglot payloads in practice by avlidienbrunn at HackPra
 

Andere mochten auch

Secure programming language basis
Secure programming language basisSecure programming language basis
Secure programming language basisAnkita Bhalla
 
"CERT Secure Coding Standards" by Dr. Mark Sherman
"CERT Secure Coding Standards" by Dr. Mark Sherman"CERT Secure Coding Standards" by Dr. Mark Sherman
"CERT Secure Coding Standards" by Dr. Mark ShermanRinaldi Rampen
 
Deploying Static Application Security Testing on a Large Scale
Deploying Static Application Security Testing on a Large ScaleDeploying Static Application Security Testing on a Large Scale
Deploying Static Application Security Testing on a Large ScaleAchim D. Brucker
 
Security asp.net application
Security asp.net applicationSecurity asp.net application
Security asp.net applicationZAIYAUL HAQUE
 
Microsoft asp.net identity security
Microsoft asp.net identity  securityMicrosoft asp.net identity  security
Microsoft asp.net identity securityrustd
 
Code review for secure web applications
Code review for secure web applicationsCode review for secure web applications
Code review for secure web applicationssilviad74
 
3Es of Ransomware
3Es of Ransomware3Es of Ransomware
3Es of RansomwareSunil Kumar
 
Http2 Security Perspective
Http2 Security PerspectiveHttp2 Security Perspective
Http2 Security PerspectiveSunil Kumar
 
Beefing Up Security In ASP.NET Dot Net Bangalore 3rd meet up on May 16 2015
Beefing Up Security In ASP.NET Dot Net Bangalore 3rd meet up on May 16 2015Beefing Up Security In ASP.NET Dot Net Bangalore 3rd meet up on May 16 2015
Beefing Up Security In ASP.NET Dot Net Bangalore 3rd meet up on May 16 2015gmaran23
 
Beefing Up Security In ASP.NET Part 2 Dot Net Bangalore 4th meet up on August...
Beefing Up Security In ASP.NET Part 2 Dot Net Bangalore 4th meet up on August...Beefing Up Security In ASP.NET Part 2 Dot Net Bangalore 4th meet up on August...
Beefing Up Security In ASP.NET Part 2 Dot Net Bangalore 4th meet up on August...gmaran23
 
Security certifications
Security certificationsSecurity certifications
Security certificationsManas Deep
 
Practical Security Testing for Developers using OWASP ZAP at Dot Net Bangalor...
Practical Security Testing for Developers using OWASP ZAP at Dot Net Bangalor...Practical Security Testing for Developers using OWASP ZAP at Dot Net Bangalor...
Practical Security Testing for Developers using OWASP ZAP at Dot Net Bangalor...gmaran23
 
Simplified Security Code Review Process
Simplified Security Code Review ProcessSimplified Security Code Review Process
Simplified Security Code Review ProcessSherif Koussa
 
Secure Coding - Web Application Security Vulnerabilities and Best Practices
Secure Coding - Web Application Security Vulnerabilities and Best PracticesSecure Coding - Web Application Security Vulnerabilities and Best Practices
Secure Coding - Web Application Security Vulnerabilities and Best PracticesWebsecurify
 

Andere mochten auch (20)

Python Securidad and Criptografia
Python Securidad and CriptografiaPython Securidad and Criptografia
Python Securidad and Criptografia
 
Secure programming language basis
Secure programming language basisSecure programming language basis
Secure programming language basis
 
"CERT Secure Coding Standards" by Dr. Mark Sherman
"CERT Secure Coding Standards" by Dr. Mark Sherman"CERT Secure Coding Standards" by Dr. Mark Sherman
"CERT Secure Coding Standards" by Dr. Mark Sherman
 
Deploying Static Application Security Testing on a Large Scale
Deploying Static Application Security Testing on a Large ScaleDeploying Static Application Security Testing on a Large Scale
Deploying Static Application Security Testing on a Large Scale
 
Security asp.net application
Security asp.net applicationSecurity asp.net application
Security asp.net application
 
Microsoft asp.net identity security
Microsoft asp.net identity  securityMicrosoft asp.net identity  security
Microsoft asp.net identity security
 
Code review for secure web applications
Code review for secure web applicationsCode review for secure web applications
Code review for secure web applications
 
Buffer overflow null
Buffer overflow nullBuffer overflow null
Buffer overflow null
 
3Es of Ransomware
3Es of Ransomware3Es of Ransomware
3Es of Ransomware
 
Http2 Security Perspective
Http2 Security PerspectiveHttp2 Security Perspective
Http2 Security Perspective
 
Beefing Up Security In ASP.NET Dot Net Bangalore 3rd meet up on May 16 2015
Beefing Up Security In ASP.NET Dot Net Bangalore 3rd meet up on May 16 2015Beefing Up Security In ASP.NET Dot Net Bangalore 3rd meet up on May 16 2015
Beefing Up Security In ASP.NET Dot Net Bangalore 3rd meet up on May 16 2015
 
Null meet Code Review
Null meet Code ReviewNull meet Code Review
Null meet Code Review
 
Beefing Up Security In ASP.NET Part 2 Dot Net Bangalore 4th meet up on August...
Beefing Up Security In ASP.NET Part 2 Dot Net Bangalore 4th meet up on August...Beefing Up Security In ASP.NET Part 2 Dot Net Bangalore 4th meet up on August...
Beefing Up Security In ASP.NET Part 2 Dot Net Bangalore 4th meet up on August...
 
Security certifications
Security certificationsSecurity certifications
Security certifications
 
Practical Security Testing for Developers using OWASP ZAP at Dot Net Bangalor...
Practical Security Testing for Developers using OWASP ZAP at Dot Net Bangalor...Practical Security Testing for Developers using OWASP ZAP at Dot Net Bangalor...
Practical Security Testing for Developers using OWASP ZAP at Dot Net Bangalor...
 
ASP.NET Core Security
ASP.NET Core SecurityASP.NET Core Security
ASP.NET Core Security
 
Simplified Security Code Review Process
Simplified Security Code Review ProcessSimplified Security Code Review Process
Simplified Security Code Review Process
 
Web Application Firewall
Web Application FirewallWeb Application Firewall
Web Application Firewall
 
ASP.NET Web Security
ASP.NET Web SecurityASP.NET Web Security
ASP.NET Web Security
 
Secure Coding - Web Application Security Vulnerabilities and Best Practices
Secure Coding - Web Application Security Vulnerabilities and Best PracticesSecure Coding - Web Application Security Vulnerabilities and Best Practices
Secure Coding - Web Application Security Vulnerabilities and Best Practices
 

Ähnlich wie Secure C# Coding Practices

04. xss and encoding
04.  xss and encoding04.  xss and encoding
04. xss and encodingEoin Keary
 
Top 10 Web Security Vulnerabilities
Top 10 Web Security VulnerabilitiesTop 10 Web Security Vulnerabilities
Top 10 Web Security VulnerabilitiesCarol McDonald
 
Top Ten Java Defense for Web Applications v2
Top Ten Java Defense for Web Applications v2Top Ten Java Defense for Web Applications v2
Top Ten Java Defense for Web Applications v2Jim Manico
 
(Don't) Go Tracing Server Calls
(Don't) Go Tracing Server Calls(Don't) Go Tracing Server Calls
(Don't) Go Tracing Server CallsBrandon Hunter
 
2013 OWASP Top 10
2013 OWASP Top 102013 OWASP Top 10
2013 OWASP Top 10bilcorry
 
CS166 Final project
CS166 Final projectCS166 Final project
CS166 Final projectKaya Ota
 
Dmytro Kochergin - "The OWASP TOP 10 - Typical Attacks on Web Applications an...
Dmytro Kochergin - "The OWASP TOP 10 - Typical Attacks on Web Applications an...Dmytro Kochergin - "The OWASP TOP 10 - Typical Attacks on Web Applications an...
Dmytro Kochergin - "The OWASP TOP 10 - Typical Attacks on Web Applications an...LogeekNightUkraine
 
Web security: Securing untrusted web content at browsers
Web security: Securing untrusted web content at browsersWeb security: Securing untrusted web content at browsers
Web security: Securing untrusted web content at browsersPhú Phùng
 
Hacking Client Side Insecurities
Hacking Client Side InsecuritiesHacking Client Side Insecurities
Hacking Client Side Insecuritiesamiable_indian
 
Application Security around OWASP Top 10
Application Security around OWASP Top 10Application Security around OWASP Top 10
Application Security around OWASP Top 10Sastry Tumuluri
 
Web security for developers
Web security for developersWeb security for developers
Web security for developersSunny Neo
 
XSS Primer - Noob to Pro in 1 hour
XSS Primer - Noob to Pro in 1 hourXSS Primer - Noob to Pro in 1 hour
XSS Primer - Noob to Pro in 1 hoursnoopythesecuritydog
 
Neoito — Secure coding practices
Neoito — Secure coding practicesNeoito — Secure coding practices
Neoito — Secure coding practicesNeoito
 
JavaScript - Chapter 3 - Introduction
 JavaScript - Chapter 3 - Introduction JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 3 - IntroductionWebStackAcademy
 

Ähnlich wie Secure C# Coding Practices (20)

04. xss and encoding
04.  xss and encoding04.  xss and encoding
04. xss and encoding
 
Top 10 Web Security Vulnerabilities
Top 10 Web Security VulnerabilitiesTop 10 Web Security Vulnerabilities
Top 10 Web Security Vulnerabilities
 
Top Ten Java Defense for Web Applications v2
Top Ten Java Defense for Web Applications v2Top Ten Java Defense for Web Applications v2
Top Ten Java Defense for Web Applications v2
 
Web Apps Security
Web Apps SecurityWeb Apps Security
Web Apps Security
 
(Don't) Go Tracing Server Calls
(Don't) Go Tracing Server Calls(Don't) Go Tracing Server Calls
(Don't) Go Tracing Server Calls
 
Web Security 101
Web Security 101Web Security 101
Web Security 101
 
2013 OWASP Top 10
2013 OWASP Top 102013 OWASP Top 10
2013 OWASP Top 10
 
CS166 Final project
CS166 Final projectCS166 Final project
CS166 Final project
 
Dmytro Kochergin - "The OWASP TOP 10 - Typical Attacks on Web Applications an...
Dmytro Kochergin - "The OWASP TOP 10 - Typical Attacks on Web Applications an...Dmytro Kochergin - "The OWASP TOP 10 - Typical Attacks on Web Applications an...
Dmytro Kochergin - "The OWASP TOP 10 - Typical Attacks on Web Applications an...
 
Web security: Securing untrusted web content at browsers
Web security: Securing untrusted web content at browsersWeb security: Securing untrusted web content at browsers
Web security: Securing untrusted web content at browsers
 
Hacking Client Side Insecurities
Hacking Client Side InsecuritiesHacking Client Side Insecurities
Hacking Client Side Insecurities
 
Application Security around OWASP Top 10
Application Security around OWASP Top 10Application Security around OWASP Top 10
Application Security around OWASP Top 10
 
Web vulnerabilities
Web vulnerabilitiesWeb vulnerabilities
Web vulnerabilities
 
Web security for developers
Web security for developersWeb security for developers
Web security for developers
 
21 05-2018
21 05-201821 05-2018
21 05-2018
 
New web attacks-nethemba
New web attacks-nethembaNew web attacks-nethemba
New web attacks-nethemba
 
Web Security
Web SecurityWeb Security
Web Security
 
XSS Primer - Noob to Pro in 1 hour
XSS Primer - Noob to Pro in 1 hourXSS Primer - Noob to Pro in 1 hour
XSS Primer - Noob to Pro in 1 hour
 
Neoito — Secure coding practices
Neoito — Secure coding practicesNeoito — Secure coding practices
Neoito — Secure coding practices
 
JavaScript - Chapter 3 - Introduction
 JavaScript - Chapter 3 - Introduction JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 3 - Introduction
 

Kürzlich hochgeladen

SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 

Kürzlich hochgeladen (20)

Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Odoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting ServiceOdoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting Service
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 

Secure C# Coding Practices

  • 1. Secure Programming in C# Siddharth Bezalwar @be_siddharth siddharth.bezalwar@gmail.com
  • 2. Agenda ● Common mistakes(Insecure coding practice). ● Illustrations based on OWASP Top 10 Web vulnerabilities. ● Secure code practices.
  • 3. Secure Coding? Developing practice to guard against the accidental introduction of vulnerabilities.
  • 4. Quick Look C # – Simple, modern, general-purpose, object-oriented programming language. – Developed by Microsoft within its .NET initiative led by Anders Hejlsberg. – Very much based on C and C++ programming language
  • 5. Vulnerabilities OWASP Top 10 2013 Vulnerabilities – A1-Injection(SQL Injection) – A2- Broken Authentication And Session Mgt. (Password Storage) – A3-Cross-site scripting – A5-Security Misconfiguration – A8-CSRF
  • 6. A1-Injection(SQL Injection) SQL injection is a code injection technique, used to attack data-driven applications, in which nefarious SQL statements are inserted into an entry field for execution (e.g. to dump the database contents to the attacker).
  • 7. Vulnerable Code Normal input: SELECT * FROM ProductDB where id =' 1 ' AND name=' XYZ ' and cost=' 123 '; Malicious input('or'='1'='1): SELECT * FROM ProductDB where id=' 1’or'1’='1 ' AND name = ' XYZ 'or'1'='1 ' AND cost =' 123'or'1'='1 ';
  • 8. Incorrect Mitigation ● Client side validations. ● Blacklisting of SQL keywords ● Checking number of rows returned.
  • 9. Secure Code Parameterized sql query and it’s working: • Parameters i.e. user inputs are never inserted directly into the statement. • A system stored procedure called sp_executesql is called with given SQL statement and parameters. • Parameters are treated as data instead of parsing out as a SQL statement string.
  • 10. Leaks or flaws in the authentication or session management functions (e.g., exposed accounts, passwords, session IDs) to impersonate users. Developers frequently build custom authentication and session management schemes, but building these correctly is hard. A2- Broken Authentication and Session Management.
  • 11. Secure Implementation ● Do not store passwords in plain text. ● Don't attempt to implement your own hashing schemes, use strong and valid, time proven and tested cryptography algorithms such as ASP.NET's Identity (be aware of the low 1000 iteration count). ● For scenario's where implementation is required, use a unique salt with a high level of entropy with each password hash. Hash with a valid hashing algorithm such as PBKDF2 and Bcrypt with a high level of hashing rounds. ● https://cmatskas.com/-net-password-hashing-using-pbkdf2/ Password Storage:
  • 12. Wacky Hash Functions ● md5(sha1(password)) ● md5(md5(salt) + md5(password)) ● sha1(sha1(password)) ● sha1(str_rot13(password + salt)) ● md5(sha1(md5(md5(password) + sha1(password)) + md5(password)))
  • 13. A3-Cross-site Scripting Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications. XSS enables attackers to inject client-side scripts into web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same- origin policy.
  • 14. Vuln. Code (Reflected) • Sanitization(encoding) of user input is missing. • User’s input is included in web page and treated as code by the victim’s browser. User Input: <script>alert(‘Hacked’)</script>
  • 15. Secure Implementation ● ValidateRequest="true" – rejects the input because it includes potentially dangerous HTML characters. On .aspx file <%@ Page Language="C#" ValidateRequest="true" %> ● Encode HTML Output – Server.HmlEncode(HttpServerUtility) – HttpUtility.HtmlEncode ● Encode URL Output – Server.UrlEncode(HttpServerUtility) – HttpUtility.UrlEncode
  • 16. Secure Implementation contd. To safely allow restricted HTML input ● Disable ASP.NET request validation by the adding the ValidateRequest="false" attribute to the @ Page directive. ● Encode the string input with the HtmlEncode method. ● Use a StringBuilder and call its Replace method to selectively remove the encoding on the HTML elements that you want to permit
  • 18. Vuln. Code (DOM) HTMLcontent is set without validation and sanitization.
  • 19. Secure Code Creates text node and appends it to the DOM element.
  • 20. • HTML escape then JavaScript escape in HTML subcontext. <%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%> • URL escape then JavaScript escape in URL attribute subcontext. <%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%> • JavaScript escape in HTML and CSS attribute context. For HTML attribute ,escape the untrusted input and then set the attribute of DOM element. <%=Encoder.encodeForJS(untrustedData)%> For CSS attribute document.body.style.backgroundImage = "url(< %=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%>)" Secure Implementation Use ESAPI ( https://www.owasp.org/index.php/ESAPI )
  • 21. A5-Security Misconfiguration Security Misconfiguration arises when Security settings are defined, implemented, and maintained as defaults. Good security requires a secure configuration defined and deployed for the application, web server, database server, and platform. It is equally important to have the software up to date.
  • 22. Web.Config File Debug settings: <compilation debug="false" targetFramework="4.5"/> Request Processing: <httpRuntime enableVersionHeader="false" requestValidationMode="4.0" /> Cookie Settings: <httpCookies httpOnlyCookies="true" requireSSL="true" lockItem="true"/> Trace Settings: <trace enabled="false"/> Web Application settings (<system.web>)
  • 23. Directory Browsing Setting: <directoryBrowse enabled="false"/> Web server settings (<system.webServer>) Custom Header Setting: The <customHeaders> element of the <httpProtocol> element specifies custom HTTP headers Web.Config File
  • 24. <httpProtocol> <customHeaders> <remove name="Access-Control-Allow-Origin"/> <add name="Access-Control-Allow-Origin" value="http://domain.com"/> <remove name="X-Powered-By"/> <remove name="X-Frame-Options"/> <add name="X-Frame-Options" value="SAMEORIGIN"/> <remove name="X-Content-Type-Options"/> <add name="X-Content-Type-Options" value="nosniff"/> <remove name="X-XSS-Protection"/> <add name="X-XSS-Protection" value="1; mode=block"/> <remove name="X-Strict-Transport-Security"/> <add name="X-Strict-Transport-Security" value="max-age=15768000; includeSubDomains"/> <remove name="X-Content-Security-Policy"/> <add name="X-Content-Security-Policy" value="default-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline fonts.googleapis.com; font-src 'self' fonts.gstatic.com;"/> <remove name="X-WebKit-CSP"/> <add name="X-WebKit-CSP" value="default-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' fonts.googleapis.com; font-src 'self' fonts.gstatic.com;"/> </customHeaders> </httpProtocol>
  • 25. A8- Cross-site request forgery ● Cross-Site Request Forgery (CSRF) is an attack that forces an end user to execute unwanted actions on a web application in which they're currently authenticated. CSRF attacks specifically target state-changing requests, not theft of data, since the attacker has no way to see the response to the forged request.
  • 26. Wrong Assumptions ● Assuming that SSL/TLS will thwart CSRF attacks just because the cookie is marked "Secure" and/or "HTTPOnly" ● Referer header verification as the only protection ● Any CSRF protection is null and void given the presence of XSS ● Cookie double-submission when the cookie utilized is the session cookie.
  • 27. Secure Implementation Use Anti-Forgery Tokens 1.Generate the security token (or grab it from the session state) and send the token as a session cookie (again, managed in the session state, unique per session) as well as within a hidden value in each form. 2.Once the user submits the form, validate the token stored in the session state against the token included in the submitted form value. On failure, disregard form.
  • 28. Rendering token as a hidden field on aspx page. Secure Implementation
  • 29. Secure Implementation Method for generating random token and response handling
  • 30. Secure Implementation Generating token and saving it in session
  • 31. Secure Implementation Validating token received from request against the token saved in session state