Add Scan Settings page, Work on saving data

TODO:
- Finish saving data
This commit is contained in:
astatin3
2024-04-19 23:11:46 -06:00
parent 96285a4c58
commit c21af54bcd
7 changed files with 481 additions and 77 deletions
+120 -11
View File
@@ -5,16 +5,83 @@ import os
import re
import geoip2.database
def countScannedIps():
files = utils.listSubdirs("data/")
count = 0
for file in files:
if file.split("-")[0] != "scan":
continue
with open('data/'+file) as f:
#Count lines in scan files, Masscan has a 2 line header, so hence -2
count += sum(1 for _ in f)-2
return count
import socket
import struct
import time
def checksum(data):
"""
Calculate the checksum of the ICMP packet data.
"""
sum = 0
for i in range(0, len(data), 2):
sum += (data[i] << 8) + data[i+1]
sum = (sum & 0xffff) + (sum >> 16)
sum = ~sum & 0xffff
return sum
import time
import random
import select
import array
def ping_chksum(packet:bytes):
if len(packet) % 2 != 0:
packet += b'\0'
res = sum(struct.unpack("!%sH" % (len(packet) // 2), packet))
res = (res >> 16) + (res & 0xffff)
res += res >> 16
return (~res) & 0xffff
def ping(host:str, timeout:int, sock):
returnVal = (False, -1)
try:
# Craft the ICMP echo request packet
packet_id = int(time.time() * 1000) & 0xFFFF
header = struct.pack("bbHHh", 8, 0, 0, packet_id, 1)
data = b"ping"
checksum = ping_chksum(header + data)
header = struct.pack("bbHHh", 8, 0, socket.htons(checksum), packet_id, 1)
packet = header + data
# Send the ICMP echo request
sock.sendto(packet, (host, 1))
# Receive the ICMP echo reply
start_time = time.time()
while True:
remaining_time = timeout - (time.time() - start_time)
if remaining_time <= 0:
return False
ready = select.select([sock], [], [], remaining_time)
if ready[0]:
data, addr = sock.recvfrom(1024)
icmp_header = data[20:28]
type, code, checksum, p_id, sequence = struct.unpack("bbHHh", icmp_header)
if p_id == packet_id:
return returVal
except:pass
return returnVal
# return False
# def countScannedIps():
# files = utils.listSubdirs("data/")
# count = 0
# for file in files:
# if file.split("-")[0] != "scan":
# continue
# with open('data/'+file) as f:
# #Count lines in scan files, Masscan has a 2 line header, so hence -2
# count += sum(1 for _ in f)-2
# return count
def getMostCommon(protocol: str, n: int):
nmap_services_file = "/usr/share/nmap/nmap-services"
@@ -125,7 +192,7 @@ if not utils.pathExists(ASN_DB_PATH):
def geolocation(ip_address):
def geolocation(ip_address: str):
try:
# Attempt to retrieve city-level information
with geoip2.database.Reader(CITY_DB_PATH) as reader:
@@ -166,3 +233,45 @@ def geolocation(ip_address):
'ip': ip_address,
'error': 'No geolocation data found'
}
def parseIpList(path: str):
with open(path, "r") as f:
lines = f.readlines()
return [line.rstrip()
for line in lines
if not line.startswith('#') and not line.startswith('\n')
]
def ipToInt(ip: str):
octets = [int(n) for n in ip.split('.')]
return (octets[0] << 24) + (octets[1] << 16) + (octets[2] << 8) + octets[3]
def ipInCIDR(ip: str, ip_CIDR: str):
range_parts = ip_CIDR.split('/')
range_mask = int(range_parts[1])
range_mask_num = (0xFFFFFFFF << (32 - range_mask)) & 0xFFFFFFFF
return (ipToInt(ip) & range_mask_num) == (ipToInt(range_parts[0]) & range_mask_num)
def ipInRange(ip: str, ip_range: str):
ip_int = ipToInt(ip)
start_ip_int, end_ip_int = [ipToInt(ip) for ip in ip_range.split('-')]
return start_ip_int <= ip_int <= end_ip_int
def ipInArray(ip: str, ipRangeArray: list):
for ipRange in ipRangeArray:
if "/" in ipRange:
if ipInCIDR(ip, ipRange):
return True
elif "-" in ipRange:
if ipInRange(ip, ipRange):
return True
elif ip == ipRange:
return True
return False