Skip to content

Commit

Permalink
Pep8 reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jeb16157 committed Feb 17, 2021
1 parent 602e250 commit 092d3dc
Showing 1 changed file with 63 additions and 45 deletions.
108 changes: 63 additions & 45 deletions Python/attacker.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,71 @@
import random #need the random library to generate random port numbers
import random # need the random library to generate random port numbers
import math

from . import packet


class attacker:
def __init__(self, connectionToServer, connectionToRouter, client_IP, server_IP, fileGen,packetSize, bandwidthToMserver, ipAddress = "", packet_ID_prefix = "A-", attackType = 0, reflecting = 1):
self.clientIP = client_IP #client's IP, used for generating spoofed packetsDropped
self.serverIP = server_IP #IP of server used for SYN-ACK Flood
self.curTime = 0 #Tracks the current time of the simulation
self.counterPacketsSent = 0 #Counter for the summary file
self.connectionToServer = connectionToServer #stores the connectionObject connecting the attacker to the server
def __init__(self, connectionToServer, connectionToRouter, client_IP,
server_IP, fileGen, packetSize, bandwidthToMserver,
ipAddress="", packet_ID_prefix="A-", attackType=0,
reflecting=1):
self.clientIP = client_IP # client's IP
self.serverIP = server_IP # IP of server used for SYN-ACK Flood
self.curTime = 0 # Tracks the current time of the simulation
self.counterPacketsSent = 0 # Counter for the summary file
self.connectionToServer = connectionToServer
self.connectionToRouter = connectionToRouter
self.curTime = 0
self.packetsSent = 0 #counter for logging purposes
self.packetsReceived = 0 #counter for logging purposes (currently unused but it makes it easier for the log file to operate)
self.packetsDropped = 0 #counter for packets dropped (currently unused but it makes it easier for the log file to operate)
self.packetsGenerated = 0 #Counter for number of packets generated (dw - in the case of a dumb attacker it ends up being the same as packetsSent, but I was adding this variable to all classes for consistency and symmetry in the summary file)
self.packetsDroppedOnConnection = 0 #specifies the number of packets that dropped on the connectionObject due to bandwidth after being sent by the device
self.fileGen = fileGen #File management object
self.packetIDPrefix = packet_ID_prefix #Used for packet IDs as a prefix so that we're not using the IPs
self.packetsSent = 0 # Counter For Packets sent
self.packetsReceived = 0 # Counter for logging purposes
self.packetsDropped = 0 # counter for packets dropped
self.packetsGenerated = 0 # Counter for number of packets generated
self.packetsDroppedOnConnection = 0 # Number of packets dropped
self.fileGen = fileGen # File management object
self.packetIDPrefix = packet_ID_prefix # Used for packet IDs
self.ipAddress = ipAddress
self.packetSize = packetSize
#self.sendRate = sendRate

self.bandwidthToMserver = bandwidthToMserver
### DW AUDIT 31JAN2021 self.transmissionDelay = (self.packetSize / self.sendRate) * 1000

self.transmissionDelay = (self.packetSize / self.bandwidthToMserver) * 1000
self.sendingPacket = None
self.timeToNextIteration = 1
self.attackType = attackType
self.reflecting = reflecting

self.openPort = None
self.checkOpenPorts = 0

def processPackets(self):
if(self.sendingPacket == None):
if(self.sendingPacket is None):
self.timeToNextIteration -= self.transmissionDelay
if(self.timeToNextIteration >= 0):
self.packetsGenerated += 1
packet = self.generateSYN() #generate SYN packet
packet = self.generateSYN() # generate SYN packet
self.sendingPacket = [packet, self.curTime]

else:
self.timeToNextIteration += self.transmissionDelay
self.packetsGenerated += 1
packet = self.generateSYN() #generate SYN packet
packet = self.generateSYN() # generate SYN packet
self.sendingPacket = [packet, (self.curTime + self.transmissionDelay) - (self.timeToNextIteration)]
self.timeToNextIteration -= self.transmissionDelay

elif(self.sendingPacket != None):
elif(self.sendingPacket is not None):
leftoverTime = self.sendingPacket[1] - self.curTime
self.timeToNextIteration -= leftoverTime


def sendPackets(self): #Puts packets onto the connection
def sendPackets(self): # Puts packets onto the connection
if((self.sendingPacket[1] - self.curTime) < 1):
if(self.sendingPacket[0].dstIP == self.serverIP):
print(self.sendingPacket[0].dstIP)
self.packetsSent += 1 #increment counter
self.connectionToServer.transferPacketOut(self.sendingPacket[0]) #place packet on connection
self.packetsSent += 1 # increment counter
self.connectionToServer.transferPacketOut(self.sendingPacket[0]) # place packet on connection
self.sendingPacket = None
else:
self.packetsSent += 1 #increment counter
self.connectionToRouter.transferPacketOut(self.sendingPacket[0]) #place packet on connection
self.packetsSent += 1 # increment counter
self.connectionToRouter.transferPacketOut(self.sendingPacket[0]) # place packet on connection
self.sendingPacket = None

