-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdshield.py
66 lines (56 loc) · 2.11 KB
/
dshield.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Make API Requests to https://isc.sans.edu/api/ and https://dshield.org/api/
TODO
1. Make some flags to let the user choose return type - currently json, text, xml, csv are available
2. Actually fix the json formatting / printing
"""
import re
# import json
import argparse
import urllib.request
apiUrl = "https://isc.sans.edu/api/"
apiUrlMirror = "https://dshield.org/api/"
def makeHttpRequest(url):
try:
with urllib.request.urlopen(url) as apiResponse:
pageResponse = apiResponse.read()
return pageResponse
except urllib.error.URLError as e:
return e.reason
def searchIpAddr(ipAddr):
if (re.match(r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$', ipAddr)):
ipUrl = apiUrl + "ip/" + ipAddr + "?text"
apiResponse = makeHttpRequest(ipUrl)
return apiResponse.decode("utf-8")
else:
return "\n[!] Invalid IP Address"
def searchPort(portNumber):
if (re.match(r'^\d+$', portNumber) and int(portNumber) > 0 and int(portNumber) < 65536):
portUrl = apiUrl + "port/" + portNumber + "?text"
apiResponse = makeHttpRequest(portUrl)
return apiResponse.decode("utf-8")
# -- JSON Attempt:
# portUrl = apiUrl + "port/" + portNumber + "?json"
# jsonResponse = json.load(apiResponse.decode("utf-8"))
# print(jsonResponse)
# this json dumps only kinda works how I want it to...
# return json.dumps(apiResponse.decode("utf-8"), indent=2)
else:
return "\n[!] Invalid port number"
# -=-=-=-=-=-= "Main Program" -=-=-=-=-=-=-=-=-=-=- #
# Set up the arg parser:
parser = argparse.ArgumentParser(description="Make HTTP(S) Requests to the SANS DShield API")
parser.add_argument("-i", dest="ipAddr", help="Specify IP Address to search.")
parser.add_argument("-p", dest="port", help="Specify port to search.")
# parser.add_argument("--csv", help="Flag this for CSV output")
# parser.add_argument("--text", help="Flag this for plain text output")
args = parser.parse_args()
if not args.ipAddr and not args.port:
parser.print_help()
elif args.port:
print(searchPort(args.port))
elif args.ipAddr:
print(searchIpAddr(args.ipAddr))
else:
print("[!] Unknown Error")
parser.print_help()