• No results found

Java Web Security Antipatterns

N/A
N/A
Protected

Academic year: 2021

Share "Java Web Security Antipatterns"

Copied!
95
0
0

Loading.... (view fulltext now)

Full text

(1)

Java Web Security

Antipatterns

Dominik Schadow |

bridging

IT

(2)

Failed with nothing but

the best intentions

(3)

Architect

Implement

Maintain

(4)

A

rc

hit

(5)

Software that is secure by design

Know the web application

Know all external entities

Know all data flows

Identify all risks

(6)
(7)
(8)
(9)
(10)

Good engineering involves thinking

about how things can be made to work;

the security mindset involves thinking

about how things can be made to fail. It

involves thinking like an attacker […].

www.schneier.com/blog/archives/2008/03/the_security_mi_1.html

(11)

Implement

as plaintext

encrypted

trivially hashed

(12)
(13)

Passw0rd$

d281fdbe0555b913d1c29f99143a3ad7bc66cf83

2e2c68bc1e9187cc6919fcb8564f1483

AKNtqLC_DZM32Jk7pgF4FpRVapo6QFEdROpsflwHkw

2q6rfK2mev4fAQFlRXbH2DecJTYLvF3LMD

(14)

Passw0rd$

d281fdbe0555b913d1c29f99143a3ad7bc66cf83

2e2c68bc1e9187cc6919fcb8564f1483

AKNtqLC_DZM32Jk7pgF4FpRVapo6QFEdROpsflwHkw

2q6rfK2mev4fAQFlRXbH2DecJTYLvF3LMD

SHA1

(15)

Passw0rd$

d281fdbe0555b913d1c29f99143a3ad7bc66cf83

2e2c68bc1e9187cc6919fcb8564f1483

AKNtqLC_DZM32Jk7pgF4FpRVapo6QFEdROpsflwHkw

2q6rfK2mev4fAQFlRXbH2DecJTYLvF3LMD

SHA1

MD5

(16)

Passw0rd$

d281fdbe0555b913d1c29f99143a3ad7bc66cf83

2e2c68bc1e9187cc6919fcb8564f1483

AKNtqLC_DZM32Jk7pgF4FpRVapo6QFEdROpsflwHkw

2q6rfK2mev4fAQFlRXbH2DecJTYLvF3LMD

SHA1

MD5

AES

(17)
(18)

PBKDF2

Iterations against brute force attacks

Available in plain Java

(19)
(20)

bcrypt

Iterations against brute force attacks

Integrated in Spring Security

(21)

@Configuration

@EnableWebMvcSecurity

public class WebSecurityConfig extends

WebSecurityConfigurerAdapter {

@Bean

public PasswordEncoder passwordEncoder() {

return new BCryptPasswordEncoder(10);

}

}

(22)

@Configuration

@EnableWebMvcSecurity

public class WebSecurityConfig extends

WebSecurityConfigurerAdapter {

@Bean

public PasswordEncoder passwordEncoder() {

return new BCryptPasswordEncoder(10);

}

}

(23)

@Configuration

@EnableWebMvcSecurity

public class WebSecurityConfig extends

WebSecurityConfigurerAdapter {

@Bean

public PasswordEncoder passwordEncoder() {

return new BCryptPasswordEncoder(10);

}

}

(24)

@Configuration

@EnableWebMvcSecurity

public class WebSecurityConfig extends

WebSecurityConfigurerAdapter {

@Bean

public PasswordEncoder passwordEncoder() {

return new BCryptPasswordEncoder(10);

}

}

(25)

scrypt

Memory against brute force attacks

(26)
(27)

Set period of time to change passwords

User logs in successfully

Calculate new hash

Update hash & salt Calculate new salt

User tries to log in

Period of time expired

Set not changed passwords to null

(28)
(29)

<h:inputSecret id="password" maxlength="1024">

<f:validateLength minimum="10" maximum="1024"/> </h:inputSecret>

(30)

<h:inputSecret id="password" maxlength="1024">

<f:validateLength minimum="10" maximum="1024"/>

(31)

private byte[] hash(PBEKeySpec keySpec) { return secretKeyFactory.generateSecret

(keySpec).getEncoded(); }

(32)

