def discover(self, packet): """ Discover the sensitive data in the specified packet :param packet: The scapy packet :type packet: Packet """ assert isinstance(packet, Packet) self.current_packet = packet for layer in self.__packet_layers(packet): try: name = layer.__class__.__name__ layer_action = self.layers[name] layer_action.discover(layer) except Exception as e: if isinstance(e, ExplicitDropException): self.__report_increment_layer(name, 'explicit_drop', True) self.__report_increment_packet(packet, 'explicit_drop', True) elif isinstance(e, ImplicitDropException): self.__report_increment_layer(name, 'implicit_drop', True) self.__report_increment_packet(packet, 'implicit_drop', True) else: self.__report_increment_layer(name, 'error', True) self.__report_increment_packet(packet, 'error', True) raise_drop_exception(e, "layer = '{}'".format(name)) else: if layer_action.name == 'anonymize': self.__report_increment_layer(name, 'anonymized', True) elif layer_action.name == 'pass': self.__report_increment_layer(name, 'pass', True) self.__report_increment_packet(packet, 'anonymized', True) self.current_packet = Packet
def validate(self, layer): validation = list() for field in layer.fields.keys(): try: validation.append(self.__validate_field(layer, field)) except Exception as e: raise_drop_exception(e, "field = '{}'".format(field)) return '{}:\n {}'.format(layer.__class__.__name__, '\n '.join(validation))
def __discover_value(action, value): """ Discover the value with the specified action :param action: The action :type action: Action :param value: The value :type value: object """ value_type = type(value) value = value_type(value) # Convert the object to its type try: action.discover(value) except Exception as e: raise_drop_exception(e, "action = '{}', value = {}".format(action.name, repr(value)))
def __anonymize_value(action, value): """ Anonymize the value with the specified action :param action: The action :type action: Action :param value: The value :type value: object :return The anonymized value :rtype int | long | str """ value_type = type(value) value = value_type(value) # Convert the object to its type try: value = action.anonymize(value) except Exception as e: raise_drop_exception(e, "action = '{}', value = {}".format(action.name, repr(value))) else: if value is not None: value = value_type(value) # Cast to the original type return value
def validate(self, packet): """ Validate the sensitive data in the specified packet. The sensitive date will be replaced with a empty string. :param packet: The scapy packet :type packet: Packet :return: The packet under text format :rtype str """ assert isinstance(packet, Packet) self.current_packet = packet validation = list() for layer in self.__packet_layers(packet): try: name = layer.__class__.__name__ layer_validation = self.layers[name].validate(layer) if layer_validation is not None: validation.append(layer_validation) except Exception as e: raise_drop_exception(e, "layer = '{}'".format(name)) self.current_packet = None return '\n\n'.join(validation)
def anonymize(self, layer): for field in layer.fields.keys(): try: self.__anonymize_field(layer, field) except Exception as e: raise_drop_exception(e, "field = '{}'".format(field))
def discover(self, layer): for field in layer.fields.keys(): try: self.__discover_field(layer, field) except Exception as e: raise_drop_exception(e, "field = '{}'".format(field))