Application Security – JavaScript Web Tokens

JWT’s and User Initiated Logouts

Manual Penetration Testing

A discussion:

JavaScript Web Tokens (JWTs)

The following article is a discussion that explores JavaScript Web Tokens, and how developers generate JWT signing keys and how they create, verify, and terminate sessions.

Author: Anthony Cole, Sr. Penetration Testing Engineer and AppSec SME

Background

JavaScript web tokens are well documented and are used for authentication. JWT’s consist of three parts; the header, payload and signature. The header is used to describe the JWT, specifically, it will indicate how the signature is calculated. The payload of the JWT will contain claims about the user presenting the JWT, claims are just another word for attributes. There are a few predesignated claims there should be included in your JWT, other claims can be free form, that means the payload can contain any data that the developer wants it to contain. The signature is used to prevent tampering of the JWT.  The most common signing algorithms are HS256, RS256, and ES256, however, there are other algorithms.  One of the most important claims that should always exist in the payload is the “exp” claim. The “exp” claim represents the epoch time that the JWT will expire, without this claim the JWT is valid forever.

The expiration claim on the JWT introduces an interesting issue with session management, when a user intentionally terminates a session, how do you invalidate the JWT?  A common design pattern that I see on application tests is for the client-side application to receive a JWT from the server and then store it in the browser’s session storage. Subsequent Ajax requests will include the JWT as a bearer token in the authorization header.  When a user intentionally terminates a session, otherwise known as logging out, the application will simply delete the JWT from the browser’s session storage. This has the effect of causing the browser to believe the session was terminated because the application can no longer authenticate to the server since the application can no longer find the JWT in the session store.  However, this is an illusion, deleting the JWT from the session store does nothing to invalidate the session, the deleted JWT will remain valid until the expiration date has been reached. If an attacker had captured the JWT, they would be able to use it until the exp claim lapses.  This type of session handling runs afoul of the OWASP best practices, OWASP states that a secure session termination requires at least the following components.

  • Availability of user interface controls that allow the user to manually log out.
  • Session termination after a given amount of time without activity (session timeout).
  • Proper invalidation of server-side session state.

This leaves the developer with the before mentioned question, how can a JWT be invalidated when the user logs out?  One solution that I have heard is to have short lived expirations and to use a refresh token to re-issue the JWT upon expiration.  While this solution limits the window in which an abandoned session could be used, it doesn’t actually solve the problem.  I have seen a few other recommendations that should work in theory, but they are not always practical or easy to implement. Below I describe the easiest method that I have found.  It makes changes to how developers generate JWT signing keys and how they create, verify, and terminate sessions.

application security

Session Creation

When the session is created, the session key should be generated and stored in the datastore.  For the purposes of this writing, it will be assumed the datastore is a database table with the following fields.

  • SessionID GUID
  • SessionKey TEXT
  • ExpirationDate Timestamp

Once the JWT is populated and ready to be signed, the KID claim should be populated with the SessionID GUID and the JWT should be signed with the SessionKey value as demonstrated with the below pseudo code.

$key = SecureRandom.Generate(32); // 32 random bytes

$id = GUID.New(); // get new 32-bit GUID

$expires = date_add($now, “1h”); // expire in 1 hour

$db.execute(“insert into SessionKeys(SessionID, SessionKey, ExpirationDate) values(?, ?, ?)”, $id, $key,$expires);

$header = {“alg”:”HS256”, “kid”:”$id”}

$payload = {“exp”:”$expires”}; // insert claims here

$jwt = JWT.sign($header, $payload, $key)

Session Verification

When it is time to verify the JWT, the application should read the KID claim and lookup that value in the database using the below pseudo code.

$qSession = $db.Query(“select * from SessionKeys where SessionID = ?”, $jwt[“kid”]);

It goes without saying that parameterized statements should always be used when querying a database.

$isvalid = JWT.verify($jwt, $qSession[“SessionKey”]);

Session Termination

When the user initiates a logout, the JWT verified using the process outlined above and then the session key should be deleted from the database as shown in the below pseudo code.

$qSession = $db.Query(“select * from SessionKeys where SessionID = ?”, $jwt[“kid”]);

$isValid = JWT.verify(jwt, qSession[“SessionKey”]);

If($isValid)

{

               $db.execute(“delete from SessionKeys where SessionKey = ? or ExpirationDate <= ?”, $qSession[“SessionID”], $now);

}

By deleting the session key, the JWT is no longer able to be validated and is now in a de facto state of being expired.  The “or ExpirationDate <= $now” portion of the above pseudo code does some automated garbage collection by deleting the current session and any sessions that have already expired.  At this point, it is safe for the client-side application to delete the JWT from session storage.

Penetration Testing Services

Conclusion

JWTs are a convenient way to implement authentication, however they are not without their complexities, managing JWTs can be likened to managing PKI.  With PKI, it can be difficult to invalidate certificates once they are issued, revocations require a whole other process (CRLs) to maintain a healthy PKI.  The same can be said for managing JWT’s, however, the revocation process can be simplified using the process outlined in this writing.

