Page
1
(This page has no text content)
Page
2
(This page has no text content)
Page
3
Ethical Hacker's Penetration Testing Guide Vulnerability Assessment and Attack Simulation on Web, Mobile, Network Services and Wireless Networks Samir Kumar Rakshit www.bpbonline.com
Page
4
Copyright © 2022 BPB Online All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor BPB Online or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. BPB Online has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, BPB Online cannot guarantee the accuracy of this information. Group Product Manager: Marianne Conor Publishing Product Manager: Eva Brawn Senior Editor: Connell Content Development Editor: Melissa Monroe Technical Editor: Anne Stokes Copy Editor: Joe Austin Language Support Editor: Justin Baldwin Project Coordinator: Tyler Horan Proofreader: Khloe Styles Indexer: V. Krishnamurthy Production Designer: Malcolm D'Souza Marketing Coordinator: Kristen Kramer First published: June 2022 Published by BPB Online WeWork, 119 Marylebone Road London NW1 5PU UK | UAE | INDIA | SINGAPORE ISBN 978-93-55512-154 www.bpbonline.com
Page
5
Foreword I still remember a spark in Samir's eyes after attending a secure coding training class I taught for his team nearly a decade back. Both of us were working at Symantec at the time and I was with the central Product Security Group. In days that followed, he came back with questions and insights geared at helping him bolster his new found love for software security. Over time, he started championing software security techniques, tools, and processes within his engineering team, hence truly embodying the spirit of a Software Security Champion - a security-minded engineering team member! Security Champions are critical if any software security initiative has to result in a collaborative security culture at scale, and its folks like Samir who make it happen! Recently, Samir filled me in on this Penetrating Testing (a.k.a. Pen Testing) book that he was in the process of publishing, felt super happy for him. But, when he followed up requesting me to write this Foreword, it was humbling. It was one of those, "The student has become a master!" moment, and needless to say, an honor to oblige. This book is for anyone (especially in an engineering role or a student) to demystify the world of manual penetration testing. Throughout, Samir has explained critical security vulnerabilities to hunt (or test) in a product using easy to follow replication steps. Product could be an application, mobile app, API etc. His style of writing is easy to understand. To ease understanding of the basics, he opens the book by dedicating the first chapter to an overview of Web and related technologies, filled with data flows and examples. Manual pen testing continues to be the most effective way to find vulnerabilities, in particular, business logic flaws. This book helps learners think like an attacker/threat actor, a skill that's necessary to build securer products from the get go. Many times, organizations tend to use commercial or open source pen testing tools to identify commonly known vulnerabilities. In such instances, going through the content of this book can help readers double-click on the 'Why' and 'How' better versus becoming a mere 'Tool Junkie'.
Page
6
Testing techniques explained throughout the book can also be used to create security-focused Unit Test Cases or be added to a team's Security Testing Automation Framework. Samir has dedicated an entire chapter to the security automation aspect in which he shares a number of practical implementations/use cases. Readers can readily reuse and/or build on top of this. He plans to zoom in on this aspect in future editions. Learning and practicing these techniques can also be useful for anyone interested in delving into the world of Bug Bounty contests held by organizations across the globe. Great work, Samir, wishing you the best of luck for the success of this book and all future endeavors! —Vishal Asthana, CISSP AWS Cloud Practitioner asthana@alumni.usc.edu
Page
7
Dedicated to My dear parents, Sunil Rakshit, Pratima Rakshit and my wife, Rajani, best friend, guide, and a great motivator!
Page
8
About the Author Samir Kumar Rakshit is working for Web Application Security for more than a decade including companies like Symantec, DigiCert Inc. He has been participating on various Hackfest and Cyberwar games as part of Symantec and got top spots from his business unit. He has given many trainings related to Web Application Security for the global employees at Symantec, DigiCert. He was a Security Lead and Security Champion at Symantec. He has a lot of interest in innovation efforts and has several patents. He was a Global Inventor Mentor at Symantec. He has presented various papers in internal conferences at Symantec/VeriSign. Samir lives in Bangalore and loves to spend free time with his lovely daughter, Nivedita and reading books, writing poetry, blogs, listening to music, and helping people implement Rainwater Harvesting.
Page
9
About the Reviewer Matthias Bertschy started his career in 2005 as a System Administrator. In 2011 he joined a leading security solution provider in Switzerland to become a Security System Engineer. He was certified as an Ethical Hacker in 2012 (GPEN certification) and validated his first four years as a professional with a CISSP in 2015. He discovered Kubernetes in 2016 and has become a regular contributor since 2017 and a recognized expert in this technology. His list of certifications include Red Hat Certified Architect and every Kubernetes certifications, including Certified Kubernetes Security Specialist. Today he is contracted as a Senior Kubernetes Architect helping Geneva's oldest private bank with its digital transformation.
Page
10
Acknowledgement Writing acknowledgement is an important part of a book as this is the window of opportunity for expressing writer's gratitude to the people behind the scenes without whose help the book could not be a reality. And for me first in the list is my wife, Rajani, who is also my best friend, guide, and motivator for 20+ years! She never had any doubt about my capabilities for writing this book and always had confidence that the outcome would be a nice one! Then comes my little sweet daughter, Nivedita (named after siter Nivedita), who still follows up with me if something else is remaining for the book as I am still busy writing and cannot spend enough time with her! I could not have completed this book without their support! It was from a training session by Vishal Asthana from Symantec Product Security Group that made me passionate about application security. I would be forever grateful to him for all the guidance and agreeing to write foreword for this book. I would like to thank my managers, Jitu Patil, Rajendra, Quentin, Geetha, Asad, Suhas, Umashankar, Matt, Murali, Ashwin, Padam, Hari etc. and HR partner Kaitlan for their guidance and support! Thanks to late jethu late Santosh Rakshit and kaku Anil Rakshit, daadu late BC Mallick, in-laws, Nageshwara Rao and Anusurya Devi, granny Bhaskaramma Bhim, other relatives and to all my friends, Rajat, Ruchi, Beladeb, Jai, Deb, Nilesh, Utpal, Asif, Mahesh, Sudeep, Brahmaji, Satish, Pankaj, Saurabh, Azhar, Marasami, Vasantha, Veeresh, Jagath etc. who were there to motivate and support me! Over a decade, I have learned so much from the Security Community through tons of free materials, guidance, support in understand various concepts. I would like to specifically call out some names here(Twitter Handle) and request my readers to follow them for great pentest resources and guidance: @owasp, @PortSwigger, @vivekramac, @stokfredrik, @brutelogic, @NahamSec, @TomNomNom, @hakluke, @Jhaddix, @snyff, @zseano, @s0md3v, @HusseiN98D, @ADITYASHENDE17, @KathanP19 @cyb3rops, @gregxsunday, @hacksplained, @hunter0x7, @psiinon, @martenmickos, @nnwakelam, @EdOverflow,
Page
11
@dhakal_ananda, @_JohnHammond, @defparam, @samwcyo, @joohoi, @terjanq, @jon_bottarini, @codingo_, @0xatul, @b0rk, @irsdl, @sunnynehrabro, @harshbothra_, @DanielMiessler, @troyhunt, @pentester.land, @ajxchapman, @InsiderPhD, @Alra3ees, @zaproxy, @Farah_Hawaa etc. This book wouldn't have been possible if I hadn't had the support and patience from the editors of BPB Publications. My gratitude goes to the team at BPB Publications, Nrip Jain for helping me with the formalities! Last but not the least, considering the difficult time we went through because of Covid-19 scenarios, whenever I felt hopeless, there was this quote from A. P. J. Abdul Kalam which gave me strength and motivation to complete this work: "Dream is not the thing you see in sleep but is that thing that doesn't let you sleep."
Page
12
Preface Application Security is an ocean, and I have the privilege to explore it a bit on my day-to-day work in protecting applications by trying to break those before someone else does. In the process I have learned a lot from everyone (various developers, architects, external pentesters, security community etc.). This gave me the opportunities to explore further and share my understanding through application security trainings, Hackfest and conferences! This book collates those learnings to share with a wider audience who are interested in exploring penetration testing concepts for finding security flaws or vulnerability. This book would give a detail introduction of Web and related technologies, emerging threat scenarios around Application Security, hands-on techniques to perform pentest. Each topic is covered by explaining core concepts before diving into security concepts and then moving towards various penetration testing methodologies. Over last decade most applications have moved towards web which gave enormous rise to the reported security issues related to web-based applications. The importance of web security is becoming more prominent as to build consumer confidence it is required that the web application, must be secure, the sensitive information shared by those are maintained securely. We have seen rise of complex security vulnerabilities along with simple flaws causing serious damages to the business. We have also seen more security best practices like OWASP, PCI, etc. been followed in SDLC process to build secure web application. There are variety of applications like Web, Thick Client, Mobile App, Network Services, Wireless Network etc. which require us to have in-depth understanding of the platform, technology, and various tools etc. while performing pentesting. And to learn we need to have lab setup with some vulnerable application to try our hand in exploring the vulnerabilities inside out.
Page
13
Application security requires us securing the sensitive data, third party dependencies and monitoring application for malicious activities on our application. It also requires us to keep automating repetitive pentest scenarios so that we can cover more scenarios, save time and money! Apart from pentesting for finding security vulnerabilities we also need to be aware about the best practices and guidelines for securing our application. This book tries to cover all such concepts and methodologies in various chapters so that we have a streamlined approach to learn penetration testing! Hope the readers would enjoy those! Over the 14 chapters in this book, reader will learn the following: Chapter 1 introduces us to the fundamental understanding of the web related technologies. It teaches the basic understanding of Session management, REST API, HTTP, HTTP2, HTTPS, and Web Application architecture with examples, tools etc. Chapter 2 discusses the various aspects of Secure Code Review process which gives a pen tester better understanding of the code flow and reduce false positive. Chapter 3 teaches various pentest mythologies for performing injection attacks like XSS, SQL, Command Injection etc. on REST API/UI and covers tools like SQLMap etc. Chapter 4 delves into details of pentest of REST API, Fuzzing and Dynamic Scanning (DAST) tools like OWASP ZAP, Burp Proxy, and walking through various aspects like, configurations, techniques, and tips. Chapter 5 teaches about Unvalidated Open Redirects/Forwards vulnerabilities. Chapter 6 delves into various techniques to pentest Authentication, Authorization bypass and finding security flaws in Business Logic. Chapter 7 teaches various methodologies to perform pentesting for Sensitive data, Components with Known Vulnerabilities, Security Logging, and performing Security Monitoring for malicious attacks on application. Chapter 8 introduces various methodologies for exploiting File Upload Functionality and XXE attack. Chapter 9 teaches basic pentest concepts for performing security testing of Thick Client application.
Page
14
Chapter 10 teaches basic pentest concepts for performing security testing of Network Services. Chapter 11 introduces basic concepts on performing pentest on Wireless Network. Chapter 12 delves into details about various aspects of Mobile App (Android) and performing basic pentest and, learn security best practices. Chapter 13 introduces the concept of security automation and teaches some basics on performing Security Automation using Python Chapter 14 guides to setup pentest lab with tools like Kali, Nessus, XAMPP, Virtual Box and vulnerable applications like DVWA, DVTA etc.
Page
15
Coloured Images Please follow the link to download the Coloured Images of the book: https://rebrand.ly/85cecc We have code bundles from our rich catalogue of books and videos available at https://github.com/bpbpublications. Check them out! Errata We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors, if any, that may have occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at : errata@bpbonline.com Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family. Did you know that BPB offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.bpbonline.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at: business@bpbonline.com for more details. At www.bpbonline.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive
Page
16
exclusive discounts and offers on BPB books and eBooks.
Page
17
Piracy If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at business@bpbonline.com with a link to the material. If you are interested in becoming an author If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit www.bpbonline.com. We have worked with thousands of developers and tech professionals, just like you, to help them share their insights with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Reviews Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions. We at BPB can understand what you think about our products, and our authors can see your feedback on their book. Thank you! For more information about BPB, please visit www.bpbonline.com.
Page
18
Table of Contents 1. Overview of Web and Related Technologies and Understanding the Application Introduction Structure Objectives Static vs dynamic web application, cookies Static web application: No cookies, no state/session Example of static web application Dynamic web application (web application with session) Web technologies: HTTP methods, response codes, and importance HTTP response codes Introduction to HTTP2 HTTPS basics Hashing, salting, encrypting Representational state transfer (REST) Google Dorking/Google hacking Simple Google Dorks Syntax for Recon Web application architecture and understanding the application (Recon) Visual site map Basic Linux/Windows commands Conclusion References 2. Web Penetration Testing – Through Code Review Introduction Structure Objectives OWASP survey on effective detection methods for web vulnerabilities OWASP top 10 vulnerabilities OWASP' top 10 web application security risks Attack surface Code review: Things to look for while reviewing
Page
19
URL encoding and Same Origin Policy (SOP) URL encoding and escaping: The key is "In which order things are done" URL, encoding, and escaping: Things to review Same Origin Policy (SOP) Code viewing for Cross Site Scripting (XSS) SQL injection: The deadliest beast IDOR/BOLA/Auth bypass is the new pandemic Code review: Unrestricted file upload Code review: Scary mistakes Code review: Cryptography, hashing, and salt: Nothing is secure forever Code review: Unvalidated URL Redirects Conclusion References 3. Web Penetration Testing – Injection Attacks Introduction Structure Objective Basic usages of Burp Proxy in pentesting Proxying REST API request using Postman and Burp Proxy Pentesting for XSS XSS in HTML context XSS in HTML attribute context XSS in URL context (works on PHP based application) XSS in JavaScript context XSS with headers and cookies: Application which processes header information XSS with certificate request or SSL certificate information DOM XSS Pentesting for SQL Injection Pentesting for Simple SQL Injection Pen testing for error-based SQL Injection Blind SQL injection Pen testing for time based Blind SQL Injection Important usages of SQLMap for detecting SQL Injection
Page
20
What to notice while the SQLMap scan just started? Running SQLMap against Rest API How to send POST request (Example: for REST API) using SQLMap? Running SQLMap when URL does not have any query string SQLMapper/CO2 extension for Burp Suite Pentesting for Command Injection Locating sensitive files in the server Blind command injection Conclusion References 4. Fuzzing, Dynamic Scanning of REST API, and Web Application Introduction Structure Objective Fuzzing Web Application and REST API Fuzz Faster U Fool (Ffuf): A fast web fuzzer written in Go Fuzzing REST API by adding various HTTP Headers Fuzzing authenticated pages/REST API end points with cookies Various usage options of Ffuf Using Burp Suite Turbo Intruder (Fuzzer that supports HTTP2) Basic tricks in analyzing the output of fuzzing to conclude our findings Dynamic scanning of REST API and web application with OWASP ZAP Pentest REST API using OWASP ZAP Various setting and tricks while using OWASP ZAP Add your host in scope for scanning Configure your application for ZAP Active scanning Various Active scan settings for Input Vectors in OWAZP ZAP Other advanced settings of ZAP ZAP Community scripts Why will automation without your brain not get any good result? Conclusion References 5. Web Penetration Testing – Unvalidated Redirects/Forwards, SSRF