Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CSEGroup22SDP/Python/attacker.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
135 lines (122 sloc)
6.68 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
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 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.bandwidthToMserver = bandwidthToMserver | |
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 is None): | |
self.timeToNextIteration -= self.transmissionDelay | |
if(self.timeToNextIteration >= 0): | |
self.packetsGenerated += 1 | |
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 | |
self.sendingPacket = [packet, (self.curTime + self.transmissionDelay) - (self.timeToNextIteration)] | |
self.timeToNextIteration -= self.transmissionDelay | |
elif(self.sendingPacket is not None): | |
leftoverTime = self.sendingPacket[1] - self.curTime | |
self.timeToNextIteration -= leftoverTime | |
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.sendingPacket = None | |
else: | |
self.packetsSent += 1 # increment counter | |
self.connectionToRouter.transferPacketOut(self.sendingPacket[0]) # place packet on connection | |
self.sendingPacket = None | |
def generateSYN(self): | |
packetType = "SYN" | |
destinationIP = self.serverIP | |
sourceIP = self.clientIP | |
if (self.reflecting == 0): | |
packetType = "SYN" | |
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) | |
return npacket | |
else: | |
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) | |
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) | |
self.checkOpenPorts += 1 | |
return npacket | |
def receivePacket(self, packet): | |
self.packetsReceived += 1 | |
if(packet.packetType == "PONG" and self.openPort is None): | |
self.openPort = packet.srcPortNumber | |
# 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 | |
self.timeToNextIteration = 1 | |
while(self.timeToNextIteration > 0): | |
self.processPackets() | |
self.sendPackets() |