KEMBAR78
Secure Coding - Web Application Security Vulnerabilities and Best Practices | PDF
Secure Coding
Web Application SecurityVulnerabilities and Best Practices
What is Secure
Coding?
Is it this?
...or this?
...maybe even this?
Security Principles
โ€ข Minimise Attack Surface Area	

โ€ข Establish Secure Defaults	

โ€ข Principle of Least Privilege	

โ€ข Principle of Defence in Depth	

โ€ข Fail Securely	

โ€ข Separation of Duties	

โ€ข Avoid Security by Obscurity	

โ€ข Keep Security Simple	

โ€ข Fix Security Issues Correctly
Minimise Attack Surface
โ€ข Every feature or technology is a risk.	

โ€ข Secure development is all about reducing
the risk by minimising the attack surface.
Thanks Boromir.
Establish Secure
Defaults
โ€ข By default a system should be secure out-
of-the-box.	

โ€ข It should be up to the user to reduce their
security if allowed.
Trust Morpheus!
Principle of Least
Privilege
โ€ข Use the least possible privilege to perform
the required business task.
Donโ€™t be the luser!
Principle of Defence in
Depth
โ€ข Always consider that upper layers are
already compromised.
This is how we do it.
Fail Securely
โ€ข Code fails regularly.
Fail Securely
isAdmin = true;	
!
try {	
codeWhichMayFail();	
isAdmin = isUserInRole("Administrator");	
} catch (Exception ex) {	
log.write(ex.toString());	
}
Separation of Duties
โ€ข Some roles have different levels of trust
than normal users.
Hell yeah!?!
Avoid Security By
Obscurity
โ€ข Security By Obscurity is a weak security
control.	

โ€ข Security By Obscurity depends on
knowledge.
Donโ€™t be like Dawson!
Keep Security Simple
โ€ข Simplicity leads to better understanding the
system and its constraints.
Please!
Fix Security Issues
Correctly
โ€ข Understand the root cause of the problem.	

โ€ข Identify the the pattern of the problem.	

โ€ข Some issues are wide-spread across the
code base.	

โ€ข Develop a Fix	

โ€ข Develop Tests
Fix Security Issues Correctly
PHP Hash Collision DOS(CVE-2011-4885)
โ€ข Problem: PHP was found vulnerable to a
denial of service by submitting a large
amount of specially crafted variables	

โ€ข Solution: max_input_vars was introduced
to limit the number of variables that can be
used in a request
Fix Security Issues Correctly
PHP Remote Code Execution(CVE-2012-0830)
if (sapi_module.input_filter(PARSE_POST, var, &val, val_len, &new_val_len TSRMLS_CC)) {	
php_register_variable_safe(var, val, new_val_len, array_ptr TSRMLS_CC);	
}	
!
... code removed ...	
!
PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars_array TSRMLS_DC)	
{	
!
... code removed ...	
!
if (is_array) {	
!
... code removed ...	
!
if (zend_hash_num_elements(symtable1) <= PG(max_input_vars)) {	
if (zend_hash_num_elements(symtable1) == PG(max_input_vars)) {	
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));	
}	
MAKE_STD_ZVAL(gpc_element);	
array_init(gpc_element);	
zend_symtable_update(symtable1, escaped_index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);	
}	
!
... code removed ...	
!
symtable1 = Z_ARRVAL_PP(gpc_element_p);	
!
... code removed ...	
!
}
Fix Security Issues Correctly
PHP Remote Code Execution(CVE-2012-0830)
โ€ข Vulnerability occurs when max_input_vars is
exceeded and the variable is an array.	

โ€ข Code execution occurs when Z_ARRVAL_PP is
called to obtain reference of an updated
hashtable.	

โ€ข If number of variables is greater than
max_input_vars, gpc_element will point to the
previous variable value, which is not initialised
memory.
Security in Languages
Rails/Grails/MVC
โ€ข Model/View/Controller and scaffolding
paradigm is often abused.
Python
โ€ข Python has a funny way of dealing with
different data types.
Python
Number Rounding
round(4000/5000)	
# vs	
round(4000.0/5000)
JavaScript Type
Problems
โ€ข JavaScript has loose semantics on its types.
JavaScript
Types Differences
{} + {} = NaN	
{} + [] = 0	
[] + {} = "[object Object]"	
[] + [] = ""	
{} - 1 = -1	
[] - 1 = -1	
-1 + {} = "-1[object Object]"	
-1 + [] = "-1"
JavaScript
Obfuscation
$=~[];$={___:++$,$$$$:(![]+"")[$],__$:++$,$_$_:(![]+"")
[$],_$_:++$,$_$$:({}+"")[$],$$_$:($[$]+"")[$],_$$:++$,$$
$_:(!""+"")[$],$__:++$,$_$:++$,$$__:({}+"")[$],$$_:++$,$$
$:++$,$___:++$,$__$:++$};$.$_=($.$_=$+"")[$.$_$]+($._$=$.
$_[$.__$])+($.$$=($.$+"")[$.__$])+((!$)+"")[$._$$]+($.__=
$.$_[$.$$_])+($.$=(!""+"")[$.__$])+($._=(!""+"")[$._$_])+
$.$_[$.$_$]+$.__+$._$+$.$;$.$$=$.$+(!""+"")[$._$$]+$.__+
$._+$.$+$.$$;$.$=($.___)[$.$_][$.$_];$.$($.$($.$$+"""+$.
$_$_+(![]+"")[$._$_]+$.$$$_+""+$.__$+$.$$_+$._$_+$.__
+"("+$.__$+""+$.$__+$.___+")"+""")())();	
!
// equal to	
!
alert(1);
C
โ€ข In C the type system is completely
arbitrary. You can do whatever you like
with pointers.
Ruby
โ€ข The Ruby language supports the use of
system commands.	

โ€ข Kernel.system provides means of injecting
malicious input into the application to
bypass security measures.
Struts
โ€ข Struts allows you to do dynamic method invocation	

โ€ข http://host/struts2_security_vulnerability/
changepassword!changePassword.action?
newPassword=my_new_password&username=bruce
โ€ข <init-param>โ€จ
<param-
name>struts.enable.DynamicMethodInvocation</
param-name><param-value>false</param-
value></init-param>
Thanks!

Secure Coding - Web Application Security Vulnerabilities and Best Practices