About the Author

Anthony Cole, Sr. Security Engineer

Anthony Cole is a Sr. Security Consultant with over 22 years of experience in information technology, IT security and software development. Anthony is fully GIAC certified in all facets of information security, enabling him to facilitate successful outcomes for customers. Anthony’s vast knowledge of both offensive and defensive security ensures that Redbot Security’s customers will receive the best service in the industry.

Anthony is Redbot Security’s AppSec SME and formerly a Sr. Level Application Penetration Testing Engineer for NetSpi and Presidio as well as Blutique LLC’s Chief Technical Officer and Sr. Application Developer.

Redbot Security

Redbot Security is a boutique penetration testing firm with a Sr. Level Team of industry experts. Since Redbot Security is a smaller more specialized penetration testing group, the company is able to focus on building client relationships and delivering a premier customer experience through continuously engaged Senior Engineers.

Learn More.

Penetration Testing Quote
Related Posts
Load More Posts
Who is Redbot Security’s lead engineer?2022-07-26T17:37:56+00:00

Redbot Security’s principal security engineer is Andrew Bindner who is also Redbot Security’s CSO.  Andrew  was formerly a manager at Rapid7 and Coalfire Sr. Penetration Tester with 20+ years of hands-on security experience leading teams or working individually on highly technical engagements for a wide variety of commercial and government industries in IT and security.

Who is Redbot Security?2022-07-27T18:47:42+00:00

Redbot Security is a U.S. based Boutique Penetration Testing company that specializes in Network and Application Testing.  The company employs a small group of highly talented and experienced Sr. Level Engineers.

What is Redbot Security’s Manual Controlled Penetration Testing?2022-08-22T15:06:13+00:00

MCPT® or Manual Controlled Penetration Testing [manual penetration testing] is a controlled assessment of networks and applications that is able to safely identify and validate real world vulnerabilities that are potentially exploitable.  Manual Penetration Testing removes false positives and provides proof of concept reporting along with a exploit storyboard for easier remediation.

What Framework does Redbot Security follow?2022-07-26T17:52:04+00:00

REDBOT SECURITY’S HYBRID APPROACH TO PENETRATION TESTING SOURCES INDUSTRY-LEADING FRAMEWORKS AND COMBINES SENIOR-LEVEL TALENT WITH OVER 20 YEARS OF EXPERIENCE TO TAILOR ALL CLIENT ENGAGEMENTS. SOME FRAMEWORKS AND TESTING GUIDES LEVERAGED BY REDBOT SECURITY INCLUDE:

  • NIST SPECIAL PUBLICATION 800-115
  • PCI PENETRATION TESTING GUIDE
  • OPEN WEB APPLICATION SECURITY PROJECT
  • OWASP WSTGV4
  • OWASP TOP 10 LISTS
  • OWASP SECURITY PROJECTS
  • PENETRATION TESTING EXECUTION STANDARD (PTES)
  • OPEN-SOURCE SECURITY TESTING METHODOLOGY MANUAL (OSSTMM)
  • INFORMATION SYSTEMS SECURITY ASSESSMENT FRAMEWORK (ISSAF)
  • MITRE ATT&CK FRAMEWORK
What are the stages in a penetration test?2023-01-22T17:27:15+00:00

The Six Stages of Penetration Testing

  • Discovery. The first phase of penetration testing is OSINT and Discovery.
  • Testing. Testing phase is performed by qualified engineers that utilize both automated and manual exploitation testing techniques and tools
  • Assessment. Determine Risk to organization
  • Knowledge Sharing.  Provide clear results with Remediation planning
  • Remediation.  Organization remediates findings that pose a risk.
  • Retesting. Retesting of remediated vulnerabilities and final report delivery

Learn more about penetration testing services

Redbot Security is a boutique penetration testing house with a team of highly skilled U.S. Based Senior Level Engineers that specialize in providing ‘Penetration Testing Services’ for a wide range of industries.  The Company delivers True Manual Penetration Testing.

To learn more about Penetration Testing Services you can visit our in-depth articles that discuss a wide range of penetration testing services, or visit our Frequently Asked Questions page to quickly find the penetration testing information you are seeking.

If you have specific questions related to a penetration testing project, please reach out to us!

What are Penetration Testing Services?2023-01-22T17:02:57+00:00

Definition: Penetration Testing Services will  simulate a hacking attack and is usually performed by qualified penetration testing companies.  The simulated attack will test the security of networks, applications and devices. Many qualified Penetration testing engineers utilize the same tools and techniques that a malicious actor will use in the real world.  Once the Penetration Test is complete the business is able to access and remediate vulnerabilities that were found within their systems.

Is Redbot Security hiring?2022-07-26T17:38:58+00:00

