|
| 1 | +import requests |
| 2 | +from config import * |
| 3 | +import sqlite3 |
| 4 | +from flask import Flask, render_template, request |
| 5 | +app = Flask(__name__) |
| 6 | + |
| 7 | +# Edit database file to reflect the right name/location of the netflow DB |
| 8 | +conn = sqlite3.connect('netflow2.db') |
| 9 | +conn.row_factory = sqlite3.Row |
| 10 | +#conn.row_factory = sqlite3.Row |
| 11 | +c = conn.cursor() |
| 12 | +print("Deleting rows...") |
| 13 | +# Blacklist considers Google DNS malicious.. cuz.. bad guys use it... |
| 14 | +# Remove it from our database.. |
| 15 | +c.execute("DELETE FROM shodan WHERE dest=?", ("8.8.8.8",)) |
| 16 | +conn.commit() |
| 17 | + |
| 18 | +# Prints all of the junk in the shodan table.. for debugging. |
| 19 | +for i in c.execute("SELECT * FROM shodan"): |
| 20 | + for j in i: |
| 21 | + pass |
| 22 | + #print(j) |
| 23 | +c.execute("SELECT name FROM sqlite_master WHERE type='table';") |
| 24 | +#print(c.fetchone()) |
| 25 | +numSamples = 0 |
| 26 | +''' |
| 27 | +# Converts |
| 28 | +def dict_factory(cursor, row): |
| 29 | + d = {} |
| 30 | + for idx, col in enumerate(cursor.description): |
| 31 | + d[col[0]] = row[idx] |
| 32 | + return d |
| 33 | +''' |
| 34 | + |
| 35 | +portNumber = "*" |
| 36 | +def getData(): |
| 37 | + global result1 |
| 38 | + conn = sqlite3.connect('netflow2.db') |
| 39 | + #conn.row_factory = sqlite3.Row |
| 40 | + c = conn.cursor() |
| 41 | + #print(numSamples) |
| 42 | + c.execute("SELECT COUNT(*) FROM (SELECT DISTINCT dest, src FROM shodan LIMIT ?)", (numSamples,)) |
| 43 | + unique = c.fetchall() |
| 44 | + for row in unique: |
| 45 | + print("Total unique IP Addresses: " + str(row[0])) |
| 46 | + unique_IPs = str(row[0]) |
| 47 | + bytes = 0 |
| 48 | + #c.execute("SELECT * FROM traffic WHERE src!=? ORDER BY time DESC LIMIT ?", (home, numSamples)) |
| 49 | + c.execute("SELECT * FROM shodan ORDER BY time DESC LIMIT ?", (numSamples,)) |
| 50 | + #print(result1) |
| 51 | + result1 = [i for i in c.fetchall()] |
| 52 | + #for i in result1: |
| 53 | + #print(i) |
| 54 | + #print(result1) |
| 55 | + print("Outbound: " + humansize(bytes)) |
| 56 | + outbound = humansize(bytes) |
| 57 | + bytes = 0 |
| 58 | + print(portNumber) |
| 59 | + for row in c.execute("SELECT * FROM shodan WHERE dest!=? AND dport=? ORDER BY time DESC LIMIT ?", (home, portNumber, numSamples)): |
| 60 | + totalbytes = int(row[3]) * int(row[2]) |
| 61 | + bytes = bytes + totalbytes |
| 62 | + result2 = c.fetchall() |
| 63 | + #print(result2) |
| 64 | + result2 = {item[6]: item for item in result2} |
| 65 | + |
| 66 | + print("Inbound: " + humansize(bytes)) |
| 67 | + inbound = humansize(bytes) |
| 68 | + #print(inbound.split()[0]) |
| 69 | + total = float(inbound.split()[0]) + float(outbound.split()[0]) |
| 70 | + #print(str(total)) |
| 71 | + return unique_IPs, outbound.split()[0], outbound.split()[1], \ |
| 72 | + inbound.split()[0], inbound.split()[1], str(total) |
| 73 | + |
| 74 | +try: |
| 75 | + home = requests.get('http://ipquail.com/ip').text.strip("\n\r") |
| 76 | + print(home) |
| 77 | +except: |
| 78 | + home = static_ip |
| 79 | + |
| 80 | +suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] |
| 81 | + |
| 82 | +# Sweet function that converts bytes into readable form |
| 83 | +def humansize(nbytes): |
| 84 | + i = 0 |
| 85 | + while nbytes >= 1024 and i < len(suffixes)-1: |
| 86 | + nbytes /= 1024. |
| 87 | + i += 1 |
| 88 | + f = ('%.2f' % nbytes).rstrip('0').rstrip('.') |
| 89 | + return '%s %s' % (f, suffixes[i]) |
| 90 | + |
| 91 | +# DO MATH. |
| 92 | +def sqlStats(): |
| 93 | + #for row in c.execute("SELECT COUNT (DISTINCT src) FROM traffic"): |
| 94 | + for row in c.execute("SELECT COUNT(*) FROM (SELECT DISTINCT dest, src FROM shodan)"): |
| 95 | + print("Total unique IP Addresses: " + str(row[0])) |
| 96 | + unique_IPs = str(row[0]) |
| 97 | + bytes = 0 |
| 98 | + |
| 99 | + for row in c.execute("SELECT * FROM shodan WHERE src=?", (home,)): |
| 100 | + totalbytes = int(row[3]) * int(row[2]) |
| 101 | + bytes = bytes + totalbytes |
| 102 | + print("Outbound: " + humansize(bytes)) |
| 103 | + outbound = humansize(bytes) |
| 104 | + bytes = 0 |
| 105 | + for row in c.execute("SELECT * FROM shodan WHERE src!=?", (home,)): |
| 106 | + totalbytes = int(row[3]) * int(row[2]) |
| 107 | + bytes = bytes + totalbytes |
| 108 | + print("Inbound: " + humansize(bytes)) |
| 109 | + inbound = humansize(bytes) |
| 110 | + return unique_IPs, outbound, inbound |
| 111 | + |
| 112 | +#sqlStats() |
| 113 | + |
| 114 | +def maxRowsTable(): |
| 115 | + for row in c.execute("SELECT COUNT(time) FROM shodan"): |
| 116 | + maxNumberRows=row[0] |
| 117 | + return maxNumberRows |
| 118 | + |
| 119 | +## Flask Web Page Handling |
| 120 | +@app.route("/") |
| 121 | +def index(): |
| 122 | + unique_IPs, outbound, outlabel, inbound, inlabel, total = getData() |
| 123 | + iframe = 'iframe.html' |
| 124 | + templateData = { |
| 125 | + 'unique' : unique_IPs, |
| 126 | + 'outbound' : outbound, |
| 127 | + 'inbound' : inbound, |
| 128 | + 'total' : total, |
| 129 | + 'outlabel' : outlabel, |
| 130 | + 'inlabel' : inlabel, |
| 131 | + 'result1': result1, |
| 132 | + 'iframe' : iframe |
| 133 | + } |
| 134 | + return render_template('index.html', **templateData) |
| 135 | + |
| 136 | +@app.route('/', methods=['POST']) |
| 137 | +def formPost(): |
| 138 | + global numSamples |
| 139 | + global portNumber |
| 140 | + global IPAddress |
| 141 | + |
| 142 | + try: |
| 143 | + portNumber = int(request.form['portNumber']) |
| 144 | + except: |
| 145 | + portNumber = 8888 |
| 146 | + try: |
| 147 | + numSamples = int(request.form['numSamples']) |
| 148 | + except: |
| 149 | + numSamples = "1000000000" |
| 150 | + try: |
| 151 | + IPAddress = str(request.form['IPAddress']) |
| 152 | + except: |
| 153 | + IPAddress = "*" |
| 154 | + numMaxSamples = maxRowsTable() |
| 155 | + if (int(numSamples) > int(numMaxSamples)): |
| 156 | + numSamples = (int(numMaxSamples) -1) |
| 157 | + unique_IPs, outbound, outlabel, inbound, inlabel, total = getData() |
| 158 | + templateData = { |
| 159 | + 'unique': unique_IPs, |
| 160 | + 'outbound': outbound, |
| 161 | + 'inbound': inbound, |
| 162 | + 'total': total, |
| 163 | + 'outlabel': outlabel, |
| 164 | + 'inlabel': inlabel, |
| 165 | + 'result1': result1 |
| 166 | + } |
| 167 | + return render_template('index.html', **templateData) |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + app.run(host="0.0.0.0", port=80, debug=False) |
0 commit comments