Ping function, some imports and error handling

This commit is contained in:
Kaushik 2021-12-23 23:31:16 +05:30
parent 26d762f1fc
commit 3cc3aa40f8
3 changed files with 91 additions and 55 deletions

View File

@ -3,6 +3,9 @@ from PyQt5.QtGui import *
from PyQt5.QtCore import * from PyQt5.QtCore import *
from PyQt5.QtWidgets import * from PyQt5.QtWidgets import *
from scapy.all import * from scapy.all import *
from scapy.layers.inet import *
from scapy.layers.l2 import *
class NetworkMonitorThread(QObject): class NetworkMonitorThread(QObject):
def __init__(self, interface, parent=None): def __init__(self, interface, parent=None):
@ -12,6 +15,7 @@ class NetworkMonitorThread(QObject):
self.end = False self.end = False
quitBool = pyqtSignal(int) quitBool = pyqtSignal(int)
def endSniff(self): def endSniff(self):
QApplication.processEvents() QApplication.processEvents()
print("Ending") print("Ending")
@ -37,6 +41,7 @@ class NetworkMonitorThread(QObject):
return layers return layers
packetData = pyqtSignal(tuple) packetData = pyqtSignal(tuple)
def handlePacket(self, packet): def handlePacket(self, packet):
self.packetList.append(packet) self.packetList.append(packet)
QApplication.processEvents() QApplication.processEvents()
@ -69,10 +74,12 @@ class NetworkMonitorThread(QObject):
protocol = "" protocol = ""
if UDP in packet: if UDP in packet:
protocol = "UDP" protocol = "UDP"
info = "{} -> {} len={} chksum={}".format(packet[UDP].sport, info = "{} -> {} len={} chksum={}".format(
packet[UDP].sport,
packet[UDP].dport, packet[UDP].dport,
packet[UDP].len, packet[UDP].len,
packet[UDP].chksum) packet[UDP].chksum
)
elif TCP in packet: elif TCP in packet:
flags = { flags = {
'F': 'FIN', 'F': 'FIN',
@ -87,23 +94,32 @@ class NetworkMonitorThread(QObject):
flgs = str([flags[x] for x in packet.sprintf('%TCP.flags%')]) flgs = str([flags[x] for x in packet.sprintf('%TCP.flags%')])
protocol = "TCP" protocol = "TCP"
info = "{} -> {} {} seq={} ack={} window={}".format(packet[TCP].sport, info = "{} -> {} {} seq={} ack={} window={}".format(
packet[TCP].sport,
packet[TCP].dport, packet[TCP].dport,
flgs, flgs,
packet[TCP].seq, packet[TCP].seq,
packet[TCP].ack, packet[TCP].ack,
packet[TCP].window) packet[TCP].window
)
elif ARP in packet: elif ARP in packet:
protocol = "ARP" protocol = "ARP"
info = "hwtype={} ptype={} hwlen={} plen={} op={}".format(packet[ARP].hwtype, info = "hwtype={} ptype={} hwlen={} plen={} op={}".format(
packet[ARP].hwtype,
packet[ARP].ptype, packet[ARP].ptype,
packet[ARP].hwlen, packet[ARP].hwlen,
packet[ARP].plen, packet[ARP].plen,
packet[ARP].op) packet[ARP].op
)
QApplication.processEvents() QApplication.processEvents()
return (protocol, info) return (protocol, info)
def startSniff(self): def startSniff(self):
QApplication.processEvents() QApplication.processEvents()
self.pkts = sniff(count=0, iface=self.interface, prn=self.handlePacket, stop_filter=lambda x: self.sniffStatus()) self.pkts = sniff(
count=0,
iface=self.interface,
prn=self.handlePacket,
stop_filter={lambda x: self.sniffStatus()}
)

18
ping.py
View File

@ -3,12 +3,14 @@ from PyQt5.QtGui import *
from PyQt5.QtCore import * from PyQt5.QtCore import *
from PyQt5.QtWidgets import * from PyQt5.QtWidgets import *
from scapy.all import * from scapy.all import *
from scapy.layers.inet import IP
from scapy.layers.inet import ICMP
class Ping(QWidget): class Ping(QWidget):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.hostn = QLabel("Enter Address/Hostname to ping:") self.hostn = QLabel("Enter Address/Hostname to ping:")
self.host = QLineEdit() self.host = QLineEdit()
@ -17,6 +19,8 @@ class Ping(QWidget):
self.startPingBtn.clicked.connect(self.startPing) self.startPingBtn.clicked.connect(self.startPing)
self.result = QTextEdit() self.result = QTextEdit()
self.result.setReadOnly(True)
self.layoutPing = QVBoxLayout(self) self.layoutPing = QVBoxLayout(self)
self.layoutPing.addWidget(self.hostn) self.layoutPing.addWidget(self.hostn)
self.layoutPing.addWidget(self.host) self.layoutPing.addWidget(self.host)
@ -24,5 +28,13 @@ class Ping(QWidget):
self.layoutPing.addWidget(self.result) self.layoutPing.addWidget(self.result)
self.setLayout(self.layoutPing) self.setLayout(self.layoutPing)
def startPing(): def startPing(self):
pass if(len(self.host.text()) >= 1):
try:
packet = IP(dst=self.host.text(), ttl=10)/ICMP()
output = sr1(packet, timeout=2)
if output is not None:
self.result.setText(output.summary())
except socket.gaierror as e:
self.result.setText(
"Invalid address/Could not get address info")

View File

@ -4,6 +4,7 @@ from PyQt5.QtWidgets import *
from scapy.all import * from scapy.all import *
from scapy.layers.inet import traceroute from scapy.layers.inet import traceroute
class TraceRoute(QWidget): class TraceRoute(QWidget):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -26,10 +27,17 @@ class TraceRoute(QWidget):
self.setLayout(self.layoutTrace) self.setLayout(self.layoutTrace)
def startTrace(self): def startTrace(self):
result, unans = traceroute(target=self.host.text(), dport=80, verbose=0) if(len(self.host.text()) >= 1):
try:
result, unans = traceroute(
target=self.host.text(), dport=80, verbose=0)
output = [] output = []
output.append("\tRoute path\tResponse time") output.append("\tRoute path\tResponse time")
for snd, rcv in result: for snd, rcv in result:
output.append(str(f"{snd.ttl}\t{rcv.src}\t\t{(int((rcv.time - snd.sent_time)*1000))} ms")) output.append(
str(f"{snd.ttl}\t{rcv.src}\t\t{(int((rcv.time - snd.sent_time)*1000))} ms"))
output.append(f"\nUnanswered packets: {len(unans)}") output.append(f"\nUnanswered packets: {len(unans)}")
self.result.setText('\n'.join(output)) self.result.setText('\n'.join(output))
except socket.gaierror as e:
self.result.setText(
"Invalid address/Could not get address info")