def generateSYN(self):
Expand All @@ -74,39 +77,54 @@ def generateSYN(self):
destinationIP = self.clientIP
sourceIP = self.ipAddress
if(self.attackType == 0):
portNumber = random.randint(0, 500) #generate a random port number
npacket = packet.packet(sourceIP, portNumber, destinationIP, 80, packetType, random.randint(0, 10000), 0, self.packetIDPrefix + str(self.packetsGenerated),self.packetSize, self.ipAddress) #return the packet
self.fileGen.addToLog(npacket.packetID, "Generated at Attacker", npacket.sequenceNumber, npacket.ackNumber, npacket.packetType, self.curTime)
portNumber = random.randint(0, 500) # generate a random port number
npacket = packet.packet(sourceIP, portNumber, destinationIP, 80,
packetType, random.randint(0, 10000), 0,
self.packetIDPrefix + str(self.packetsGenerated),
self.packetSize, self.ipAddress) # return the packet
self.fileGen.addToLog(npacket.packetID, "Generated at Attacker",
npacket.sequenceNumber, npacket.ackNumber,
npacket.packetType, self.curTime)
return npacket
else:
if(self.openPort == None):
portNumber = random.randint(0, 500) #generate a random port number
npacket = packet.packet(self.ipAddress, 20,"2.0.0.0", portNumber, "PING", random.randint(0, 10000), 0, self.packetIDPrefix + str(self.packetsGenerated),self.packetSize, self.ipAddress)
self.fileGen.addToLog(npacket.packetID, "Generated at Attacker", npacket.sequenceNumber, npacket.ackNumber, npacket.packetType, self.curTime)
if(self.openPort is None):
portNumber = random.randint(0, 500) # generate a random port number
npacket = packet.packet(self.ipAddress, 20, "2.0.0.0", portNumber,
"PING", random.randint(0, 10000), 0,
self.packetIDPrefix + str(self.packetsGenerated),
self.packetSize, self.ipAddress)
self.fileGen.addToLog(npacket.packetID, "Generated at Attacker",
npacket.sequenceNumber, npacket.ackNumber,
npacket.packetType, self.curTime)
return npacket
elif(self.checkOpenPorts >= 10):
npacket = packet.packet(self.ipAddress, 20,"2.0.0.0", self.openPort, "PING", random.randint(0, 10000), 0, self.packetIDPrefix + str(self.packetsGenerated),self.packetSize, self.ipAddress)
self.fileGen.addToLog(npacket.packetID, "Generated at Attacker", npacket.sequenceNumber, npacket.ackNumber, npacket.packetType, self.curTime)
npacket = packet.packet(self.ipAddress, 20, "2.0.0.0", self.openPort,
"PING", random.randint(0, 10000), 0,
self.packetIDPrefix + str(self.packetsGenerated),
self.packetSize, self.ipAddress)
self.fileGen.addToLog(npacket.packetID, "Generated at Attacker",
npacket.sequenceNumber, npacket.ackNumber,
npacket.packetType, self.curTime)
self.checkOpenPorts = 0
return npacket
else:
npacket = packet.packet(sourceIP, 20, destinationIP, self.openPort, packetType, random.randint(0, 10000), 0, self.packetIDPrefix + str(self.packetsGenerated),self.packetSize, self.ipAddress) #return the packet
self.fileGen.addToLog(npacket.packetID, "Generated at Attacker", npacket.sequenceNumber, npacket.ackNumber, npacket.packetType, self.curTime)
npacket = packet.packet(sourceIP, 20, destinationIP, self.openPort,
packetType, random.randint(0, 10000), 0,
self.packetIDPrefix + str(self.packetsGenerated),
self.packetSize, self.ipAddress) # return the packet
self.fileGen.addToLog(npacket.packetID, "Generated at Attacker",
npacket.sequenceNumber, npacket.ackNumber,
npacket.packetType, self.curTime)
self.checkOpenPorts += 1
return npacket



def receivePacket(self, packet):
if(packet.packetType == "PONG" and self.openPort == None):
if(packet.packetType == "PONG" and self.openPort is None):
self.openPort = packet.srcPortNumber
#print("One open port found: "+ str(self.openPort))
# print("One open port found: "+ str(self.openPort))
elif(packet.packetType == "PONG-RST"):
if(packet.srcPortNumber == self.openPort):
self.openPort = None




def updateTime(self):
self.curTime += 1
Expand Down

0 comments on commit 092d3dc

Please sign in to comment.