private byte[] hash(PBEKeySpec keySpec) {

return secretKeyFactory.generateSecret

(keySpec).getEncoded();

(33)

Implement

Changing password

(34)
(35)

Prevent unintended password change

Cross-Site Request Forgery vulnerability

Session id knowledge

(36)
(37)
(38)

Implement

Disabling pasting passwords

(39)

Disabling pasting into password fields

Does not

stop any attack

Does not

provide any more security

Does not

stop any attack

Does not

provide any more security

(40)
(41)
(42)
(43)
(44)

@WebFilter(urlPatterns = {"/*"})

public class HSTS implements Filter { public void doFilter(…) {

HttpServletResponse response = (HttpServletResponse) res;

response.addHeader(

"Strict-Transport-Security",

"max-age=31556926");

chain.doFilter(req, response); } // … }
(45)

@WebFilter(urlPatterns = {"/*"})

public class HSTS implements Filter { public void doFilter(…) {

HttpServletResponse response = (HttpServletResponse) res;

response.addHeader(

"Strict-Transport-Security",

"max-age=31556926");

chain.doFilter(req, response); } // … }
(46)

@WebFilter(urlPatterns = {"/*"})

public class HSTS implements Filter { public void doFilter(…) {

HttpServletResponse response = (HttpServletResponse) res;

response.addHeader

(

"Strict-Transport-Security",

"max-age=31556926");

chain.doFilter(req, response); } // … }
(47)

@WebFilter(urlPatterns = {"/*"})

public class HSTS implements Filter { public void doFilter(…) {

HttpServletResponse response = (HttpServletResponse) res;

response.addHeader(

"Strict-Transport-Security"

,

"max-age=31556926");

chain.doFilter(req, response); } // … }
(48)

@WebFilter(urlPatterns = {"/*"})

public class HSTS implements Filter { public void doFilter(…) {

HttpServletResponse response = (HttpServletResponse) res;

response.addHeader(

"Strict-Transport-Security",

"max-age=31556926"

);

chain.doFilter(req, response); } // … }
(49)

HSTS stops any insecure communication

Requires HTTPS connection

No effect on HTTP connections

All resources via HTTPS

Includes scripts, images, …

Requires valid certificate

(50)

Implement

(51)
(52)
(53)

SECURITY_SUCCESS

Successful security check (e.g. successful login)

SECURITY_FAILURE

Failed security check (e.g. failed login)

SECURITY_AUDIT

Record security events for audit (e.g. account edited)

(54)

Use an always active log level or

separate log file

(55)

log.warn(

SecurityMarkers.SECURITY_AUDIT,

"User {} has edited his account“,

username);

(56)

log.warn

(

SecurityMarkers.SECURITY_AUDIT,

"User {} has edited his account“,

username);

(57)

log.warn(

SecurityMarkers.SECURITY_AUDIT

,

"User {} has edited his account“,

username);

(58)

Implement

Skipping session configuration

Keeping session id after log-in

(59)

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml> false </failOnMissingWebXml> </configuration> </plugin>

(60)

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml> false </failOnMissingWebXml> </configuration> </plugin>

(61)
(62)

<web-app ... version="3.1"> <session-config>

<!-- idle timeout after session expires --> <session-timeout>30</session-timeout>

<cookie-config>

<!-- prevent session id script access --> <http-only>true</http-only>

<!-- transfer cookie via https only --> <secure>true</secure>

</cookie-config>

<!-- session id in cookie, not URL --> <tracking-mode>COOKIE</tracking-mode>

</session-config> </web-app>

(63)

<web-app ... version="3.1"> <session-config>

<!-- idle timeout after session expires --> <session-timeout>30</session-timeout>

<cookie-config>

<!-- prevent session id script access --> <http-only>true</http-only>

<!-- transfer cookie via https only --> <secure>true</secure>

</cookie-config>

<!-- session id in cookie, not URL --> <tracking-mode>COOKIE</tracking-mode>

</session-config> </web-app>

(64)

<web-app ... version="3.1"> <session-config>

<!-- idle timeout after session expires --> <session-timeout>30</session-timeout>

<cookie-config>

<!-- prevent session id script access --> <http-only>true</http-only>

<!-- transfer cookie via https only --> <secure>true</secure>

</cookie-config>

<!-- session id in cookie, not URL --> <tracking-mode>COOKIE</tracking-mode>

</session-config> </web-app>

(65)

<web-app ... version="3.1"> <session-config>

<!-- idle timeout after session expires --> <session-timeout>30</session-timeout>

<cookie-config>

<!-- prevent session id script access --> <http-only>true</http-only>

<!-- transfer cookie via https only --> <secure>true</secure>

</cookie-config>

<!-- session id in cookie, not URL --> <tracking-mode>COOKIE</tracking-mode>

</session-config> </web-app>

(66)

<web-app ... version="3.1"> <session-config>

<!-- idle timeout after session expires --> <session-timeout>30</session-timeout>

<cookie-config>

<!-- prevent session id script access --> <http-only>true</http-only>

<!-- transfer cookie via https only --> <secure>true</secure>

</cookie-config>

<!-- session id in cookie, not URL --> <tracking-mode>COOKIE</tracking-mode>

</session-config> </web-app>

(67)

User usually receives a session id

when entering web application

(68)

4E01EF46D8446D1C

10CB5C08EDA69DD1

(69)

Session hijacking

(70)

Session fixation

(71)
(72)

<form th:action="@{/logout}" method="post"> <button type="submit">Log out</button>

(73)

<form th:action="@{/logout}" method="post"> <button type="submit">Log out</button>

(74)

<form action="/logout" method="post">


<input type="hidden"

name="${_csrf.parameterName}" value="${_csrf.token}"/>


<input type="submit" value="Logout"/>


(75)

<form action="/logout" method="post">


<input type="hidden"

name="${_csrf.parameterName}" value="${_csrf.token}"/>


<input type="submit" value="Logout"/>


(76)

Limit session duration

(77)

Force HTTPS

(78)
(79)

@WebServlet

public class Login extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) { // ...

request.changeSessionId(); // ...

} }

(80)

@WebServlet

public class Login extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) { // ...

request.changeSessionId(); // ...

} }

(81)

@WebServlet

public class Login extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) { // ...

request.changeSessionId();

// ... }

(82)
(83)

@WebServlet

public class Logout extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) { // ...

request.getSession().invalidate(); // ...

} }

