def handle_pck(self, packet: Packet) -> Packet: if packet[IP].src == self.server_addr: if not self.send_payload: self.log.debug("Sending of payload is deactivated.") return packet cp3_pck = CP3Package(packet[NTP]) if self.sending_status == 1: cp3_pck.set_cp3_mode_1() else: cp3_pck.set_cp3_mode_2() cp3_pck.add_payload(self.__next_payload()) self.log.info('CP3 payload added for intercepted package: ' + str(cp3_pck.extract_payload()) + " in CP3 mode " + str(cp3_pck.get_cp3_mode())) complete_pck = packet[IP] / packet[UDP] / NTP() complete_pck[NTP] = cp3_pck.ntp() return complete_pck elif packet[IP].src == self.client_addr: cp3_pck = CP3Package(packet[NTP]) if not self.cp3_handler.read_incoming_pck(cp3_pck): return packet ntp_restored = self.cp3_handler.restore_pck(cp3_pck) complete_restored = packet[IP] / packet[UDP] / ntp_restored complete_restored[NTP] = ntp_restored return complete_restored else: self.log.error( 'The given packet was not matching the sender/receiver address!' ) return packet
def __restore_ntp_pck(self, ntp: NTP) -> NTP: self.log.debug('Send timestamp for reconstruction: ' + str(ntp.sent)) sent_time_stamp = datetime.fromtimestamp( ntplib.ntp_to_system_time(ntp.sent)) sent_time_stamp = sent_time_stamp.replace(year=datetime.now().year) sent_time_stamp_as_ntp = ntplib.system_to_ntp_time( sent_time_stamp.timestamp()) ntp.sent = sent_time_stamp_as_ntp self.log.debug('Send timestamp after reconstruction: ' + str(ntp.sent)) pck = CP3Package(ntp) if NTPMode.from_bit_string(pck.mode()) is NTPMode.CLIENT: self.log.debug("Restored in Client mode") ntp.ref = 0 ntp.orig = 0 ntp.recv = 0 if NTPMode.from_bit_string(pck.mode()) is NTPMode.SERVER \ or NTPMode.from_bit_string(pck.mode()) is NTPMode.BROADCAST_SERVER: self.log.debug("Restored in Server mode") origin_last_32 = pck.origin_timestamp()[32:64] received_last_32 = pck.receive_timestamp()[32:64] transmit_first_32 = pck.origin_timestamp()[0:32] pck.set_origin_timestamp(transmit_first_32 + origin_last_32) pck.set_receive_timestamp(transmit_first_32 + received_last_32) ntp = pck.ntp() self.log.debug("Reconstruction complete.") #ntp.show() return ntp
def test_restore_pck_server_pck_other_timestamps_filled_with_send(self): # Arrange client = CP3Handler('') pck = CP3Package() first_32 = '11111111111111111111000000011111' origin_last_32 = '11111111111110011110111111111111' received_last_32 = '11111111110111011110111111111111' transmit = '1111111111111111111111111111111111111111111111111111111111111111' pck.set_origin_timestamp(first_32 + origin_last_32) pck.set_receive_timestamp(first_32 + received_last_32) pck.set_transmit_timestamp(transmit) pck.set_mode(NTPMode.to_bit_string(NTPMode.SERVER)) # Act result_pck = RawNTP(client.restore_pck(pck)) # Assert self.assertNotEqual(result_pck.transmit_timestamp(), transmit) self.assertEqual(result_pck.origin_timestamp()[32:64], origin_last_32) self.assertEqual(result_pck.receive_timestamp()[32:64], received_last_32) self.assertEqual(result_pck.origin_timestamp()[0:32], result_pck.origin_timestamp()[0:32]) self.assertEqual(result_pck.receive_timestamp()[0:32], result_pck.origin_timestamp()[0:32]) self.assertEqual( datetime.fromtimestamp( ntplib.ntp_to_system_time(result_pck.ntp().sent)).year, datetime.now().year)
def restore_pck(self, pck: CP3Package) -> NTP: """ Restores the original values of the given CP3 packages, depending on the NTP mode. :param pck: :return: """ ntp = pck.ntp() return self.__restore_ntp_pck(ntp)
def test_get_cp3_mode_pck_is_none_mode_returned(self): # Arrange pck = CP3Package() # Act result = pck.get_cp3_mode() # Assert self.assertEqual(result, CP3Mode.NONE)
def test_set_cp3_mode_2_correct_datetime_set(self): # Arrange pck = CP3Package(ntp_pck=init_ntp_pck()) # Act pck.set_cp3_mode_2() # Assert time = datetime.fromtimestamp(ntplib.ntp_to_system_time(pck.ntp().sent)).year self.assertEqual(time, 2000)
def test_get_cp3_mode_pck_has_no_specific_year_none_mode_returned(self): # Arrange pck = CP3Package() pck.set_transmit_timestamp(ZEROS_64) # Act result = pck.get_cp3_mode() # Assert self.assertEqual(result, CP3Mode.NONE)
def test_handle_incoming_pck_is_not_marked_no_action_performed(self): # Arrange client = CP3Handler('') pck = CP3Package() # Act client.read_incoming_pck(pck) # Assert self.assertEqual('', client.msg)
def read_incoming_pck(self, pck: CP3Package) -> bool: mode = pck.get_cp3_mode() if mode is CP3Mode.PCK_1: self.msg = pck.extract_payload() self.log.debug("CP3_Mode_1 package received and payload set: " + self.msg) return True elif mode is CP3Mode.PCK_2: self.msg += pck.extract_payload() self.log.debug( "CP3_Mode_1 package received and complete payload now: " + self.msg) decrypted_bytes = decrypt_bits( self.msg, BitArray(bin=self.static_key_bits).bytes) self.log.info("Decrypted payload: " + str(decrypted_bytes)) return True else: self.log.debug("Package had no corresponding CP3 mode") return False
def test_get_cp3_mode_pck_has_year_2000_mode_2_returned(self): # Arrange ntp_timestamp = ntplib.system_to_ntp_time(datetime.now().replace(year=2000).timestamp()) ntp_pck = NTP() ntp_pck.sent = ntp_timestamp pck = CP3Package(ntp_pck=ntp_pck) # Act result = pck.get_cp3_mode() # Assert self.assertEqual(result, CP3Mode.PCK_2)
def create_cp3_pck(self, payload_bits: str, cp3_mode: CP3Mode, ntp_mode: NTPMode) -> NTP: assert len(payload_bits) == 64 pck = CP3Package() pck.add_payload(payload_bits) if cp3_mode is CP3Mode.PCK_1: pck.set_cp3_mode_1() elif cp3_mode is CP3Mode.PCK_2: pck.set_cp3_mode_2() pck.set_mode(NTPMode.to_bit_string(ntp_mode)) return pck.ntp()
def test_handle_incoming_pck_is_marked_with_1_msg_added(self): # Arrange client = CP3Handler('') ntp_timestamp = ntplib.system_to_ntp_time( datetime.now().replace(year=1995).timestamp()) ntp_pck = NTP() ntp_pck.sent = ntp_timestamp pck = CP3Package(ntp_pck=ntp_pck) origin_first_32 = '11111111111110011110111111111111' received_first_32 = '11111110111111011110111111111111' last_32 = '11111111111111111111111111111111' pck.set_origin_timestamp(origin_first_32 + last_32) pck.set_receive_timestamp(received_first_32 + last_32) # Act client.read_incoming_pck(pck) # Assert self.assertEqual(origin_first_32 + received_first_32, client.msg)
def test_restore_pck_client_pck_with_changed_year(self): # Arrange client = CP3Handler('') pck = init_ntp_pck() pck.sent = ntplib.system_to_ntp_time( datetime.fromtimestamp(ntplib.ntp_to_system_time( pck.sent)).replace(year=2006).timestamp()) # Act result_pck = client.restore_pck(CP3Package(ntp_pck=pck)) # Assert self.assertEqual(result_pck.ref, 0) self.assertEqual(result_pck.recv, 0) self.assertEqual(result_pck.orig, 0) self.assertEqual( datetime.fromtimestamp(ntplib.ntp_to_system_time( result_pck.sent)).year, datetime.now().year)