|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Created using Metafidv2 by Matthew Bryant (mandatory) |
| 4 | +# Unauthorized use is stricly prohibited, please contact [email protected] with questions/comments. |
| 5 | +from __future__ import print_function |
| 6 | +import requests |
| 7 | +import getpass |
| 8 | +import json |
| 9 | +import time |
| 10 | +import csv |
| 11 | +import sys |
| 12 | +import os |
| 13 | +from bs4 import BeautifulSoup |
| 14 | + |
| 15 | +class cloudflare_enum: |
| 16 | + def __init__( self ): |
| 17 | + # Master list of headers to be used in each connection |
| 18 | + self.global_headers = { |
| 19 | + } |
| 20 | + self.verbose = True |
| 21 | + |
| 22 | + self.s = requests.Session() |
| 23 | + self.s.headers.update( self.global_headers ) |
| 24 | + self.atok = '' |
| 25 | + |
| 26 | + def log_in( self, username, password ): |
| 27 | + parse_dict = {} |
| 28 | + |
| 29 | + r = self.s.get('https://www.cloudflare.com/', ) |
| 30 | + |
| 31 | + new_headers = { |
| 32 | + 'Referer': 'https://www.cloudflare.com/', |
| 33 | + } |
| 34 | + self.s.headers.update( dict( new_headers.items() + self.global_headers.items() ) ) |
| 35 | + r = self.s.get('https://www.cloudflare.com/a/login', ) |
| 36 | + parse_dict[ 'security_token_0' ] = self.find_between_r( r.text, '"security_token":"', '"}};</script>' ) # http://xkcd.com/292/ |
| 37 | + |
| 38 | + post_data = { |
| 39 | + 'email': username, |
| 40 | + 'password': password, |
| 41 | + 'security_token': parse_dict[ 'security_token_0' ], |
| 42 | + } |
| 43 | + new_headers = { |
| 44 | + 'Referer': 'https://www.cloudflare.com/a/login', |
| 45 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 46 | + } |
| 47 | + self.s.headers.update( dict( new_headers.items() + self.global_headers.items() ) ) |
| 48 | + r = self.s.post('https://www.cloudflare.com/a/login', data=post_data) |
| 49 | + self.atok = self.find_between_r( r.text, 'window.bootstrap = {"atok":"', '","locale":"' ) # http://xkcd.com/292/ |
| 50 | + |
| 51 | + def get_domain_dns( self, domain ): |
| 52 | + parse_dict = {} |
| 53 | + post_data = { |
| 54 | + "betas": [], |
| 55 | + "created_on": "2015-08-24T00:27:16.048Z", |
| 56 | + "development_mode": False, |
| 57 | + "jump_start": True, |
| 58 | + "meta": {}, |
| 59 | + "modified_on": 'null', |
| 60 | + "name": domain, |
| 61 | + "owner": {}, |
| 62 | + "paused": False, |
| 63 | + "status": "initializing", |
| 64 | + "type": "full" |
| 65 | + } |
| 66 | + |
| 67 | + new_headers = { |
| 68 | + 'Content-Type': 'application/json; charset=UTF-8', |
| 69 | + 'X-Requested-With': 'XMLHttpRequest', |
| 70 | + 'Referer': 'https://www.cloudflare.com/a/add-site', |
| 71 | + 'Pragma': 'no-cache', |
| 72 | + 'Cache-Control': 'no-cache', |
| 73 | + 'X-ATOK': self.atok, |
| 74 | + } |
| 75 | + self.s.headers.update( dict( new_headers.items() + self.global_headers.items() ) ) |
| 76 | + r = self.s.post('https://www.cloudflare.com/api/v4/zones', data=json.dumps( post_data )) |
| 77 | + data = json.loads( r.text ) |
| 78 | + success = data['success'] |
| 79 | + if not success: |
| 80 | + print( r.text ) |
| 81 | + return False |
| 82 | + |
| 83 | + request_id = data['result']['id'] |
| 84 | + time.sleep( 60 ) |
| 85 | + |
| 86 | + get_data = { |
| 87 | + 'per_page': '100', |
| 88 | + 'direction': 'asc', |
| 89 | + 'page': '1', |
| 90 | + 'order': 'type', |
| 91 | + } |
| 92 | + new_headers = { |
| 93 | + 'X-Requested-With': 'XMLHttpRequest', |
| 94 | + 'Referer': 'https://www.cloudflare.com/a/setup/' + domain + '/step/2', |
| 95 | + 'X-ATOK': self.atok, |
| 96 | + } |
| 97 | + self.s.headers.update( dict( new_headers.items() + self.global_headers.items() ) ) |
| 98 | + r = self.s.get('https://www.cloudflare.com/api/v4/zones/' + request_id + '/dns_records', params=get_data) |
| 99 | + return_data = json.loads( r.text ) |
| 100 | + |
| 101 | + new_headers = { |
| 102 | + 'X-Requested-With': 'XMLHttpRequest', |
| 103 | + 'Referer': 'https://www.cloudflare.com/a/setup/' + domain + '/step/2', |
| 104 | + 'X-ATOK': self.atok, |
| 105 | + } |
| 106 | + self.s.headers.update( dict( new_headers.items() + self.global_headers.items() ) ) |
| 107 | + r = self.s.delete('https://www.cloudflare.com/api/v4/zones/' + request_id, ) |
| 108 | + |
| 109 | + get_data = { |
| 110 | + 'status': 'initializing,pending', |
| 111 | + 'per_page': '50', |
| 112 | + 'page': '1', |
| 113 | + } |
| 114 | + new_headers = { |
| 115 | + 'X-Requested-With': 'XMLHttpRequest', |
| 116 | + 'Referer': 'https://www.cloudflare.com/a/add-site', |
| 117 | + 'X-ATOK': self.atok, |
| 118 | + } |
| 119 | + self.s.headers.update( dict( new_headers.items() + self.global_headers.items() ) ) |
| 120 | + r = self.s.get('https://www.cloudflare.com/api/v4/zones', params=get_data) |
| 121 | + |
| 122 | + return return_data['result'] |
| 123 | + |
| 124 | + def get_spreadsheet( self, domain ): |
| 125 | + dns_data = self.get_domain_dns( domain ) |
| 126 | + if dns_data: |
| 127 | + filename = domain.replace( ".", "_" ) + ".csv" |
| 128 | + |
| 129 | + with open( filename, 'wb' ) as csvfile: |
| 130 | + dns_writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) |
| 131 | + dns_writer.writerow( [ "name", "type", "content" ] ) |
| 132 | + for record in dns_data: |
| 133 | + dns_writer.writerow( [ record["name"], record["type"], record["content"] ] ) |
| 134 | + |
| 135 | + self.statusmsg( "Spreadsheet created at " + os.getcwd() + "/" + filename ) |
| 136 | + |
| 137 | + def print_banner( self ): |
| 138 | + if self.verbose: |
| 139 | + print(""" |
| 140 | + |
| 141 | + `..--------..` |
| 142 | + .-:///::------::///:.` |
| 143 | + `-//:-.`````````````.-://:.` ` ` |
| 144 | + .://-.```````````````````.-://-` : `- . |
| 145 | + `-//:.........................-://. /. -: `:` `` |
| 146 | + `://--------:::://////:::--------://-::.::`:- .:. |
| 147 | + ``.---..` `///::::::///////////////////:::::::///::::::--:.`.-. |
| 148 | + .://::::///::///::///////////////////////////:::///:-----::--:-` ` |
| 149 | + `:/:-...--:://////////////////////////////////////////----------.--.` |
| 150 | + `:/:..-:://////////////////////////////////////////////-----------.```` |
| 151 | + .//-::////////////////////////////////////:::::////////-...--------...` |
| 152 | + -/////////////////////////////////////////////::::----:. `.-::::::-..`` |
| 153 | + ``.--:////////////////////////////////////////////////::-..```-///::::///:-` |
| 154 | + `.:///::::://////////////////////////////////////:::::::::::::::-----......-:/:. |
| 155 | + `-//:-----::::://///////////////////////////////:///////////////////:-::::---..-//:` |
| 156 | + `:/:---://+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//+++//::--//: |
| 157 | + `//:-/+oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo+++oooo+//://. |
| 158 | + :///ossssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssosssssso+//: |
| 159 | + `//+sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss+/- |
| 160 | + `//+ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo+++++/. |
| 161 | + `````````````````````````````````````````````````````````````````````````````````````` |
| 162 | + Cloudflare DNS Enumeration Tool v1.3 |
| 163 | + Created by mandatory |
| 164 | + Modified by yamakira |
| 165 | + """ ) |
| 166 | + |
| 167 | + def pprint( self, input_dict ): |
| 168 | + print( json.dumps(input_dict, sort_keys=True, indent=4, separators=(',', ': ')) ) |
| 169 | + |
| 170 | + def statusmsg( self, msg ): |
| 171 | + if self.verbose: |
| 172 | + print( "[ STATUS ] " + msg ) |
| 173 | + |
| 174 | + def errormsg( self, msg ): |
| 175 | + if self.verbose: |
| 176 | + print( "[ ERROR ] " + msg ) |
| 177 | + |
| 178 | + def successmsg( self, msg ): |
| 179 | + if self.verbose: |
| 180 | + print( "[ SUCCESS ] " + msg ) |
| 181 | + |
| 182 | + def find_between_r( self, s, first, last ): |
| 183 | + try: |
| 184 | + start = s.rindex( first ) + len( first ) |
| 185 | + end = s.rindex( last, start ) |
| 186 | + return s[start:end] |
| 187 | + except ValueError: |
| 188 | + return "" |
| 189 | + |
| 190 | + def find_between( s, first, last ): |
| 191 | + try: |
| 192 | + start = s.index( first ) + len( first ) |
| 193 | + end = s.index( last, start ) |
| 194 | + return s[start:end] |
| 195 | + except ValueError: |
| 196 | + return "" |
| 197 | + |
| 198 | + def get_cookie_from_file( self, cookie_file ): |
| 199 | + return_dict = {} |
| 200 | + with open( cookie_file ) as tmp: |
| 201 | + data = tmp.readlines() |
| 202 | + tmp_data = [] |
| 203 | + for i, item in enumerate(data): |
| 204 | + if " " in data[i]: |
| 205 | + pew = data[i].split( " " ) |
| 206 | + return_dict[ pew[5] ] = pew[6] |
| 207 | + |
| 208 | + return return_dict |
| 209 | + |
| 210 | + def get_creds(self): |
| 211 | + username = sys.argv[1] |
| 212 | + password = getpass.getpass('Provide your cloudflare password:') |
| 213 | + return username,password |
| 214 | + |
| 215 | +if __name__ == "__main__": |
| 216 | + if len( sys.argv ) < 2: |
| 217 | + print( "Usage: " + sys. argv[ 0] + " [email protected] domain.com" ) |
| 218 | + else: |
| 219 | + cloud = cloudflare_enum() |
| 220 | + username,password = cloud.get_creds() |
| 221 | + cloud.print_banner() |
| 222 | + cloud.log_in(username,password) |
| 223 | + cloud.get_spreadsheet(sys.argv[2]) |
0 commit comments