(84)

@WebServlet

public class Logout extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) { // ...

request.getSession().invalidate();

// ... }

(85)
(86)

M

aint

(87)

Frameworks and

libraries decline

(88)
(89)
(90)

<reporting> <plugins><plugin> <groupId>org.owasp</groupId> <artifactId>dependency-check-maven</artifactId> <version>1.3.1</version> <reportSets> <reportSet> <reports> <report>aggregate</report> </reports> </reportSet> </reportSets> </plugin></plugins> </reporting>

(91)
(92)
(93)

Plan security with threat modeling

Think (like an attacker) during implementation

(94)
(95)

[email protected]

www.bridging-it.de

Demo Projects


github.com/dschadow/JavaSecurity

HTTP Strict Transport Security RFC

tools.ietf.org/html/rfc6797

Microsoft Threat Modeling Tool 


www.microsoft.com/en-us/sdl/adopt/ threatmodeling.aspx

Mozilla SeaSponge


air.mozilla.org/mozilla-winter-of-security-seasponge-a-tool-for-easy-threat-modeling

OWASP Dependency Check

www.owasp.org/index.php/

OWASP_Dependency_Check OWASP Security Logging


www.owasp.org/index.php/ OWASP_Security_Logging_Project Spring Security
 projects.spring.io/spring-security Pictures
 www.dreamstime.com Koenigstr. 42
 70173 Stuttgart
 Germany Blog blog.dominikschadow.de
 Twitter @dschadow

www.bridging-it.de blog.dominikschadow.de

References

Related documents

large compared to the estimates of financial means available to terrorist organizations. While substantial amounts of assets were frozen in the aftermath of the

Finally, a global partnership for development will only be achieved when affected communities – including people who use drugs and subsistence farmers involved in illicit

Važnost trgovine između država članica EU očituje se u tome da je trgovina robom (izvoz i uvoz zajedno) na unutarnjem tržištu bila veća od trgovine izvan Europske

Click on “Edit” link by Complete Manager Evaluation to view and/or edit the Manager’s evaluation of the employee.. Define and Establish

Наукова новизна та теоретичне значення дослідження полягає у тому, що вперше досліджено організаційно-методичні засади професійної підготовки фахівців

The cell e.s.d.'s are taken into account individually in the estimation of e.s.d.'s in distances, angles and torsion angles; correlations between e.s.d.'s in cell parameters are

The biological control of the watermelon seedling blight and fruit blotch disease was investigated by screening the potential use of antagonistic bacteria.. Between May and

Students will apply knowledge of literary techniques, including foreshadowing, metaphor, simile, personification, onomatopoeia, alliteration, and flashback, to understand