23
Securing Java Web Applications An introduction Jonas Flesch me@jonasflesch.com

Securing java web applications

Embed Size (px)

Citation preview

Page 1: Securing java web applications

Securing Java Web Applications

An introduction Jonas Flesch

[email protected]

Page 2: Securing java web applications

Index• Spring Security

• Passwords

• Sql Injection

• JSTL

• Client sent content

• Stacktraces

• Test

• Legal issues

Page 3: Securing java web applications

STEP 1Use Spring Security!!

Page 4: Securing java web applications

Spring Security

• Authentication

.formLogin()

.loginPage("/login") .loginProcessingUrl("/authenticate") .failureUrl("/login?error=true") .usernameParameter("username") .passwordParameter("password") .permitAll();

Page 5: Securing java web applications

Spring Security

• Authorization

@Controller@Secured(Roles.ROLE_ADMINISTRATOR) @RequestMapping(UserController.BASE_URL) public class UserController extends BaseController {

Page 6: Securing java web applications

Spring Security

• Cross Site Request Forgery Token

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>

Page 7: Securing java web applications

Spring Security• Good practices headers

Page 8: Securing java web applications

Step 2Passwords

Page 9: Securing java web applications

Passwords• Store it using a strong salted hash

• Bcrypt

• Never send it by e-mail or store it in plain text

• Protect user creation/password recovery forms with captcha

• Recaptcha when possible

• JCaptcha second choice

Page 10: Securing java web applications

Step 3SQL Injection

Page 11: Securing java web applications

SQL Injection

• Always use SQL Parameters:

@SqlUpdate("UPDATE User ug " + " SET DsEmail = :dsEmail" + " WHERE idUser = :idUser")

Page 12: Securing java web applications

Step 4Use JSTL carefully

Page 13: Securing java web applications

JSTL• Wrong: <input type="hidden" name="uuid" value="${UUID}"/>

• Correct: <input type="hidden" name="uuid" value="<c:out value=“${UUID}”/>"/>

• Why? <input type="hidden" name="uuid" value=“”><script>alert(1)</script>”/>”/>

• c:out escapes the string with html entities like &lt;

Page 14: Securing java web applications

Step 5Never trust content from the

client

Page 15: Securing java web applications

Never trust content from the client

• Never use file names from uploads

• Use UUID as filename when saving to the hard drive

• Put a file size limit

• Endless uploads can crash the server

• Validations made on Javascript should be done again in the server

Page 16: Securing java web applications

Step 6Hide the stacktraces!!!

Page 17: Securing java web applications
Page 18: Securing java web applications

Hide the stack traces• Evil user can discover:

• Frameworks/versions

• Paths

• Pieces of code/details of implementation

• Solution:

• Spring MVC @ControllerAdvice @ExceptionHandler

• Web.xml error-page

Page 19: Securing java web applications

Step 7Test it!

Page 20: Securing java web applications

Test• OWASP ZAP

• Automated testing

• Every error found is important

• Use the proxy in every functionality

• Can be integrated to the Continuous Integration

• Evil user in the scenarios

• Automate it too!

Page 21: Securing java web applications

Step 8Legal issues

Page 22: Securing java web applications

Legal issues• Privacy police

• Terms of Use

• Age validation

• Copied images/logotypes

• Personal Data storage (document number, birth date, etc)

• Classified disclosure