Table of Contents for
Python Web Penetration Testing Cookbook

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Python Web Penetration Testing Cookbook by Dave Mound Published by Packt Publishing, 2015
  1. Cover
  2. Table of Contents
  3. Python Web Penetration Testing Cookbook
  4. Python Web Penetration Testing Cookbook
  5. Credits
  6. About the Authors
  7. About the Reviewers
  8. www.PacktPub.com
  9. Disclamer
  10. Preface
  11. What you need for this book
  12. Who this book is for
  13. Sections
  14. Conventions
  15. Reader feedback
  16. Customer support
  17. 1. Gathering Open Source Intelligence
  18. Gathering information using the Shodan API
  19. Scripting a Google+ API search
  20. Downloading profile pictures using the Google+ API
  21. Harvesting additional results from the Google+ API using pagination
  22. Getting screenshots of websites with QtWebKit
  23. Screenshots based on a port list
  24. Spidering websites
  25. 2. Enumeration
  26. Performing a ping sweep with Scapy
  27. Scanning with Scapy
  28. Checking username validity
  29. Brute forcing usernames
  30. Enumerating files
  31. Brute forcing passwords
  32. Generating e-mail addresses from names
  33. Finding e-mail addresses from web pages
  34. Finding comments in source code
  35. 3. Vulnerability Identification
  36. Automated URL-based Directory Traversal
  37. Automated URL-based Cross-site scripting
  38. Automated parameter-based Cross-site scripting
  39. Automated fuzzing
  40. jQuery checking
  41. Header-based Cross-site scripting
  42. Shellshock checking
  43. 4. SQL Injection
  44. Checking jitter
  45. Identifying URL-based SQLi
  46. Exploiting Boolean SQLi
  47. Exploiting Blind SQL Injection
  48. Encoding payloads
  49. 5. Web Header Manipulation
  50. Testing HTTP methods
  51. Fingerprinting servers through HTTP headers
  52. Testing for insecure headers
  53. Brute forcing login through the Authorization header
  54. Testing for clickjacking vulnerabilities
  55. Identifying alternative sites by spoofing user agents
  56. Testing for insecure cookie flags
  57. Session fixation through a cookie injection
  58. 6. Image Analysis and Manipulation
  59. Hiding a message using LSB steganography
  60. Extracting messages hidden in LSB
  61. Hiding text in images
  62. Extracting text from images
  63. Enabling command and control using steganography
  64. 7. Encryption and Encoding
  65. Generating an MD5 hash
  66. Generating an SHA 1/128/256 hash
  67. Implementing SHA and MD5 hashes together
  68. Implementing SHA in a real-world scenario
  69. Generating a Bcrypt hash
  70. Cracking an MD5 hash
  71. Encoding with Base64
  72. Encoding with ROT13
  73. Cracking a substitution cipher
  74. Cracking the Atbash cipher
  75. Attacking one-time pad reuse
  76. Predicting a linear congruential generator
  77. Identifying hashes
  78. 8. Payloads and Shells
  79. Extracting data through HTTP requests
  80. Creating an HTTP C2
  81. Creating an FTP C2
  82. Creating an Twitter C2
  83. Creating a simple Netcat shell
  84. 9. Reporting
  85. Converting Nmap XML to CSV
  86. Extracting links from a URL to Maltego
  87. Extracting e-mails to Maltego
  88. Parsing Sslscan into CSV
  89. Generating graphs using plot.ly
  90. Index

jQuery checking

One of the lesser checked but more serious OWASP Top 10 vulnerabilities is the use of libraries or modules with known vulnerabilities. This can often mean versions of web frameworks that are out of date, but it also includes JavaScript libraries that perform specific functions. In this circumstance, we are checking jQuery; I have checked other libraries with this script but for the purposes of an example, but I will stick to jQuery.

We will create a script that identifies whether a site uses jQuery, retrieve it's version number, and then compare that against the latest version number to determine whether it is up to date.

How to do it…

The following is our script:

import requests
import re
from bs4 import BeautifulSoup
import sys

scripts = []

if len(sys.argv) != 2:
  print "usage: %s url" % (sys.argv[0])
  sys.exit(0)

tarurl = sys.argv[1]
url = requests.get(tarurl)
soup = BeautifulSoup(url.text)
for line in soup.find_all('script'):
  newline = line.get('src')
  scripts.append(newline)

for script in scripts:
  if "jquery.min" in str(script).lower():
    url = requests.get(script)
    versions = re.findall(r'\d[0-9a-zA-Z._:-]+',url.text)
    if versions[0] == "2.1.1" or versions[0] == "1.12.1":
      print "Up to date"
    else:
      print "Out of date"
      print "Version detected: "+versions[0]

The following is an example of the output produced when using this script:

http://candycrate.com
Out of Date
Version detected: 1.4.2

How it works…

As ever, we import our libraries and create an empty library to house our future identified scripts:

scripts = []

For this script, we have created a simple usage guide that detects whether a URL has been provided. It reads the number of sys.argv, and if it is not equal to 2, including the script itself, then it prints out a guide:

if len(sys.argv) != 2:
  print "usage: %s url" % (sys.argv[0])
  sys.exit(0)

We take our target URL from the sys.argv list and open it:

tarurl = sys.argv[1]
url = requests.get(tarurl)

As with before, we use beautiful soup to take the page apart; however, this time we are identifying scripts and pulling their src values in order to obtain the URLs of the js libraries being that are used. This collects together all the potential libraries that could be jQuery. Bear in mind that if you extend the usage to include different types of library, this list of URLs can be very useful:

for line in soup.find_all('script'):
  newline = line.get('src')
  scripts.append(newline)

For each identified script, we then check to see if there is any mention of jquery.min, which would indicate the core jQuery file:

for script in scripts:
  if "jquery.min" in str(script).lower():

We then use regex to identify the version number. In jQuery files, this will be the first thing mentioned that fits the given regex. The regex looks for 0-9 or a-z followed by a period that is repeated infinite amount of times. This is the format that the majority of version numbers take and jQuery is no different:

versions = re.findall(r'\d[0-9a-zA-Z._:-]+',url.text)

The re.findall method finds all strings that match this regex; however, as mentioned, we only want the first one. We identify it with comments[0]. We check to see whether this is equal to the hardcoded values of the current jQuery version, at time of writing. These will need to be updated manually. If the value is equal to either of the current versions, the script will state that it is up to date, alternatively if it is not equal it will print the detected version along with an out of date message:

if versions[0] == "2.1.1" or versions[0] == "1.12.1":
      print "Up to date"
    else:
      print "Out of date"
      print "Version detected: "+versions[0]

There's more…

This recipe is obviously extendable and can be applied to any JavaScript library by simply adding to the detection strings and versions.

If the string was to be extended to include other libraries, such as insecure Django or flask libraries, the script would have to be altered to handle the alternate way that they are stated, as they are obviously not declared as JavaScript libraries.