Yes, Redbot Security is always on the lookout for top talent and pays the industry’s top pay.  You can learn more about opportunities on Redbot Security’s career page.

How long has Redbot Security been in business?2022-07-26T17:44:23+00:00

The company started as a VAR, partnering with Palo Alto, Fortinet and HPE in 2016 and transitioned to Pen-testing Company early 2019.

How do we schedule our service with Redbot Security?2022-07-26T17:28:19+00:00

Service scheduling is easy.  The first step is to contact us via our contact form and let us know what type of project you have.  Once we determine scope we provide a quick cost estimate.  When the estimate is approved we issue a contract and begin scheduling of your project.  We are rapid in our response, delivery of estimate and scheduling.

Does Redbot Security Test Critical Infrastructure?2022-07-26T17:37:42+00:00

Yes.  Redbot Security provides Industrial testing of ICS/SCADA networks that operate water, electric, manufacturing, transportation and more.

Does Redbot Security share a sample report?2022-07-26T17:40:19+00:00

Yes, Redbot Security will share a sample report with potential clients that sign a Mutual NDA and have a valid project.

Does Redbot Security Provide Social Engineering?2023-01-22T17:52:21+00:00

Yes, Redbot Security provides both physical and electronic Social Engineering and will utilize real word tactics to simulate an attack on a company. Want to know more about social engineering?  View Social Hacking article here.

Learn more about penetration testing services

Redbot Security is a boutique penetration testing house with a team of highly skilled U.S. Based Senior Level Engineers that specialize in providing ‘Penetration Testing Services’ for a wide range of industries.  The Company delivers True Manual Penetration Testing.

To learn more about Penetration Testing Services you can visit our in-depth articles that discuss a wide range of penetration testing services, or visit our Frequently Asked Questions page to quickly find the penetration testing information you are seeking.

If you have specific questions related to a penetration testing project, please reach out to us!

Does Redbot Security Provide Retesting?2022-07-26T17:28:10+00:00

Yes,  After your initial penetration test is performed, we deliver your 1st report that has proof of exploits and remediation steps to take to fix issues.  Once your company remediates findings, Redbot Security will perform a retest to validate that your issues have been resolved.  We then deliver a final report and client letter of attestation (if needed).  All of our retesting is built-in to our pricing model.

Does Redbot Security provide MDR?2023-01-23T16:54:31+00:00

No, Redbot Security does not provide Managed Threat Detection and Response, however the company provides Dark Web Monitoring and focuses on Penetration Testing only.

Does Redbot Security have verifiable certifications?2022-07-26T17:50:19+00:00

Yes, the combined team list only certifications that are verifiable.  The current team certifications are as follows:

Amazon Web Services Cloud Practitioner, CompTIA A+ CISSP, Certified Cloudera Administrator for Hadoop (CCAH), Certified Ethical Hacker (CEH), Cisco Certified Network Associate (CCNA), GIAC, CompTIA Linux+, Marine Corp Red Team Operator, Metasploit Professional, Certified Specialist, Nexpose, Certified Administrator (NCA,) Microsoft Certified Professional (MCP), CompTIA Network+, CompTIA IT, Operations Specialist (CIOS), CompTIA Secure Infrastructure Specialist (CSIS), Offensive Security Certified Professional (OSCP), GIAC Certified Penetration Tester (GPEN), Metasploit Professional, Certified Specialist Rapid7, Advanced Vulnerability Manager Rapid7, Network Assault Certified Rapid7, Application Assault Certified, GIAC Exploit Researcher, Advanced Penetration Tester (GXPN), GIAC Mobile Device Security Analyst (GMOB), GIAC Advanced Smartphone Forensics (GASF), GIAC Reverse Engineering Malware (GREM), GIAC Network Forensics Analyst (GNFA), GIAC Certified Intrusion Analyst (GCFA), GIAC Certified Forensic Examiner (GCFE), GIAC Security Essentials (GSEC), Portswigger Burpsuite Certified Practitioner, Cisco Certified Network Associate (CCNA), Cisco Certified Network Associate-wireless, Certified Ethical Hacker (CEH), CompTIA Network+US Navy, Joint Cyber Analyst Course (JCAC)

Does Redbot Security have to be onsite to test?2022-07-26T17:37:50+00:00

No. Redbot Security can test from a remote perspective, however many times with critical system testing Redbot Security will recommend onsite testing.

Does Redbot Security have a corporate office?2022-07-26T17:38:01+00:00

Yes. Redbot Security is located in the heart of Downtown Denver at the Dominion Towers.  Redbot Security’s Corporate office address is 600 17th Street, Denver, Colorado, USA.

Does Redbot Security employ U.S. Based Engineers?2023-01-24T16:02:13+00:00

Yes, due to security concerns, Redbot Security’s Engineering Team is 100% U.S. based, background checked and certified Full-time Sr. Level employees. Redbot Security does not use independent contractors, freelancers or sub contractors.

2023-01-16T18:54:09+00:00

Leave A Comment