<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Eoin</id>
		<title>OWASP - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Eoin"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/Eoin"/>
		<updated>2026-05-02T18:45:51Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.2</generator>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Category:OWASP_Code_Review_Project&amp;diff=5455</id>
		<title>Category:OWASP Code Review Project</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Category:OWASP_Code_Review_Project&amp;diff=5455"/>
				<updated>2006-06-09T08:38:03Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Roadmap */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Project Contributors ==&lt;br /&gt;
&lt;br /&gt;
The OWASP Code Review project was concieved by Eoin Keary the OWASP Ireland Founder and Chapter Lead.&lt;br /&gt;
We are actively seeking techies to add new sections as new web technologies emerge. &lt;br /&gt;
If you are interested in volunteering for the project, or have a comment, question, or suggestion, please drop me a line mailto:eoin.keary@owasp.org&lt;br /&gt;
&lt;br /&gt;
==Volunteers Needed==&lt;br /&gt;
&lt;br /&gt;
Yes please, drop me a line.&lt;br /&gt;
Need help on this one, dont be shy, all help appreciated&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Contents==&lt;br /&gt;
[[OWASP Code Review Guide Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
==Roadmap==&lt;br /&gt;
&lt;br /&gt;
View the [[OWASP Code Review Project Roadmap]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Security_Code_Review_Coverage&amp;diff=5454</id>
		<title>Security Code Review Coverage</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Security_Code_Review_Coverage&amp;diff=5454"/>
				<updated>2006-06-09T08:37:07Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
Contact author: [mailto:eoinkeary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Transactional Analysis: ==&lt;br /&gt;
&lt;br /&gt;
''“For every input there will be an equal and opposite output (Well sort of)”''&lt;br /&gt;
&lt;br /&gt;
A Major part of actually performing a Secure Code inspection is performing a transactional analysis.&lt;br /&gt;
An application takes inputs and produces output of some kind.&lt;br /&gt;
Attacking applications is down to using the streams for input and trying to sail a battleship up them that the application is not expecting.&lt;br /&gt;
Firstly all input to the code needs to be defined. Input for example can be:&lt;br /&gt;
* Browser input (HTTP)&lt;br /&gt;
* Cookies&lt;br /&gt;
* Other Entity  (machines/external processes).&lt;br /&gt;
* Property files&lt;br /&gt;
* ….&lt;br /&gt;
It is the input that changes the state of an application. It is the input streams attackers use to attack applications. Without any input into any system the system would be 100% secure? (probably not).&lt;br /&gt;
&lt;br /&gt;
So we need to define the input points, the path the input takes in the application and any output resulting from the input received.&lt;br /&gt;
&lt;br /&gt;
Transactional analysis is of paramount importance when performing code inspection. Any input stream that is overlooked may be a potential door for an attacker.&lt;br /&gt;
&lt;br /&gt;
Transactional analysis includes any cookie or state information passed between the client and server and not just information inputted by the user, the payload.&lt;br /&gt;
&lt;br /&gt;
Take into account any potential errors that can occur in the application for a given input, are the errors being caught? &lt;br /&gt;
&lt;br /&gt;
Transactional Analysis includes dynamic and static data flow analysis:&lt;br /&gt;
Where and when are variables set and how the variable are used throughout the workflow, how attributes of objects and parameters might affect other data within the program. It determines if the parameters, method calls, and data exchange mechanisms implement the required security.&lt;br /&gt;
&lt;br /&gt;
All transactions within the application need to be identified and analysed along with the relevant security functions they invoke.&lt;br /&gt;
The areas that are covered during transaction analysis are:&lt;br /&gt;
* Authentication &lt;br /&gt;
* Authorisation&lt;br /&gt;
* Cookie Management&lt;br /&gt;
* Data/Input Validation from all external sources.&lt;br /&gt;
* Error Handling /Information Leakage&lt;br /&gt;
* Logging /Auditing&lt;br /&gt;
* Cryptography (Data at rest and in transit)&lt;br /&gt;
* Secure Code Environment&lt;br /&gt;
* Session Management (Login/Logout)&lt;br /&gt;
&lt;br /&gt;
== General principles (What to look for) ==&lt;br /&gt;
&lt;br /&gt;
For each of the areas above a reviewer must look at the following principles in the enforcement of the requirement:&lt;br /&gt;
&lt;br /&gt;
===Authentication: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure all internal and external connections (user and entity) go through an appropriate and adequate form of authentication. Be assured that this control cannot be bypassed.&lt;br /&gt;
* Ensure all pages enforce the requirement for authentication.&lt;br /&gt;
* Ensure that whenever authentication credentials or any other sensitive information is passed, only accept the information via the HTTP “POST” method and will not accept it via the HTTP “GET” method.&lt;br /&gt;
* Any page deemed by the business or the development team as being outside the scope of authentication should be reviewed in order to assess any possibility of security breach.&lt;br /&gt;
* Ensure that authentication credentials do not traverse the wire in clear text form.&lt;br /&gt;
* Ensure not development/debug backdoors are present in production code.&lt;br /&gt;
&lt;br /&gt;
===Authorization: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure that there are authorization mechanisms in place.&lt;br /&gt;
* Ensure that the application has clearly defined the user types and the rights of said users.&lt;br /&gt;
* Ensure there is a least privilege stance in operation.&lt;br /&gt;
* Ensure that the Authorization mechanisms work properly, fail securely, and cannot be circumvented.&lt;br /&gt;
* Ensure that authorisation is checked on every request.&lt;br /&gt;
* Ensure not development/debug backdoors are present in production code.&lt;br /&gt;
&lt;br /&gt;
===Cookie Management: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure that sensitive information is not comprised.&lt;br /&gt;
* Ensure that unauthorized activities cannot take place via cookie manipulation.&lt;br /&gt;
* Ensure that proper encryption is in use. &lt;br /&gt;
* Ensure secure flag is set to prevent accidental transmission over “the wire” in a non-secure manner.&lt;br /&gt;
* Determine if all state transitions in the application code properly check for the cookies and enforce their use.&lt;br /&gt;
* Ensure the session data is being validated.&lt;br /&gt;
* Ensure cookie contains as little private information as possible.&lt;br /&gt;
* Ensure entire cookie should be encrypted if sensitive data is persisted in the cookie.&lt;br /&gt;
* Define all cookies being used by the application, their name and why they are needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data/Input Validation: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure that a DV mechanism is present.&lt;br /&gt;
* Ensure all input that can (and will) be modified by a malicious user such as http headers, input fields, hidden fields, drop down lists &amp;amp; other web components are properly validated.&lt;br /&gt;
* Ensure that the proper length checks on all input exist.&lt;br /&gt;
* Ensure that all fields, cookies, http headers/bodies &amp;amp; form fields are validated.&lt;br /&gt;
* Ensure that the data is well formed and contains only known good chars is possible.&lt;br /&gt;
* Ensure that the data validation occurs on the server side.&lt;br /&gt;
* Examine where data validation occurs and if a centralized model or decentralized model is used.&lt;br /&gt;
* Ensure there are no backdoors in the data validation model.&lt;br /&gt;
* '''''Golden Rule: All external input, no matter what it is, is examined and validated.'''''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Error Handling/Information leakage: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure that all method/function calls that return a value have proper error handling and return value checking.&lt;br /&gt;
* Ensure that exceptions and error conditions are properly handled.&lt;br /&gt;
* Ensure that no system errors can be returned to the user.&lt;br /&gt;
* Ensure that the application fails in a secure manner.&lt;br /&gt;
* Ensure resources are released if an error occurs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Logging/Auditing: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure that no sensitive information is logged in the event of an error.&lt;br /&gt;
* Ensure the payload being logged is of a defined maximum length and that the logging mechanism enforces that length.&lt;br /&gt;
* Ensure no sensitive data can be logged; E.g. cookies, HTTP “GET” method, authentication credentials.&lt;br /&gt;
* Examine if the application will audit the actions being taken by the application on behalf of the client particularly and data manipulation/Create, Update, Delete (CUD) operations.&lt;br /&gt;
* Ensure successful &amp;amp; unsuccessful authentication is logged.&lt;br /&gt;
* Ensure application errors are logged.&lt;br /&gt;
* Examine the application for debug logging with the view to logging of sensitive data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Cryptography: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure no sensitive data is transmitted in the clear, internally or externally.&lt;br /&gt;
* Ensure the application is implementing known good cryptographic methods. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Secure Code Environment: ===&lt;br /&gt;
&lt;br /&gt;
* Examine the file structure, are any components that should not be directly accessible available to the user.&lt;br /&gt;
* Examine all memory allocations/de-allocations.&lt;br /&gt;
* Examine the application for dynamic SQL and determine is vulnerable to injection.&lt;br /&gt;
* Examine the application for “main()” executable functions and debug harnesses/backdoors&lt;br /&gt;
* Search for commented out code, commented out test code, which may contain sensitive information.&lt;br /&gt;
* Ensure all logical decisions have a default clause.&lt;br /&gt;
* Ensure no development environment kit is contained on the build directories.&lt;br /&gt;
* Search for any calls to the underlying operating system or file open calls and examine the error possibilities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Session management: ===&lt;br /&gt;
&lt;br /&gt;
* Examine how and when a session is created for a user, unauthenticated and authenticated.&lt;br /&gt;
* Examine the session ID and verify is a complex enough to fulfil requirements regarding strength.&lt;br /&gt;
* Examine how sessions are stored: E.g. In a database, in memory etc.&lt;br /&gt;
* Examine how the application tracks sessions.&lt;br /&gt;
* Determine the actions the application takes if an invalid session ID occurs.&lt;br /&gt;
* Examine session invalidation.&lt;br /&gt;
* Determine how multithreaded/multi-user session management is performed.&lt;br /&gt;
* Determine the session HTTP inactivity timeout.&lt;br /&gt;
* Determine how the log-out functionality functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== &lt;br /&gt;
Understand what you are reviewing: ==&lt;br /&gt;
Many modern applications are developed on frameworks. These frameworks provide the developer less work to do as the framework does much of the “House Keeping”. So the objects developed by the development team shall extend the functionality of the framework. &lt;br /&gt;
&amp;lt;u&amp;gt;It is here that the knowledge of a given framework and language which the framework and application is implemented in is of paramount importance&amp;lt;/u&amp;gt;. Much of the transactional functionality may not be visible in the developers code and handled in “Parent” classes. &lt;br /&gt;
&lt;br /&gt;
The analyst must be aware and knowledgeable of the underlying framework&lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
===Java: ===&lt;br /&gt;
In struts the ''struts-config.xml'' and the ''web.xml'' files are the core points to view the transactional functionality of an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot; ?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE struts-config PUBLIC&lt;br /&gt;
          &amp;quot;-//Apache Software Foundation//DTD Struts Configuration 1.0//EN&amp;quot;&lt;br /&gt;
          &amp;quot;http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;struts-config&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Form Bean Definitions  --&amp;gt;&lt;br /&gt;
  &amp;lt;form-beans&amp;gt;&lt;br /&gt;
  	&amp;lt;form-bean name=&amp;quot;login&amp;quot; type=&amp;quot;test.struts.LoginForm&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;!--  Global Forward Definitions  --&amp;gt;&lt;br /&gt;
  &amp;lt;global-forwards&amp;gt;&lt;br /&gt;
  &amp;lt;/global-forwards&amp;gt;&lt;br /&gt;
  &amp;lt;!--  Action Mapping Definitions  --&amp;gt;&lt;br /&gt;
  &amp;lt;action-mappings&amp;gt;&lt;br /&gt;
    &amp;lt;action&lt;br /&gt;
    	path=&amp;quot;/login&amp;quot;&lt;br /&gt;
    	type=&amp;quot;test.struts.LoginAction&amp;quot; &amp;gt;&lt;br /&gt;
	    &amp;lt;forward name=&amp;quot;valid&amp;quot; path=&amp;quot;/jsp/MainMenu.jsp&amp;quot; /&amp;gt;&lt;br /&gt;
	    &amp;lt;forward name=&amp;quot;invalid&amp;quot; path=&amp;quot;/jsp/LoginView.jsp&amp;quot; /&amp;gt;&lt;br /&gt;
	&amp;lt;/action&amp;gt;&lt;br /&gt;
  &amp;lt;/action-mappings&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
 &amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot;&lt;br /&gt;
 value=&amp;quot;/test/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/plug-in&amp;gt;&lt;br /&gt;
 &amp;lt;/struts-config&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The ''struts-config.xml'' file contains the action mappings for each HTTP request while the ''web.xml'' file contains the deployment descriptor.&lt;br /&gt;
&lt;br /&gt;
'''Example''': The struts framework has a validator engine, which relies on regular expressions to validate the input data. The beauty of the validator is that no code has to be written for each form bean. (Form bean is the java object which received the data from the HTTP request) . The validator is not enabled by default in struts. To enable the validator a plug-in must be defined in the &amp;lt;plug-in&amp;gt; section of struts-config.xml in Red above. The property defined tells the struts framework where the custom validation rules are defined (validation.xml) and a definition of the actual rules themselves (validation-rules.xml).&lt;br /&gt;
&lt;br /&gt;
Without a proper understanding of the struts framework and by simply auditing the java code one would net see any validation being executed and one does not see the relationship between the defined rules and the java functions.&lt;br /&gt;
&lt;br /&gt;
The action mappings in Blue define the action taken by the application upon receiving a request. Here, above we can see that when the URL contains /login the '''''LoginAction''''' shall be called. From the action mappings we can see the transactions the application performs when external input is received. &lt;br /&gt;
&lt;br /&gt;
===.NET: ===&lt;br /&gt;
ASP.NET/ IIS applications use an optional XML-based configuration file named ''web.config'', to maintain application configuration settings. This covers issues such as authentication, authorisation, Error pages, HTTP settings, debug settings, web service settings etc..&lt;br /&gt;
&lt;br /&gt;
Without knowledge of these files a transactional analysis would be very difficult and not accurate.&lt;br /&gt;
&lt;br /&gt;
Optionally, you may provide a file ''web.config'' at the root of the virtual directory for a Web application. If the file is absent, the default configuration settings in ''machine.config'' will be used. If the file is present, any settings in ''web.config'' will override the default settings.&lt;br /&gt;
&lt;br /&gt;
Example of the web.config file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;authentication mode=&amp;quot;Forms&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;forms name=&amp;quot;name&amp;quot;&lt;br /&gt;
          loginUrl=&amp;quot;url&amp;quot; &lt;br /&gt;
          protection=&amp;quot;Encryption&amp;quot;&lt;br /&gt;
          timeout=&amp;quot;30&amp;quot; path=&amp;quot;/&amp;quot; &amp;gt;&lt;br /&gt;
          requireSSL=&amp;quot;true|&amp;quot;&lt;br /&gt;
          slidingExpiration=&amp;quot;false&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;credentials passwordFormat=&amp;quot;Clear&amp;quot;&amp;gt;&lt;br /&gt;
         &amp;lt;user name=&amp;quot;username&amp;quot; password=&amp;quot;password&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/credentials&amp;gt;&lt;br /&gt;
   &amp;lt;/forms&amp;gt;&lt;br /&gt;
   &amp;lt;passport redirectUrl=&amp;quot;internal&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/authentication&amp;gt;&lt;br /&gt;
&lt;br /&gt;
OK so from this config file snippet we can see that:&lt;br /&gt;
&lt;br /&gt;
'''authentication mode''': The default authentication mode is  ASP.NET forms-based authentication.&lt;br /&gt;
&lt;br /&gt;
'''loginUrl: '''Specifies the URL where the request is redirected for logon if no valid authentication cookie is found.&lt;br /&gt;
&lt;br /&gt;
'''protection: '''Sepcifies that the cookie is encrypted using 3DES or DES but DV is not performed on the cookie. Beware of plaintext attacks!!&lt;br /&gt;
&lt;br /&gt;
'''timeout: '''Cookie expiry time in minutes&lt;br /&gt;
&lt;br /&gt;
The point to make here is that many of the important security settings are not set in the code per se but in the framework configuration files. &lt;br /&gt;
Knowledge of the framework is of paramount importance when reviewing framework-based applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5453</id>
		<title>Reviewing Code for SQL Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5453"/>
				<updated>2006-06-09T08:36:32Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
Contact author: [mailto:eoinkeary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
== What SQL injection is: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL injection is a security vulnerability that occurs in the persistence/database layer of a web application. &lt;br /&gt;
This vulnerability is derived from the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities based on poor input validation and bad design that can occur whenever one programming or scripting language is embedded inside another.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How to Locate potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A secure way to build SQL statements is to construct all queries with PreparedStatement instead of Statement and/or to use parameterized stored procedures. &lt;br /&gt;
Parameterized stored procedures are compiled before user input is added, making it impossible for a hacker to modify the actual SQL statement.&lt;br /&gt;
&lt;br /&gt;
The account used to make the database connection must have “Least privilege” If the application only requires read access then the account must be given read access only.&lt;br /&gt;
&lt;br /&gt;
Avoid disclosing error information: Weak error handling is a great way for an attacker to profile SQL injection attacks. Uncaught SQL errors normally give too much information to the user and contain things like table names and procedure names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best practices when dealing with DB’s ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use Database stored procedures, but even stored procedures can be vulnerable.&lt;br /&gt;
Use parametrized queries instead of dynamic SQL statements.&lt;br /&gt;
Data validate all external input:&lt;br /&gt;
Ensure that all SQL statements recognize user inputs as variables, and that statements are precompiled before the actual inputs are substituted for the variables in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== SQL Injection Example: ==&lt;br /&gt;
&lt;br /&gt;
String DRIVER = &amp;quot;com.ora.jdbc.Driver&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String DataURL = &amp;quot;jdbc:db://localhost:5112/users&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String LOGIN = &amp;quot;admin&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String PASSWORD = &amp;quot;admin123&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Class.forName(DRIVER);&lt;br /&gt;
&lt;br /&gt;
//Make connection to DB&lt;br /&gt;
Connection connection = DriverManager.getConnection(DataURL, LOGIN, PASSWORD);&lt;br /&gt;
&lt;br /&gt;
String Username = request.getParameter(&amp;quot;USER&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
String Password = request.getParameter(&amp;quot;PASSWORD&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
int iUserID = -1;&lt;br /&gt;
&lt;br /&gt;
String sLoggedUser = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; +Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Statement selectStatement = connection.createStatement ();&lt;br /&gt;
ResultSet resultSet = selectStatement.executeQuery(sel);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if (resultSet.next()) {&lt;br /&gt;
&lt;br /&gt;
       iUserID = resultSet.getInt(1);&lt;br /&gt;
       sLoggedUser = resultSet.getString(2);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
PrintWriter writer = response.getWriter ();&lt;br /&gt;
&lt;br /&gt;
if (iUserID &amp;gt;= 0) {&lt;br /&gt;
       writer.println (&amp;quot;User logged in: &amp;quot; + sLoggedUser);&lt;br /&gt;
} else {&lt;br /&gt;
&lt;br /&gt;
       writer.println (&amp;quot;Access Denied!&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When SQL statements are dynamically created as software executes, there is an opportunity for a &lt;br /&gt;
security breach as the input data can truncate or malform or even expand the original SQL query!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Firstly the request.getParameter retrieves the data for the SQL query directly from the HTTP request without any Data validation (Min/Max length, Permitted characters, malicious characters). This error gives rise to the ability to input SQL as the payload and alter the functionality in the statement.&lt;br /&gt;
&lt;br /&gt;
The application places the payload directly into the statement causing the SQL vulnerability:&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== .NET ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code and therefore the payload can not be injected. Using a parameters collection lets you enforce type and length checks. Values outside of the range trigger an exception. Make sure you handle the exception correctly.&lt;br /&gt;
Example of the SqlParameterCollection:&lt;br /&gt;
&lt;br /&gt;
using System.Data;&lt;br /&gt;
&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
using (SqlConnection conn = new SqlConnection(connectionString))&lt;br /&gt;
{&lt;br /&gt;
  DataSet dataObj = new DataSet();&lt;br /&gt;
&lt;br /&gt;
  SqlDataAdapter sqlAdapter = new SqlDataAdapter( &amp;quot;StoredProc&amp;quot;, conn);&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;&lt;br /&gt;
&lt;br /&gt;
 //specify param type&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters.Add(&amp;quot;@usrId&amp;quot;, SqlDbType.VarChar, 15); &lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters[&amp;quot;@usrId &amp;quot;].Value = UID.Text; // Add data from user&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.Fill(dataObj); // populate and execute proc&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Stored procedures don’t always protect against SQL injection:'''&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE dbo.RunAnyQuery &lt;br /&gt;
@parameter NVARCHAR(50)&lt;br /&gt;
&lt;br /&gt;
AS&lt;br /&gt;
        EXEC sp_executesql @parameter&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The above procedure shall execute any SQL you pass to it. The directive sp_executesql is a system stored procedure in Microsoft® SQL Server™&lt;br /&gt;
&lt;br /&gt;
Lets pass it.&lt;br /&gt;
&lt;br /&gt;
DROP TABLE ORDERS;&lt;br /&gt;
&lt;br /&gt;
Guess what happens? So we must be careful of not falling into the “We’re secure, we are using stored procedures” trap!&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Security_Code_Review_Coverage&amp;diff=5452</id>
		<title>Security Code Review Coverage</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Security_Code_Review_Coverage&amp;diff=5452"/>
				<updated>2006-06-09T08:35:38Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
Contact author: [mailto:eoinkeary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Transactional Analysis: ==&lt;br /&gt;
&lt;br /&gt;
''“For every input there will be an equal and opposite output (Well sort of)”''&lt;br /&gt;
&lt;br /&gt;
A Major part of actually performing a Secure Code inspection is performing a transactional analysis.&lt;br /&gt;
An application takes inputs and produces output of some kind.&lt;br /&gt;
Attacking applications is down to using the streams for input and trying to sail a battleship up them that the application is not expecting.&lt;br /&gt;
Firstly all input to the code needs to be defined. Input for example can be:&lt;br /&gt;
* Browser input (HTTP)&lt;br /&gt;
* Cookies&lt;br /&gt;
* Other Entity  (machines/external processes).&lt;br /&gt;
* Property files&lt;br /&gt;
* ….&lt;br /&gt;
It is the input that changes the state of an application. It is the input streams attackers use to attack applications. Without any input into any system the system would be 100% secure? (probably not).&lt;br /&gt;
&lt;br /&gt;
So we need to define the input points, the path the input takes in the application and any output resulting from the input received.&lt;br /&gt;
&lt;br /&gt;
Transactional analysis is of paramount importance when performing code inspection. Any input stream that is overlooked may be a potential door for an attacker.&lt;br /&gt;
&lt;br /&gt;
Transactional analysis includes any cookie or state information passed between the client and server and not just information inputted by the user, the payload.&lt;br /&gt;
&lt;br /&gt;
Take into account any potential errors that can occur in the application for a given input, are the errors being caught? &lt;br /&gt;
&lt;br /&gt;
Transactional Analysis includes dynamic and static data flow analysis:&lt;br /&gt;
Where and when are variables set and how the variable are used throughout the workflow, how attributes of objects and parameters might affect other data within the program. It determines if the parameters, method calls, and data exchange mechanisms implement the required security.&lt;br /&gt;
&lt;br /&gt;
All transactions within the application need to be identified and analysed along with the relevant security functions they invoke.&lt;br /&gt;
The areas that are covered during transaction analysis are:&lt;br /&gt;
* Authentication &lt;br /&gt;
* Authorisation&lt;br /&gt;
* Cookie Management&lt;br /&gt;
* Data/Input Validation from all external sources.&lt;br /&gt;
* Error Handling /Information Leakage&lt;br /&gt;
* Logging /Auditing&lt;br /&gt;
* Cryptography (Data at rest and in transit)&lt;br /&gt;
* Secure Code Environment&lt;br /&gt;
* Session Management (Login/Logout)&lt;br /&gt;
&lt;br /&gt;
== General principles (What to look for) ==&lt;br /&gt;
&lt;br /&gt;
For each of the areas above a reviewer must look at the following principles in the enforcement of the requirement:&lt;br /&gt;
&lt;br /&gt;
===Authentication: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure all internal and external connections (user and entity) go through an appropriate and adequate form of authentication. Be assured that this control cannot be bypassed.&lt;br /&gt;
* Ensure all pages enforce the requirement for authentication.&lt;br /&gt;
* Ensure that whenever authentication credentials or any other sensitive information is passed, only accept the information via the HTTP “POST” method and will not accept it via the HTTP “GET” method.&lt;br /&gt;
* Any page deemed by the business or the development team as being outside the scope of authentication should be reviewed in order to assess any possibility of security breach.&lt;br /&gt;
* Ensure that authentication credentials do not traverse the wire in clear text form.&lt;br /&gt;
* Ensure not development/debug backdoors are present in production code.&lt;br /&gt;
&lt;br /&gt;
===Authorization: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure that there are authorization mechanisms in place.&lt;br /&gt;
* Ensure that the application has clearly defined the user types and the rights of said users.&lt;br /&gt;
* Ensure there is a least privilege stance in operation.&lt;br /&gt;
* Ensure that the Authorization mechanisms work properly, fail securely, and cannot be circumvented.&lt;br /&gt;
* Ensure that authorisation is checked on every request.&lt;br /&gt;
* Ensure not development/debug backdoors are present in production code.&lt;br /&gt;
&lt;br /&gt;
===Cookie Management: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure that sensitive information is not comprised.&lt;br /&gt;
* Ensure that unauthorized activities cannot take place via cookie manipulation.&lt;br /&gt;
* Ensure that proper encryption is in use. &lt;br /&gt;
* Ensure secure flag is set to prevent accidental transmission over “the wire” in a non-secure manner.&lt;br /&gt;
* Determine if all state transitions in the application code properly check for the cookies and enforce their use.&lt;br /&gt;
* Ensure the session data is being validated.&lt;br /&gt;
* Ensure cookie contains as little private information as possible.&lt;br /&gt;
* Ensure entire cookie should be encrypted if sensitive data is persisted in the cookie.&lt;br /&gt;
* Define all cookies being used by the application, their name and why they are needed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data/Input Validation: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure that a DV mechanism is present.&lt;br /&gt;
* Ensure all input that can (and will) be modified by a malicious user such as http headers, input fields, hidden fields, drop down lists &amp;amp; other web components are properly validated.&lt;br /&gt;
* Ensure that the proper length checks on all input exist.&lt;br /&gt;
* Ensure that all fields, cookies, http headers/bodies &amp;amp; form fields are validated.&lt;br /&gt;
* Ensure that the data is well formed and contains only known good chars is possible.&lt;br /&gt;
* Ensure that the data validation occurs on the server side.&lt;br /&gt;
* Examine where data validation occurs and if a centralized model or decentralized model is used.&lt;br /&gt;
* Ensure there are no backdoors in the data validation model.&lt;br /&gt;
* '''''Golden Rule: All external input, no matter what it is, is examined and validated.'''''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Error Handling/Information leakage: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure that all method/function calls that return a value have proper error handling and return value checking.&lt;br /&gt;
* Ensure that exceptions and error conditions are properly handled.&lt;br /&gt;
* Ensure that no system errors can be returned to the user.&lt;br /&gt;
* Ensure that the application fails in a secure manner.&lt;br /&gt;
* Ensure resources are released if an error occurs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Logging/Auditing: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure that no sensitive information is logged in the event of an error.&lt;br /&gt;
* Ensure the payload being logged is of a defined maximum length and that the logging mechanism enforces that length.&lt;br /&gt;
* Ensure no sensitive data can be logged; E.g. cookies, HTTP “GET” method, authentication credentials.&lt;br /&gt;
* Examine if the application will audit the actions being taken by the application on behalf of the client particularly and data manipulation/Create, Update, Delete (CUD) operations.&lt;br /&gt;
* Ensure successful &amp;amp; unsuccessful authentication is logged.&lt;br /&gt;
* Ensure application errors are logged.&lt;br /&gt;
* Examine the application for debug logging with the view to logging of sensitive data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Cryptography: ===&lt;br /&gt;
&lt;br /&gt;
* Ensure no sensitive data is transmitted in the clear, internally or externally.&lt;br /&gt;
* Ensure the application is implementing known good cryptographic methods. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Secure Code Environment: ===&lt;br /&gt;
&lt;br /&gt;
* Examine the file structure, are any components that should not be directly accessible available to the user.&lt;br /&gt;
* Examine all memory allocations/de-allocations.&lt;br /&gt;
* Examine the application for dynamic SQL and determine is vulnerable to injection.&lt;br /&gt;
* Examine the application for “main()” executable functions and debug harnesses/backdoors&lt;br /&gt;
* Search for commented out code, commented out test code, which may contain sensitive information.&lt;br /&gt;
* Ensure all logical decisions have a default clause.&lt;br /&gt;
* Ensure no development environment kit is contained on the build directories.&lt;br /&gt;
* Search for any calls to the underlying operating system or file open calls and examine the error possibilities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Session management: ===&lt;br /&gt;
&lt;br /&gt;
* Examine how and when a session is created for a user, unauthenticated and authenticated.&lt;br /&gt;
* Examine the session ID and verify is a complex enough to fulfil requirements regarding strength.&lt;br /&gt;
* Examine how sessions are stored: E.g. In a database, in memory etc.&lt;br /&gt;
* Examine how the application tracks sessions.&lt;br /&gt;
* Determine the actions the application takes if an invalid session ID occurs.&lt;br /&gt;
* Examine session invalidation.&lt;br /&gt;
* Determine how multithreaded/multi-user session management is performed.&lt;br /&gt;
* Determine the session HTTP inactivity timeout.&lt;br /&gt;
* Determine how the log-out functionality functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== &lt;br /&gt;
Understand what you are reviewing: ==&lt;br /&gt;
Many modern applications are developed on frameworks. These frameworks provide the developer less work to do as the framework does much of the “House Keeping”. So the objects developed by the development team shall extend the functionality of the framework. &lt;br /&gt;
&amp;lt;u&amp;gt;It is here that the knowledge of a given framework and language which the framework and application is implemented in is of paramount importance&amp;lt;/u&amp;gt;. Much of the transactional functionality may not be visible in the developers code and handled in “Parent” classes. &lt;br /&gt;
&lt;br /&gt;
The analyst must be aware and knowledgeable of the underlying framework&lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
===Java: ===&lt;br /&gt;
In struts the ''struts-config.xml'' and the ''web.xml'' files are the core points to view the transactional functionality of an application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot; ?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE struts-config PUBLIC&lt;br /&gt;
          &amp;quot;-//Apache Software Foundation//DTD Struts Configuration 1.0//EN&amp;quot;&lt;br /&gt;
          &amp;quot;http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;struts-config&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Form Bean Definitions  --&amp;gt;&lt;br /&gt;
  &amp;lt;form-beans&amp;gt;&lt;br /&gt;
  	&amp;lt;form-bean name=&amp;quot;login&amp;quot; type=&amp;quot;test.struts.LoginForm&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;!--  Global Forward Definitions  --&amp;gt;&lt;br /&gt;
  &amp;lt;global-forwards&amp;gt;&lt;br /&gt;
  &amp;lt;/global-forwards&amp;gt;&lt;br /&gt;
  &amp;lt;!--  Action Mapping Definitions  --&amp;gt;&lt;br /&gt;
  &amp;lt;action-mappings&amp;gt;&lt;br /&gt;
    &amp;lt;action&lt;br /&gt;
    	path=&amp;quot;/login&amp;quot;&lt;br /&gt;
    	type=&amp;quot;test.struts.LoginAction&amp;quot; &amp;gt;&lt;br /&gt;
	    &amp;lt;forward name=&amp;quot;valid&amp;quot; path=&amp;quot;/jsp/MainMenu.jsp&amp;quot; /&amp;gt;&lt;br /&gt;
	    &amp;lt;forward name=&amp;quot;invalid&amp;quot; path=&amp;quot;/jsp/LoginView.jsp&amp;quot; /&amp;gt;&lt;br /&gt;
	&amp;lt;/action&amp;gt;&lt;br /&gt;
  &amp;lt;/action-mappings&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
 &amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot;&lt;br /&gt;
 value=&amp;quot;/test/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/plug-in&amp;gt;&lt;br /&gt;
 &amp;lt;/struts-config&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The ''struts-config.xml'' file contains the action mappings for each HTTP request while the ''web.xml'' file contains the deployment descriptor.&lt;br /&gt;
&lt;br /&gt;
'''Example''': The struts framework has a validator engine, which relies on regular expressions to validate the input data. The beauty of the validator is that no code has to be written for each form bean. (Form bean is the java object which received the data from the HTTP request) . The validator is not enabled by default in struts. To enable the validator a plug-in must be defined in the &amp;lt;plug-in&amp;gt; section of struts-config.xml in Red above. The property defined tells the struts framework where the custom validation rules are defined (validation.xml) and a definition of the actual rules themselves (validation-rules.xml).&lt;br /&gt;
&lt;br /&gt;
Without a proper understanding of the struts framework and by simply auditing the java code one would net see any validation being executed and one does not see the relationship between the defined rules and the java functions.&lt;br /&gt;
&lt;br /&gt;
The action mappings in Blue define the action taken by the application upon receiving a request. Here, above we can see that when the URL contains /login the '''''LoginAction''''' shall be called. From the action mappings we can see the transactions the application performs when external input is received. &lt;br /&gt;
&lt;br /&gt;
===.NET: ===&lt;br /&gt;
ASP.NET/ IIS applications use an optional XML-based configuration file named ''web.config'', to maintain application configuration settings. This covers issues such as authentication, authorisation, Error pages, HTTP settings, debug settings, web service settings etc..&lt;br /&gt;
&lt;br /&gt;
Without knowledge of these files a transactional analysis would be very difficult and not accurate.&lt;br /&gt;
&lt;br /&gt;
Optionally, you may provide a file ''web.config'' at the root of the virtual directory for a Web application. If the file is absent, the default configuration settings in ''machine.config'' will be used. If the file is present, any settings in ''web.config'' will override the default settings.&lt;br /&gt;
&lt;br /&gt;
Example of the web.config file:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;authentication mode=&amp;quot;Forms&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;forms name=&amp;quot;name&amp;quot;&lt;br /&gt;
          loginUrl=&amp;quot;url&amp;quot; &lt;br /&gt;
          protection=&amp;quot;Encryption&amp;quot;&lt;br /&gt;
          timeout=&amp;quot;30&amp;quot; path=&amp;quot;/&amp;quot; &amp;gt;&lt;br /&gt;
          requireSSL=&amp;quot;true|&amp;quot;&lt;br /&gt;
          slidingExpiration=&amp;quot;false&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;credentials passwordFormat=&amp;quot;Clear&amp;quot;&amp;gt;&lt;br /&gt;
         &amp;lt;user name=&amp;quot;username&amp;quot; password=&amp;quot;password&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/credentials&amp;gt;&lt;br /&gt;
   &amp;lt;/forms&amp;gt;&lt;br /&gt;
   &amp;lt;passport redirectUrl=&amp;quot;internal&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/authentication&amp;gt;&lt;br /&gt;
&lt;br /&gt;
OK so from this config file snippet we can see that:&lt;br /&gt;
&lt;br /&gt;
'''authentication mode''': The default authentication mode is  ASP.NET forms-based authentication.&lt;br /&gt;
&lt;br /&gt;
'''loginUrl: '''Specifies the URL where the request is redirected for logon if no valid authentication cookie is found.&lt;br /&gt;
&lt;br /&gt;
'''protection: '''Sepcifies that the cookie is encrypted using 3DES or DES but DV is not performed on the cookie. Beware of plaintext attacks!!&lt;br /&gt;
&lt;br /&gt;
'''timeout: '''Cookie expiry time in minutes&lt;br /&gt;
&lt;br /&gt;
The point to make here is that many of the important security settings are not set in the code per se but in the framework configuration files. &lt;br /&gt;
Knowledge of the framework is of paramount importance when reviewing framework-based applications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=The_Secure_Code_Environment&amp;diff=5451</id>
		<title>The Secure Code Environment</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=The_Secure_Code_Environment&amp;diff=5451"/>
				<updated>2006-06-09T08:35:11Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
Contact author: [mailto:eoinkeary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Secure Code Environment ==&lt;br /&gt;
&lt;br /&gt;
Another important thing to be aware of is when you receive the code make sure it is identical in deployment layout to what would go to production. Having great code but it being deployed in unprotected folders on the application server is not a great idea.&lt;br /&gt;
Attackers do code reviews also and what better than to code review the potential target application.&lt;br /&gt;
For example: try in “'''''Google'''''”:&lt;br /&gt;
&amp;lt;u&amp;gt;http://www.google.com/search?q=%0D%0Aintitle%3Aindex.of+WEB-INF&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This lists exposed “''Web-Inf''” directories on WebSphere®, Tomcat and other app servers.&lt;br /&gt;
&lt;br /&gt;
''The WEB-INF directory tree contains web application classes, pre-compiled JSP files, server side libraries, session information and files such as '''web.xml''' and '''webapp.properties'''. ''&lt;br /&gt;
&lt;br /&gt;
So be sure the code base is identical to production. Ensuring that we have a “''secure code environment''” is also an important part of an application secure code inspection.&lt;br /&gt;
&lt;br /&gt;
The code may be “bullet proof” but if it is accessible to a user this may cause other problems. Remember the developer is not the only one to perform code reviews, attackers also do this. The only visible surface that a user should see are the “suggestions” rendered by the browser upon receiving the HTML from the backend server. Any request to the backend server outside the strict context of the application should be refused and not be visible. Generally think of ''“That which is not explicitly granted is denied”''.&lt;br /&gt;
&lt;br /&gt;
Example of the Tomcat web.xml to prevent directory indexing:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;servlet&amp;gt;&lt;br /&gt;
 &amp;lt;servlet-name&amp;gt;default&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;
 &amp;lt;servlet-class&amp;gt;org.apache.catalina.servlets.DefaultServlet&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;
 &amp;lt;init-param&amp;gt;&lt;br /&gt;
 &amp;lt;param-name&amp;gt;debug&amp;lt;/param-name&amp;gt;&lt;br /&gt;
 &amp;lt;param-value&amp;gt;0&amp;lt;/param-value&amp;gt;&lt;br /&gt;
 &amp;lt;/init-param&amp;gt;&lt;br /&gt;
 &amp;lt;init-param&amp;gt;&lt;br /&gt;
 &amp;lt;param-name&amp;gt;listings&amp;lt;/param-name&amp;gt;&lt;br /&gt;
 &amp;lt;param-value&amp;gt;false&amp;lt;/param-value&amp;gt;&lt;br /&gt;
 &amp;lt;/init-param&amp;gt;&lt;br /&gt;
 &amp;lt;init-param&amp;gt;&lt;br /&gt;
 &amp;lt;param-name&amp;gt;readonly&amp;lt;/param-name&amp;gt;&lt;br /&gt;
 &amp;lt;param-value&amp;gt;true&amp;lt;/param-value&amp;gt;&lt;br /&gt;
 &amp;lt;/init-param&amp;gt;&lt;br /&gt;
 &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;&lt;br /&gt;
 &amp;lt;/servlet&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
So to deny access to all directories we put:&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;Directory /&amp;gt;&lt;br /&gt;
 Order Deny,Allow&lt;br /&gt;
 Deny from All&lt;br /&gt;
 &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And then override this for the directories we require access to:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Also in Apache HTTP server to ensure directories like WEB-INF and META-INF are protected the following should be added to the ''httpd.conf'', the main configuration file for the Apache web server&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;Directory /usr/users/*/public_html&amp;gt; &lt;br /&gt;
 Order Deny,Allow &lt;br /&gt;
 Allow from all &lt;br /&gt;
 &amp;lt;/Directory&amp;gt; &lt;br /&gt;
 &amp;lt;Directory /usr/local/httpd&amp;gt; &lt;br /&gt;
 Order Deny,Allow &lt;br /&gt;
 Allow from all &lt;br /&gt;
 &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On Apache servers, if we wish to specify permissions for a directory and subdirectories we add a '''''.htaccess''''' file.&lt;br /&gt;
&lt;br /&gt;
To protect the .htaccess file itself we palce:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;Files .htaccess&amp;gt;&lt;br /&gt;
 order allow,deny&lt;br /&gt;
 deny from all&lt;br /&gt;
 &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To stop directory indexing we place the following directive into the .htaccess file:&lt;br /&gt;
'''IndexIgnore *'''&lt;br /&gt;
The * is a wildcard to prevent all files from being indexed.&lt;br /&gt;
&lt;br /&gt;
== Protecting JSP pages ==&lt;br /&gt;
&lt;br /&gt;
If using the Struts framework we do not want users access any JSP page directly. Accessing the JSP directly without going through the request processor can enable the attacker to view any server-side code in the JSP.&lt;br /&gt;
Lets say initial page can is a HTML document. So the HTTP GET from the browser retrieves this page. Any subsequent page must go through the framework.&lt;br /&gt;
Add the following lines to the '''web.xml''' file to prevent users from accessing any JSP page directly:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;web-app&amp;gt;&lt;br /&gt;
   ...&lt;br /&gt;
  &amp;lt;security-constraint&amp;gt;&lt;br /&gt;
    &amp;lt;web-resource-collection&amp;gt;&lt;br /&gt;
      &amp;lt;web-resource-name&amp;gt;no_access&amp;lt;/web-resource-name&amp;gt;&lt;br /&gt;
      &amp;lt;url-pattern&amp;gt;*.jsp&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;
    &amp;lt;/web-resource-collection&amp;gt;&lt;br /&gt;
    &amp;lt;auth-constraint/&amp;gt;&lt;br /&gt;
  &amp;lt;/security-constraint&amp;gt;&lt;br /&gt;
  ...&lt;br /&gt;
 &amp;lt;/web-app&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this directive in '''web.xml''' a HTTP request for a JSP page directly will fail.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A clean environment ==&lt;br /&gt;
&lt;br /&gt;
When reviewing the environment we must see if the directories contain any artefacts from development. These files may not be referenced in any way and hence the application server gives no protection to them. Files such as .'''bak, .old, .tmp''' etc should be removed as they contain source code.&lt;br /&gt;
&lt;br /&gt;
Source code should not go into production directories. The complied class files are all that is required in most cases. All source code should be removed and only the “executables” should remain.&lt;br /&gt;
&lt;br /&gt;
No development tools should be present on a production environment. For example a java application should only need a JRE (Java Runtime Environment) and not a JDK (Java Development Kit) to function.&lt;br /&gt;
&lt;br /&gt;
Test and debug code should be removed from all source code and configuration files. Even commented out code should be removed as a precaution. Test code can contain backdoors that circumvent the workflow in the application and at worst contain valid authentication credentials or account details.&lt;br /&gt;
&lt;br /&gt;
Comments on code and Meta tags pertaining to the IDE used or technology used to develop the application should be removed. Some comments can divulge important information regarding bugs in code or pointers to functionality. This is particularly important with client side code such as JSP’s and ASP files.&lt;br /&gt;
&lt;br /&gt;
A copyright and confidentiality statement should be at the top of every file. This mitigates any confusion regarding who owns the code. This may seem trivial but it is important to state who owns the code.&lt;br /&gt;
&lt;br /&gt;
To sum up, code review includes looking at the configuration of the application server and not just the code. Knowledge of the server in question is important and information is easily available on the web.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OS_Injection&amp;diff=5450</id>
		<title>OS Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OS_Injection&amp;diff=5450"/>
				<updated>2006-06-09T08:34:17Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
Contact author: [mailto:eoinkeary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Injection flaws allow attackers to pass malicious code through a web application to another sub system.&lt;br /&gt;
Depending on the subsystem different types of injection attack can be performed:&lt;br /&gt;
RDBMS: SQL Injection&lt;br /&gt;
WebBrowser/Appserver: SQL Injection&lt;br /&gt;
OS-shell: Operating system commands Calling external applications from your application.&lt;br /&gt;
&lt;br /&gt;
==How to locate the potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
Many developers believe text fields are the only areas for data validation. This is an incorrect assumption. Any external input must be data validated:&lt;br /&gt;
&lt;br /&gt;
Text fields, List boxes, radio buttons, check boxes, cookies, HTTP header data, HTTP post data, hidden fields, parameter names and parameter values.&lt;br /&gt;
… This is not an exhaustive list.&lt;br /&gt;
&lt;br /&gt;
“Process to process” or “entity-to-entity” communication must be investigated also. Any code that communicates with an upstream or downstream process and accepts input from said entity must be reviewed.&lt;br /&gt;
&lt;br /&gt;
All injection flaws are input validation errors. The presence if an injection flaw is an indication of incorrect data validation on the input received from an external source our outside the boundary of trust, which gets more blurred every year.&lt;br /&gt;
&lt;br /&gt;
Basically for this type of vulnerability we need to find all input streams into the application. This can be from a users browser, CLI or fat client but also from upstream processes that “feed” our application.&lt;br /&gt;
&lt;br /&gt;
An example would be to search the code base for the use of API’s or packages that are normally used for communication purposes.&lt;br /&gt;
&lt;br /&gt;
The '''java.io''', '''java.sql''', '''java.net''', '''java.rmi''', '''java.xml''' packages are all used for application communication. Searching for methods from those packages in the code base can yield results. A less “scientific” method is to search for common keywords such as “UserID”, “LoginID” or “Password”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Vulnerable Patterns for OS injection ==&lt;br /&gt;
What we should be looking for is relationships between the application and the operating system. The application utilising functions of the underlying operating system.&lt;br /&gt;
&lt;br /&gt;
In java using the Runtime object, '''java.lang.Runtime''' does this.&lt;br /&gt;
In .NET calls such as '''System.Diagnostics.Process.Start '''are used to call underlying OS functions. &lt;br /&gt;
In PHP we may look for calls such as '''exec()''' or '''passthru()'''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
&lt;br /&gt;
We have a class that eventually gets input from the user via a HTTP request.&lt;br /&gt;
This class is used to execute some native exe on the application server and return a result.&lt;br /&gt;
&lt;br /&gt;
 public class DoStuff {&lt;br /&gt;
 public string executeCommand(String userName)&lt;br /&gt;
 {	try {&lt;br /&gt;
 		String myUid = userName;&lt;br /&gt;
 		Runtime rt = Runtime.getRuntime();&lt;br /&gt;
 		rt.exec(&amp;quot;doStuff.exe &amp;quot; +”-“ +myUid); // Call exe with userID&lt;br /&gt;
 	}catch(Exception e)&lt;br /&gt;
 		{&lt;br /&gt;
 e.printStackTrace();&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ok, so the method executeCommand calls '''''doStuff.exe''''' via the '''''java.lang.runtime''''' static method '''''getRuntime()'''''. The parameter passed is not validated in any way in this class. We are assuming that the data has not been data validated prior to calling this method. ''Transactional analysis should have encountered any data validation prior to this point.''&lt;br /&gt;
Inputting “Joe69” would result in the following MS DOS command:&lt;br /&gt;
'''''doStuff.exe –Joe69'''''&lt;br /&gt;
Lets say we input '''''Joe69 &amp;amp; netstat –a''''' we would get the following response:&lt;br /&gt;
The exe doStuff would execute passing in the User Id Joe69, but then the dos command '''''netstat''''' would be called. How this works is the passing of the parameter “&amp;amp;” into the application, which in turn is used as a command appender in MS DOS and hence the command after the &amp;amp; character is executed.&lt;br /&gt;
&lt;br /&gt;
UNIX:&lt;br /&gt;
An attacker might insert the string '''“; cat /etc/hosts”''' the contents of the UNIX hosts file might be exposed to the attacker. &lt;br /&gt;
&lt;br /&gt;
.NET Example:&lt;br /&gt;
 namespace ExternalExecution&lt;br /&gt;
 {&lt;br /&gt;
 class CallExternal&lt;br /&gt;
 {&lt;br /&gt;
 static void Main(string[] args)&lt;br /&gt;
 {&lt;br /&gt;
 String arg1=args[0];&lt;br /&gt;
 System.Diagnostics.Process.Start(&amp;quot;doStuff.exe&amp;quot;, arg1);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Yet again there is no data validation to speak of here. Assuming no upstream validation occurring in another class.&lt;br /&gt;
&lt;br /&gt;
These attacks include calls to the operating system via system calls, the use of external programs via shell commands, as well as calls to backend databases via SQL (i.e., SQL injection). Complete scripts written in perl, python, shell, bat and other languages can be injected into poorly designed web applications and executed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Good Patterns &amp;amp; procedures to prevent OS injection==&lt;br /&gt;
&lt;br /&gt;
See the Data Validation section.&lt;br /&gt;
&lt;br /&gt;
==Related Articles==&lt;br /&gt;
[[Interpreter Injection]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
[[Category:Attack]]&lt;br /&gt;
[[Category:Input Validation]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Error_Handling&amp;diff=5449</id>
		<title>Error Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Error_Handling&amp;diff=5449"/>
				<updated>2006-06-09T08:33:11Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Error, Exception handling &amp;amp; Logging. ==&lt;br /&gt;
&lt;br /&gt;
Contact author: [mailto:eoinkeary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An important aspect of secure application development is to prevent information leakage. Error messages give an attacker great insight into the inner workings of an application. &lt;br /&gt;
&lt;br /&gt;
The purpose of reviewing the Error Handling code is to assure the application fails safely under all possible error conditions, expected and unexpected. No sensitive information is presented to the user when an error occurs.&lt;br /&gt;
&lt;br /&gt;
For example SQL injection is much tougher to successfully pull off without some healthy error messages. It lessens the attack footprint and our attacker would have to resort to use “blind SQL injection” which is more difficult and time consuming.&lt;br /&gt;
&lt;br /&gt;
A well-planned error/exception handling strategy is important for three reasons:&lt;br /&gt;
&lt;br /&gt;
#	Good error handling does not give an attacker any information which is a means to an end, attacking the application&lt;br /&gt;
#	A proper centralised error strategy is easier to maintain and reduces the chance of any uncaught errors “Bubbling up” to the front end of an application.&lt;br /&gt;
#	Information leakage can lead to social engineering exploits.&lt;br /&gt;
&lt;br /&gt;
Some development languages provide checked exceptions which mean that the compiler shall complain if an exception for a particular API call is not caught Java and C# are good examples of this.&lt;br /&gt;
Languages like C++ and C do not provide this safety net. Languages with checked exception handling still are prone to information leakage as not all types of error are checked for.&lt;br /&gt;
&lt;br /&gt;
When an exception or error is thrown we also need to log this occurrence. Sometimes this is due to bad development, but it can be the result of an attack or some other service your application relies on failing.&lt;br /&gt;
&lt;br /&gt;
All code paths that can cause an exception to be thrown should check for success in order for the exception not to be thrown.&lt;br /&gt;
&lt;br /&gt;
To avoid a NullPointerException we should check is the object being accessed is not null.&lt;br /&gt;
&lt;br /&gt;
==Generic error messages==&lt;br /&gt;
&lt;br /&gt;
We should use a localized description string in every exception, a friendly error reason such as “System Error – Please try again later”. When the user sees an error message, it will be derived from this description string of the exception that was thrown, and never from the exception class which may contain a stack trace, line number where the error occurred, class name or method name.&lt;br /&gt;
&lt;br /&gt;
Do not expose sensitive information in exception messages. Information such as paths on the local file system is considered privileged information; any system internal information should be hidden from the user. As mentioned before an attacker could use this information to gather private user information from the application or components that make up the app.&lt;br /&gt;
&lt;br /&gt;
Don’t put people’s names or any internal contact information in error messages. Don’t put any “human” information, which would lead to a level of familiarity and a social engineering exploit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How to locate the potentially vulnerable code==&lt;br /&gt;
&lt;br /&gt;
===JAVA===&lt;br /&gt;
&lt;br /&gt;
In java we have the concept of an error object, the Exception object.&lt;br /&gt;
This lives in the java package java.lang and is derived from the Throwable object&lt;br /&gt;
Exceptions are thrown when an abnormal occurrence has occurred. Another object derived from Throwable is the Error object, which is thrown when something more serious occurs.&lt;br /&gt;
&lt;br /&gt;
Information leakage can occur when developers use some exception methods, which ‘bubble’ to the user UI due to a poor error handling strategy.&lt;br /&gt;
The methods are as follows:&lt;br /&gt;
printStackTrace()&lt;br /&gt;
getStackTrace()&lt;br /&gt;
&lt;br /&gt;
Also another object to look at is the java.lang.system package:&lt;br /&gt;
&lt;br /&gt;
setErr() and the System.err field.&lt;br /&gt;
&lt;br /&gt;
===.NET===&lt;br /&gt;
&lt;br /&gt;
In .NET a System.Exception object exists. Commonly used child objects such as ApplicationException and SystemException are used.&lt;br /&gt;
It is not recommended that you throw or catch a SystemException this is thrown by runtime.&lt;br /&gt;
&lt;br /&gt;
When an error occurs, either the system or the currently executing application reports it by throwing an exception containing information about the error, similar to java. Once thrown, an exception is handled by the application or by the default exception handler.&lt;br /&gt;
This Exception object contains similar methods to the java implementation such as:&lt;br /&gt;
&lt;br /&gt;
StackTrace &lt;br /&gt;
Source&lt;br /&gt;
Message&lt;br /&gt;
HelpLink&lt;br /&gt;
&lt;br /&gt;
In .NET we need to look at the error handling strategy from the point of view of global error handling and the handling of unexpected errors. This can be done in many ways and this article is not an exhaustive list.&lt;br /&gt;
Firstly an Error Event is thrown when an unhandled exception is thrown. This is part of the TemplateControl class.&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUITemplateControlClassErrorTopic.asp&lt;br /&gt;
&lt;br /&gt;
==Error handling can be done in three ways in .NET==&lt;br /&gt;
&lt;br /&gt;
*In the web.config file's customErrors section. &lt;br /&gt;
*In the global.asax file's Application_Error sub. &lt;br /&gt;
*On the aspx or associated codebehind page in the Page_Error sub&lt;br /&gt;
&lt;br /&gt;
The order of error handling events in .NET is as follows: &lt;br /&gt;
#	On the Page in the Page_Error sub.&lt;br /&gt;
#	The global.asax Application_Error sub &lt;br /&gt;
#	The web.config file &lt;br /&gt;
&lt;br /&gt;
It is recommended to look in these areas to understand the error strategy of the application.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Vulnerable Patterns for Error Handling==&lt;br /&gt;
&lt;br /&gt;
===Page_Error===&lt;br /&gt;
&lt;br /&gt;
Page_Error is page level handling which is run on the server side.&lt;br /&gt;
Below is an example but the error information is a little too informative and hence bad practice.&lt;br /&gt;
&lt;br /&gt;
'''FIXME: code formatting'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;script language=&amp;quot;C#&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sub Page_Error(Source As Object, E As EventArgs)&lt;br /&gt;
&lt;br /&gt;
Dim message As String = &amp;quot;&amp;lt;font face=verdana color=red&amp;gt;&amp;lt;h1&amp;gt;&amp;quot; &amp;amp; Request.Url.ToString()&amp;amp; &amp;quot;&amp;lt;/h1&amp;gt;&amp;quot; &amp;amp;&lt;br /&gt;
&lt;br /&gt;
&amp;quot;&amp;lt;pre&amp;gt;&amp;lt;font color='red'&amp;gt;&amp;quot; &amp;amp; Server.GetLastError().ToString()&amp;amp; &amp;quot;&amp;lt;/pre&amp;gt;&amp;lt;/font&amp;gt;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Response.Write(message) // display message&lt;br /&gt;
&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The red text in the example above has a number of issues:&lt;br /&gt;
Firstly it redisplays the HTTP request to the user in the form of Request.Url.ToString() Assuming there has been no data validation prior to this point we are vulnerable to cross site scripting attacks!! Secondly the error message and stack trace is displayed to the user using Server.GetLastError().ToString() which divulges internal information regarding the application.&lt;br /&gt;
&lt;br /&gt;
After the Page_Error is called, the Application_Error sub is called:&lt;br /&gt;
&lt;br /&gt;
===Global.asax===&lt;br /&gt;
&lt;br /&gt;
When an error occurs, the Application_Error sub is called. In this method we can&lt;br /&gt;
log the error and redirect to another page.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;%@ Import Namespace=&amp;quot;System.Diagnostics&amp;quot; %&amp;gt;&lt;br /&gt;
   &amp;lt;script language=&amp;quot;C#&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&lt;br /&gt;
     void Application_Error(Object sender, EventArgs e) {&lt;br /&gt;
          String Message = &amp;quot;\n\nURL: http://localhost/&amp;quot; + Request.Path&lt;br /&gt;
                           + &amp;quot;\n\nMESSAGE:\n &amp;quot; + Server.GetLastError().Message&lt;br /&gt;
                           + &amp;quot;\n\nSTACK TRACE:\n&amp;quot; + Server.GetLastError().StackTrace;&lt;br /&gt;
          // Insert into Event Log&lt;br /&gt;
          EventLog Log = new EventLog();&lt;br /&gt;
          Log.Source = LogName;&lt;br /&gt;
          Log.WriteEntry(Message, EventLogEntryType.Error);&lt;br /&gt;
        Server.Redirect(Error.htm) // this shall also clear the error&lt;br /&gt;
     }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Above is an example of code in Global.asax and the Application_Error method.&lt;br /&gt;
The error is logged and then the user is redirected. Unvalidated parameters are being&lt;br /&gt;
logged here in the form of Request.Path. Care must be taken not to log or redisplay &lt;br /&gt;
unvalidated input from any external source. &lt;br /&gt;
&lt;br /&gt;
===Web.config===&lt;br /&gt;
Web.config has a custom errors tag which can be used to handle errors. This is called last and if Page_error or Application_error  called and has functionality that functionality shall be executed first. As long as the previous two handling mechanisms do not redirect or clear (Response.Redirect or a Server.ClearError) this shall be called. And you shall be forwarded to the page defined in web.config&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;customErrors defaultRedirect=&amp;quot;error.html&amp;quot; mode=&amp;quot;On|Off|RemoteOnly&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;error statusCode=&amp;quot;statuscode&amp;quot; redirect=&amp;quot;url&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/customErrors&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The “On&amp;quot; directive means that custom errors are enabled. If no defaultRedirect is specified, users see a generic error. &lt;br /&gt;
&amp;quot;Off&amp;quot; directive means that custom errors are disabled. This allows display of detailed errors. &lt;br /&gt;
&amp;quot;RemoteOnly&amp;quot; specifies that custom errors are shown only to remote clients, and ASP.NET errors are shown to the local host. This is the default. &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;customErrors mode=&amp;quot;On&amp;quot; defaultRedirect=&amp;quot;error.html&amp;quot;&amp;gt;&lt;br /&gt;
     &amp;lt;error statusCode=&amp;quot;500&amp;quot; redirect=&amp;quot;err500.aspx&amp;quot;/&amp;gt;&lt;br /&gt;
     &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;notHere.aspx&amp;quot;/&amp;gt;&lt;br /&gt;
     &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;notAuthz.aspx&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/customErrors&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Try &amp;amp; Catch (Java/ .NET)==&lt;br /&gt;
&lt;br /&gt;
Code that might throw exceptions should be in a try block and code that handles exceptions in a catch block. &lt;br /&gt;
The catch block is a series of statements beginning with the keyword catch, followed by an exception type and an action to be taken. These are very similar in Java and .NET&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
Java Try-Catch:&lt;br /&gt;
&lt;br /&gt;
 public class DoStuff {&lt;br /&gt;
     public static void Main() {&lt;br /&gt;
         try {&lt;br /&gt;
             StreamReader sr = File.OpenText(&amp;quot;stuff.txt&amp;quot;);&lt;br /&gt;
             Console.WriteLine(&amp;quot;Reading line {0}&amp;quot;, sr.ReadLine());    &lt;br /&gt;
         }&lt;br /&gt;
         catch(Exception e) {&lt;br /&gt;
             Console.WriteLine(&amp;quot;An error occurred. Please leave to room”);&lt;br /&gt;
 	 logerror(“Error: “, e);&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
.NET try – catch&lt;br /&gt;
&lt;br /&gt;
 public void run() {&lt;br /&gt;
             while (!stop) {&lt;br /&gt;
                 try {&lt;br /&gt;
 &lt;br /&gt;
                     // Perform work here&lt;br /&gt;
 &lt;br /&gt;
                 } catch (Throwable t) {&lt;br /&gt;
                     // Log the exception and continue&lt;br /&gt;
 		WriteToUser(“An Error has occurred, put the kettle on”);&lt;br /&gt;
                     logger.log(Level.SEVERE, &amp;quot;Unexception exception&amp;quot;, t);&lt;br /&gt;
                 }&lt;br /&gt;
             }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
In general, it is best practice to catch a specific type of exception rather than use the basic catch(Exception) or catch(Throwable) statement in the case of Java.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Releasing resources and good housekeeping==&lt;br /&gt;
&lt;br /&gt;
If the language in question has a finally method use it. The finally method is guaranteed to always be called. The finally method can be used to release resources referenced by the method that threw the exception. This is very important. An example would be if a method gained a database connection from a pool of connections and an exception occurred without finally the connection object shall not be returned to the pool for some time (until the timeout). This can lead to pool exhaustion. finally() is called even if no exception is thrown.&lt;br /&gt;
&lt;br /&gt;
 try {&lt;br /&gt;
        System.out.println(&amp;quot;Entering try statement&amp;quot;);&lt;br /&gt;
        out = new PrintWriter(new FileWriter(&amp;quot;OutFile.txt&amp;quot;));&lt;br /&gt;
      //Do Stuff….&lt;br /&gt;
 &lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
        System.err.println(&amp;quot;Error occurred!”);&lt;br /&gt;
 &lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
        System.err.println(&amp;quot;Input exception &amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
    } finally {&lt;br /&gt;
 &lt;br /&gt;
        if (out != null) { &lt;br /&gt;
            out.close(); // RELEASE RESOURCES&lt;br /&gt;
        } &lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
A Java example showing finally() being used to release system resources.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Centralised exception handling (Struts Example)==&lt;br /&gt;
&lt;br /&gt;
Building an infrastructure for consistent error reporting proves more difficult than error handling.&lt;br /&gt;
Struts provides the ActionMessages &amp;amp; ActionErrors classes for maintaining a stack of error messages to be reported, which can be used with JSP tags like &amp;lt;html: error&amp;gt; to display these error messages to the user. &lt;br /&gt;
&lt;br /&gt;
To report a different severity of a message in a different manner (like error, warning, or information) the following tasks are required: &lt;br /&gt;
&lt;br /&gt;
* Register, instantiate the errors under the appropriate severity&lt;br /&gt;
* Identify these messages and show them in a constant manner.&lt;br /&gt;
&lt;br /&gt;
Struts ActionErrors class makes error handling quite easy:&lt;br /&gt;
&lt;br /&gt;
 ActionErrors errors = new ActionErrors()&lt;br /&gt;
 errors.add(&amp;quot;fatal&amp;quot;, new ActionError(&amp;quot;....&amp;quot;)); &lt;br /&gt;
 errors.add(&amp;quot;error&amp;quot;, new ActionError(&amp;quot;....&amp;quot;)); &lt;br /&gt;
 errors.add(&amp;quot;warning&amp;quot;, new ActionError(&amp;quot;....&amp;quot;));&lt;br /&gt;
 errors.add(&amp;quot;information&amp;quot;, new ActionError(&amp;quot;....&amp;quot;)); &lt;br /&gt;
 saveErrors(request,errors); // Important to do this&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now we have added the errors we display them by using tags in the HTML page.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;logic:messagePresent property=&amp;quot;error&amp;quot;&amp;gt; &lt;br /&gt;
 &amp;lt;html:messages property=&amp;quot;error&amp;quot; id=&amp;quot;errMsg&amp;quot; &amp;gt;&lt;br /&gt;
     &amp;lt;bean:write name=&amp;quot;errMsg&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/html:messages&amp;gt;&lt;br /&gt;
 &amp;lt;/logic:messagePresent &amp;gt;&lt;br /&gt;
 &lt;br /&gt;
==Logging==&lt;br /&gt;
&lt;br /&gt;
Logging is the recording of information into storage that details who performed what and when they did it (like an audit trail) This can also cover debug messages implemented during development as well as any messages reflecting problems or states within the application. It should be an audit of everything that the business deems important to track about the applications use. Logging provides a detective method to ensure that the other security mechanisms being used are performing correctly. &lt;br /&gt;
&lt;br /&gt;
Logging should be at least done at the following events:&lt;br /&gt;
&lt;br /&gt;
Authentication: Successful &amp;amp; unsuccessful attempts.&lt;br /&gt;
Authorization requests.&lt;br /&gt;
Data manipulation: Any (CUD) Create, Update, Delete actions performed on the application.&lt;br /&gt;
Session activity: Termination/Logout events.&lt;br /&gt;
&lt;br /&gt;
A good logging strategy should include the recording of any errors that occur in the application.&lt;br /&gt;
The application should have the ability to detect and record possible malicious use such as events that cause unexpected errors or defy the state model of the application. Users who attempt to get access to data that they shouldn’t, and incoming data that does not meet validation rules or has been tampered with. In general any error condition which could not occur without an attempt by the user to circumvent the application logic.&lt;br /&gt;
&lt;br /&gt;
Logging should give us the information required to form a proper audit trail of a users actions. &lt;br /&gt;
Leading from this the date/time the actions were performed would be useful.&lt;br /&gt;
Logging functionality should not log a any personal or sensitive data pertaining to the user of function at hand that is being recorded; An example of this if your application is accepting HTTP GET the payload is in the URL and the GET shall be loged. This may result in logging sensitive data.&lt;br /&gt;
&lt;br /&gt;
Logging should follow best practice regarding data validation; maximum length of information, malicious characters….&lt;br /&gt;
We should ensure that logging functionality only log’s messages of a reasonable length and that this length is enforced.&lt;br /&gt;
&lt;br /&gt;
Common open source logging solutions:&lt;br /&gt;
Log4J:		 http://logging.apache.org/log4j/docs/index.html&lt;br /&gt;
&lt;br /&gt;
Log4net:	 http://logging.apache.org/log4net/&lt;br /&gt;
&lt;br /&gt;
Commons Logging: http://jakarta.apache.org/commons/logging/index.html&lt;br /&gt;
&lt;br /&gt;
In Tomcat(5.5), if no custom logger is defined (log4J) then everything is logged via Commons Logging and ultimately ends up in catalina.out.&lt;br /&gt;
catalina.out grows endlessly and does not recycle/rollover. Log4J provides “Rollover” functionality, which limits the size of the log. Log4J also gives the option to specify “appenders” which can redirect the log data to other destinations such as a port, syslog or even a database or JMS.&lt;br /&gt;
&lt;br /&gt;
The parts of log4J which should be considered apart from the actual data being logged by the application are contained in the log4j.properties file:&lt;br /&gt;
&lt;br /&gt;
 #&lt;br /&gt;
 # Configures Log4j as the Tomcat system logger&lt;br /&gt;
 #&lt;br /&gt;
 &lt;br /&gt;
 #&lt;br /&gt;
 # Configure the logger to output info level messages into a rolling log file.&lt;br /&gt;
 #&lt;br /&gt;
 log4j.rootLogger=INFO, R&lt;br /&gt;
 &lt;br /&gt;
 #&lt;br /&gt;
 # To continue using the &amp;quot;catalina.out&amp;quot; file (which grows forever),&lt;br /&gt;
 # comment out the above line and uncomment the next.&lt;br /&gt;
 #&lt;br /&gt;
 #log4j.rootLogger=ERROR, A1&lt;br /&gt;
 &lt;br /&gt;
 #&lt;br /&gt;
 # Configuration for standard output (&amp;quot;catalina.out&amp;quot;).&lt;br /&gt;
 #&lt;br /&gt;
 log4j.appender.A1=org.apache.log4j.ConsoleAppender&lt;br /&gt;
 log4j.appender.A1.layout=org.apache.log4j.PatternLayout&lt;br /&gt;
 #&lt;br /&gt;
 # Print the date in ISO 8601 format&lt;br /&gt;
 #&lt;br /&gt;
 log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n&lt;br /&gt;
 &lt;br /&gt;
 #&lt;br /&gt;
 # Configuration for a rolling log file (&amp;quot;tomcat.log&amp;quot;).&lt;br /&gt;
 #&lt;br /&gt;
 log4j.appender.R=org.apache.log4j.DailyRollingFileAppender&lt;br /&gt;
 log4j.appender.R.DatePattern='.'yyyy-MM-dd&lt;br /&gt;
 #&lt;br /&gt;
 # Edit the next line to point to your logs directory.&lt;br /&gt;
 # The last part of the name is the log file name.&lt;br /&gt;
 #&lt;br /&gt;
 log4j.appender.R.File=/usr/local/tomcat/logs/tomcat.log&lt;br /&gt;
 log4j.appender.R.layout=org.apache.log4j.PatternLayout&lt;br /&gt;
 #&lt;br /&gt;
 # Print the date in ISO 8601 format&lt;br /&gt;
 #&lt;br /&gt;
 log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p %c - %m%n&lt;br /&gt;
 &lt;br /&gt;
 #&lt;br /&gt;
 # Application logging options&lt;br /&gt;
 #&lt;br /&gt;
 #log4j.logger.org.apache=DEBUG&lt;br /&gt;
 #log4j.logger.org.apache=INFO&lt;br /&gt;
 #log4j.logger.org.apache.struts=DEBUG&lt;br /&gt;
 #log4j.logger.org.apache.struts=INFO&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Data_Validation_(Code_Review)&amp;diff=5448</id>
		<title>Data Validation (Code Review)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Data_Validation_(Code_Review)&amp;diff=5448"/>
				<updated>2006-06-09T08:32:01Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
Contact author: [mailto:eoinkeary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
==Canoncalization of input. ==&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
&lt;br /&gt;
''(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).''&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.1 delves much more into this subject.&lt;br /&gt;
&lt;br /&gt;
==Data validation strategy ==&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “'''Known Good'''” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “'''Known bad'''”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy, which are listed from the strongest to the weakest as follows. &lt;br /&gt;
&lt;br /&gt;
# '''Exact Match''' (Constrain)            &lt;br /&gt;
# '''Known Good''' (Accept)&lt;br /&gt;
# '''Reject Known bad''' (Reject)&lt;br /&gt;
# '''Encode Known bad''' (Sanitise)&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
&lt;br /&gt;
'''Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.'''&lt;br /&gt;
&lt;br /&gt;
* '''Exact Match''': (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
* '''Known Good''': If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
* '''Reject Known bad''': We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
* '''Encode Known Bad''': This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
'''HTML-encoding and URL-encoding user input when writing back to the client'''. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
==Good Patterns for Data validation ==&lt;br /&gt;
&lt;br /&gt;
===Data Validation examples ===&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
  $string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
'''preg_grep()''' could also be used for a '''''True''''' or '''''False''''' result. This would enable us to let “'''''only known good'''''” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;u&amp;gt;http://www.regxlib.com/CheatSheet.aspx&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  ^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
  ^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
  [abcde] 	Matches any single character specified in set&lt;br /&gt;
  [^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
===Framework Example:(Struts 1.2) ===&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
# Enables us to have a central area for data validation.&lt;br /&gt;
# Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
  &amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
    validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
  &amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
  package com.pcs.necronomicon&lt;br /&gt;
  import org.apache.struts.validator.ValidatorForm;  &lt;br /&gt;
  public class LogonForm extends ValidatorForm {&lt;br /&gt;
    private String username;&lt;br /&gt;
    private String password;    &lt;br /&gt;
    public String getUsername() {&lt;br /&gt;
      return username;&lt;br /&gt;
    }    &lt;br /&gt;
    public void setUsername(String username) {&lt;br /&gt;
      this.username = username;&lt;br /&gt;
    }  &lt;br /&gt;
    public String getPassword() {&lt;br /&gt;
      return password;&lt;br /&gt;
    }&lt;br /&gt;
  public void setPassword(String password) {&lt;br /&gt;
      this.password = password;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;form-beans&amp;gt;&lt;br /&gt;
   &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot;&lt;br /&gt;
             type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;form-validation&amp;gt;&lt;br /&gt;
  &amp;lt;formset&amp;gt;&lt;br /&gt;
    &amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;field property=&amp;quot;'''username'''&amp;quot; &lt;br /&gt;
            depends=&amp;quot;'''required'''&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
 &amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in '''Application.properties'''. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
  errors.required={0} is required.&lt;br /&gt;
  errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
  errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
  errors.invalid={0} is invalid.&lt;br /&gt;
  errors.byte={0} must be a byte.&lt;br /&gt;
  errors.short={0} must be a short.&lt;br /&gt;
  errors.integer={0} must be an integer.&lt;br /&gt;
  errors.long={0} must be a long.0.   &lt;br /&gt;
  errors.float={0} must be a float.&lt;br /&gt;
  errors.double={0} must be a double.&lt;br /&gt;
  errors.date={0} is not a date.&lt;br /&gt;
  errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
  errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
  errors.email={0} is an invalid e-mail address.&lt;br /&gt;
  prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; &lt;br /&gt;
            depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
         &amp;lt;var&amp;gt;&lt;br /&gt;
            &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
            &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
     &amp;lt;/form&amp;gt;&lt;br /&gt;
   &amp;lt;/formset&amp;gt;&lt;br /&gt;
  &amp;lt;/form-validation&amp;gt;&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Framework example:(.NET)===&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past.&lt;br /&gt;
The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
'''''&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;'''''&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
* RequiredFieldValidator – Makes the associated input control a required field.&lt;br /&gt;
* CompareValidator – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
* RangeValidator – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
* RegularExpressionValidator – Checks user input against a regular expression.&lt;br /&gt;
 &lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;head&amp;gt;&lt;br /&gt;
  &amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
  &amp;lt;/head&amp;gt;&lt;br /&gt;
  &amp;lt;body&amp;gt;  &lt;br /&gt;
  &amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;  &lt;br /&gt;
  &amp;lt;form runat=server&amp;gt;&lt;br /&gt;
  &amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;&lt;br /&gt;
          &amp;lt;asp:RequiredFieldValidator runat=server &lt;br /&gt;
              ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; *&lt;br /&gt;
          &amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
      &amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;        &lt;br /&gt;
  &amp;lt;asp:RegularExpressionValidator runat=server display=dynamic&lt;br /&gt;
              controltovalidate=&amp;quot;Name&amp;quot; &lt;br /&gt;
              errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; &lt;br /&gt;
              validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;  &lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
  &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/body&amp;gt;&lt;br /&gt;
  &amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Length Checking==&lt;br /&gt;
&lt;br /&gt;
Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app.&lt;br /&gt;
&lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an external request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
==Never Rely on Client-Side Data Validation ==&lt;br /&gt;
&lt;br /&gt;
''Client-side validation can always be bypassed.''&lt;br /&gt;
''Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? ''&lt;br /&gt;
''Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.''&lt;br /&gt;
'''Remember: Data validation must be always done on the server side.'''&lt;br /&gt;
'''A code review focuses on server side code. Any client side security code is not and cannot be considered security.'''&lt;br /&gt;
&lt;br /&gt;
Data validation of parameter names:&lt;br /&gt;
&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
''  UserId =3o1nk395y''&lt;br /&gt;
''  password=letMeIn123''&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (''UserId'',''password'' from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Web services data validation==&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
 &amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
 &amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
 	'''&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;'''&lt;br /&gt;
 		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
 		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
 		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
 	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
 	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
 		'''&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;'''&lt;br /&gt;
 			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
 			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
 			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
 		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
 	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
 &amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as '''xsd:string'''.&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Data_Validation_(Code_Review)&amp;diff=5447</id>
		<title>Data Validation (Code Review)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Data_Validation_(Code_Review)&amp;diff=5447"/>
				<updated>2006-06-09T08:31:41Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
Contact author: [mailto:eoinkeary@owasp.org Eoin Keary]&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
==Canoncalization of input. ==&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
&lt;br /&gt;
''(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).''&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.1 delves much more into this subject.&lt;br /&gt;
&lt;br /&gt;
==Data validation strategy ==&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “'''Known Good'''” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “'''Known bad'''”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy, which are listed from the strongest to the weakest as follows. &lt;br /&gt;
&lt;br /&gt;
# '''Exact Match''' (Constrain)            &lt;br /&gt;
# '''Known Good''' (Accept)&lt;br /&gt;
# '''Reject Known bad''' (Reject)&lt;br /&gt;
# '''Encode Known bad''' (Sanitise)&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
&lt;br /&gt;
'''Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.'''&lt;br /&gt;
&lt;br /&gt;
* '''Exact Match''': (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
* '''Known Good''': If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
* '''Reject Known bad''': We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
* '''Encode Known Bad''': This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
'''HTML-encoding and URL-encoding user input when writing back to the client'''. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
==Good Patterns for Data validation ==&lt;br /&gt;
&lt;br /&gt;
===Data Validation examples ===&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
  $string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
'''preg_grep()''' could also be used for a '''''True''''' or '''''False''''' result. This would enable us to let “'''''only known good'''''” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;u&amp;gt;http://www.regxlib.com/CheatSheet.aspx&amp;lt;/u&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  ^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
  ^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
  [abcde] 	Matches any single character specified in set&lt;br /&gt;
  [^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
===Framework Example:(Struts 1.2) ===&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
# Enables us to have a central area for data validation.&lt;br /&gt;
# Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
  &amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
    validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
  &amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
  package com.pcs.necronomicon&lt;br /&gt;
  import org.apache.struts.validator.ValidatorForm;  &lt;br /&gt;
  public class LogonForm extends ValidatorForm {&lt;br /&gt;
    private String username;&lt;br /&gt;
    private String password;    &lt;br /&gt;
    public String getUsername() {&lt;br /&gt;
      return username;&lt;br /&gt;
    }    &lt;br /&gt;
    public void setUsername(String username) {&lt;br /&gt;
      this.username = username;&lt;br /&gt;
    }  &lt;br /&gt;
    public String getPassword() {&lt;br /&gt;
      return password;&lt;br /&gt;
    }&lt;br /&gt;
  public void setPassword(String password) {&lt;br /&gt;
      this.password = password;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;form-beans&amp;gt;&lt;br /&gt;
   &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot;&lt;br /&gt;
             type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;form-validation&amp;gt;&lt;br /&gt;
  &amp;lt;formset&amp;gt;&lt;br /&gt;
    &amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;field property=&amp;quot;'''username'''&amp;quot; &lt;br /&gt;
            depends=&amp;quot;'''required'''&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
 &amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in '''Application.properties'''. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
  errors.required={0} is required.&lt;br /&gt;
  errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
  errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
  errors.invalid={0} is invalid.&lt;br /&gt;
  errors.byte={0} must be a byte.&lt;br /&gt;
  errors.short={0} must be a short.&lt;br /&gt;
  errors.integer={0} must be an integer.&lt;br /&gt;
  errors.long={0} must be a long.0.   &lt;br /&gt;
  errors.float={0} must be a float.&lt;br /&gt;
  errors.double={0} must be a double.&lt;br /&gt;
  errors.date={0} is not a date.&lt;br /&gt;
  errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
  errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
  errors.email={0} is an invalid e-mail address.&lt;br /&gt;
  prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; &lt;br /&gt;
            depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
         &amp;lt;var&amp;gt;&lt;br /&gt;
            &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
            &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
     &amp;lt;/form&amp;gt;&lt;br /&gt;
   &amp;lt;/formset&amp;gt;&lt;br /&gt;
  &amp;lt;/form-validation&amp;gt;&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Framework example:(.NET)===&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past.&lt;br /&gt;
The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
'''''&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;'''''&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
* RequiredFieldValidator – Makes the associated input control a required field.&lt;br /&gt;
* CompareValidator – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
* RangeValidator – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
* RegularExpressionValidator – Checks user input against a regular expression.&lt;br /&gt;
 &lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;head&amp;gt;&lt;br /&gt;
  &amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
  &amp;lt;/head&amp;gt;&lt;br /&gt;
  &amp;lt;body&amp;gt;  &lt;br /&gt;
  &amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;  &lt;br /&gt;
  &amp;lt;form runat=server&amp;gt;&lt;br /&gt;
  &amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;&lt;br /&gt;
          &amp;lt;asp:RequiredFieldValidator runat=server &lt;br /&gt;
              ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; *&lt;br /&gt;
          &amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
      &amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
      &amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;        &lt;br /&gt;
  &amp;lt;asp:RegularExpressionValidator runat=server display=dynamic&lt;br /&gt;
              controltovalidate=&amp;quot;Name&amp;quot; &lt;br /&gt;
              errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; &lt;br /&gt;
              validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;  &lt;br /&gt;
    &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
  &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/body&amp;gt;&lt;br /&gt;
  &amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Length Checking==&lt;br /&gt;
&lt;br /&gt;
Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app.&lt;br /&gt;
&lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an external request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
==Never Rely on Client-Side Data Validation ==&lt;br /&gt;
&lt;br /&gt;
''Client-side validation can always be bypassed.''&lt;br /&gt;
''Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? ''&lt;br /&gt;
''Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.''&lt;br /&gt;
'''Remember: Data validation must be always done on the server side.'''&lt;br /&gt;
'''A code review focuses on server side code. Any client side security code is not and cannot be considered security.'''&lt;br /&gt;
&lt;br /&gt;
Data validation of parameter names:&lt;br /&gt;
&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
''  UserId =3o1nk395y''&lt;br /&gt;
''  password=letMeIn123''&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (''UserId'',''password'' from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Web services data validation==&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
 &amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
 &amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
 	'''&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;'''&lt;br /&gt;
 		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
 		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
 		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
 	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
 	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
 	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
 		'''&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;'''&lt;br /&gt;
 			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
 			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
 			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
 		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
 	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
 &amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as '''xsd:string'''.&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5446</id>
		<title>Reviewing Code for SQL Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5446"/>
				<updated>2006-06-09T08:29:55Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
== What SQL injection is: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL injection is a security vulnerability that occurs in the persistence/database layer of a web application. &lt;br /&gt;
This vulnerability is derived from the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities based on poor input validation and bad design that can occur whenever one programming or scripting language is embedded inside another.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How to Locate potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A secure way to build SQL statements is to construct all queries with PreparedStatement instead of Statement and/or to use parameterized stored procedures. &lt;br /&gt;
Parameterized stored procedures are compiled before user input is added, making it impossible for a hacker to modify the actual SQL statement.&lt;br /&gt;
&lt;br /&gt;
The account used to make the database connection must have “Least privilege” If the application only requires read access then the account must be given read access only.&lt;br /&gt;
&lt;br /&gt;
Avoid disclosing error information: Weak error handling is a great way for an attacker to profile SQL injection attacks. Uncaught SQL errors normally give too much information to the user and contain things like table names and procedure names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best practices when dealing with DB’s ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use Database stored procedures, but even stored procedures can be vulnerable.&lt;br /&gt;
Use parametrized queries instead of dynamic SQL statements.&lt;br /&gt;
Data validate all external input:&lt;br /&gt;
Ensure that all SQL statements recognize user inputs as variables, and that statements are precompiled before the actual inputs are substituted for the variables in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== SQL Injection Example: ==&lt;br /&gt;
&lt;br /&gt;
String DRIVER = &amp;quot;com.ora.jdbc.Driver&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String DataURL = &amp;quot;jdbc:db://localhost:5112/users&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String LOGIN = &amp;quot;admin&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String PASSWORD = &amp;quot;admin123&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Class.forName(DRIVER);&lt;br /&gt;
&lt;br /&gt;
//Make connection to DB&lt;br /&gt;
Connection connection = DriverManager.getConnection(DataURL, LOGIN, PASSWORD);&lt;br /&gt;
&lt;br /&gt;
String Username = request.getParameter(&amp;quot;USER&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
String Password = request.getParameter(&amp;quot;PASSWORD&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
int iUserID = -1;&lt;br /&gt;
&lt;br /&gt;
String sLoggedUser = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; +Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Statement selectStatement = connection.createStatement ();&lt;br /&gt;
ResultSet resultSet = selectStatement.executeQuery(sel);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if (resultSet.next()) {&lt;br /&gt;
&lt;br /&gt;
       iUserID = resultSet.getInt(1);&lt;br /&gt;
       sLoggedUser = resultSet.getString(2);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
PrintWriter writer = response.getWriter ();&lt;br /&gt;
&lt;br /&gt;
if (iUserID &amp;gt;= 0) {&lt;br /&gt;
       writer.println (&amp;quot;User logged in: &amp;quot; + sLoggedUser);&lt;br /&gt;
} else {&lt;br /&gt;
&lt;br /&gt;
       writer.println (&amp;quot;Access Denied!&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When SQL statements are dynamically created as software executes, there is an opportunity for a &lt;br /&gt;
security breach as the input data can truncate or malform or even expand the original SQL query!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Firstly the request.getParameter retrieves the data for the SQL query directly from the HTTP request without any Data validation (Min/Max length, Permitted characters, malicious characters). This error gives rise to the ability to input SQL as the payload and alter the functionality in the statement.&lt;br /&gt;
&lt;br /&gt;
The application places the payload directly into the statement causing the SQL vulnerability:&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== .NET ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code and therefore the payload can not be injected. Using a parameters collection lets you enforce type and length checks. Values outside of the range trigger an exception. Make sure you handle the exception correctly.&lt;br /&gt;
Example of the SqlParameterCollection:&lt;br /&gt;
&lt;br /&gt;
using System.Data;&lt;br /&gt;
&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
using (SqlConnection conn = new SqlConnection(connectionString))&lt;br /&gt;
{&lt;br /&gt;
  DataSet dataObj = new DataSet();&lt;br /&gt;
&lt;br /&gt;
  SqlDataAdapter sqlAdapter = new SqlDataAdapter( &amp;quot;StoredProc&amp;quot;, conn);&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;&lt;br /&gt;
&lt;br /&gt;
 //specify param type&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters.Add(&amp;quot;@usrId&amp;quot;, SqlDbType.VarChar, 15); &lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters[&amp;quot;@usrId &amp;quot;].Value = UID.Text; // Add data from user&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.Fill(dataObj); // populate and execute proc&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Stored procedures don’t always protect against SQL injection:'''&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE dbo.RunAnyQuery &lt;br /&gt;
@parameter NVARCHAR(50)&lt;br /&gt;
&lt;br /&gt;
AS&lt;br /&gt;
        EXEC sp_executesql @parameter&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The above procedure shall execute any SQL you pass to it. The directive sp_executesql is a system stored procedure in Microsoft® SQL Server™&lt;br /&gt;
&lt;br /&gt;
Lets pass it.&lt;br /&gt;
&lt;br /&gt;
DROP TABLE ORDERS;&lt;br /&gt;
&lt;br /&gt;
Guess what happens? So we must be careful of not falling into the “We’re secure, we are using stored procedures” trap!&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Buffer_Overruns_and_Overflows&amp;diff=5445</id>
		<title>Buffer Overruns and Overflows</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Buffer_Overruns_and_Overflows&amp;diff=5445"/>
				<updated>2006-06-09T08:27:55Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
Contact author: [mailto:eoinkeary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
==The buffer ==&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
==How to locate the potentially vulnerable code==&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays''':&lt;br /&gt;
  int x[20];&lt;br /&gt;
  int y[20][5];&lt;br /&gt;
  int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
  printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
  %x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
'''Over flows:'''&lt;br /&gt;
&lt;br /&gt;
  strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Vulnerable Patterns for buffer overflows==&lt;br /&gt;
&lt;br /&gt;
===‘Vanilla’ buffer overflow: ===&lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  void copyData(char *userId) {  &lt;br /&gt;
     char  smallBuffer['''10''']; // size of 10  &lt;br /&gt;
     '''strcpy'''(smallBuffer, userId);&lt;br /&gt;
  }  &lt;br /&gt;
  int main(int argc, char *argv[]) {  &lt;br /&gt;
  char *userId = &amp;quot;'''01234567890'''&amp;quot;; // Payload of 11&lt;br /&gt;
  copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The Format String: ===&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
  %x        hexadecimal (unsigned int)&lt;br /&gt;
  %s        string ((const) (unsigned) char *)&lt;br /&gt;
  %n        number of bytes written so far, (* int)&lt;br /&gt;
  %d        decimal (int)&lt;br /&gt;
  %u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
===Crashing an application: ===&lt;br /&gt;
&lt;br /&gt;
  printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the '''printf''' function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
===Walking the stack: ===&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Worse again is using the '''%n''' directive in '''printf()'''. This directive takes an '''int*''' and '''writes''' the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the '''printf()''' family of functions,  '''printf(),fprintf(), sprintf(), snprintf().''' Also '''syslog()''' (writes system log information) and setproctitle(''const char *fmt'', ''...''); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Integer overflows: ===&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
            int val;&lt;br /&gt;
            val = 0x7fffffff; 	/* 2147483647*/&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
            return 0;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
        }&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the '''''if '''''condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
The line '''myArray[v2]  = v1''' assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
== Good Patterns &amp;amp; procedures to prevent buffer overflows: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
C library functions such as '''strcpy (), strcat (), sprintf ()''' and '''vsprintf ()''' operate on null terminated strings and perform no bounds checking. '''gets ()''' is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The '''scanf ()''' family of functions also may result in buffer overflows.&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==.NET &amp;amp; Java ==&lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is ''managed''. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5444</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5444"/>
				<updated>2006-06-09T08:27:11Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[SQL Injection]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5443</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5443"/>
				<updated>2006-06-09T08:26:32Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Error Handling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[SQL Injection]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5442</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5442"/>
				<updated>2006-06-09T08:25:44Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[SQL Injection]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5441</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5441"/>
				<updated>2006-06-09T08:24:52Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Error Handling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[SQL Injection]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5440</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5440"/>
				<updated>2006-06-09T08:24:28Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[SQL Injection]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5439</id>
		<title>Reviewing Code for SQL Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5439"/>
				<updated>2006-06-09T08:22:39Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* SQL Injection Example: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What SQL injection is: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL injection is a security vulnerability that occurs in the persistence/database layer of a web application. &lt;br /&gt;
This vulnerability is derived from the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities based on poor input validation and bad design that can occur whenever one programming or scripting language is embedded inside another.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How to Locate potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A secure way to build SQL statements is to construct all queries with PreparedStatement instead of Statement and/or to use parameterized stored procedures. &lt;br /&gt;
Parameterized stored procedures are compiled before user input is added, making it impossible for a hacker to modify the actual SQL statement.&lt;br /&gt;
&lt;br /&gt;
The account used to make the database connection must have “Least privilege” If the application only requires read access then the account must be given read access only.&lt;br /&gt;
&lt;br /&gt;
Avoid disclosing error information: Weak error handling is a great way for an attacker to profile SQL injection attacks. Uncaught SQL errors normally give too much information to the user and contain things like table names and procedure names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best practices when dealing with DB’s ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use Database stored procedures, but even stored procedures can be vulnerable.&lt;br /&gt;
Use parametrized queries instead of dynamic SQL statements.&lt;br /&gt;
Data validate all external input:&lt;br /&gt;
Ensure that all SQL statements recognize user inputs as variables, and that statements are precompiled before the actual inputs are substituted for the variables in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== SQL Injection Example: ==&lt;br /&gt;
&lt;br /&gt;
String DRIVER = &amp;quot;com.ora.jdbc.Driver&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String DataURL = &amp;quot;jdbc:db://localhost:5112/users&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String LOGIN = &amp;quot;admin&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String PASSWORD = &amp;quot;admin123&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Class.forName(DRIVER);&lt;br /&gt;
&lt;br /&gt;
//Make connection to DB&lt;br /&gt;
Connection connection = DriverManager.getConnection(DataURL, LOGIN, PASSWORD);&lt;br /&gt;
&lt;br /&gt;
String Username = request.getParameter(&amp;quot;USER&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
String Password = request.getParameter(&amp;quot;PASSWORD&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
int iUserID = -1;&lt;br /&gt;
&lt;br /&gt;
String sLoggedUser = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; +Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Statement selectStatement = connection.createStatement ();&lt;br /&gt;
ResultSet resultSet = selectStatement.executeQuery(sel);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if (resultSet.next()) {&lt;br /&gt;
&lt;br /&gt;
       iUserID = resultSet.getInt(1);&lt;br /&gt;
       sLoggedUser = resultSet.getString(2);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
PrintWriter writer = response.getWriter ();&lt;br /&gt;
&lt;br /&gt;
if (iUserID &amp;gt;= 0) {&lt;br /&gt;
       writer.println (&amp;quot;User logged in: &amp;quot; + sLoggedUser);&lt;br /&gt;
} else {&lt;br /&gt;
&lt;br /&gt;
       writer.println (&amp;quot;Access Denied!&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When SQL statements are dynamically created as software executes, there is an opportunity for a &lt;br /&gt;
security breach as the input data can truncate or malform or even expand the original SQL query!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Firstly the request.getParameter retrieves the data for the SQL query directly from the HTTP request without any Data validation (Min/Max length, Permitted characters, malicious characters). This error gives rise to the ability to input SQL as the payload and alter the functionality in the statement.&lt;br /&gt;
&lt;br /&gt;
The application places the payload directly into the statement causing the SQL vulnerability:&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== .NET ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code and therefore the payload can not be injected. Using a parameters collection lets you enforce type and length checks. Values outside of the range trigger an exception. Make sure you handle the exception correctly.&lt;br /&gt;
Example of the SqlParameterCollection:&lt;br /&gt;
&lt;br /&gt;
using System.Data;&lt;br /&gt;
&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
using (SqlConnection conn = new SqlConnection(connectionString))&lt;br /&gt;
{&lt;br /&gt;
  DataSet dataObj = new DataSet();&lt;br /&gt;
&lt;br /&gt;
  SqlDataAdapter sqlAdapter = new SqlDataAdapter( &amp;quot;StoredProc&amp;quot;, conn);&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;&lt;br /&gt;
&lt;br /&gt;
 //specify param type&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters.Add(&amp;quot;@usrId&amp;quot;, SqlDbType.VarChar, 15); &lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters[&amp;quot;@usrId &amp;quot;].Value = UID.Text; // Add data from user&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.Fill(dataObj); // populate and execute proc&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Stored procedures don’t always protect against SQL injection:'''&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE dbo.RunAnyQuery &lt;br /&gt;
@parameter NVARCHAR(50)&lt;br /&gt;
&lt;br /&gt;
AS&lt;br /&gt;
        EXEC sp_executesql @parameter&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The above procedure shall execute any SQL you pass to it. The directive sp_executesql is a system stored procedure in Microsoft® SQL Server™&lt;br /&gt;
&lt;br /&gt;
Lets pass it.&lt;br /&gt;
&lt;br /&gt;
DROP TABLE ORDERS;&lt;br /&gt;
&lt;br /&gt;
Guess what happens? So we must be careful of not falling into the “We’re secure, we are using stored procedures” trap!&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5438</id>
		<title>Reviewing Code for SQL Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5438"/>
				<updated>2006-06-09T08:18:47Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* SQL Injection Example: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What SQL injection is: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL injection is a security vulnerability that occurs in the persistence/database layer of a web application. &lt;br /&gt;
This vulnerability is derived from the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities based on poor input validation and bad design that can occur whenever one programming or scripting language is embedded inside another.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How to Locate potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A secure way to build SQL statements is to construct all queries with PreparedStatement instead of Statement and/or to use parameterized stored procedures. &lt;br /&gt;
Parameterized stored procedures are compiled before user input is added, making it impossible for a hacker to modify the actual SQL statement.&lt;br /&gt;
&lt;br /&gt;
The account used to make the database connection must have “Least privilege” If the application only requires read access then the account must be given read access only.&lt;br /&gt;
&lt;br /&gt;
Avoid disclosing error information: Weak error handling is a great way for an attacker to profile SQL injection attacks. Uncaught SQL errors normally give too much information to the user and contain things like table names and procedure names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best practices when dealing with DB’s ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use Database stored procedures, but even stored procedures can be vulnerable.&lt;br /&gt;
Use parametrized queries instead of dynamic SQL statements.&lt;br /&gt;
Data validate all external input:&lt;br /&gt;
Ensure that all SQL statements recognize user inputs as variables, and that statements are precompiled before the actual inputs are substituted for the variables in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== SQL Injection Example: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String DRIVER = &amp;quot;com.ora.jdbc.Driver&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String DataURL = &amp;quot;jdbc:db://localhost:5112/users&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String LOGIN = &amp;quot;admin&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String PASSWORD = &amp;quot;admin123&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Class.forName(DRIVER);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
//Make connection to DB&lt;br /&gt;
Connection connection = DriverManager.getConnection(DataURL, LOGIN, PASSWORD);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String Username = request.getParameter(&amp;quot;USER&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
String Password = request.getParameter(&amp;quot;PASSWORD&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int iUserID = -1;&lt;br /&gt;
&lt;br /&gt;
String sLoggedUser = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; +Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Statement selectStatement = connection.createStatement ();&lt;br /&gt;
&lt;br /&gt;
ResultSet resultSet = selectStatement.executeQuery(sel);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if (resultSet.next()) {&lt;br /&gt;
&lt;br /&gt;
       iUserID = resultSet.getInt(1);&lt;br /&gt;
&lt;br /&gt;
       sLoggedUser = resultSet.getString(2);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
PrintWriter writer = response.getWriter ();&lt;br /&gt;
&lt;br /&gt;
if (iUserID &amp;gt;= 0) {&lt;br /&gt;
&lt;br /&gt;
       writer.println (&amp;quot;User logged in: &amp;quot; + sLoggedUser);&lt;br /&gt;
&lt;br /&gt;
} else {&lt;br /&gt;
&lt;br /&gt;
       writer.println (&amp;quot;Access Denied!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When SQL statements are dynamically created as software executes, there is an opportunity for a &lt;br /&gt;
security breach as the input data can truncate or malform or even expand the original SQL query!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Firstly the request.getParameter retrieves the data for the SQL query directly from the HTTP request without any Data validation (Min/Max length, Permitted characters, malicious characters). This error gives rise to the ability to input SQL as the payload and alter the functionality in the statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The application places the payload directly into the statement causing the SQL vulnerability:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== .NET&lt;br /&gt;
 ==&lt;br /&gt;
&lt;br /&gt;
Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code and therefore the payload can not be injected. Using a parameters collection lets you enforce type and length checks. Values outside of the range trigger an exception. Make sure you handle the exception correctly.&lt;br /&gt;
Example of the SqlParameterCollection:&lt;br /&gt;
&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
using (SqlConnection conn = new SqlConnection(connectionString))&lt;br /&gt;
{&lt;br /&gt;
  DataSet dataObj = new DataSet();&lt;br /&gt;
  SqlDataAdapter sqlAdapter = new SqlDataAdapter( &amp;quot;StoredProc&amp;quot;, conn);&lt;br /&gt;
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;&lt;br /&gt;
 //specify param type&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters.Add(&amp;quot;@usrId&amp;quot;, SqlDbType.VarChar, 15); &lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters[&amp;quot;@usrId &amp;quot;].Value = UID.Text; // Add data from user&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.Fill(dataObj); // populate and execute proc&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Stored procedures don’t always protect against SQL injection:&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE dbo.RunAnyQuery &lt;br /&gt;
@parameter NVARCHAR(50)&lt;br /&gt;
&lt;br /&gt;
AS&lt;br /&gt;
        EXEC sp_executesql @parameter&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The above procedure shall execute any SQL you pass to it. The directive sp_executesql is a system stored procedure in Microsoft® SQL Server™&lt;br /&gt;
Lets pass it.&lt;br /&gt;
&lt;br /&gt;
DROP TABLE ORDERS;&lt;br /&gt;
Guess what happens? So we must be careful of not falling into the “We’re secure, we are using stored procedures” trap!&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5437</id>
		<title>Reviewing Code for SQL Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5437"/>
				<updated>2006-06-09T08:17:04Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* SQL Injection Example: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What SQL injection is: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL injection is a security vulnerability that occurs in the persistence/database layer of a web application. &lt;br /&gt;
This vulnerability is derived from the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities based on poor input validation and bad design that can occur whenever one programming or scripting language is embedded inside another.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How to Locate potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A secure way to build SQL statements is to construct all queries with PreparedStatement instead of Statement and/or to use parameterized stored procedures. &lt;br /&gt;
Parameterized stored procedures are compiled before user input is added, making it impossible for a hacker to modify the actual SQL statement.&lt;br /&gt;
&lt;br /&gt;
The account used to make the database connection must have “Least privilege” If the application only requires read access then the account must be given read access only.&lt;br /&gt;
&lt;br /&gt;
Avoid disclosing error information: Weak error handling is a great way for an attacker to profile SQL injection attacks. Uncaught SQL errors normally give too much information to the user and contain things like table names and procedure names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best practices when dealing with DB’s ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use Database stored procedures, but even stored procedures can be vulnerable.&lt;br /&gt;
Use parametrized queries instead of dynamic SQL statements.&lt;br /&gt;
Data validate all external input:&lt;br /&gt;
Ensure that all SQL statements recognize user inputs as variables, and that statements are precompiled before the actual inputs are substituted for the variables in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== SQL Injection Example: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String DRIVER = &amp;quot;com.ora.jdbc.Driver&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String DataURL = &amp;quot;jdbc:db://localhost:5112/users&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String LOGIN = &amp;quot;admin&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String PASSWORD = &amp;quot;admin123&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Class.forName(DRIVER);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
//Make connection to DB&lt;br /&gt;
Connection connection = DriverManager.getConnection(DataURL, LOGIN, PASSWORD);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String Username = request.getParameter(&amp;quot;USER&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
String Password = request.getParameter(&amp;quot;PASSWORD&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int iUserID = -1;&lt;br /&gt;
String sLoggedUser = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; +Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Statement selectStatement = connection.createStatement ();&lt;br /&gt;
ResultSet resultSet = selectStatement.executeQuery(sel);&lt;br /&gt;
&lt;br /&gt;
if (resultSet.next()) {&lt;br /&gt;
iUserID = resultSet.getInt(1);&lt;br /&gt;
sLoggedUser = resultSet.getString(2);&lt;br /&gt;
}&lt;br /&gt;
PrintWriter writer = response.getWriter ();&lt;br /&gt;
if (iUserID &amp;gt;= 0) {&lt;br /&gt;
writer.println (&amp;quot;User logged in: &amp;quot; + sLoggedUser);&lt;br /&gt;
} else {&lt;br /&gt;
writer.println (&amp;quot;Access Denied!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When SQL statements are dynamically created as software executes, there is an opportunity for a security breach as the input data can truncate or malform or even expand the original SQL query!&lt;br /&gt;
&lt;br /&gt;
Firstly the request.getParameter retrieves the data for the SQL query directly from the HTTP request without any Data validation (Min/Max length, Permitted characters, malicious characters). This error gives rise to the ability to input SQL as the payload and alter the functionality in the statement.&lt;br /&gt;
&lt;br /&gt;
The application places the payload directly into the statement causing the SQL vulnerability:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot;&lt;br /&gt;
Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== .NET&lt;br /&gt;
 ==&lt;br /&gt;
&lt;br /&gt;
Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code and therefore the payload can not be injected. Using a parameters collection lets you enforce type and length checks. Values outside of the range trigger an exception. Make sure you handle the exception correctly.&lt;br /&gt;
Example of the SqlParameterCollection:&lt;br /&gt;
&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
using (SqlConnection conn = new SqlConnection(connectionString))&lt;br /&gt;
{&lt;br /&gt;
  DataSet dataObj = new DataSet();&lt;br /&gt;
  SqlDataAdapter sqlAdapter = new SqlDataAdapter( &amp;quot;StoredProc&amp;quot;, conn);&lt;br /&gt;
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;&lt;br /&gt;
 //specify param type&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters.Add(&amp;quot;@usrId&amp;quot;, SqlDbType.VarChar, 15); &lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters[&amp;quot;@usrId &amp;quot;].Value = UID.Text; // Add data from user&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.Fill(dataObj); // populate and execute proc&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Stored procedures don’t always protect against SQL injection:&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE dbo.RunAnyQuery &lt;br /&gt;
@parameter NVARCHAR(50)&lt;br /&gt;
&lt;br /&gt;
AS&lt;br /&gt;
        EXEC sp_executesql @parameter&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The above procedure shall execute any SQL you pass to it. The directive sp_executesql is a system stored procedure in Microsoft® SQL Server™&lt;br /&gt;
Lets pass it.&lt;br /&gt;
&lt;br /&gt;
DROP TABLE ORDERS;&lt;br /&gt;
Guess what happens? So we must be careful of not falling into the “We’re secure, we are using stored procedures” trap!&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5436</id>
		<title>Reviewing Code for SQL Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5436"/>
				<updated>2006-06-09T08:16:36Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* SQL Injection Example: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What SQL injection is: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL injection is a security vulnerability that occurs in the persistence/database layer of a web application. &lt;br /&gt;
This vulnerability is derived from the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities based on poor input validation and bad design that can occur whenever one programming or scripting language is embedded inside another.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How to Locate potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A secure way to build SQL statements is to construct all queries with PreparedStatement instead of Statement and/or to use parameterized stored procedures. &lt;br /&gt;
Parameterized stored procedures are compiled before user input is added, making it impossible for a hacker to modify the actual SQL statement.&lt;br /&gt;
&lt;br /&gt;
The account used to make the database connection must have “Least privilege” If the application only requires read access then the account must be given read access only.&lt;br /&gt;
&lt;br /&gt;
Avoid disclosing error information: Weak error handling is a great way for an attacker to profile SQL injection attacks. Uncaught SQL errors normally give too much information to the user and contain things like table names and procedure names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best practices when dealing with DB’s ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use Database stored procedures, but even stored procedures can be vulnerable.&lt;br /&gt;
Use parametrized queries instead of dynamic SQL statements.&lt;br /&gt;
Data validate all external input:&lt;br /&gt;
Ensure that all SQL statements recognize user inputs as variables, and that statements are precompiled before the actual inputs are substituted for the variables in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== SQL Injection Example: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String DRIVER = &amp;quot;com.ora.jdbc.Driver&amp;quot;;&lt;br /&gt;
String DataURL = &amp;quot;jdbc:db://localhost:5112/users&amp;quot;;&lt;br /&gt;
String LOGIN = &amp;quot;admin&amp;quot;;&lt;br /&gt;
String PASSWORD = &amp;quot;admin123&amp;quot;;&lt;br /&gt;
Class.forName(DRIVER);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
//Make connection to DB&lt;br /&gt;
Connection connection = DriverManager.getConnection(DataURL, LOGIN, PASSWORD);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String Username = request.getParameter(&amp;quot;USER&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
String Password = request.getParameter(&amp;quot;PASSWORD&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int iUserID = -1;&lt;br /&gt;
String sLoggedUser = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; +Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Statement selectStatement = connection.createStatement ();&lt;br /&gt;
ResultSet resultSet = selectStatement.executeQuery(sel);&lt;br /&gt;
&lt;br /&gt;
if (resultSet.next()) {&lt;br /&gt;
iUserID = resultSet.getInt(1);&lt;br /&gt;
sLoggedUser = resultSet.getString(2);&lt;br /&gt;
}&lt;br /&gt;
PrintWriter writer = response.getWriter ();&lt;br /&gt;
if (iUserID &amp;gt;= 0) {&lt;br /&gt;
writer.println (&amp;quot;User logged in: &amp;quot; + sLoggedUser);&lt;br /&gt;
} else {&lt;br /&gt;
writer.println (&amp;quot;Access Denied!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When SQL statements are dynamically created as software executes, there is an opportunity for a security breach as the input data can truncate or malform or even expand the original SQL query!&lt;br /&gt;
&lt;br /&gt;
Firstly the request.getParameter retrieves the data for the SQL query directly from the HTTP request without any Data validation (Min/Max length, Permitted characters, malicious characters). This error gives rise to the ability to input SQL as the payload and alter the functionality in the statement.&lt;br /&gt;
&lt;br /&gt;
The application places the payload directly into the statement causing the SQL vulnerability:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot;&lt;br /&gt;
Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== .NET&lt;br /&gt;
 ==&lt;br /&gt;
&lt;br /&gt;
Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code and therefore the payload can not be injected. Using a parameters collection lets you enforce type and length checks. Values outside of the range trigger an exception. Make sure you handle the exception correctly.&lt;br /&gt;
Example of the SqlParameterCollection:&lt;br /&gt;
&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
using (SqlConnection conn = new SqlConnection(connectionString))&lt;br /&gt;
{&lt;br /&gt;
  DataSet dataObj = new DataSet();&lt;br /&gt;
  SqlDataAdapter sqlAdapter = new SqlDataAdapter( &amp;quot;StoredProc&amp;quot;, conn);&lt;br /&gt;
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;&lt;br /&gt;
 //specify param type&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters.Add(&amp;quot;@usrId&amp;quot;, SqlDbType.VarChar, 15); &lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters[&amp;quot;@usrId &amp;quot;].Value = UID.Text; // Add data from user&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.Fill(dataObj); // populate and execute proc&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Stored procedures don’t always protect against SQL injection:&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE dbo.RunAnyQuery &lt;br /&gt;
@parameter NVARCHAR(50)&lt;br /&gt;
&lt;br /&gt;
AS&lt;br /&gt;
        EXEC sp_executesql @parameter&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The above procedure shall execute any SQL you pass to it. The directive sp_executesql is a system stored procedure in Microsoft® SQL Server™&lt;br /&gt;
Lets pass it.&lt;br /&gt;
&lt;br /&gt;
DROP TABLE ORDERS;&lt;br /&gt;
Guess what happens? So we must be careful of not falling into the “We’re secure, we are using stored procedures” trap!&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5435</id>
		<title>Reviewing Code for SQL Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5435"/>
				<updated>2006-06-09T08:16:11Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* SQL INJECTION EXAMPLE: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What SQL injection is: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL injection is a security vulnerability that occurs in the persistence/database layer of a web application. &lt;br /&gt;
This vulnerability is derived from the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities based on poor input validation and bad design that can occur whenever one programming or scripting language is embedded inside another.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How to Locate potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A secure way to build SQL statements is to construct all queries with PreparedStatement instead of Statement and/or to use parameterized stored procedures. &lt;br /&gt;
Parameterized stored procedures are compiled before user input is added, making it impossible for a hacker to modify the actual SQL statement.&lt;br /&gt;
&lt;br /&gt;
The account used to make the database connection must have “Least privilege” If the application only requires read access then the account must be given read access only.&lt;br /&gt;
&lt;br /&gt;
Avoid disclosing error information: Weak error handling is a great way for an attacker to profile SQL injection attacks. Uncaught SQL errors normally give too much information to the user and contain things like table names and procedure names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best practices when dealing with DB’s ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use Database stored procedures, but even stored procedures can be vulnerable.&lt;br /&gt;
Use parametrized queries instead of dynamic SQL statements.&lt;br /&gt;
Data validate all external input:&lt;br /&gt;
Ensure that all SQL statements recognize user inputs as variables, and that statements are precompiled before the actual inputs are substituted for the variables in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== SQL Injection Example: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String DRIVER = &amp;quot;com.ora.jdbc.Driver&amp;quot;;&lt;br /&gt;
String DataURL = &amp;quot;jdbc:db://localhost:5112/users&amp;quot;;&lt;br /&gt;
String LOGIN = &amp;quot;admin&amp;quot;;&lt;br /&gt;
String PASSWORD = &amp;quot;admin123&amp;quot;;&lt;br /&gt;
Class.forName(DRIVER);&lt;br /&gt;
&lt;br /&gt;
//Make connection to DB&lt;br /&gt;
Connection connection = DriverManager.getConnection(DataURL, LOGIN, PASSWORD);&lt;br /&gt;
&lt;br /&gt;
String Username = request.getParameter(&amp;quot;USER&amp;quot;); // From HTTP request&lt;br /&gt;
String Password = request.getParameter(&amp;quot;PASSWORD&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
int iUserID = -1;&lt;br /&gt;
String sLoggedUser = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; +Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Statement selectStatement = connection.createStatement ();&lt;br /&gt;
ResultSet resultSet = selectStatement.executeQuery(sel);&lt;br /&gt;
&lt;br /&gt;
if (resultSet.next()) {&lt;br /&gt;
iUserID = resultSet.getInt(1);&lt;br /&gt;
sLoggedUser = resultSet.getString(2);&lt;br /&gt;
}&lt;br /&gt;
PrintWriter writer = response.getWriter ();&lt;br /&gt;
if (iUserID &amp;gt;= 0) {&lt;br /&gt;
writer.println (&amp;quot;User logged in: &amp;quot; + sLoggedUser);&lt;br /&gt;
} else {&lt;br /&gt;
writer.println (&amp;quot;Access Denied!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When SQL statements are dynamically created as software executes, there is an opportunity for a security breach as the input data can truncate or malform or even expand the original SQL query!&lt;br /&gt;
&lt;br /&gt;
Firstly the request.getParameter retrieves the data for the SQL query directly from the HTTP request without any Data validation (Min/Max length, Permitted characters, malicious characters). This error gives rise to the ability to input SQL as the payload and alter the functionality in the statement.&lt;br /&gt;
&lt;br /&gt;
The application places the payload directly into the statement causing the SQL vulnerability:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot;&lt;br /&gt;
Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== .NET&lt;br /&gt;
 ==&lt;br /&gt;
&lt;br /&gt;
Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code and therefore the payload can not be injected. Using a parameters collection lets you enforce type and length checks. Values outside of the range trigger an exception. Make sure you handle the exception correctly.&lt;br /&gt;
Example of the SqlParameterCollection:&lt;br /&gt;
&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
using (SqlConnection conn = new SqlConnection(connectionString))&lt;br /&gt;
{&lt;br /&gt;
  DataSet dataObj = new DataSet();&lt;br /&gt;
  SqlDataAdapter sqlAdapter = new SqlDataAdapter( &amp;quot;StoredProc&amp;quot;, conn);&lt;br /&gt;
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;&lt;br /&gt;
 //specify param type&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters.Add(&amp;quot;@usrId&amp;quot;, SqlDbType.VarChar, 15); &lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters[&amp;quot;@usrId &amp;quot;].Value = UID.Text; // Add data from user&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.Fill(dataObj); // populate and execute proc&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Stored procedures don’t always protect against SQL injection:&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE dbo.RunAnyQuery &lt;br /&gt;
@parameter NVARCHAR(50)&lt;br /&gt;
&lt;br /&gt;
AS&lt;br /&gt;
        EXEC sp_executesql @parameter&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The above procedure shall execute any SQL you pass to it. The directive sp_executesql is a system stored procedure in Microsoft® SQL Server™&lt;br /&gt;
Lets pass it.&lt;br /&gt;
&lt;br /&gt;
DROP TABLE ORDERS;&lt;br /&gt;
Guess what happens? So we must be careful of not falling into the “We’re secure, we are using stored procedures” trap!&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5434</id>
		<title>Reviewing Code for SQL Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5434"/>
				<updated>2006-06-09T08:14:50Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What SQL injection is: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL injection is a security vulnerability that occurs in the persistence/database layer of a web application. &lt;br /&gt;
This vulnerability is derived from the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities based on poor input validation and bad design that can occur whenever one programming or scripting language is embedded inside another.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How to Locate potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A secure way to build SQL statements is to construct all queries with PreparedStatement instead of Statement and/or to use parameterized stored procedures. &lt;br /&gt;
Parameterized stored procedures are compiled before user input is added, making it impossible for a hacker to modify the actual SQL statement.&lt;br /&gt;
&lt;br /&gt;
The account used to make the database connection must have “Least privilege” If the application only requires read access then the account must be given read access only.&lt;br /&gt;
&lt;br /&gt;
Avoid disclosing error information: Weak error handling is a great way for an attacker to profile SQL injection attacks. Uncaught SQL errors normally give too much information to the user and contain things like table names and procedure names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best practices when dealing with DB’s ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use Database stored procedures, but even stored procedures can be vulnerable.&lt;br /&gt;
Use parametrized queries instead of dynamic SQL statements.&lt;br /&gt;
Data validate all external input:&lt;br /&gt;
Ensure that all SQL statements recognize user inputs as variables, and that statements are precompiled before the actual inputs are substituted for the variables in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== SQL INJECTION EXAMPLE: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String DRIVER = &amp;quot;com.ora.jdbc.Driver&amp;quot;;&lt;br /&gt;
String DataURL = &amp;quot;jdbc:db://localhost:5112/users&amp;quot;;&lt;br /&gt;
String LOGIN = &amp;quot;admin&amp;quot;;&lt;br /&gt;
String PASSWORD = &amp;quot;admin123&amp;quot;;&lt;br /&gt;
Class.forName(DRIVER);&lt;br /&gt;
&lt;br /&gt;
//Make connection to DB&lt;br /&gt;
Connection connection = DriverManager.getConnection(DataURL, LOGIN, PASSWORD);&lt;br /&gt;
&lt;br /&gt;
String Username = request.getParameter(&amp;quot;USER&amp;quot;); // From HTTP request&lt;br /&gt;
String Password = request.getParameter(&amp;quot;PASSWORD&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
int iUserID = -1;&lt;br /&gt;
String sLoggedUser = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; +Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Statement selectStatement = connection.createStatement ();&lt;br /&gt;
ResultSet resultSet = selectStatement.executeQuery(sel);&lt;br /&gt;
&lt;br /&gt;
if (resultSet.next()) {&lt;br /&gt;
iUserID = resultSet.getInt(1);&lt;br /&gt;
sLoggedUser = resultSet.getString(2);&lt;br /&gt;
}&lt;br /&gt;
PrintWriter writer = response.getWriter ();&lt;br /&gt;
if (iUserID &amp;gt;= 0) {&lt;br /&gt;
writer.println (&amp;quot;User logged in: &amp;quot; + sLoggedUser);&lt;br /&gt;
} else {&lt;br /&gt;
writer.println (&amp;quot;Access Denied!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
When SQL statements are dynamically created as software executes, there is an opportunity for a security breach as the input data can truncate or malform or even expand the original SQL query!&lt;br /&gt;
&lt;br /&gt;
Firstly the request.getParameter retrieves the data for the SQL query directly from the HTTP request without any Data validation (Min/Max length, Permitted characters, malicious characters). This error gives rise to the ability to input SQL as the payload and alter the functionality in the statement.&lt;br /&gt;
&lt;br /&gt;
The application places the payload directly into the statement causing the SQL vulnerability:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot;&lt;br /&gt;
Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== .NET&lt;br /&gt;
 ==&lt;br /&gt;
&lt;br /&gt;
Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code and therefore the payload can not be injected. Using a parameters collection lets you enforce type and length checks. Values outside of the range trigger an exception. Make sure you handle the exception correctly.&lt;br /&gt;
Example of the SqlParameterCollection:&lt;br /&gt;
&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
using (SqlConnection conn = new SqlConnection(connectionString))&lt;br /&gt;
{&lt;br /&gt;
  DataSet dataObj = new DataSet();&lt;br /&gt;
  SqlDataAdapter sqlAdapter = new SqlDataAdapter( &amp;quot;StoredProc&amp;quot;, conn);&lt;br /&gt;
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;&lt;br /&gt;
 //specify param type&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters.Add(&amp;quot;@usrId&amp;quot;, SqlDbType.VarChar, 15); &lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters[&amp;quot;@usrId &amp;quot;].Value = UID.Text; // Add data from user&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.Fill(dataObj); // populate and execute proc&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Stored procedures don’t always protect against SQL injection:&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE dbo.RunAnyQuery &lt;br /&gt;
@parameter NVARCHAR(50)&lt;br /&gt;
&lt;br /&gt;
AS&lt;br /&gt;
        EXEC sp_executesql @parameter&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The above procedure shall execute any SQL you pass to it. The directive sp_executesql is a system stored procedure in Microsoft® SQL Server™&lt;br /&gt;
Lets pass it.&lt;br /&gt;
&lt;br /&gt;
DROP TABLE ORDERS;&lt;br /&gt;
Guess what happens? So we must be careful of not falling into the “We’re secure, we are using stored procedures” trap!&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5433</id>
		<title>Reviewing Code for SQL Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Reviewing_Code_for_SQL_Injection&amp;diff=5433"/>
				<updated>2006-06-09T08:13:34Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== What SQL injection is: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL injection is a security vulnerability that occurs in the persistence/database layer of a web application. &lt;br /&gt;
This vulnerability is derived from the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities based on poor input validation and bad design that can occur whenever one programming or scripting language is embedded inside another.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== How to Locate potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A secure way to build SQL statements is to construct all queries with PreparedStatement instead of Statement and/or to use parameterized stored procedures. &lt;br /&gt;
Parameterized stored procedures are compiled before user input is added, making it impossible for a hacker to modify the actual SQL statement.&lt;br /&gt;
&lt;br /&gt;
The account used to make the database connection must have “Least privilege” If the application only requires read access then the account must be given read access only.&lt;br /&gt;
&lt;br /&gt;
Avoid disclosing error information: Weak error handling is a great way for an attacker to profile SQL injection attacks. Uncaught SQL errors normally give too much information to the user and contain things like table names and procedure names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Best practices when dealing with DB’s ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use Database stored procedures, but even stored procedures can be vulnerable.&lt;br /&gt;
Use parametrized queries instead of dynamic SQL statements.&lt;br /&gt;
Data validate all external input:&lt;br /&gt;
Ensure that all SQL statements recognize user inputs as variables, and that statements are precompiled before the actual inputs are substituted for the variables in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== SQL INJECTION EXAMPLE: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String DRIVER = &amp;quot;com.ora.jdbc.Driver&amp;quot;;&lt;br /&gt;
String DataURL = &amp;quot;jdbc:db://localhost:5112/users&amp;quot;;&lt;br /&gt;
String LOGIN = &amp;quot;admin&amp;quot;;&lt;br /&gt;
String PASSWORD = &amp;quot;admin123&amp;quot;;&lt;br /&gt;
Class.forName(DRIVER);&lt;br /&gt;
&lt;br /&gt;
//Make connection to DB&lt;br /&gt;
Connection connection = DriverManager.getConnection(DataURL, LOGIN, PASSWORD);&lt;br /&gt;
&lt;br /&gt;
String Username = request.getParameter(&amp;quot;USER&amp;quot;); // From HTTP request&lt;br /&gt;
String Password = request.getParameter(&amp;quot;PASSWORD&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
int iUserID = -1;&lt;br /&gt;
String sLoggedUser = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; +Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Statement selectStatement = connection.createStatement ();&lt;br /&gt;
ResultSet resultSet = selectStatement.executeQuery(sel);&lt;br /&gt;
&lt;br /&gt;
if (resultSet.next()) {&lt;br /&gt;
iUserID = resultSet.getInt(1);&lt;br /&gt;
sLoggedUser = resultSet.getString(2);&lt;br /&gt;
}&lt;br /&gt;
PrintWriter writer = response.getWriter ();&lt;br /&gt;
if (iUserID &amp;gt;= 0) {&lt;br /&gt;
writer.println (&amp;quot;User logged in: &amp;quot; + sLoggedUser);&lt;br /&gt;
} else {&lt;br /&gt;
writer.println (&amp;quot;Access Denied!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
When SQL statements are dynamically created as software executes, there is an opportunity for a security breach as the input data can truncate or malform or even expand the original SQL query!&lt;br /&gt;
&lt;br /&gt;
Firstly the request.getParameter retrieves the data for the SQL query directly from the HTTP request without any Data validation (Min/Max length, Permitted characters, malicious characters). This error gives rise to the ability to input SQL as the payload and alter the functionality in the statement.&lt;br /&gt;
&lt;br /&gt;
The application places the payload directly into the statement causing the SQL vulnerability:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot;&lt;br /&gt;
Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== .NET&lt;br /&gt;
 ==&lt;br /&gt;
&lt;br /&gt;
Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code and therefore the payload can not be injected. Using a parameters collection lets you enforce type and length checks. Values outside of the range trigger an exception. Make sure you handle the exception correctly.&lt;br /&gt;
Example of the SqlParameterCollection:&lt;br /&gt;
&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
using (SqlConnection conn = new SqlConnection(connectionString))&lt;br /&gt;
{&lt;br /&gt;
  DataSet dataObj = new DataSet();&lt;br /&gt;
  SqlDataAdapter sqlAdapter = new SqlDataAdapter( &amp;quot;StoredProc&amp;quot;, conn);&lt;br /&gt;
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;&lt;br /&gt;
 //specify param type&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters.Add(&amp;quot;@usrId&amp;quot;, SqlDbType.VarChar, 15); &lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters[&amp;quot;@usrId &amp;quot;].Value = UID.Text; // Add data from user&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.Fill(dataObj); // populate and execute proc&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Stored procedures don’t always protect against SQL injection:&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE dbo.RunAnyQuery &lt;br /&gt;
@parameter NVARCHAR(50)&lt;br /&gt;
&lt;br /&gt;
AS&lt;br /&gt;
        EXEC sp_executesql @parameter&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The above procedure shall execute any SQL you pass to it. The directive sp_executesql is a system stored procedure in Microsoft® SQL Server™&lt;br /&gt;
Lets pass it.&lt;br /&gt;
&lt;br /&gt;
DROP TABLE ORDERS;&lt;br /&gt;
Guess what happens? So we must be careful of not falling into the “We’re secure, we are using stored procedures” trap!&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5432</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5432"/>
				<updated>2006-06-09T08:11:12Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* SQL Injection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[SQL Injection]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5431</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5431"/>
				<updated>2006-06-09T08:09:40Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* XSS Attacks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[SQL Injection]] ==&lt;br /&gt;
&lt;br /&gt;
What SQL injection is:&lt;br /&gt;
&lt;br /&gt;
SQL injection is a security vulnerability that occurs in the persistence/database layer of a web application. &lt;br /&gt;
This vulnerability is derived from the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities based on poor input validation and bad design that can occur whenever one programming or scripting language is embedded inside another.&lt;br /&gt;
&lt;br /&gt;
How to Locate potentially vulnerable code&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A secure way to build SQL statements is to construct all queries with PreparedStatement instead of Statement and/or to use parameterized stored procedures. &lt;br /&gt;
Parameterized stored procedures are compiled before user input is added, making it impossible for a hacker to modify the actual SQL statement.&lt;br /&gt;
&lt;br /&gt;
The account used to make the database connection must have “Least privilege” If the application only requires read access then the account must be given read access only.&lt;br /&gt;
&lt;br /&gt;
Avoid disclosing error information: Weak error handling is a great way for an attacker to profile SQL injection attacks. Uncaught SQL errors normally give too much information to the user and contain things like table names and procedure names.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Best practices when dealing with DB’s&lt;br /&gt;
&lt;br /&gt;
Use Database stored procedures, but even stored procedures can be vulnerable.&lt;br /&gt;
Use parametrized queries instead of dynamic SQL statements.&lt;br /&gt;
Data validate all external input:&lt;br /&gt;
Ensure that all SQL statements recognize user inputs as variables, and that statements are precompiled before the actual inputs are substituted for the variables in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL INJECTION EXAMPLE:&lt;br /&gt;
&lt;br /&gt;
String DRIVER = &amp;quot;com.ora.jdbc.Driver&amp;quot;;&lt;br /&gt;
String DataURL = &amp;quot;jdbc:db://localhost:5112/users&amp;quot;;&lt;br /&gt;
String LOGIN = &amp;quot;admin&amp;quot;;&lt;br /&gt;
String PASSWORD = &amp;quot;admin123&amp;quot;;&lt;br /&gt;
Class.forName(DRIVER);&lt;br /&gt;
&lt;br /&gt;
//Make connection to DB&lt;br /&gt;
Connection connection = DriverManager.getConnection(DataURL, LOGIN, PASSWORD);&lt;br /&gt;
&lt;br /&gt;
String Username = request.getParameter(&amp;quot;USER&amp;quot;); // From HTTP request&lt;br /&gt;
String Password = request.getParameter(&amp;quot;PASSWORD&amp;quot;); // From HTTP request&lt;br /&gt;
&lt;br /&gt;
int iUserID = -1;&lt;br /&gt;
String sLoggedUser = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot; +Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Statement selectStatement = connection.createStatement ();&lt;br /&gt;
ResultSet resultSet = selectStatement.executeQuery(sel);&lt;br /&gt;
&lt;br /&gt;
if (resultSet.next()) {&lt;br /&gt;
iUserID = resultSet.getInt(1);&lt;br /&gt;
sLoggedUser = resultSet.getString(2);&lt;br /&gt;
}&lt;br /&gt;
PrintWriter writer = response.getWriter ();&lt;br /&gt;
if (iUserID &amp;gt;= 0) {&lt;br /&gt;
writer.println (&amp;quot;User logged in: &amp;quot; + sLoggedUser);&lt;br /&gt;
} else {&lt;br /&gt;
writer.println (&amp;quot;Access Denied!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
When SQL statements are dynamically created as software executes, there is an opportunity for a security breach as the input data can truncate or malform or even expand the original SQL query!&lt;br /&gt;
&lt;br /&gt;
Firstly the request.getParameter retrieves the data for the SQL query directly from the HTTP request without any Data validation (Min/Max length, Permitted characters, malicious characters). This error gives rise to the ability to input SQL as the payload and alter the functionality in the statement.&lt;br /&gt;
&lt;br /&gt;
The application places the payload directly into the statement causing the SQL vulnerability:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
String sel = &amp;quot;SELECT User_id, Username FROM USERS WHERE Username = '&amp;quot;&lt;br /&gt;
Username + &amp;quot;' AND Password = '&amp;quot; + Password + &amp;quot;'&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
.NET&lt;br /&gt;
&lt;br /&gt;
Parameter collections such as SqlParameterCollection provide type checking and length validation. If you use a parameters collection, input is treated as a literal value, and SQL Server does not treat it as executable code and therefore the payload can not be injected. Using a parameters collection lets you enforce type and length checks. Values outside of the range trigger an exception. Make sure you handle the exception correctly.&lt;br /&gt;
Example of the SqlParameterCollection:&lt;br /&gt;
&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
using (SqlConnection conn = new SqlConnection(connectionString))&lt;br /&gt;
{&lt;br /&gt;
  DataSet dataObj = new DataSet();&lt;br /&gt;
  SqlDataAdapter sqlAdapter = new SqlDataAdapter( &amp;quot;StoredProc&amp;quot;, conn);&lt;br /&gt;
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;&lt;br /&gt;
 //specify param type&lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters.Add(&amp;quot;@usrId&amp;quot;, SqlDbType.VarChar, 15); &lt;br /&gt;
  sqlAdapter.SelectCommand.Parameters[&amp;quot;@usrId &amp;quot;].Value = UID.Text; // Add data from user&lt;br /&gt;
&lt;br /&gt;
  sqlAdapter.Fill(dataObj); // populate and execute proc&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Stored procedures don’t always protect against SQL injection:&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE dbo.RunAnyQuery &lt;br /&gt;
@parameter NVARCHAR(50)&lt;br /&gt;
&lt;br /&gt;
AS&lt;br /&gt;
        EXEC sp_executesql @parameter&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The above procedure shall execute any SQL you pass to it. The directive sp_executesql is a system stored procedure in Microsoft® SQL Server™&lt;br /&gt;
Lets pass it.&lt;br /&gt;
&lt;br /&gt;
DROP TABLE ORDERS;&lt;br /&gt;
Guess what happens? So we must be careful of not falling into the “We’re secure, we are using stored procedures” trap!&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=XSS_Attacks&amp;diff=5430</id>
		<title>XSS Attacks</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=XSS_Attacks&amp;diff=5430"/>
				<updated>2006-06-09T08:07:01Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[OWASP Code Review Guide Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
XSS attacks&lt;br /&gt;
Bad code example:&lt;br /&gt;
If the text inputted by the user is reflected back and has not been data validated the browser shall interpret the inputted script as part of the mark up and execute the code accordingly.&lt;br /&gt;
To mitigate this type of vulnerability we need to perform a number of security tasks in our code:&lt;br /&gt;
&lt;br /&gt;
# Data validate.&lt;br /&gt;
# Encode Unsafe output.&lt;br /&gt;
 &lt;br /&gt;
 import org.apache.struts.action.*; &lt;br /&gt;
 import org.apache.commons.beanutils.BeanUtils; &lt;br /&gt;
 import javax.servlet.http.HttpServletRequest; &lt;br /&gt;
 import javax.servlet.http.HttpServletResponse; &lt;br /&gt;
 &lt;br /&gt;
 public final class InsertEmployeeAction extends Action { &lt;br /&gt;
 &lt;br /&gt;
 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ &lt;br /&gt;
 &lt;br /&gt;
 // Setting up objects and vairables.&lt;br /&gt;
 &lt;br /&gt;
 Obj1 service = new Obj1(); &lt;br /&gt;
 ObjForm objForm = (ObjForm) form; &lt;br /&gt;
 InfoADT adt = new InfoADT (); &lt;br /&gt;
 BeanUtils.copyProperties(adt, objForm); &lt;br /&gt;
 &lt;br /&gt;
 	String searchQuery = objForm.getqueryString();&lt;br /&gt;
 	String payload = objForm.getPayLoad();&lt;br /&gt;
 try { &lt;br /&gt;
 service.doWork(adt);  / /do something with the data&lt;br /&gt;
 ActionMessages messages = new ActionMessages(); &lt;br /&gt;
 ActionMessage message = new ActionMessage(&amp;quot;success&amp;quot;, adt.getName() ); &lt;br /&gt;
 messages.add( ActionMessages.GLOBAL_MESSAGE, message ); &lt;br /&gt;
 saveMessages( request, messages ); &lt;br /&gt;
 request.setAttribute(&amp;quot;Record&amp;quot;, adt); &lt;br /&gt;
 return (mapping.findForward(&amp;quot;success&amp;quot;));&lt;br /&gt;
 }&lt;br /&gt;
 catch( DatabaseException de ) &lt;br /&gt;
 {&lt;br /&gt;
 ActionErrors errors = new ActionErrors(); &lt;br /&gt;
 ActionError error = new ActionError(&amp;quot;error.employee.databaseException&amp;quot; + “Payload: “+payload);&lt;br /&gt;
 errors.add( ActionErrors.GLOBAL_ERROR, error ); &lt;br /&gt;
 saveErrors( request, errors ); return (mapping.findForward(&amp;quot;error: &amp;quot;+ searchQuery)); &lt;br /&gt;
 } &lt;br /&gt;
 } &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The red text above shows some common mistakes in the development of this struts action class.&lt;br /&gt;
Firstly the data passed in the HttpServletRequest is placed into a parameter without being data validated.&lt;br /&gt;
&lt;br /&gt;
Focusing on XSS we can see that this action class returns either a message, ActionMessage  in the case of the function being successful.&lt;br /&gt;
In the case of an error the code in the Try/Catch block is executed and we can see here that the data inputted by the user, the data contained in the HttpServletRequest is returned to the user, unvalidated and exactly in the format in which the user inputted it.&lt;br /&gt;
&lt;br /&gt;
 import java.io.*; &lt;br /&gt;
 import javax.servlet.http.*; &lt;br /&gt;
 import javax.servlet.*; &lt;br /&gt;
 &lt;br /&gt;
 public class HelloServlet extends HttpServlet &lt;br /&gt;
 { &lt;br /&gt;
 public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException &lt;br /&gt;
 { &lt;br /&gt;
 &lt;br /&gt;
 String input = req.getHeader(“USERINPUT”);&lt;br /&gt;
 &lt;br /&gt;
 PrintWriter out = res.getWriter(); &lt;br /&gt;
 out.println(input);  // echo User input.&lt;br /&gt;
 out.close(); 	&lt;br /&gt;
 } &lt;br /&gt;
 } &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A second example of an XSS vulnerable function. Echoing un-validated user input back to the browser would give a nice large vulnerability footprint.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
.NET Example (ASP.NET version 1.1 ASP.NET version 2.0):&lt;br /&gt;
&lt;br /&gt;
The server side code for a VB.NET application may have similar functionality&lt;br /&gt;
&lt;br /&gt;
 ' SearchResult.aspx.vb &lt;br /&gt;
 Imports System &lt;br /&gt;
 Imports System.Web &lt;br /&gt;
 Imports System.Web.UI &lt;br /&gt;
 Imports System.Web.UI.WebControls &lt;br /&gt;
 &lt;br /&gt;
 Public Class SearchPage Inherits System.Web.UI.Page &lt;br /&gt;
 &lt;br /&gt;
 Protected txtInput As TextBox &lt;br /&gt;
 Protected cmdSearch As Button &lt;br /&gt;
 Protected lblResult As Label Protected &lt;br /&gt;
 &lt;br /&gt;
 Sub cmdSearch _Click(Source As Object, _ e As EventArgs) &lt;br /&gt;
 	&lt;br /&gt;
 // Do Search…..&lt;br /&gt;
 	// …………&lt;br /&gt;
 &lt;br /&gt;
 lblResult.Text=&amp;quot;You Searched for: &amp;quot; &amp;amp; txtInput.Text &lt;br /&gt;
 &lt;br /&gt;
 // Display Search Results…..&lt;br /&gt;
 // …………&lt;br /&gt;
 &lt;br /&gt;
 End Sub &lt;br /&gt;
 End Class&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is a VB.NET example of a Cross Site Script vulnerable piece of search functionality which echoes back the data inputted by the user. To mitigate against this we need proper data validation and in the case of stored XSS attacks we need to encode known bad (as mentioned before).&lt;br /&gt;
&lt;br /&gt;
In the .NET framework there are some in-built security functions which can assist in data validation and HTML encoding, namley, ASP.NET 1.1 '''request validation '''feature and '''HttpUtility.HtmlEncode'''.&lt;br /&gt;
&lt;br /&gt;
Microsoft in their wisdom state that you should not rely solely on ASP.NET request validation&lt;br /&gt;
and that it should be used in conjunction with your own data validation, such as regular expressions (mentioned below).&lt;br /&gt;
&lt;br /&gt;
The request validation feature is disabled on an individual page by specifying in the page directive  &lt;br /&gt;
&lt;br /&gt;
  '''&amp;lt;%@ Page validateRequest=&amp;quot;false&amp;quot; %&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
or by setting '''ValidateRequest=&amp;quot;false&amp;quot;''' on the '''@ Pages''' element. &lt;br /&gt;
&lt;br /&gt;
or in the '''web.config''' file:&lt;br /&gt;
&lt;br /&gt;
You can disable request validation by adding a &lt;br /&gt;
&lt;br /&gt;
  &amp;lt;'''pages'''&amp;gt; element with '''validateRequest=&amp;quot;false&amp;quot;'''&lt;br /&gt;
&lt;br /&gt;
So when reviewing code make sure the validateRequest directive is enabled an if not, investigate what method of DV is being used, if any.&lt;br /&gt;
Check that ASP.NET Request validation Is enabled in '''Machine.config'''&lt;br /&gt;
Request validation is enabled by ASP.NET by default. You can see the following default setting in the '''Machine.config''' file.&lt;br /&gt;
&lt;br /&gt;
  '''&amp;lt;pages validateRequest=&amp;quot;true&amp;quot; ... /&amp;gt; '''&lt;br /&gt;
&lt;br /&gt;
HTML Encoding:&lt;br /&gt;
&lt;br /&gt;
Content to be displayed can easily be encoded using the HtmlEncode function. This is done by calling:&lt;br /&gt;
&lt;br /&gt;
  '''Server.HtmlEncode(string)'''&lt;br /&gt;
&lt;br /&gt;
Using the html encoder example for a form:&lt;br /&gt;
&lt;br /&gt;
Text Box: &amp;lt;%@ Page Language=&amp;quot;C#&amp;quot; ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt; &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;script runat=&amp;quot;server&amp;quot;&amp;gt; &lt;br /&gt;
 void searchBtn _Click(object sender, EventArgs e) { &lt;br /&gt;
 Response.Write(HttpUtility.HtmlEncode(inputTxt.Text)); } &lt;br /&gt;
 &amp;lt;/script&amp;gt; &lt;br /&gt;
 &amp;lt;html&amp;gt; &lt;br /&gt;
 &amp;lt;body&amp;gt; &lt;br /&gt;
 &amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt; &lt;br /&gt;
 &amp;lt;div&amp;gt; &lt;br /&gt;
 &amp;lt;asp:TextBox ID=&amp;quot;inputTxt&amp;quot; Runat=&amp;quot;server&amp;quot; TextMode=&amp;quot;MultiLine&amp;quot; Width=&amp;quot;382px&amp;quot; Height=&amp;quot;152px&amp;quot;&amp;gt; &lt;br /&gt;
 &amp;lt;/asp:TextBox&amp;gt; &lt;br /&gt;
 &amp;lt;asp:Button ID=&amp;quot;searchBtn&amp;quot; Runat=&amp;quot;server&amp;quot; Text=&amp;quot;Submit&amp;quot; OnClick=&amp;quot; searchBtn _Click&amp;quot; /&amp;gt; &lt;br /&gt;
 &amp;lt;/div&amp;gt; &lt;br /&gt;
 &amp;lt;/form&amp;gt; &lt;br /&gt;
 &amp;lt;/body&amp;gt; &lt;br /&gt;
 &amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Stored Cross Site Script:'''&lt;br /&gt;
Using Html encoding to encode potentially unsafe output.:&lt;br /&gt;
&lt;br /&gt;
Malicious script can be stored/persisted in a database and shall not execute until retrieved by a user. This can also be the case in bulletin boards and some early web email clients. This incubated attack can sit dormant for a long period of time until a user decides to view the page where the injected script is present. At this point the script shall execute on the users browser:&lt;br /&gt;
&lt;br /&gt;
The original source of input for the injected script may be from another vulnerable application, which is common in enterprise architectures. Therefore the application at hand may have good input data validation but the data persisted may not have been entered via this application per se, but via another application.&lt;br /&gt;
&lt;br /&gt;
In this case we cannot be 100% sure the data to be displayed to the user is 100% safe (as it could of found its way in via another path in the enterprise).&lt;br /&gt;
The approach to mitigate against this si to ensure the data sent to the browser is not going to be interpreted by the browser as mark-up and should be treated as user data.&lt;br /&gt;
&lt;br /&gt;
We encode known bad to mitigate against this “enemy within”. This in effect assures the browser interprets any special characters as data and markup. &lt;br /&gt;
How is this done?&lt;br /&gt;
HTML encoding usually means '''&amp;amp;lt;''' becomes '''&amp;amp;amp;lt;''', '''&amp;amp;gt;''' becomes '''&amp;amp;amp;gt;''', '''&amp;amp;amp;''' becomes '''&amp;amp;amp;amp;''', and '''&amp;amp;quot;''' becomes '''&amp;amp;amp;quot;'''.&lt;br /&gt;
&lt;br /&gt;
From	To&lt;br /&gt;
&lt;br /&gt;
&amp;amp;lt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	&amp;amp;amp;lt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	&amp;amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;#40;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	&amp;amp;amp;&amp;amp;#35;40;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;#41;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	&amp;amp;amp;&amp;amp;#35;41;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;#35;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	&amp;amp;amp;&amp;amp;#35;35;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;amp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	&amp;amp;amp;amp;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;quot;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;	&amp;amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
So for example the text &amp;lt;script&amp;gt; would be displayed as &amp;lt;script&amp;gt; but on viewing the markup it would be represented by &amp;amp;amp;lt;script&amp;amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Alternate XSS Syntax]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
[[Category:Attack]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5429</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5429"/>
				<updated>2006-06-09T08:05:19Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Buffer Overruns and Overflows */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5428</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5428"/>
				<updated>2006-06-09T08:04:28Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5427</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5427"/>
				<updated>2006-06-09T08:03:16Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
Data Validation&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
Canoncalization of input.&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.x delves much more into this subject.&lt;br /&gt;
Data validation strategy&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “Known Good” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “Known bad”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
Data Validation Strategy&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
1.	Exact Match (Constrain)&lt;br /&gt;
2.	Known Good (Accept)&lt;br /&gt;
3.	Reject Known bad (Reject)&lt;br /&gt;
4.      Encode Known bad (Sanitise)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.&lt;br /&gt;
&lt;br /&gt;
·	Exact Match: (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
·	Known Good: If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
·	Reject Known bad: We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
·	Encode Known Bad: This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
HTML-encoding and URL-encoding user input when writing back to the client. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Good Patterns for Data validation&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
preg_grep() could also be used for a True or False result. This would enable us to let “only known good” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
http://www.regxlib.com/CheatSheet.htm&lt;br /&gt;
&lt;br /&gt;
^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
[abcde] 	Matches any single character specified in set&lt;br /&gt;
[^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Framework Example(Struts 1.2)&lt;br /&gt;
&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
Enables us to have a central area for data validation.&lt;br /&gt;
Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
1.	What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
2.	The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
&lt;br /&gt;
  validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
package com.pcs.necronomicon&lt;br /&gt;
import org.apache.struts.validator.ValidatorForm;&lt;br /&gt;
public class LogonForm extends ValidatorForm {  &lt;br /&gt;
&lt;br /&gt;
  private String username;&lt;br /&gt;
  private String password;&lt;br /&gt;
  &lt;br /&gt;
  public String getUsername() {&lt;br /&gt;
&lt;br /&gt;
    return username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
  public void setUsername(String username) {&lt;br /&gt;
&lt;br /&gt;
    this.username = username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  public String getPassword() {&lt;br /&gt;
&lt;br /&gt;
    return password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
public void setPassword(String password) {&lt;br /&gt;
&lt;br /&gt;
    this.password = password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot; type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-validation&amp;gt;&lt;br /&gt;
&amp;lt;formset&amp;gt;&lt;br /&gt;
&amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in Application.properties. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
errors.required={0} is required.&lt;br /&gt;
errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
errors.invalid={0} is invalid.&lt;br /&gt;
errors.byte={0} must be a byte.&lt;br /&gt;
errors.short={0} must be a short.&lt;br /&gt;
errors.integer={0} must be an integer.&lt;br /&gt;
errors.long={0} must be a long.0.   &lt;br /&gt;
errors.float={0} must be a float.&lt;br /&gt;
errors.double={0} must be a double.&lt;br /&gt;
errors.date={0} is not a date.&lt;br /&gt;
errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
errors.email={0} is an invalid e-mail address.&lt;br /&gt;
prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;var&amp;gt;&lt;br /&gt;
         &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
         &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; &lt;br /&gt;
staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Framework example(.NET):'''&lt;br /&gt;
&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past. The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
&lt;br /&gt;
'''RequiredFieldValidator''' – Makes the associated input control a required field.&lt;br /&gt;
&lt;br /&gt;
'''CompareValidator''' – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
&lt;br /&gt;
'''RangeValidator''' – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
&lt;br /&gt;
'''RegularExpressionValidator''' – Checks user input against a regular expression.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;form runat=server&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RequiredFieldValidator runat=server ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; &lt;br /&gt;
&amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;    &lt;br /&gt;
&amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RegularExpressionValidator runat=server display=dynamic controltovalidate=&amp;quot;Name&amp;quot; errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Length Checking:'''&lt;br /&gt;
Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app. &lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an xternal request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''''Never Rely on Client-Side Data Validation'''' Client-side validation can always be bypassed. Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.&lt;br /&gt;
&lt;br /&gt;
'''Remember''': Data validation must be always done on the server side.&lt;br /&gt;
A code review focuses on server side code. Any client side security code is not and cannot be considered security.&lt;br /&gt;
&lt;br /&gt;
'''Data validation of parameter names'''&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
&lt;br /&gt;
UserId =3o1nk395y&lt;br /&gt;
password=letMeIn123&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (UserId,password from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web services data validation&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as xsd:string.&lt;br /&gt;
&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5426</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5426"/>
				<updated>2006-06-09T08:01:14Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* OS Injection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
Data Validation&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
Canoncalization of input.&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.x delves much more into this subject.&lt;br /&gt;
Data validation strategy&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “Known Good” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “Known bad”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
Data Validation Strategy&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
1.	Exact Match (Constrain)&lt;br /&gt;
2.	Known Good (Accept)&lt;br /&gt;
3.	Reject Known bad (Reject)&lt;br /&gt;
4.      Encode Known bad (Sanitise)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.&lt;br /&gt;
&lt;br /&gt;
·	Exact Match: (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
·	Known Good: If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
·	Reject Known bad: We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
·	Encode Known Bad: This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
HTML-encoding and URL-encoding user input when writing back to the client. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Good Patterns for Data validation&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
preg_grep() could also be used for a True or False result. This would enable us to let “only known good” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
http://www.regxlib.com/CheatSheet.htm&lt;br /&gt;
&lt;br /&gt;
^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
[abcde] 	Matches any single character specified in set&lt;br /&gt;
[^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Framework Example(Struts 1.2)&lt;br /&gt;
&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
Enables us to have a central area for data validation.&lt;br /&gt;
Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
1.	What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
2.	The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
&lt;br /&gt;
  validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
package com.pcs.necronomicon&lt;br /&gt;
import org.apache.struts.validator.ValidatorForm;&lt;br /&gt;
public class LogonForm extends ValidatorForm {  &lt;br /&gt;
&lt;br /&gt;
  private String username;&lt;br /&gt;
  private String password;&lt;br /&gt;
  &lt;br /&gt;
  public String getUsername() {&lt;br /&gt;
&lt;br /&gt;
    return username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
  public void setUsername(String username) {&lt;br /&gt;
&lt;br /&gt;
    this.username = username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  public String getPassword() {&lt;br /&gt;
&lt;br /&gt;
    return password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
public void setPassword(String password) {&lt;br /&gt;
&lt;br /&gt;
    this.password = password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot; type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-validation&amp;gt;&lt;br /&gt;
&amp;lt;formset&amp;gt;&lt;br /&gt;
&amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in Application.properties. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
errors.required={0} is required.&lt;br /&gt;
errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
errors.invalid={0} is invalid.&lt;br /&gt;
errors.byte={0} must be a byte.&lt;br /&gt;
errors.short={0} must be a short.&lt;br /&gt;
errors.integer={0} must be an integer.&lt;br /&gt;
errors.long={0} must be a long.0.   &lt;br /&gt;
errors.float={0} must be a float.&lt;br /&gt;
errors.double={0} must be a double.&lt;br /&gt;
errors.date={0} is not a date.&lt;br /&gt;
errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
errors.email={0} is an invalid e-mail address.&lt;br /&gt;
prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;var&amp;gt;&lt;br /&gt;
         &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
         &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; &lt;br /&gt;
staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Framework example(.NET):'''&lt;br /&gt;
&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past. The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
&lt;br /&gt;
'''RequiredFieldValidator''' – Makes the associated input control a required field.&lt;br /&gt;
&lt;br /&gt;
'''CompareValidator''' – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
&lt;br /&gt;
'''RangeValidator''' – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
&lt;br /&gt;
'''RegularExpressionValidator''' – Checks user input against a regular expression.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;form runat=server&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RequiredFieldValidator runat=server ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; &lt;br /&gt;
&amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;    &lt;br /&gt;
&amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RegularExpressionValidator runat=server display=dynamic controltovalidate=&amp;quot;Name&amp;quot; errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Length Checking:'''&lt;br /&gt;
Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app. &lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an xternal request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''''Never Rely on Client-Side Data Validation'''' Client-side validation can always be bypassed. Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.&lt;br /&gt;
&lt;br /&gt;
'''Remember''': Data validation must be always done on the server side.&lt;br /&gt;
A code review focuses on server side code. Any client side security code is not and cannot be considered security.&lt;br /&gt;
&lt;br /&gt;
'''Data validation of parameter names'''&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
&lt;br /&gt;
UserId =3o1nk395y&lt;br /&gt;
password=letMeIn123&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (UserId,password from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web services data validation&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as xsd:string.&lt;br /&gt;
&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
[[OWASP Code Review Guide Table of Contents]]__TOC__&lt;br /&gt;
&lt;br /&gt;
Injection flaws allow attackers to pass malicious code through a web application to another sub system.&lt;br /&gt;
Depending on the subsystem different types of injection attack can be performed:&lt;br /&gt;
RDBMS: SQL Injection&lt;br /&gt;
WebBrowser/Appserver: SQL Injection&lt;br /&gt;
OS-shell: Operating system commands Calling external applications from your application.&lt;br /&gt;
&lt;br /&gt;
==How to locate the potentially vulnerable code ==&lt;br /&gt;
&lt;br /&gt;
Many developers believe text fields are the only areas for data validation. This is an incorrect assumption. Any external input must be data validated:&lt;br /&gt;
&lt;br /&gt;
Text fields, List boxes, radio buttons, check boxes, cookies, HTTP header data, HTTP post data, hidden fields, parameter names and parameter values.&lt;br /&gt;
… This is not an exhaustive list.&lt;br /&gt;
&lt;br /&gt;
“Process to process” or “entity-to-entity” communication must be investigated also. Any code that communicates with an upstream or downstream process and accepts input from said entity must be reviewed.&lt;br /&gt;
&lt;br /&gt;
All injection flaws are input validation errors. The presence if an injection flaw is an indication of incorrect data validation on the input received from an external source our outside the boundary of trust, which gets more blurred every year.&lt;br /&gt;
&lt;br /&gt;
Basically for this type of vulnerability we need to find all input streams into the application. This can be from a users browser, CLI or fat client but also from upstream processes that “feed” our application.&lt;br /&gt;
&lt;br /&gt;
An example would be to search the code base for the use of API’s or packages that are normally used for communication purposes.&lt;br /&gt;
&lt;br /&gt;
The '''java.io''', '''java.sql''', '''java.net''', '''java.rmi''', '''java.xml''' packages are all used for application communication. Searching for methods from those packages in the code base can yield results. A less “scientific” method is to search for common keywords such as “UserID”, “LoginID” or “Password”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Vulnerable Patterns for OS injection ==&lt;br /&gt;
What we should be looking for is relationships between the application and the operating system. The application utilising functions of the underlying operating system.&lt;br /&gt;
&lt;br /&gt;
In java using the Runtime object, '''java.lang.Runtime''' does this.&lt;br /&gt;
In .NET calls such as '''System.Diagnostics.Process.Start '''are used to call underlying OS functions. &lt;br /&gt;
In PHP we may look for calls such as '''exec()''' or '''passthru()'''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''':&lt;br /&gt;
&lt;br /&gt;
We have a class that eventually gets input from the user via a HTTP request.&lt;br /&gt;
This class is used to execute some native exe on the application server and return a result.&lt;br /&gt;
&lt;br /&gt;
 public class DoStuff {&lt;br /&gt;
 public string executeCommand(String userName)&lt;br /&gt;
 {	try {&lt;br /&gt;
 		String myUid = userName;&lt;br /&gt;
 		Runtime rt = Runtime.getRuntime();&lt;br /&gt;
 		rt.exec(&amp;quot;doStuff.exe &amp;quot; +”-“ +myUid); // Call exe with userID&lt;br /&gt;
 	}catch(Exception e)&lt;br /&gt;
 		{&lt;br /&gt;
 e.printStackTrace();&lt;br /&gt;
 		}&lt;br /&gt;
 	}&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ok, so the method executeCommand calls '''''doStuff.exe''''' via the '''''java.lang.runtime''''' static method '''''getRuntime()'''''. The parameter passed is not validated in any way in this class. We are assuming that the data has not been data validated prior to calling this method. ''Transactional analysis should have encountered any data validation prior to this point.''&lt;br /&gt;
Inputting “Joe69” would result in the following MS DOS command:&lt;br /&gt;
'''''doStuff.exe –Joe69'''''&lt;br /&gt;
Lets say we input '''''Joe69 &amp;amp; netstat –a''''' we would get the following response:&lt;br /&gt;
The exe doStuff would execute passing in the User Id Joe69, but then the dos command '''''netstat''''' would be called. How this works is the passing of the parameter “&amp;amp;” into the application, which in turn is used as a command appender in MS DOS and hence the command after the &amp;amp; character is executed.&lt;br /&gt;
&lt;br /&gt;
UNIX:&lt;br /&gt;
An attacker might insert the string '''“; cat /etc/hosts”''' the contents of the UNIX hosts file might be exposed to the attacker. &lt;br /&gt;
&lt;br /&gt;
.NET Example:&lt;br /&gt;
 namespace ExternalExecution&lt;br /&gt;
 {&lt;br /&gt;
 class CallExternal&lt;br /&gt;
 {&lt;br /&gt;
 static void Main(string[] args)&lt;br /&gt;
 {&lt;br /&gt;
 String arg1=args[0];&lt;br /&gt;
 System.Diagnostics.Process.Start(&amp;quot;doStuff.exe&amp;quot;, arg1);&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Yet again there is no data validation to speak of here. Assuming no upstream validation occurring in another class.&lt;br /&gt;
&lt;br /&gt;
These attacks include calls to the operating system via system calls, the use of external programs via shell commands, as well as calls to backend databases via SQL (i.e., SQL injection). Complete scripts written in perl, python, shell, bat and other languages can be injected into poorly designed web applications and executed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Good Patterns &amp;amp; procedures to prevent OS injection==&lt;br /&gt;
&lt;br /&gt;
See the Data Validation section.&lt;br /&gt;
&lt;br /&gt;
==Related Articles==&lt;br /&gt;
[[Interpreter Injection]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
[[Category:Attack]]&lt;br /&gt;
[[Category:Input Validation]]&lt;br /&gt;
&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Category:OWASP_Code_Review_Project&amp;diff=5425</id>
		<title>Category:OWASP Code Review Project</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Category:OWASP_Code_Review_Project&amp;diff=5425"/>
				<updated>2006-06-09T07:58:21Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Downloads */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Project Contributors ==&lt;br /&gt;
&lt;br /&gt;
The OWASP Code Review project was concieved by Eoin Keary the OWASP Ireland Founder and Chapter Lead.&lt;br /&gt;
We are actively seeking techies to add new sections as new web technologies emerge. &lt;br /&gt;
If you are interested in volunteering for the project, or have a comment, question, or suggestion, please drop me a line mailto:eoin.keary@owasp.org&lt;br /&gt;
&lt;br /&gt;
==Volunteers Needed==&lt;br /&gt;
&lt;br /&gt;
Yes please, drop me a line.&lt;br /&gt;
Need help on this one, dont be shy, all help appreciated&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Contents==&lt;br /&gt;
[[OWASP Code Review Guide Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
==Roadmap==&lt;br /&gt;
&lt;br /&gt;
View the [[OWASP Code Review Project Roadmap]]&lt;br /&gt;
&lt;br /&gt;
{{Template:Stub}}&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5424</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5424"/>
				<updated>2006-06-09T07:55:03Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* OS Injection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
Data Validation&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
Canoncalization of input.&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.x delves much more into this subject.&lt;br /&gt;
Data validation strategy&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “Known Good” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “Known bad”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
Data Validation Strategy&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
1.	Exact Match (Constrain)&lt;br /&gt;
2.	Known Good (Accept)&lt;br /&gt;
3.	Reject Known bad (Reject)&lt;br /&gt;
4.      Encode Known bad (Sanitise)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.&lt;br /&gt;
&lt;br /&gt;
·	Exact Match: (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
·	Known Good: If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
·	Reject Known bad: We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
·	Encode Known Bad: This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
HTML-encoding and URL-encoding user input when writing back to the client. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Good Patterns for Data validation&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
preg_grep() could also be used for a True or False result. This would enable us to let “only known good” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
http://www.regxlib.com/CheatSheet.htm&lt;br /&gt;
&lt;br /&gt;
^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
[abcde] 	Matches any single character specified in set&lt;br /&gt;
[^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Framework Example(Struts 1.2)&lt;br /&gt;
&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
Enables us to have a central area for data validation.&lt;br /&gt;
Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
1.	What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
2.	The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
&lt;br /&gt;
  validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
package com.pcs.necronomicon&lt;br /&gt;
import org.apache.struts.validator.ValidatorForm;&lt;br /&gt;
public class LogonForm extends ValidatorForm {  &lt;br /&gt;
&lt;br /&gt;
  private String username;&lt;br /&gt;
  private String password;&lt;br /&gt;
  &lt;br /&gt;
  public String getUsername() {&lt;br /&gt;
&lt;br /&gt;
    return username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
  public void setUsername(String username) {&lt;br /&gt;
&lt;br /&gt;
    this.username = username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  public String getPassword() {&lt;br /&gt;
&lt;br /&gt;
    return password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
public void setPassword(String password) {&lt;br /&gt;
&lt;br /&gt;
    this.password = password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot; type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-validation&amp;gt;&lt;br /&gt;
&amp;lt;formset&amp;gt;&lt;br /&gt;
&amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in Application.properties. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
errors.required={0} is required.&lt;br /&gt;
errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
errors.invalid={0} is invalid.&lt;br /&gt;
errors.byte={0} must be a byte.&lt;br /&gt;
errors.short={0} must be a short.&lt;br /&gt;
errors.integer={0} must be an integer.&lt;br /&gt;
errors.long={0} must be a long.0.   &lt;br /&gt;
errors.float={0} must be a float.&lt;br /&gt;
errors.double={0} must be a double.&lt;br /&gt;
errors.date={0} is not a date.&lt;br /&gt;
errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
errors.email={0} is an invalid e-mail address.&lt;br /&gt;
prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;var&amp;gt;&lt;br /&gt;
         &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
         &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; &lt;br /&gt;
staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Framework example(.NET):'''&lt;br /&gt;
&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past. The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
&lt;br /&gt;
'''RequiredFieldValidator''' – Makes the associated input control a required field.&lt;br /&gt;
&lt;br /&gt;
'''CompareValidator''' – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
&lt;br /&gt;
'''RangeValidator''' – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
&lt;br /&gt;
'''RegularExpressionValidator''' – Checks user input against a regular expression.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;form runat=server&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RequiredFieldValidator runat=server ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; &lt;br /&gt;
&amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;    &lt;br /&gt;
&amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RegularExpressionValidator runat=server display=dynamic controltovalidate=&amp;quot;Name&amp;quot; errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Length Checking:'''&lt;br /&gt;
Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app. &lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an xternal request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''''Never Rely on Client-Side Data Validation'''' Client-side validation can always be bypassed. Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.&lt;br /&gt;
&lt;br /&gt;
'''Remember''': Data validation must be always done on the server side.&lt;br /&gt;
A code review focuses on server side code. Any client side security code is not and cannot be considered security.&lt;br /&gt;
&lt;br /&gt;
'''Data validation of parameter names'''&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
&lt;br /&gt;
UserId =3o1nk395y&lt;br /&gt;
password=letMeIn123&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (UserId,password from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web services data validation&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as xsd:string.&lt;br /&gt;
&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
OS Injection&lt;br /&gt;
&lt;br /&gt;
Injection flaws allow attackers to pass malicious code through a web application to another sub system.&lt;br /&gt;
Depending on the subsystem different types of injection attack can be performed:&lt;br /&gt;
RDBMS: SQL Injection&lt;br /&gt;
WebBrowser/Appserver: SQL Injection&lt;br /&gt;
OS-shell: Operating system commands Calling external applications from your application.&lt;br /&gt;
&lt;br /&gt;
How to locate the potentially vulnerable code:&lt;br /&gt;
&lt;br /&gt;
Many developers believe text fields are the only areas for data validation. This is an incorrect assumption. Any external input must be data validated:&lt;br /&gt;
&lt;br /&gt;
Text fields, List boxes, radio buttons, check boxes, cookies, HTTP header data, HTTP post data, hidden fields, parameter names and parameter values.&lt;br /&gt;
… This is not an exhaustive list.&lt;br /&gt;
&lt;br /&gt;
“Process to process” or “entity-to-entity” communication must be investigated also. Any code that communicates with an upstream or downstream process and accepts input from said entity must be reviewed.&lt;br /&gt;
&lt;br /&gt;
All injection flaws are input validation errors. The presence if an injection flaw is an indication of incorrect data validation on the input received from an external source our outside the boundary of trust, which gets more blurred every year.&lt;br /&gt;
&lt;br /&gt;
Basically for this type of vulnerability we need to find all input streams into the application. This can be from a users browser, CLI or fat client but also from upstream processes that “feed” our application.&lt;br /&gt;
&lt;br /&gt;
An example would be to search the code base for the use of API’s or packages that are normally used for communication purposes.&lt;br /&gt;
&lt;br /&gt;
The java.io, java.sql, java.net, java.rmi, java.xml packages are all used for application communication. Searching for methods from those packages in the code base can yield results. A less “scientific” method is to search for common keywords such as “UserID”, “LoginID” or “Password”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Vulnerable Patterns for OS injection:'''&lt;br /&gt;
What we should be looking for is relationships between the application and the operating system. The application utilising functions of the underlying operating system.&lt;br /&gt;
&lt;br /&gt;
In java using the Runtime object, java.lang.Runtime does this.&lt;br /&gt;
In .NET calls such as System.Diagnostics.Process.Start are used to call underlying OS functions. &lt;br /&gt;
In PHP we may look for calls such as exec() or passthru().&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
We have a class that eventually gets input from the user via a HTTP request.&lt;br /&gt;
This class is used to execute some native exe on the application server and return a result.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class DoStuff {&lt;br /&gt;
public string executeCommand(String userName)&lt;br /&gt;
{	try {&lt;br /&gt;
		String myUid = userName;&lt;br /&gt;
		Runtime rt = Runtime.getRuntime();&lt;br /&gt;
		rt.exec(&amp;quot;doStuff.exe &amp;quot; +”-“ +myUid); // Call exe with userID&lt;br /&gt;
	}catch(Exception e)&lt;br /&gt;
		{&lt;br /&gt;
e.printStackTrace();&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ok, so the method executeCommand calls doStuff.exe via the java.lang.runtime static method getRuntime(). The parameter passed is not validated in any way in this class. We are assuming that the data has not been data validated prior to calling this method. Transactional analysis should have encountered any data validation prior to this point.&lt;br /&gt;
Inputting “Joe69” would result in the following MS DOS command:&lt;br /&gt;
doStuff.exe –Joe69&lt;br /&gt;
Lets say we input Joe69 &amp;amp; netstat –a we would get the following response:&lt;br /&gt;
The exe doStuff would execute passing in the User Id Joe69, but then the dos command netstat would be called. How this works is the passing of the parameter “&amp;amp;” into the application, which in turn is used as a command appender in MS DOS and hence the command after the &amp;amp; character is executed.&lt;br /&gt;
&lt;br /&gt;
'''UNIX:'''&lt;br /&gt;
An attacker might insert the string “; cat /etc/hosts” the contents of the UNIX hosts file might be exposed to the attacker. &lt;br /&gt;
&lt;br /&gt;
'''.NET Example:'''&lt;br /&gt;
namespace ExternalExecution{class CallExternal{static void Main(string[] args){String arg1=args[0];System.Diagnostics.Process.Start(&amp;quot;doStuff.exe&amp;quot;, arg1);}}}&lt;br /&gt;
&lt;br /&gt;
Yet again there is no data validation to speak of here. Assuming no upstream validation occurring in another class.&lt;br /&gt;
&lt;br /&gt;
These attacks include calls to the operating system via system calls, the use of external programs via shell commands, as well as calls to backend databases via SQL (i.e., SQL injection). Complete scripts written in perl, python, shell, bat and other languages can be injected into poorly designed web applications and executed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Good Patterns &amp;amp; procedures to prevent OS injection:'''&lt;br /&gt;
&lt;br /&gt;
See the Data Validation section.&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5423</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5423"/>
				<updated>2006-06-09T07:50:48Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
Data Validation&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
Canoncalization of input.&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.x delves much more into this subject.&lt;br /&gt;
Data validation strategy&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “Known Good” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “Known bad”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
Data Validation Strategy&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
1.	Exact Match (Constrain)&lt;br /&gt;
2.	Known Good (Accept)&lt;br /&gt;
3.	Reject Known bad (Reject)&lt;br /&gt;
4.      Encode Known bad (Sanitise)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.&lt;br /&gt;
&lt;br /&gt;
·	Exact Match: (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
·	Known Good: If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
·	Reject Known bad: We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
·	Encode Known Bad: This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
HTML-encoding and URL-encoding user input when writing back to the client. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Good Patterns for Data validation&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
preg_grep() could also be used for a True or False result. This would enable us to let “only known good” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
http://www.regxlib.com/CheatSheet.htm&lt;br /&gt;
&lt;br /&gt;
^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
[abcde] 	Matches any single character specified in set&lt;br /&gt;
[^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Framework Example(Struts 1.2)&lt;br /&gt;
&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
Enables us to have a central area for data validation.&lt;br /&gt;
Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
1.	What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
2.	The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
&lt;br /&gt;
  validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
package com.pcs.necronomicon&lt;br /&gt;
import org.apache.struts.validator.ValidatorForm;&lt;br /&gt;
public class LogonForm extends ValidatorForm {  &lt;br /&gt;
&lt;br /&gt;
  private String username;&lt;br /&gt;
  private String password;&lt;br /&gt;
  &lt;br /&gt;
  public String getUsername() {&lt;br /&gt;
&lt;br /&gt;
    return username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
  public void setUsername(String username) {&lt;br /&gt;
&lt;br /&gt;
    this.username = username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  public String getPassword() {&lt;br /&gt;
&lt;br /&gt;
    return password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
public void setPassword(String password) {&lt;br /&gt;
&lt;br /&gt;
    this.password = password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot; type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-validation&amp;gt;&lt;br /&gt;
&amp;lt;formset&amp;gt;&lt;br /&gt;
&amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in Application.properties. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
errors.required={0} is required.&lt;br /&gt;
errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
errors.invalid={0} is invalid.&lt;br /&gt;
errors.byte={0} must be a byte.&lt;br /&gt;
errors.short={0} must be a short.&lt;br /&gt;
errors.integer={0} must be an integer.&lt;br /&gt;
errors.long={0} must be a long.0.   &lt;br /&gt;
errors.float={0} must be a float.&lt;br /&gt;
errors.double={0} must be a double.&lt;br /&gt;
errors.date={0} is not a date.&lt;br /&gt;
errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
errors.email={0} is an invalid e-mail address.&lt;br /&gt;
prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;var&amp;gt;&lt;br /&gt;
         &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
         &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; &lt;br /&gt;
staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Framework example(.NET):'''&lt;br /&gt;
&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past. The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
&lt;br /&gt;
'''RequiredFieldValidator''' – Makes the associated input control a required field.&lt;br /&gt;
&lt;br /&gt;
'''CompareValidator''' – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
&lt;br /&gt;
'''RangeValidator''' – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
&lt;br /&gt;
'''RegularExpressionValidator''' – Checks user input against a regular expression.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;form runat=server&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RequiredFieldValidator runat=server ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; &lt;br /&gt;
&amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;    &lt;br /&gt;
&amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RegularExpressionValidator runat=server display=dynamic controltovalidate=&amp;quot;Name&amp;quot; errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/math&amp;gt;&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Length Checking:'''&lt;br /&gt;
Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app. &lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an xternal request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''''Never Rely on Client-Side Data Validation'''' Client-side validation can always be bypassed. Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.&lt;br /&gt;
&lt;br /&gt;
'''Remember''': Data validation must be always done on the server side.&lt;br /&gt;
A code review focuses on server side code. Any client side security code is not and cannot be considered security.&lt;br /&gt;
&lt;br /&gt;
'''Data validation of parameter names'''&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
&lt;br /&gt;
UserId =3o1nk395y&lt;br /&gt;
password=letMeIn123&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (UserId,password from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web services data validation&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as xsd:string.&lt;br /&gt;
&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5419</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5419"/>
				<updated>2006-06-09T07:42:21Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
Data Validation&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
Canoncalization of input.&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.x delves much more into this subject.&lt;br /&gt;
Data validation strategy&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “Known Good” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “Known bad”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
Data Validation Strategy&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
1.	Exact Match (Constrain)&lt;br /&gt;
2.	Known Good (Accept)&lt;br /&gt;
3.	Reject Known bad (Reject)&lt;br /&gt;
4.      Encode Known bad (Sanitise)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.&lt;br /&gt;
&lt;br /&gt;
·	Exact Match: (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
·	Known Good: If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
·	Reject Known bad: We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
·	Encode Known Bad: This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
HTML-encoding and URL-encoding user input when writing back to the client. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Good Patterns for Data validation&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
preg_grep() could also be used for a True or False result. This would enable us to let “only known good” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
http://www.regxlib.com/CheatSheet.htm&lt;br /&gt;
&lt;br /&gt;
^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
[abcde] 	Matches any single character specified in set&lt;br /&gt;
[^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Framework Example(Struts 1.2)&lt;br /&gt;
&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
Enables us to have a central area for data validation.&lt;br /&gt;
Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
1.	What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
2.	The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
&lt;br /&gt;
  validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
package com.pcs.necronomicon&lt;br /&gt;
import org.apache.struts.validator.ValidatorForm;&lt;br /&gt;
public class LogonForm extends ValidatorForm {  &lt;br /&gt;
&lt;br /&gt;
  private String username;&lt;br /&gt;
  private String password;&lt;br /&gt;
  &lt;br /&gt;
  public String getUsername() {&lt;br /&gt;
&lt;br /&gt;
    return username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
  public void setUsername(String username) {&lt;br /&gt;
&lt;br /&gt;
    this.username = username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  public String getPassword() {&lt;br /&gt;
&lt;br /&gt;
    return password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
public void setPassword(String password) {&lt;br /&gt;
&lt;br /&gt;
    this.password = password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot; type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-validation&amp;gt;&lt;br /&gt;
&amp;lt;formset&amp;gt;&lt;br /&gt;
&amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in Application.properties. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
errors.required={0} is required.&lt;br /&gt;
errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
errors.invalid={0} is invalid.&lt;br /&gt;
errors.byte={0} must be a byte.&lt;br /&gt;
errors.short={0} must be a short.&lt;br /&gt;
errors.integer={0} must be an integer.&lt;br /&gt;
errors.long={0} must be a long.0.   &lt;br /&gt;
errors.float={0} must be a float.&lt;br /&gt;
errors.double={0} must be a double.&lt;br /&gt;
errors.date={0} is not a date.&lt;br /&gt;
errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
errors.email={0} is an invalid e-mail address.&lt;br /&gt;
prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;var&amp;gt;&lt;br /&gt;
         &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
         &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; &lt;br /&gt;
staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Framework example(.NET):'''&lt;br /&gt;
&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past. The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
&lt;br /&gt;
'''RequiredFieldValidator''' – Makes the associated input control a required field.&lt;br /&gt;
&lt;br /&gt;
'''CompareValidator''' – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
&lt;br /&gt;
'''RangeValidator''' – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
&lt;br /&gt;
'''RegularExpressionValidator''' – Checks user input against a regular expression.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;form runat=server&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RequiredFieldValidator runat=server ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; &lt;br /&gt;
&amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;    &lt;br /&gt;
&amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RegularExpressionValidator runat=server display=dynamic controltovalidate=&amp;quot;Name&amp;quot; errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Length Checking:'''&lt;br /&gt;
Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app. &lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an xternal request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''''Never Rely on Client-Side Data Validation'''' Client-side validation can always be bypassed. Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.&lt;br /&gt;
&lt;br /&gt;
'''Remember''': Data validation must be always done on the server side.&lt;br /&gt;
A code review focuses on server side code. Any client side security code is not and cannot be considered security.&lt;br /&gt;
&lt;br /&gt;
'''Data validation of parameter names'''&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
&lt;br /&gt;
UserId =3o1nk395y&lt;br /&gt;
password=letMeIn123&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (UserId,password from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web services data validation&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as xsd:string.&lt;br /&gt;
&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5418</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5418"/>
				<updated>2006-06-09T07:41:20Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
Data Validation&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
Canoncalization of input.&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.x delves much more into this subject.&lt;br /&gt;
Data validation strategy&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “Known Good” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “Known bad”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
Data Validation Strategy&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
1.	Exact Match (Constrain)&lt;br /&gt;
2.	Known Good (Accept)&lt;br /&gt;
3.	Reject Known bad (Reject)&lt;br /&gt;
4.      Encode Known bad (Sanitise)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.&lt;br /&gt;
&lt;br /&gt;
·	Exact Match: (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
·	Known Good: If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
·	Reject Known bad: We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
·	Encode Known Bad: This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
HTML-encoding and URL-encoding user input when writing back to the client. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Good Patterns for Data validation&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
preg_grep() could also be used for a True or False result. This would enable us to let “only known good” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
http://www.regxlib.com/CheatSheet.htm&lt;br /&gt;
&lt;br /&gt;
^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
[abcde] 	Matches any single character specified in set&lt;br /&gt;
[^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Framework Example(Struts 1.2)&lt;br /&gt;
&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
Enables us to have a central area for data validation.&lt;br /&gt;
Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
1.	What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
2.	The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
&lt;br /&gt;
  validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
package com.pcs.necronomicon&lt;br /&gt;
import org.apache.struts.validator.ValidatorForm;&lt;br /&gt;
public class LogonForm extends ValidatorForm {  &lt;br /&gt;
&lt;br /&gt;
  private String username;&lt;br /&gt;
  private String password;&lt;br /&gt;
  &lt;br /&gt;
  public String getUsername() {&lt;br /&gt;
&lt;br /&gt;
    return username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
  public void setUsername(String username) {&lt;br /&gt;
&lt;br /&gt;
    this.username = username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  public String getPassword() {&lt;br /&gt;
&lt;br /&gt;
    return password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
public void setPassword(String password) {&lt;br /&gt;
&lt;br /&gt;
    this.password = password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot; type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-validation&amp;gt;&lt;br /&gt;
&amp;lt;formset&amp;gt;&lt;br /&gt;
&amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in Application.properties. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
errors.required={0} is required.&lt;br /&gt;
errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
errors.invalid={0} is invalid.&lt;br /&gt;
errors.byte={0} must be a byte.&lt;br /&gt;
errors.short={0} must be a short.&lt;br /&gt;
errors.integer={0} must be an integer.&lt;br /&gt;
errors.long={0} must be a long.0.   &lt;br /&gt;
errors.float={0} must be a float.&lt;br /&gt;
errors.double={0} must be a double.&lt;br /&gt;
errors.date={0} is not a date.&lt;br /&gt;
errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
errors.email={0} is an invalid e-mail address.&lt;br /&gt;
prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;var&amp;gt;&lt;br /&gt;
         &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
         &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; &lt;br /&gt;
staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Framework example(.NET):'''&lt;br /&gt;
&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past. The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
&lt;br /&gt;
'''RequiredFieldValidator''' – Makes the associated input control a required field.&lt;br /&gt;
&lt;br /&gt;
'''CompareValidator''' – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
&lt;br /&gt;
'''RangeValidator''' – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
&lt;br /&gt;
'''RegularExpressionValidator''' – Checks user input against a regular expression.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;form runat=server&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RequiredFieldValidator runat=server ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; &lt;br /&gt;
&amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;    &lt;br /&gt;
&amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;asp:RegularExpressionValidator runat=server display=dynamic controltovalidate=&amp;quot;Name&amp;quot; errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Length Checking:'''&lt;br /&gt;
Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app. &lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an xternal request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''''Never Rely on Client-Side Data Validation'''' Client-side validation can always be bypassed. Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.&lt;br /&gt;
&lt;br /&gt;
'''Remember''': Data validation must be always done on the server side.&lt;br /&gt;
A code review focuses on server side code. Any client side security code is not and cannot be considered security.&lt;br /&gt;
&lt;br /&gt;
'''Data validation of parameter names'''&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
&lt;br /&gt;
UserId =3o1nk395y&lt;br /&gt;
password=letMeIn123&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (UserId,password from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web services data validation&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as xsd:string.&lt;br /&gt;
&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5288</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5288"/>
				<updated>2006-06-08T09:41:56Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
Data Validation&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
Canoncalization of input.&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.x delves much more into this subject.&lt;br /&gt;
Data validation strategy&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “Known Good” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “Known bad”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
Data Validation Strategy&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
1.	Exact Match (Constrain)&lt;br /&gt;
2.	Known Good (Accept)&lt;br /&gt;
3.	Reject Known bad (Reject)&lt;br /&gt;
4.      Encode Known bad (Sanitise)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.&lt;br /&gt;
&lt;br /&gt;
·	Exact Match: (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
·	Known Good: If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
·	Reject Known bad: We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
·	Encode Known Bad: This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
HTML-encoding and URL-encoding user input when writing back to the client. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Good Patterns for Data validation&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
preg_grep() could also be used for a True or False result. This would enable us to let “only known good” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
http://www.regxlib.com/CheatSheet.htm&lt;br /&gt;
&lt;br /&gt;
^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
[abcde] 	Matches any single character specified in set&lt;br /&gt;
[^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Framework Example(Struts 1.2)&lt;br /&gt;
&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
Enables us to have a central area for data validation.&lt;br /&gt;
Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
1.	What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
2.	The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
&lt;br /&gt;
  validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
package com.pcs.necronomicon&lt;br /&gt;
import org.apache.struts.validator.ValidatorForm;&lt;br /&gt;
public class LogonForm extends ValidatorForm {  &lt;br /&gt;
&lt;br /&gt;
  private String username;&lt;br /&gt;
  private String password;&lt;br /&gt;
  &lt;br /&gt;
  public String getUsername() {&lt;br /&gt;
&lt;br /&gt;
    return username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
  public void setUsername(String username) {&lt;br /&gt;
&lt;br /&gt;
    this.username = username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  public String getPassword() {&lt;br /&gt;
&lt;br /&gt;
    return password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
public void setPassword(String password) {&lt;br /&gt;
&lt;br /&gt;
    this.password = password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot; type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-validation&amp;gt;&lt;br /&gt;
&amp;lt;formset&amp;gt;&lt;br /&gt;
&amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in Application.properties. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
errors.required={0} is required.&lt;br /&gt;
errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
errors.invalid={0} is invalid.&lt;br /&gt;
errors.byte={0} must be a byte.&lt;br /&gt;
errors.short={0} must be a short.&lt;br /&gt;
errors.integer={0} must be an integer.&lt;br /&gt;
errors.long={0} must be a long.0.   &lt;br /&gt;
errors.float={0} must be a float.&lt;br /&gt;
errors.double={0} must be a double.&lt;br /&gt;
errors.date={0} is not a date.&lt;br /&gt;
errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
errors.email={0} is an invalid e-mail address.&lt;br /&gt;
prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;var&amp;gt;&lt;br /&gt;
         &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
         &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Framework example(.NET):&lt;br /&gt;
&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past.&lt;br /&gt;
&lt;br /&gt;
The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
·	RequiredFieldValidator – Makes the associated input control a required field.&lt;br /&gt;
·	CompareValidator – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
·	RangeValidator – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
·	RegularExpressionValidator – Checks user input against a regular expression.&lt;br /&gt;
&lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form runat=server&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
        &amp;lt;asp:RequiredFieldValidator runat=server ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; *&lt;br /&gt;
&amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;    &lt;br /&gt;
&amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;asp:RegularExpressionValidator runat=server display=dynamic controltovalidate=&amp;quot;Name&amp;quot; errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
&lt;br /&gt;
'''Length Checking:'''&lt;br /&gt;
Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app. &lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an external request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''''Never Rely on Client-Side Data Validation'''' Client-side validation can always be bypassed. Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? &lt;br /&gt;
&lt;br /&gt;
Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.&lt;br /&gt;
&lt;br /&gt;
'''Remember''': Data validation must be always done on the server side.&lt;br /&gt;
A code review focuses on server side code. Any client side security code is not and cannot be considered security.&lt;br /&gt;
&lt;br /&gt;
'''Data validation of parameter names'''&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
&lt;br /&gt;
UserId =3o1nk395y&lt;br /&gt;
password=letMeIn123&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (UserId,password from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web services data validation&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as xsd:string.&lt;br /&gt;
&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5287</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5287"/>
				<updated>2006-06-08T09:37:08Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
Data Validation&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
Canoncalization of input.&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.x delves much more into this subject.&lt;br /&gt;
Data validation strategy&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “Known Good” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “Known bad”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
Data Validation Strategy&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy. &lt;br /&gt;
&lt;br /&gt;
1.	Exact Match (Constrain)&lt;br /&gt;
2.	Known Good (Accept)&lt;br /&gt;
3.	Reject Known bad (Reject)&lt;br /&gt;
4.	Encode Known bad (Sanitise)&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
·	Exact Match: (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
·	Known Good: If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
·	Reject Known bad: We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
·	Encode Known Bad: This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
HTML-encoding and URL-encoding user input when writing back to the client. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Good Patterns for Data validation&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
preg_grep() could also be used for a True or False result. This would enable us to let “only known good” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
http://www.regxlib.com/CheatSheet.htm&lt;br /&gt;
&lt;br /&gt;
^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
[abcde] 	Matches any single character specified in set&lt;br /&gt;
[^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Framework Example(Struts 1.2)&lt;br /&gt;
&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
Enables us to have a central area for data validation.&lt;br /&gt;
Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
1.	What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
2.	The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
&lt;br /&gt;
  validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
package com.pcs.necronomicon&lt;br /&gt;
import org.apache.struts.validator.ValidatorForm;&lt;br /&gt;
public class LogonForm extends ValidatorForm {  &lt;br /&gt;
&lt;br /&gt;
  private String username;&lt;br /&gt;
  private String password;&lt;br /&gt;
  &lt;br /&gt;
  public String getUsername() {&lt;br /&gt;
&lt;br /&gt;
    return username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
  public void setUsername(String username) {&lt;br /&gt;
&lt;br /&gt;
    this.username = username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  public String getPassword() {&lt;br /&gt;
&lt;br /&gt;
    return password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
public void setPassword(String password) {&lt;br /&gt;
&lt;br /&gt;
    this.password = password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot; type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-validation&amp;gt;&lt;br /&gt;
&amp;lt;formset&amp;gt;&lt;br /&gt;
&amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in Application.properties. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
errors.required={0} is required.&lt;br /&gt;
errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
errors.invalid={0} is invalid.&lt;br /&gt;
errors.byte={0} must be a byte.&lt;br /&gt;
errors.short={0} must be a short.&lt;br /&gt;
errors.integer={0} must be an integer.&lt;br /&gt;
errors.long={0} must be a long.0.   &lt;br /&gt;
errors.float={0} must be a float.&lt;br /&gt;
errors.double={0} must be a double.&lt;br /&gt;
errors.date={0} is not a date.&lt;br /&gt;
errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
errors.email={0} is an invalid e-mail address.&lt;br /&gt;
prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;var&amp;gt;&lt;br /&gt;
         &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
         &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Framework example(.NET):&lt;br /&gt;
&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past.&lt;br /&gt;
&lt;br /&gt;
The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
·	RequiredFieldValidator – Makes the associated input control a required field.&lt;br /&gt;
·	CompareValidator – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
·	RangeValidator – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
·	RegularExpressionValidator – Checks user input against a regular expression.&lt;br /&gt;
&lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form runat=server&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
        &amp;lt;asp:RequiredFieldValidator runat=server ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; *&lt;br /&gt;
&amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;    &lt;br /&gt;
&amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;asp:RegularExpressionValidator runat=server display=dynamic controltovalidate=&amp;quot;Name&amp;quot; errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
&lt;br /&gt;
'''Length Checking:'''&lt;br /&gt;
Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app. &lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an external request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''''Never Rely on Client-Side Data Validation'''' Client-side validation can always be bypassed. Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? &lt;br /&gt;
&lt;br /&gt;
Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.&lt;br /&gt;
&lt;br /&gt;
'''Remember''': Data validation must be always done on the server side.&lt;br /&gt;
A code review focuses on server side code. Any client side security code is not and cannot be considered security.&lt;br /&gt;
&lt;br /&gt;
'''Data validation of parameter names'''&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
&lt;br /&gt;
UserId =3o1nk395y&lt;br /&gt;
password=letMeIn123&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (UserId,password from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web services data validation&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as xsd:string.&lt;br /&gt;
&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=File:DV_Strength.GIF&amp;diff=5286</id>
		<title>File:DV Strength.GIF</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=File:DV_Strength.GIF&amp;diff=5286"/>
				<updated>2006-06-08T09:31:16Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5285</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5285"/>
				<updated>2006-06-08T09:29:56Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
Data Validation&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
Canoncalization of input.&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.x delves much more into this subject.&lt;br /&gt;
Data validation strategy&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “Known Good” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “Known bad”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
Data Validation Strategy&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy. &lt;br /&gt;
&lt;br /&gt;
1.	Exact Match (Constrain)&lt;br /&gt;
2.	Known Good (Accept)&lt;br /&gt;
3.	Reject Known bad (Reject)&lt;br /&gt;
4.	Encode Known bad (Sanitise)&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
·	Exact Match: (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
·	Known Good: If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
·	Reject Known bad: We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
·	Encode Known Bad: This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
HTML-encoding and URL-encoding user input when writing back to the client. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Good Patterns for Data validation&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
preg_grep() could also be used for a True or False result. This would enable us to let “only known good” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
http://www.regxlib.com/CheatSheet.htm&lt;br /&gt;
&lt;br /&gt;
^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
[abcde] 	Matches any single character specified in set&lt;br /&gt;
[^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Framework Example(Struts 1.2)&lt;br /&gt;
&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
Enables us to have a central area for data validation.&lt;br /&gt;
Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
1.	What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
2.	The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
&lt;br /&gt;
  validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
package com.pcs.necronomicon&lt;br /&gt;
import org.apache.struts.validator.ValidatorForm;&lt;br /&gt;
public class LogonForm extends ValidatorForm {  &lt;br /&gt;
&lt;br /&gt;
  private String username;&lt;br /&gt;
  private String password;&lt;br /&gt;
  &lt;br /&gt;
  public String getUsername() {&lt;br /&gt;
&lt;br /&gt;
    return username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
  public void setUsername(String username) {&lt;br /&gt;
&lt;br /&gt;
    this.username = username;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  public String getPassword() {&lt;br /&gt;
&lt;br /&gt;
    return password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
public void setPassword(String password) {&lt;br /&gt;
&lt;br /&gt;
    this.password = password;&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot; type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-validation&amp;gt;&lt;br /&gt;
&amp;lt;formset&amp;gt;&lt;br /&gt;
&amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in Application.properties. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
errors.required={0} is required.&lt;br /&gt;
errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
errors.invalid={0} is invalid.&lt;br /&gt;
errors.byte={0} must be a byte.&lt;br /&gt;
errors.short={0} must be a short.&lt;br /&gt;
errors.integer={0} must be an integer.&lt;br /&gt;
errors.long={0} must be a long.0.   &lt;br /&gt;
errors.float={0} must be a float.&lt;br /&gt;
errors.double={0} must be a double.&lt;br /&gt;
errors.date={0} is not a date.&lt;br /&gt;
errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
errors.email={0} is an invalid e-mail address.&lt;br /&gt;
prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
        &amp;lt;var&amp;gt;&lt;br /&gt;
         &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
         &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Framework example(.NET):&lt;br /&gt;
&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past.&lt;br /&gt;
&lt;br /&gt;
The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
·	RequiredFieldValidator – Makes the associated input control a required field.&lt;br /&gt;
·	CompareValidator – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
·	RangeValidator – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
·	RegularExpressionValidator – Checks user input against a regular expression.&lt;br /&gt;
&lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form runat=server&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
        &amp;lt;asp:RequiredFieldValidator runat=server &lt;br /&gt;
            ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; *&lt;br /&gt;
        &amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;asp:RegularExpressionValidator runat=server display=dynamic&lt;br /&gt;
            controltovalidate=&amp;quot;Name&amp;quot; &lt;br /&gt;
            errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; &lt;br /&gt;
            validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Length Checking:&lt;br /&gt;
 Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app. &lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an external request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Never Rely on Client-Side Data Validation Client-side validation can always be bypassed. Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? &lt;br /&gt;
&lt;br /&gt;
Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.&lt;br /&gt;
&lt;br /&gt;
Remember: Data validation must be always done on the server side.&lt;br /&gt;
A code review focuses on server side code. Any client side security code is not and cannot be considered security.&lt;br /&gt;
&lt;br /&gt;
Data validation of parameter names&lt;br /&gt;
&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
UserId =3o1nk395y&lt;br /&gt;
password=letMeIn123&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (UserId,password from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web services data validation&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as xsd:string.&lt;br /&gt;
&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5284</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=5284"/>
				<updated>2006-06-08T09:19:37Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
Data Validation&lt;br /&gt;
&lt;br /&gt;
One key area in web application security is the validation of data inputted from an external source. Many application exploits a derived from weak input validation on behalf of the application. Weak data validation gives the attacked the opportunity to make the application perform some functionality which it is not meant to do.&lt;br /&gt;
&lt;br /&gt;
Canoncalization of input.&lt;br /&gt;
&lt;br /&gt;
Input can be encoded to a format that can still be interpreted correctly by the application but may not be an obvious avenue of attack.&lt;br /&gt;
&lt;br /&gt;
The encoding of ASCI to Unicode is another method of bypassing input validation. Applications rarely test for Unicode exploits and hence provides the attacker a route of attack.&lt;br /&gt;
&lt;br /&gt;
The issue to remember here is that the application is safe if Unicode representation or other malformed representation is input. The application responds correctly and recognises all possible representations of invalid characters.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
The ASCII: &amp;lt;script&amp;gt; &lt;br /&gt;
(If we simply block “&amp;lt;” and “&amp;gt;” characters the other representations below shall pass data validation and execute).&lt;br /&gt;
&lt;br /&gt;
URL encoded: %3C%73%63%72%69%70%74%3E&lt;br /&gt;
Unicode Encoded: &amp;amp;#60&amp;amp;#115&amp;amp;#99&amp;amp;#114&amp;amp;#105&amp;amp;#112&amp;amp;#116&amp;amp;#62&lt;br /&gt;
&lt;br /&gt;
The OWASP Guide 2.x delves much more into this subject.&lt;br /&gt;
Data validation strategy&lt;br /&gt;
&lt;br /&gt;
A general rule is to accept only “Known Good” characters, i.e. the characters that are to be expected. If this cannot be done the next strongest strategy is “Known bad”, where we reject all known bad characters. The issue with this is that today’s known bad list may expand tomorrow as new technologies are added to the enterprise infrastructure.&lt;br /&gt;
&lt;br /&gt;
Data Validation Strategy&lt;br /&gt;
There are a number of models to think about when designing a data validation strategy. &lt;br /&gt;
&lt;br /&gt;
1.	Exact Match (Constrain)&lt;br /&gt;
2.	Known Good (Accept)&lt;br /&gt;
3.	Reject Known bad (Reject)&lt;br /&gt;
4.	Encode Known bad (Sanitise)&lt;br /&gt;
&lt;br /&gt;
In addition there must be a check for maximum length of any input received from an external source, such as a downstream service/computer or a user at a web browser.&lt;br /&gt;
Rejected Data must not be persisted to the data store unless it is sanitised. This is a common mistake to log erroneous data but that may be what the attacker wishes your application to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
·	Exact Match: (preferred method) Only accept values from a finite list of known values.&lt;br /&gt;
E.g.: A Radio button component on a Web page has 3 settings (A, B, C). Only one of those three settings must be accepted (A or B or C). Any other value must be rejected. &lt;br /&gt;
&lt;br /&gt;
·	Known Good: If we do not have a finite list of all the possible values that can be entered into the system we uses known good approach.&lt;br /&gt;
E.g.: an email address, we know it shall contain one and only one @. It may also have one or more full stops “.”. The rest of the information can be anything from [a-z] or [A-Z] or [0-9] and some other characters such as “_ “or “–“, so we let these ranges in and define a maximum length for the address.&lt;br /&gt;
&lt;br /&gt;
·	Reject Known bad: We have a list of known bad values we do not wish to be entered into the system. This occurs on free form text areas and areas where a user may write a note. The weakness of this model is that today known bad may not be sufficient for tomorrow.&lt;br /&gt;
&lt;br /&gt;
·	Encode Known Bad: This is the weakest approach. This approach accepts all input but HTML encodes any characters within a certain character range. HTML encoding is done so if the input needs to be redisplayed the browser shall not interpret the text as script, but the text looks the same as what the user originally typed.&lt;br /&gt;
&lt;br /&gt;
HTML-encoding and URL-encoding user input when writing back to the client. In this case, the assumption is that no input is treated as HTML and all output is written back in a protected form. This is sanitisation in action. &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Good Patterns for Data validation&lt;br /&gt;
&lt;br /&gt;
A good example of a pattern for data validation to prevent OS injection in PHP applications would be as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$string = preg_replace(&amp;quot;/[^a-zA-Z0-9]/&amp;quot;, &amp;quot;&amp;quot;, $string);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This code above would replace any non alphanumeric characters with “”.&lt;br /&gt;
preg_grep() could also be used for a True or False result. This would enable us to let “only known good” characters into the application.&lt;br /&gt;
&lt;br /&gt;
Using regular expressions is a common method of restricting input character types.&lt;br /&gt;
A common mistake in the development of regular expressions is not escaping characters, which are interpreted as control characters, or not validating all avenues of input.&lt;br /&gt;
&lt;br /&gt;
Examples of regular expression are as follows:&lt;br /&gt;
&lt;br /&gt;
http://www.regxlib.com/CheatSheet.htm&lt;br /&gt;
&lt;br /&gt;
^[a-zA-Z]$  	Alpha characters only, a to z and A to Z (RegEx is case sensitive).&lt;br /&gt;
^[0-9]$ 	Numeric only (0 to 9).&lt;br /&gt;
[abcde] 	Matches any single character specified in set&lt;br /&gt;
[^abcde] 	Matches any single character not specified in set&lt;br /&gt;
&lt;br /&gt;
Framework Example&lt;br /&gt;
(Struts 1.2)&lt;br /&gt;
In the J2EE world the struts framework (1.1) contains a utility called the commons validator. This enables us to do two things.&lt;br /&gt;
&lt;br /&gt;
Enables us to have a central area for data validation.&lt;br /&gt;
Provides us with a data validation framework.&lt;br /&gt;
&lt;br /&gt;
1.	What to look for when examining struts is as follows:&lt;br /&gt;
&lt;br /&gt;
2.	The struts-config.xml file must contain the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Validator Configuration --&amp;gt;&lt;br /&gt;
&amp;lt;plug-in className=&amp;quot;org.apache.struts.validator.ValidatorPlugIn&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;set-property property=&amp;quot;pathnames&amp;quot; value=&amp;quot;/technology/WEB-INF/&lt;br /&gt;
  validator-rules.xml, /WEB-INF/validation.xml&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/plug-in&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells the framework to load the validator plug-in. It also loads the property files defined by the comma-separated list. By default a developer would add regular expressions for the defined fields in the validation.xml file.&lt;br /&gt;
&lt;br /&gt;
Next we look at the form beans for the application. In struts, form beans are on the server side and encapsulate the information sent to the application via a HTTP form.&lt;br /&gt;
We can have concrete form beans (built in code by developers) or dynamic form beans. Here is a concrete bean below:&lt;br /&gt;
&lt;br /&gt;
package com.pcs.necronomicon&lt;br /&gt;
import org.apache.struts.validator.ValidatorForm;&lt;br /&gt;
&lt;br /&gt;
public class LogonForm extends ValidatorForm {&lt;br /&gt;
  private String username;&lt;br /&gt;
  private String password;&lt;br /&gt;
  &lt;br /&gt;
  public String getUsername() {&lt;br /&gt;
    return username;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public void setUsername(String username) {&lt;br /&gt;
    this.username = username;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public String getPassword() {&lt;br /&gt;
    return password;&lt;br /&gt;
  }&lt;br /&gt;
public void setPassword(String password) {&lt;br /&gt;
    this.password = password;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Note the LoginForm extends the ValidatorForm, this is a must as the parent class (ValidatorForm) has a validate method which is called automatically and calls the rules defined in validation.xml&lt;br /&gt;
&lt;br /&gt;
Now to be assured that this form bean is being called we look at the struts-config.xml file:&lt;br /&gt;
It should have something like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-beans&amp;gt;&lt;br /&gt;
  &amp;lt;form-bean name=&amp;quot;logonForm&amp;quot;&lt;br /&gt;
            type=&amp;quot; com.pcs.necronomicon.LogonForm&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form-beans&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next we look at the validation.xml file. It should contain something similar to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form-validation&amp;gt;&lt;br /&gt;
  &amp;lt;formset&amp;gt;&lt;br /&gt;
    &amp;lt;form name=&amp;quot;logonForm&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; &lt;br /&gt;
            depends=&amp;quot;required&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the same name in the validation.xml, the struts-config.xml, this is an important relationship and is case sensitive.&lt;br /&gt;
&lt;br /&gt;
The field “username” is also case sensitive and refers to the String username in the LoginForm class.&lt;br /&gt;
&lt;br /&gt;
The “depends” directive dictates that the parameter is required. If this is blank the error defined in Application.properties. This configuration file contains error messages among other things. It is also a good place to look for information leakage issues:&lt;br /&gt;
&lt;br /&gt;
# Error messages for Validator framework validations&lt;br /&gt;
errors.required={0} is required.&lt;br /&gt;
errors.minlength={0} cannot be less than {1} characters.&lt;br /&gt;
errors.maxlength={0} cannot be greater than {2} characters.&lt;br /&gt;
errors.invalid={0} is invalid.&lt;br /&gt;
errors.byte={0} must be a byte.&lt;br /&gt;
errors.short={0} must be a short.&lt;br /&gt;
errors.integer={0} must be an integer.&lt;br /&gt;
errors.long={0} must be a long.0.   errors.float={0} must be a float.&lt;br /&gt;
errors.double={0} must be a double.&lt;br /&gt;
errors.date={0} is not a date.&lt;br /&gt;
errors.range={0} is not in the range {1} through {2}.&lt;br /&gt;
errors.creditcard={0} is not a valid credit card number.&lt;br /&gt;
errors.email={0} is an invalid e-mail address.&lt;br /&gt;
prompt.username = User Name is required.&lt;br /&gt;
&lt;br /&gt;
The error defined by arg0, prompt.username is displayed as an alert box by the struts framework to the user.&lt;br /&gt;
The developer would need to take this a step further by validating the input via regular expression:&lt;br /&gt;
      &amp;lt;field property=&amp;quot;username&amp;quot; &lt;br /&gt;
            depends=&amp;quot;required,mask&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;arg0 key=&amp;quot;prompt.username&amp;quot;/&amp;gt;&lt;br /&gt;
         &amp;lt;var&amp;gt;&lt;br /&gt;
            &amp;lt;var-name&amp;gt;mask&amp;lt;/var-name&amp;gt;&lt;br /&gt;
            &amp;lt;var-value&amp;gt;^[0-9a-zA-Z]*$&amp;lt;/var-value&amp;gt;&lt;br /&gt;
        &amp;lt;/var&amp;gt;&lt;br /&gt;
      &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/formset&amp;gt;&lt;br /&gt;
&amp;lt;/form-validation&amp;gt;&lt;br /&gt;
Here we have added the Mask directive, this specifies a variable &amp;lt;var&amp;gt;. and a regular expression. Any input into the username field which has anything other than A to Z, a to z or 0 to 9 shall cause an error to be thrown. The most common issue with this type of development is either the developer forgetting to validate all fields or a complete form. The other thing to look for is incorrect regular expressions, so learn those RegEx’s kids!!!&lt;br /&gt;
&lt;br /&gt;
We also need to check if the jsp pages have been linked up to the validation.xml finctionaltiy. This is done by &amp;lt;html:javascript&amp;gt; custom tag being included in the JSP as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html:javascript formName=&amp;quot;logonForm&amp;quot; dynamicJavascript=&amp;quot;true&amp;quot; staticJavascript=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Framework example:&lt;br /&gt;
(.NET)&lt;br /&gt;
The ASP .NET framework contains a validator framework, which has made input validation easier and less error prone than in the past.&lt;br /&gt;
The validation solution for .NET also has client and server side functionalty akin to Struts (J2EE).&lt;br /&gt;
What is a validator? According to the Miscosoft (MSDN) definition it is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;A validator is a control that checks one input control for a specific type of error condition and displays a description of that problem.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The main point to take out of this from a code review perspective is that one validator does one type of function. If we need to do a number of different checks on our input we need to use more than one validator.&lt;br /&gt;
&lt;br /&gt;
The .NET solution contains a number of controls out of the box:&lt;br /&gt;
·	RequiredFieldValidator – Makes the associated input control a required field.&lt;br /&gt;
·	CompareValidator – Compares the value entered by the user into an input control with the value entered into another input control or a constant value.&lt;br /&gt;
·	RangeValidator – Checks if the value of an input control is within a defined range of values.&lt;br /&gt;
·	RegularExpressionValidator – Checks user input against a regular expression.&lt;br /&gt;
The following is an example web page (.aspx) containing validation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Validate me baby!&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;asp:ValidationSummary runat=server HeaderText=&amp;quot;There were errors on the page:&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form runat=server&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Please enter your User Id&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
        &amp;lt;asp:RequiredFieldValidator runat=server &lt;br /&gt;
            ControlToValidate=Name ErrorMessage=&amp;quot;User ID is required.&amp;quot;&amp;gt; *&lt;br /&gt;
        &amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;User ID:&amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&amp;lt;input type=text runat=server id=Name&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;asp:RegularExpressionValidator runat=server display=dynamic&lt;br /&gt;
            controltovalidate=&amp;quot;Name&amp;quot; &lt;br /&gt;
            errormessage=&amp;quot;ID must be 6-8 letters.&amp;quot; &lt;br /&gt;
            validationexpression=&amp;quot;[a-zA-Z0-9]{6,8}&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;input type=submit runat=server id=SubmitMe value=Submit&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Remember to check to regular expressions so they are sufficient to protect the application. The “runat” directive means this code is executed at the server prior to being sent to client. When this is displayed to a users browser the code is simply HTML.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Length Checking:&lt;br /&gt;
 Another issue to consider is input length validation. If the input is limited by length this reduces the size of the script that can be injected into the web app. &lt;br /&gt;
Many web applications use operating system features and external programs to perform their functions. When a web application passes information from an HTTP request through as part of an external request, it must be carefully data validated for content and min/max length. Without data validation the attacker can inject Meta characters, malicious commands, or command modifiers, masquerading, as legitimate information and the web application will blindly pass these on to the external system for execution.&lt;br /&gt;
&lt;br /&gt;
Checking for minimum and maximum length is of paramount importance, even if the code base is not vulnerable to buffer overflow attacks.&lt;br /&gt;
&lt;br /&gt;
If a logging mechanism is employed to log all data used in a particular transaction we need to ensure that the payload received is not so big that it may affect the logging mechanism.&lt;br /&gt;
If the log file is sent a very large payload it may crash or if it is sent a very large payload repeatedly the hard disk of the app server may fill causing a denial of service. This type of attack can be used to recycle to log file, hence removing the audit trail.&lt;br /&gt;
If string parsing is performed on the payload received by the application and an extremely large string is sent repeatedly to the application the CPU cycles used by the application to parse the payload may cause service degradation or even denial of service.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Never Rely on Client-Side Data Validation Client-side validation can always be bypassed. Server-side code should perform its own validation. What if an attacker bypasses your client, or shuts off your client-side script routines, for example, by disabling JavaScript? &lt;br /&gt;
&lt;br /&gt;
Use client-side validation to help reduce the number of round trips to the server but do not rely on it for security.&lt;br /&gt;
&lt;br /&gt;
Remember: Data validation must be always done on the server side.&lt;br /&gt;
A code review focuses on server side code. Any client side security code is not and cannot be considered security.&lt;br /&gt;
&lt;br /&gt;
Data validation of parameter names&lt;br /&gt;
&lt;br /&gt;
When data is passed to a method of a web application via HTTP the payload is passed in a “key-value” pair such as &lt;br /&gt;
UserId =3o1nk395y&lt;br /&gt;
password=letMeIn123&lt;br /&gt;
&lt;br /&gt;
Previously we talked about input validation of the payload (parameter value) being passed to the application. But we also may need to check that the parameter name (UserId,password from above) have not been tampered with.&lt;br /&gt;
Invalid parameter names may cause the application to crash or act in an unexpected way.&lt;br /&gt;
The best approach is “Exact Match” as mentioned previously. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web services data validation&lt;br /&gt;
&lt;br /&gt;
The recommended input validation technique for web services is to use a schema. A schema is a “map” of all the allowable values that each parameter can take for a given web service method.&lt;br /&gt;
When a SOAP message is received by the web services handler the schema pertaining to the method being called is “run over” the message to validate the content of the soap message. &lt;br /&gt;
There are two types of web service communication methods; XML-IN/XML-OUT and REST (Representational State Transfer). &lt;br /&gt;
XML-IN/XML-OUT means that the request is in the form of a SOAP message and the reply is also SOAP. REST web services accept a URI request (Non XML) but return a XML reply. REST only supports a point-to-point solution wherein SOAP chain of communication may have multiple nodes prior to the final destination of the request.&lt;br /&gt;
Validating REST web services input it the same as validating a GET request. Validating an XML request is best done with a schema.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;xsd:schema xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot; xmlns=&amp;quot;http://server.test.com&amp;quot; targetNamespace=&amp;quot;http://server.test.com&amp;quot; elementFormDefault=&amp;quot;qualified&amp;quot; attributeFormDefault=&amp;quot;unqualified&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:complexType name=&amp;quot;AddressIn&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine1&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;addressLine2&amp;quot; type=&amp;quot;HundredANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;county&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;town&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:element name=&amp;quot;userId&amp;quot; type=&amp;quot;TenANumeric&amp;quot; nillable=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:sequence&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:complexType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;xsd:simpleType name=&amp;quot;HundredANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:maxLength value=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;xsd:simpleType name=&amp;quot;TenANumeric&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;xsd:restriction base=&amp;quot;xsd:string&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:minLength value=&amp;quot;1&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:maxLength value=&amp;quot;10&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;xsd:pattern value=&amp;quot;[a-zA-Z0-9]&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
		&amp;lt;/xsd:restriction&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;/xsd:simpleType&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/xsd:schema&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here we have a schema for an object called AddressIn. Each of the elements have restrictions applied to them and the restrictions (in red) define what valid characters can be inputted into each of the elements.&lt;br /&gt;
&lt;br /&gt;
What we need to look for is that each of the elements have a restriction applied to the as opposed to the simple type definition such as xsd:string.&lt;br /&gt;
&lt;br /&gt;
This schema also has the &amp;lt;xsd:sequence&amp;gt; tag applied to enforce the sequence of the data that is to be received.&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Dublin&amp;diff=5281</id>
		<title>Dublin</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Dublin&amp;diff=5281"/>
				<updated>2006-06-08T08:30:28Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Local News */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Chapter Template|chaptername=Ireland|leaderemail=eoin.keary@owasp.org|leadername=Eoin Keary|mailinglistsite=http://lists.sourceforge.net/lists/listinfo/owasp-ireland}}&lt;br /&gt;
&lt;br /&gt;
== Local News ==&lt;br /&gt;
&lt;br /&gt;
 '''OWASP Moves to MediaWiki Portal - 11:11, 20 May 2006 (EDT)'''&lt;br /&gt;
&lt;br /&gt;
OWASP is pleased to announce the arrival of OWASP 2.0!&lt;br /&gt;
&lt;br /&gt;
OWASP 2.0 utilizes the MediaWiki portal to manage and provide&lt;br /&gt;
the latest OWASP related information. Enjoy!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== NEXT MEETING==&lt;br /&gt;
We are taking a break for the summer.&lt;br /&gt;
Coming In September:&lt;br /&gt;
&lt;br /&gt;
Live Pentest: Anatomy of a web application pentest.&lt;br /&gt;
&lt;br /&gt;
OWASP Live CD: Preview of the OWASP Live CD &amp;quot;Pentest in your pocket&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
OWASP Testing Guide: Testing Guide walkthrough.&lt;br /&gt;
&lt;br /&gt;
The New &amp;quot;Top Ten&amp;quot;: OWASP Top 10 V2 review.&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Testing_Project_Roadmap&amp;diff=4852</id>
		<title>OWASP Testing Project Roadmap</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Testing_Project_Roadmap&amp;diff=4852"/>
				<updated>2006-06-01T13:19:09Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The project's overall goal is to...&lt;br /&gt;
&lt;br /&gt;
'''be a reference document for the purpose of performing penetration testing. This project shall provide examples of the most common web application vulnerabilities and attacks.'''&lt;br /&gt;
&lt;br /&gt;
In the near term, we are focused on the following tactical goals...&lt;br /&gt;
&lt;br /&gt;
1. '''Looking at each attack type and examine the method of testing to verify if the vulnerability exists.'''&lt;br /&gt;
&lt;br /&gt;
2. '''Examining other technical and non technical methods to examing and find vulnerabilities in applications.'''&lt;br /&gt;
&lt;br /&gt;
3. '''Discussing the OWASP testing framework and how it can help one to cover all the based when performing a pen test or risk assessment.'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Testing Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=4851</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=4851"/>
				<updated>2006-06-01T12:48:57Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Data Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Category:OWASP_Testing_Project&amp;diff=4850</id>
		<title>Category:OWASP Testing Project</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Category:OWASP_Testing_Project&amp;diff=4850"/>
				<updated>2006-06-01T12:44:39Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Testing Guide DOWNLOAD */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
This projects goal is to create a &amp;quot;best practices&amp;quot; penetration testing framework which users can implement in their own organizations and a &amp;quot;low level&amp;quot; penetration testing guide that describes how to find certain issues. &lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
Currently there are many people helping out when they can. &lt;br /&gt;
The Project lead is Eoin Keary. There are still lots of areas to be covered.&lt;br /&gt;
To contribute please email [mailto:eoin.keary@owasp.org Eoin]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Volunteers needed==&lt;br /&gt;
&lt;br /&gt;
'''Phase Two call for volunteers''' &lt;br /&gt;
Wed Mar 15 12:30:28 EST 2006 &lt;br /&gt;
&lt;br /&gt;
Work is underway on the 2nd phase of the testing guide, and we would love to hear from volunteers who could offer their knowledge in creating this phase. If you have knowledge and experience in application testing, and can spare a few hours a week, please do get in [mailto:eoin.keary@owasp.org touch]&lt;br /&gt;
&lt;br /&gt;
==Testing Guide Download==&lt;br /&gt;
[[OWASP Testing Guide]]&lt;br /&gt;
&lt;br /&gt;
==Testing Checklist==&lt;br /&gt;
[[OWASP Testing Checklist]]&lt;br /&gt;
&lt;br /&gt;
==Downloads and Materials== &lt;br /&gt;
&lt;br /&gt;
You can [http://sourceforge.net/project/showfiles.php?group_id=64424&amp;amp;package_id=62285 download] project releases from the OWASP download center. &lt;br /&gt;
&lt;br /&gt;
'''THE OWASP Testing Project Live CD'''&lt;br /&gt;
&lt;br /&gt;
The OWASP testing project is currently implementing an Application security Live CD. &lt;br /&gt;
&lt;br /&gt;
The aim of this CD is to have a complete testing suite on one Disk. The CD shall also contain the forthcoming OWASP Testing guide.&lt;br /&gt;
&lt;br /&gt;
We also hope to include the OWASP.Net suite of tools by using mono.&lt;br /&gt;
&lt;br /&gt;
The CD is being created in conjunction with Josh Perrrymon at [http://www.packetfocus.com/ Packetfocus].&lt;br /&gt;
&lt;br /&gt;
He can be contacted on:&lt;br /&gt;
[mailto:josh.perrymon@packetfocus.com Josh Perrymon]&lt;br /&gt;
&lt;br /&gt;
Also you can contact myself on [mailto:eoin.keary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
==Newest Release==&lt;br /&gt;
 &lt;br /&gt;
December 13, 2004 - Phase One Released &lt;br /&gt;
We are glad to announce that The OWASP Testing Project Phase One has finally been released. This covers the processes involved in testing web applications:&lt;br /&gt;
&lt;br /&gt;
* The scope of what to test &lt;br /&gt;
* Principles of testing &lt;br /&gt;
* Testing techniques explained &lt;br /&gt;
* The OWASP testing framework explained. &lt;br /&gt;
&lt;br /&gt;
This document is designed to help organizations understand what comprises a testing program, and to help them identify the steps that they need to undertake to build and operate that testing program on their web application&lt;br /&gt;
&lt;br /&gt;
You can now [https://sourceforge.net/project/showfiles.php?group_id=64424&amp;amp;package_id=62285 download] phase one from the OWASP download centre.&lt;br /&gt;
&lt;br /&gt;
==Roadmap==&lt;br /&gt;
&lt;br /&gt;
View the [[OWASP Testing Project Roadmap]]&lt;br /&gt;
&lt;br /&gt;
==News== &lt;br /&gt;
&lt;br /&gt;
  '''OWASP Pen Test Checklist in Italian''' &lt;br /&gt;
  Sun May 22 10:56:39 EDT 2005 &lt;br /&gt;
  I'm glad to announce we have released OWASP Pen Test Checklist in Italian. Thanks to the Italian Chapter, Massimiliano and Mateo for it's great effort to have this document translated. You can download this verion in[http://www.owasp.org/docroot/owasp/misc/OWASPWebAppPenTestList1.1_ITA.pdf PDF] or [http://www.owasp.org/docroot/owasp/misc/OWASPWebAppPenTestList1.1_ITA.doc Word]&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
  '''Checklist ver 1.17 in Spanish''' &lt;br /&gt;
  Mon Apr 04 15:37:24 EDT 2005 &lt;br /&gt;
  I'm glad to announce we have released OWASP Pen Test Checklist ver 1.17 in Spanish.Thanks to Pedro, Raul and Rogelio for it's great effort to have this document translated and to Christian by helping out with technical edition. You can download this verion [http://www.owasp.org/docroot/owasp/misc/testing_spanish.pdf PDF] or [http://www.owasp.org/docroot/owasp/misc/testing_spanish.doc Word]&lt;br /&gt;
&lt;br /&gt;
==Project Contributors== &lt;br /&gt;
&lt;br /&gt;
Contributors &lt;br /&gt;
&lt;br /&gt;
==Feedback and Participation==&lt;br /&gt;
 &lt;br /&gt;
We hope you find the information in the OWASP Testing project useful. Please contribute back to the project by sending your comments, questions, and suggestions to the OWASP Testing mailing list. Thanks!&lt;br /&gt;
&lt;br /&gt;
To join the OWASP Testing mailing list or view the archives, please visit the [http://lists.sourceforge.net/lists/listinfo/owasp-testing subscription page]. &lt;br /&gt;
&lt;br /&gt;
{{Template:Stub}}&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Category:OWASP_Testing_Project&amp;diff=4849</id>
		<title>Category:OWASP Testing Project</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Category:OWASP_Testing_Project&amp;diff=4849"/>
				<updated>2006-06-01T12:44:22Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Testing Guide */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
This projects goal is to create a &amp;quot;best practices&amp;quot; penetration testing framework which users can implement in their own organizations and a &amp;quot;low level&amp;quot; penetration testing guide that describes how to find certain issues. &lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
Currently there are many people helping out when they can. &lt;br /&gt;
The Project lead is Eoin Keary. There are still lots of areas to be covered.&lt;br /&gt;
To contribute please email [mailto:eoin.keary@owasp.org Eoin]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Volunteers needed==&lt;br /&gt;
&lt;br /&gt;
'''Phase Two call for volunteers''' &lt;br /&gt;
Wed Mar 15 12:30:28 EST 2006 &lt;br /&gt;
&lt;br /&gt;
Work is underway on the 2nd phase of the testing guide, and we would love to hear from volunteers who could offer their knowledge in creating this phase. If you have knowledge and experience in application testing, and can spare a few hours a week, please do get in [mailto:eoin.keary@owasp.org touch]&lt;br /&gt;
&lt;br /&gt;
==Testing Guide DOWNLOAD==&lt;br /&gt;
[[OWASP Testing Guide]]&lt;br /&gt;
&lt;br /&gt;
==Testing Checklist==&lt;br /&gt;
[[OWASP Testing Checklist]]&lt;br /&gt;
&lt;br /&gt;
==Downloads and Materials== &lt;br /&gt;
&lt;br /&gt;
You can [http://sourceforge.net/project/showfiles.php?group_id=64424&amp;amp;package_id=62285 download] project releases from the OWASP download center. &lt;br /&gt;
&lt;br /&gt;
'''THE OWASP Testing Project Live CD'''&lt;br /&gt;
&lt;br /&gt;
The OWASP testing project is currently implementing an Application security Live CD. &lt;br /&gt;
&lt;br /&gt;
The aim of this CD is to have a complete testing suite on one Disk. The CD shall also contain the forthcoming OWASP Testing guide.&lt;br /&gt;
&lt;br /&gt;
We also hope to include the OWASP.Net suite of tools by using mono.&lt;br /&gt;
&lt;br /&gt;
The CD is being created in conjunction with Josh Perrrymon at [http://www.packetfocus.com/ Packetfocus].&lt;br /&gt;
&lt;br /&gt;
He can be contacted on:&lt;br /&gt;
[mailto:josh.perrymon@packetfocus.com Josh Perrymon]&lt;br /&gt;
&lt;br /&gt;
Also you can contact myself on [mailto:eoin.keary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
==Newest Release==&lt;br /&gt;
 &lt;br /&gt;
December 13, 2004 - Phase One Released &lt;br /&gt;
We are glad to announce that The OWASP Testing Project Phase One has finally been released. This covers the processes involved in testing web applications:&lt;br /&gt;
&lt;br /&gt;
* The scope of what to test &lt;br /&gt;
* Principles of testing &lt;br /&gt;
* Testing techniques explained &lt;br /&gt;
* The OWASP testing framework explained. &lt;br /&gt;
&lt;br /&gt;
This document is designed to help organizations understand what comprises a testing program, and to help them identify the steps that they need to undertake to build and operate that testing program on their web application&lt;br /&gt;
&lt;br /&gt;
You can now [https://sourceforge.net/project/showfiles.php?group_id=64424&amp;amp;package_id=62285 download] phase one from the OWASP download centre.&lt;br /&gt;
&lt;br /&gt;
==Roadmap==&lt;br /&gt;
&lt;br /&gt;
View the [[OWASP Testing Project Roadmap]]&lt;br /&gt;
&lt;br /&gt;
==News== &lt;br /&gt;
&lt;br /&gt;
  '''OWASP Pen Test Checklist in Italian''' &lt;br /&gt;
  Sun May 22 10:56:39 EDT 2005 &lt;br /&gt;
  I'm glad to announce we have released OWASP Pen Test Checklist in Italian. Thanks to the Italian Chapter, Massimiliano and Mateo for it's great effort to have this document translated. You can download this verion in[http://www.owasp.org/docroot/owasp/misc/OWASPWebAppPenTestList1.1_ITA.pdf PDF] or [http://www.owasp.org/docroot/owasp/misc/OWASPWebAppPenTestList1.1_ITA.doc Word]&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
  '''Checklist ver 1.17 in Spanish''' &lt;br /&gt;
  Mon Apr 04 15:37:24 EDT 2005 &lt;br /&gt;
  I'm glad to announce we have released OWASP Pen Test Checklist ver 1.17 in Spanish.Thanks to Pedro, Raul and Rogelio for it's great effort to have this document translated and to Christian by helping out with technical edition. You can download this verion [http://www.owasp.org/docroot/owasp/misc/testing_spanish.pdf PDF] or [http://www.owasp.org/docroot/owasp/misc/testing_spanish.doc Word]&lt;br /&gt;
&lt;br /&gt;
==Project Contributors== &lt;br /&gt;
&lt;br /&gt;
Contributors &lt;br /&gt;
&lt;br /&gt;
==Feedback and Participation==&lt;br /&gt;
 &lt;br /&gt;
We hope you find the information in the OWASP Testing project useful. Please contribute back to the project by sending your comments, questions, and suggestions to the OWASP Testing mailing list. Thanks!&lt;br /&gt;
&lt;br /&gt;
To join the OWASP Testing mailing list or view the archives, please visit the [http://lists.sourceforge.net/lists/listinfo/owasp-testing subscription page]. &lt;br /&gt;
&lt;br /&gt;
{{Template:Stub}}&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Category:OWASP_Testing_Project&amp;diff=4842</id>
		<title>Category:OWASP Testing Project</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Category:OWASP_Testing_Project&amp;diff=4842"/>
				<updated>2006-06-01T12:17:40Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Volunteers needed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
This projects goal is to create a &amp;quot;best practices&amp;quot; penetration testing framework which users can implement in their own organizations and a &amp;quot;low level&amp;quot; penetration testing guide that describes how to find certain issues. &lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
Currently there are many people helping out when they can. &lt;br /&gt;
The Project lead is Eoin Keary. There are still lots of areas to be covered.&lt;br /&gt;
To contribute please email [mailto:eoin.keary@owasp.org Eoin]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Volunteers needed==&lt;br /&gt;
&lt;br /&gt;
'''Phase Two call for volunteers''' &lt;br /&gt;
Wed Mar 15 12:30:28 EST 2006 &lt;br /&gt;
&lt;br /&gt;
Work is underway on the 2nd phase of the testing guide, and we would love to hear from volunteers who could offer their knowledge in creating this phase. If you have knowledge and experience in application testing, and can spare a few hours a week, please do get in [mailto:eoin.keary@owasp.org touch]&lt;br /&gt;
&lt;br /&gt;
==Testing Guide==&lt;br /&gt;
[[OWASP Testing Guide]]&lt;br /&gt;
&lt;br /&gt;
==Testing Checklist==&lt;br /&gt;
[[OWASP Testing Checklist]]&lt;br /&gt;
&lt;br /&gt;
==Downloads and Materials== &lt;br /&gt;
&lt;br /&gt;
You can [http://sourceforge.net/project/showfiles.php?group_id=64424&amp;amp;package_id=62285 download] project releases from the OWASP download center. &lt;br /&gt;
&lt;br /&gt;
'''THE OWASP Testing Project Live CD'''&lt;br /&gt;
&lt;br /&gt;
The OWASP testing project is currently implementing an Application security Live CD. &lt;br /&gt;
&lt;br /&gt;
The aim of this CD is to have a complete testing suite on one Disk. The CD shall also contain the forthcoming OWASP Testing guide.&lt;br /&gt;
&lt;br /&gt;
We also hope to include the OWASP.Net suite of tools by using mono.&lt;br /&gt;
&lt;br /&gt;
The CD is being created in conjunction with Josh Perrrymon at [http://www.packetfocus.com/ Packetfocus].&lt;br /&gt;
&lt;br /&gt;
He can be contacted on:&lt;br /&gt;
[mailto:josh.perrymon@packetfocus.com Josh Perrymon]&lt;br /&gt;
&lt;br /&gt;
Also you can contact myself on [mailto:eoin.keary@owasp.org Eoin Keary]&lt;br /&gt;
&lt;br /&gt;
==Newest Release==&lt;br /&gt;
 &lt;br /&gt;
December 13, 2004 - Phase One Released &lt;br /&gt;
We are glad to announce that The OWASP Testing Project Phase One has finally been released. This covers the processes involved in testing web applications:&lt;br /&gt;
&lt;br /&gt;
* The scope of what to test &lt;br /&gt;
* Principles of testing &lt;br /&gt;
* Testing techniques explained &lt;br /&gt;
* The OWASP testing framework explained. &lt;br /&gt;
&lt;br /&gt;
This document is designed to help organizations understand what comprises a testing program, and to help them identify the steps that they need to undertake to build and operate that testing program on their web application&lt;br /&gt;
&lt;br /&gt;
You can now [https://sourceforge.net/project/showfiles.php?group_id=64424&amp;amp;package_id=62285 download] phase one from the OWASP download centre.&lt;br /&gt;
&lt;br /&gt;
==Roadmap==&lt;br /&gt;
&lt;br /&gt;
View the [[OWASP Testing Project Roadmap]]&lt;br /&gt;
&lt;br /&gt;
==News== &lt;br /&gt;
&lt;br /&gt;
  '''OWASP Pen Test Checklist in Italian''' &lt;br /&gt;
  Sun May 22 10:56:39 EDT 2005 &lt;br /&gt;
  I'm glad to announce we have released OWASP Pen Test Checklist in Italian. Thanks to the Italian Chapter, Massimiliano and Mateo for it's great effort to have this document translated. You can download this verion in[http://www.owasp.org/docroot/owasp/misc/OWASPWebAppPenTestList1.1_ITA.pdf PDF] or [http://www.owasp.org/docroot/owasp/misc/OWASPWebAppPenTestList1.1_ITA.doc Word]&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
  '''Checklist ver 1.17 in Spanish''' &lt;br /&gt;
  Mon Apr 04 15:37:24 EDT 2005 &lt;br /&gt;
  I'm glad to announce we have released OWASP Pen Test Checklist ver 1.17 in Spanish.Thanks to Pedro, Raul and Rogelio for it's great effort to have this document translated and to Christian by helping out with technical edition. You can download this verion [http://www.owasp.org/docroot/owasp/misc/testing_spanish.pdf PDF] or [http://www.owasp.org/docroot/owasp/misc/testing_spanish.doc Word]&lt;br /&gt;
&lt;br /&gt;
==Project Contributors== &lt;br /&gt;
&lt;br /&gt;
Contributors &lt;br /&gt;
&lt;br /&gt;
==Feedback and Participation==&lt;br /&gt;
 &lt;br /&gt;
We hope you find the information in the OWASP Testing project useful. Please contribute back to the project by sending your comments, questions, and suggestions to the OWASP Testing mailing list. Thanks!&lt;br /&gt;
&lt;br /&gt;
To join the OWASP Testing mailing list or view the archives, please visit the [http://lists.sourceforge.net/lists/listinfo/owasp-testing subscription page]. &lt;br /&gt;
&lt;br /&gt;
{{Template:Stub}}&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Category:OWASP_Code_Review_Project&amp;diff=4841</id>
		<title>Category:OWASP Code Review Project</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Category:OWASP_Code_Review_Project&amp;diff=4841"/>
				<updated>2006-06-01T12:10:16Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Volunteers Needed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Project Contributors ==&lt;br /&gt;
&lt;br /&gt;
The OWASP Code Review project was concieved by Eoin Keary the OWASP Ireland Founder and Chapter Lead.&lt;br /&gt;
We are actively seeking techies to add new sections as new web technologies emerge. &lt;br /&gt;
If you are interested in volunteering for the project, or have a comment, question, or suggestion, please drop me a line mailto:eoin.keary@owasp.org&lt;br /&gt;
&lt;br /&gt;
==Volunteers Needed==&lt;br /&gt;
&lt;br /&gt;
Yes please, drop me a line.&lt;br /&gt;
Need help on this one, dont be shy, all help appreciated&lt;br /&gt;
&lt;br /&gt;
==Downloads==&lt;br /&gt;
&lt;br /&gt;
==Contents==&lt;br /&gt;
[[OWASP Code Review Guide Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
==Roadmap==&lt;br /&gt;
&lt;br /&gt;
View the [[OWASP Code Review Project Roadmap]]&lt;br /&gt;
&lt;br /&gt;
{{Template:Stub}}&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Project]]&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=4840</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=4840"/>
				<updated>2006-06-01T12:07:36Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Buffer Overruns and Overflows */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=4839</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=4839"/>
				<updated>2006-06-01T12:05:17Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* Integer overflows: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
'''Integer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=4838</id>
		<title>OWASP Code Review Guide Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Code_Review_Guide_Table_of_Contents&amp;diff=4838"/>
				<updated>2006-06-01T12:04:47Z</updated>
		
		<summary type="html">&lt;p&gt;Eoin: /* The Format String: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==[[Code Review Introduction|Introduction]] ==&lt;br /&gt;
'''Preface''':&lt;br /&gt;
This document is not a “How to perform a Secure Code review” walkthrough but more a guide on how to perform a successful review. Knowing the mechanics of code inspection is a half the battle but I’m afraid people is the other half.&lt;br /&gt;
To Perform a proper code review, to give value to the client from a risk perspective and not from an academic or text book perspective we must understand what we are reviewing. &lt;br /&gt;
&lt;br /&gt;
Applications may have faults but the client wants to know the “real risk” and not necessarily what the security textbooks say. &lt;br /&gt;
&lt;br /&gt;
Albeit there are real vulnerabilities in real applications out there and they pose real risk but how do we define real risk as opposed to best practice?&lt;br /&gt;
&lt;br /&gt;
This document describes how to get the most out of a secure code review. What is important when managing an engagement with a client and how to keep your eye on the ball the see the “wood from the trees”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Introduction''':&lt;br /&gt;
The only possible way of developing secure software and keeping it secure going into the future is to make security part of the design. When cars are designed safety is considered and now a big selling point for people buying a new car, “How safe is it?” would be a question a potential buyer may ask, also look at the advertising referring to the “Star” rating for safety a brand/model of car has.&lt;br /&gt;
Unfortunately the software industry is not as evolved and hence people still buy software without paying any regard to the security aspect of the application.&lt;br /&gt;
&lt;br /&gt;
This is what OWASP are trying to do, to bring security in web application development into the mainstream, to make is a selling point. 30% to 35% of Microsoft’s budget for “Longhorn”&lt;br /&gt;
is earmarked for security, a sign of the times. http://news.bbc.co.uk/2/hi/business/4516269.stm&lt;br /&gt;
&lt;br /&gt;
Every day more and more vulnerabilities are discovered in popular applications, which we all know and use and even use for private transactions over the web.&lt;br /&gt;
&lt;br /&gt;
I’m writing this document not from a purest point of view. Not everything you may agree with but from experience it is rare that we can have the luxury of being a purest in the real world.&lt;br /&gt;
Many forces in the business world do not see value in spending a proportion of the budget in security and factoring some security into the project timeline.&lt;br /&gt;
&lt;br /&gt;
The usual one liners we hear in the wilderness:&lt;br /&gt;
&lt;br /&gt;
''“We never get hacked (that I know of), we don’t need security”&lt;br /&gt;
&lt;br /&gt;
“We never get hacked, we got a firewall”.&lt;br /&gt;
&lt;br /&gt;
Question: “How much does security cost”? &lt;br /&gt;
Answer: “How much shall no security cost”?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Not to know is bad; not to wish to know is worse.&amp;quot;'' &lt;br /&gt;
	- I love proverbs as you can see.&lt;br /&gt;
&lt;br /&gt;
Code inspection is a fairly low-level approach to securing code but is very effective.&lt;br /&gt;
It is in effect a look under the hood of an application (whitebox).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Code Review Project]]&lt;br /&gt;
&lt;br /&gt;
==[[Buffer Overruns and Overflows|Buffer Overruns and Overflows]] ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''The Buffer'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Buffer is an amount of contiguous memory set aside for storing information.   Example: A program has to remember certain things, like what your shopping cart contains or what data was inputted prior to the current operation this information is stored in memory in a buffer.&lt;br /&gt;
&lt;br /&gt;
'''How to locate the potentially vulnerable code:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In locating potentially vulnerable code from a buffer overflow standpoint one should look for particular signatures such as:&lt;br /&gt;
&lt;br /&gt;
'''Arrays:'''&lt;br /&gt;
&lt;br /&gt;
int x[20];&lt;br /&gt;
&lt;br /&gt;
int y[20][5];&lt;br /&gt;
&lt;br /&gt;
int x[20][5][3];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Format Strings:'''&lt;br /&gt;
&lt;br /&gt;
printf() ,fprintf(), sprintf(), snprintf().&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x,  %s, %n, %d, %u, %c, %f&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''&lt;br /&gt;
Over flows:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
strcpy (), strcat (), sprintf (), vsprintf ()&lt;br /&gt;
&lt;br /&gt;
'''Vulnerable Patterns for buffer overflows:'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''‘Vanilla’ buffer overflow:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example: A program might want to keep track of the days of the week (7). The programmer tells the computer to store a space for 7 numbers.  This is an example of a buffer. But what happens if an attempt to add 8 numbers is performed?&lt;br /&gt;
&lt;br /&gt;
Languages such as C and C++ do not perform bounds checking and therefore if the program is written in such a language the 8th piece of data would overwrite the program space of the next program in memory would result in data corruption.&lt;br /&gt;
&lt;br /&gt;
This can cause the program to crash at a minimum or a carefully crafted overflow can cause malicious code to be executed, as the overflow payload is actual code.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strcpy(smallBuffer, userId);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Buffer overflows are the result of stuffing more code into a buffer than it is meant to hold.&lt;br /&gt;
&lt;br /&gt;
'''The Format String:'''&lt;br /&gt;
&lt;br /&gt;
A format function is a function within the ANSI C specification. It can be used to tailor primitive C data types to human readable form. They are used in nearly all C programs, to output information, print error messages or process strings.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some format parameters:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
%x        hexadecimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
%s        string ((const) (unsigned) char *)&lt;br /&gt;
&lt;br /&gt;
%n        number of bytes written so far, (* int)&lt;br /&gt;
&lt;br /&gt;
%d        decimal (int)&lt;br /&gt;
&lt;br /&gt;
%u        unsigned decimal (unsigned int)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;Hello: %s\n&amp;quot;, a273150);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The %s in this case ensures that the parameter (a273150) is printer as a string.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Through supplying the format string to the format function we are able&lt;br /&gt;
&lt;br /&gt;
to control the behaviour of it. So supplying input as a format string makes our application do things its not ment to! What exactly are we able to make the application do?&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Crashing an application:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (“%s”, User_Input);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
if we supply %x (hex unsigned int) as the input the printf function shall expext to find an integer relating to that format string but no argument exists. This can not be detected at compile time. At runtime this issue shall surface.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Walking the stack:&lt;br /&gt;
&lt;br /&gt;
For every % in the argument the printf function finds it assumes that there is an associated value on the stack. In this way the function walks the stack downwards reading the corresponding values from the stack and printing them to user&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Using format strings we can execute some invalid pointer access by using a format string such as:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
printf (&amp;quot;%s%s%s%s%s%s%s%s%s%s%s%s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Worse again is using the %n directive in printf(). This directive takes an int* and writes the number of bytes so far to that location.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Where to look for this potential vulnerability. This issue is prevalent with the printf() family of functions,  printf(),fprintf(), sprintf(), snprintf(). Also syslog() (writes system log information) and setproctitle(const char *fmt, ...); (which sets the string used to display process identifier information).&lt;br /&gt;
&lt;br /&gt;
== Integer overflows: ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int main(void){&lt;br /&gt;
&lt;br /&gt;
            int val;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            val = 0x7fffffff;          /* 2147483647*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val = %d (0x%x)\n&amp;quot;, val, val);&lt;br /&gt;
&lt;br /&gt;
            printf(&amp;quot;val + 1 = %d (0x%x)\n&amp;quot;, val + 1 , val + 1); /*Overflow the int*/&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
            return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The binary representation of 0x7fffffff is 1111111111111111111111111111111 this integer is initialised with the highest positive value a signed long integer can hold.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here when we add 1 to the hex value of 0x7fffffff the value of the integer overflows and goes to a negative number (0x7fffffff + 1 = 80000000)&lt;br /&gt;
&lt;br /&gt;
In decimal this is (-2147483648). Think of the problems this may cause!!&lt;br /&gt;
&lt;br /&gt;
Compilers will not detect this and the application will not notice this issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
We get these issues when we use signed integers in comparisons or in arithmetic and also comparing signed integers with unsigned integers&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
int myArray[100];&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
    int fillArray(int v1, int v2){&lt;br /&gt;
&lt;br /&gt;
        if(v2 &amp;gt; sizeof(myArray) / sizeof(int)){&lt;br /&gt;
&lt;br /&gt;
            return -1; /* Too Big !! */&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        myArray [v2] = v1;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
        return 0;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here if v2 is a massive negative number so the if condition shall pass. This condition checks to see if v2 is bigger than the array size.&lt;br /&gt;
&lt;br /&gt;
The line myArray[v2]  = v1 assigns the value v1 to a location out of the bounds of the array causing unexpected results.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Good Patterns &amp;amp; procedures to prevent buffer overflows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
void copyData(char *userId) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   char  smallBuffer[10]; // size of 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   strncpy(smallBuffer, userId, 10); // only copy first 10 elements&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char *userId = &amp;quot;01234567890&amp;quot;; // Payload of 11&lt;br /&gt;
&lt;br /&gt;
copyData (userId); // this shall cause a buffer overload&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The code above is not vulnerable to buffer overflow as the copy functionality uses a specified length, 10.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking. gets () is another function that reads input (into a buffer) from stdin until a terminating newline or EOF (End of File) is found. The scanf () family of functions also may result in buffer overflows.&lt;br /&gt;
&lt;br /&gt;
Using strncpy(), strncat(), snprintf(), and fgets() all mitigate this problem by specifying the expected input.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Always check the bounds of an array before writing it to a buffer.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
.NET &amp;amp; Java&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
C# or C++ code in the .NET framework can be immune to buffer overflows if the code is managed. Managed code is code executed by a .NET virtual machine, such as Microsoft's. Before the code is run, the Intermediate Language is compiled into native code. The managed execution environments own runtime-aware complier performs the compilation; therefore the managed execution environment can guarantee what the code is going to do. The Java development language also does not suffer from buffer overflows; as long as native methods or system calls are not invoked buffer overflows are not an issue.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
TO DO – Unsafe methods which cause arithmetic overflows.&lt;br /&gt;
&lt;br /&gt;
==[[Data Validation (Code Review)|Data Validation]] ==&lt;br /&gt;
==[[Error Handling]]==&lt;br /&gt;
==[[OS Injection]] ==&lt;br /&gt;
==[[The Secure Code Environment]] ==&lt;br /&gt;
==[[Transaction Analysis]] ==&lt;br /&gt;
==[[XSS Attacks]] ==&lt;/div&gt;</summary>
		<author><name>Eoin</name></author>	</entry>

	</feed>