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.QtWidgets import *
from scapy.all import *
from scapy.layers.inet import *
from scapy.layers.l2 import *
class NetworkMonitorThread(QObject):
def __init__(self, interface, parent=None):
@ -12,6 +15,7 @@ class NetworkMonitorThread(QObject):
self.end = False
quitBool = pyqtSignal(int)
def endSniff(self):
QApplication.processEvents()
print("Ending")
@ -22,7 +26,7 @@ class NetworkMonitorThread(QObject):
QApplication.processEvents()
return self.end
def getLayers(self,packet):
def getLayers(self, packet):
QApplication.processEvents()
layers = []
counter = 0
@ -37,7 +41,8 @@ class NetworkMonitorThread(QObject):
return layers
packetData = pyqtSignal(tuple)
def handlePacket(self,packet):
def handlePacket(self, packet):
self.packetList.append(packet)
QApplication.processEvents()
tableViewPart = dict()
@ -52,7 +57,7 @@ class NetworkMonitorThread(QObject):
tableViewPart['layers'] = self.getLayers(packet)
QApplication.processEvents()
(protocol,info) = self.getInfo(packet)
(protocol, info) = self.getInfo(packet)
tableViewPart['info'] = info
if protocol:
tableViewPart['Protocol'] = protocol
@ -63,16 +68,18 @@ class NetworkMonitorThread(QObject):
QApplication.processEvents()
self.packetData.emit((packet, tableViewPart))
def getInfo(self,packet):
def getInfo(self, packet):
QApplication.processEvents()
info = ""
protocol = ""
if UDP in packet:
protocol = "UDP"
info = "{} -> {} len={} chksum={}".format(packet[UDP].sport,
info = "{} -> {} len={} chksum={}".format(
packet[UDP].sport,
packet[UDP].dport,
packet[UDP].len,
packet[UDP].chksum)
packet[UDP].chksum
)
elif TCP in packet:
flags = {
'F': 'FIN',
@ -87,23 +94,32 @@ class NetworkMonitorThread(QObject):
flgs = str([flags[x] for x in packet.sprintf('%TCP.flags%')])
protocol = "TCP"
info = "{} -> {} {} seq={} ack={} window={}".format(packet[TCP].sport,
info = "{} -> {} {} seq={} ack={} window={}".format(
packet[TCP].sport,
packet[TCP].dport,
flgs,
packet[TCP].seq,
packet[TCP].ack,
packet[TCP].window)
packet[TCP].window
)
elif ARP in packet:
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].hwlen,
packet[ARP].plen,
packet[ARP].op)
packet[ARP].op
)
QApplication.processEvents()
return (protocol,info)
return (protocol, info)
def startSniff(self):
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.QtWidgets import *
from scapy.all import *
from scapy.layers.inet import IP
from scapy.layers.inet import ICMP
class Ping(QWidget):
def __init__(self):
super().__init__()
self.hostn = QLabel("Enter Address/Hostname to ping:")
self.host = QLineEdit()
@ -17,6 +19,8 @@ class Ping(QWidget):
self.startPingBtn.clicked.connect(self.startPing)
self.result = QTextEdit()
self.result.setReadOnly(True)
self.layoutPing = QVBoxLayout(self)
self.layoutPing.addWidget(self.hostn)
self.layoutPing.addWidget(self.host)
@ -24,5 +28,13 @@ class Ping(QWidget):
self.layoutPing.addWidget(self.result)
self.setLayout(self.layoutPing)
def startPing():
pass
def startPing(self):
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.layers.inet import traceroute
class TraceRoute(QWidget):
def __init__(self):
super().__init__()
@ -26,10 +27,17 @@ class TraceRoute(QWidget):
self.setLayout(self.layoutTrace)
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.append("\tRoute path\tResponse time")
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)}")
self.result.setText('\n'.join(output))
except socket.gaierror as e:
self.result.setText(
"Invalid address/Could not get address info")