def remove_dot1_layer(self, packet): self.test.instance().assertEqual(type(packet), Ether) self.test.instance().assertEqual(packet.type, self.DOT1AD_TYPE) packet.type = self.DOT1Q_TYPE packet = Ether(str(packet)) payload = packet.payload self.test.instance().assertEqual(type(payload), Dot1Q) self.test.instance().assertEqual(payload.vlan, self.outer_vlan) payload = payload.payload self.test.instance().assertEqual(type(payload), Dot1Q) self.test.instance().assertEqual(payload.vlan, self.inner_vlan) payload = payload.payload packet.remove_payload() packet.add_payload(payload) return packet
def to_code(self): """ Convert to Python code as profile """ layer = Ether(self.pkt) layer.hide_defaults() # remove fields with default values imports_arr = [] layers_commands = [] # remove checksums, add imports if needed while layer: layer_class = layer.__class__.__name__ if layer_class not in vars(scapy.layers.all): # custom import found_import = False for module_path, module in list(sys.modules.items()): if not module_path.startswith( ('scapy.layers', 'scapy.contrib')): continue import_string = 'from %s import %s' % (module_path, layer_class) if import_string in imports_arr: # already present in extra imports found_import = True break if hasattr(module, layer_class): # add as extra import imports_arr.append(import_string) found_import = True break if not found_import: raise STLError('Could not determine import of layer %s' % layer.name) # remove checksums for chksum_name in ('cksum', 'chksum'): if chksum_name in layer.fields: del layer.fields[chksum_name] # remove Paddings(FCS etc.) if isinstance(layer, 'Padding'): break payload = layer.payload layer.remove_payload() if isinstance(layer, Raw): payload_data = bytes(layer) # compact form Raw('x' * 100) etc. if payload_data == payload_data[0:1] * len(payload_data): layer_command = '%s * %s)' % (Raw( payload_data[0:1]).command().rstrip(')'), len(payload_data)) else: layer_command = layer.command() layers_commands.append(layer_command) break layers_commands.append(layer.command()) layer = payload imports = '\n'.join(imports_arr) packet_code = 'packet =(' + \ (' / \n ').join(layers_commands) + ')' vm_list = [] for inst in self.fields['vm']['instructions']: if inst['type'] == 'flow_var': vm_list.append( "STLVmFlowVar(name='{name}', size={size}, op='{op}', init_value={init_value}," + "min_value={min_value}, max_value={max_value}, step={step})" .format(**inst)) elif inst['type'] == 'write_flow_var': vm_list.append( "STLVmWrFlowVar(fv_name='{name}', pkt_offset={pkt_offset}, " + "add_val={add_value}, is_big={is_big_endian})".format( **inst)) elif inst['type'] == 'write_mask_flow_var': inst = copy.copy(inst) inst['mask'] = hex(inst['mask']) vm_list.append(( "STLVmWrMaskFlowVar(fv_name='{name}', pkt_offset={pkt_offset}, " + "pkt_cast_size={pkt_cast_size}," + "mask={mask}, shift={shift}, add_value={add_value}, " + "is_big={is_big_endian})").format(**inst)) elif inst['type'] == 'fix_checksum_ipv4': vm_list.append( "STLVmFixIpv4(offset={pkt_offset})".format(**inst)) elif inst['type'] == 'trim_pkt_size': vm_list.append( "STLVmTrimPktSize(fv_name='{name}')".format(**inst)) elif inst['type'] == 'tuple_flow_var': inst = copy.copy(inst) inst['ip_min'] = ltoa(inst['ip_min']) inst['ip_max'] = ltoa(inst['ip_max']) vm_list.append( ("STLVmTupleGen(name='{name}', ip_min='{ip_min}', " + "ip_max='{ip_max}', port_min={port_min}, " + "port_max={port_max}, limit_flows={limit_flows}, " + "flags={flags})").format(**inst)) elif inst['type'] == 'flow_var_rand_limit': vm_list.append( "STLVmFlowVarRepetableRandom(name='{name}', size={size}, limit={limit}, " + "seed={seed}, min_value={min_value}, max_value={max_value})" .format(**inst)) vm_code = 'vm = STLScVmRaw([' + \ ',\n '.join(vm_list) + '])' stream_params_list = [] stream_params_list.append( 'packet = STLPktBuilder(pkt = packet, vm = vm)') if default_STLStream.name != self.name: stream_params_list.append('name = %s' % STLStream.__add_quotes(self.name)) if default_STLStream.fields['enabled'] != self.fields['enabled']: stream_params_list.append('enabled = %s' % self.fields['enabled']) if default_STLStream.fields['self_start'] != self.fields['self_start']: stream_params_list.append('self_start = %s' % self.fields['self_start']) if default_STLStream.fields['isg'] != self.fields['isg']: stream_params_list.append('isg = %s' % self.fields['isg']) if default_STLStream.fields['flow_stats'] != self.fields['flow_stats']: stream_params_list.append('flow_stats = STLFlowStats(%s)' % self.fields['flow_stats']['stream_id']) if default_STLStream.__next__ != self.__next__: stream_params_list.append('next = %s' % STLStream.__add_quotes(self.__next__)) if default_STLStream.id != self.id: stream_params_list.append('stream_id = %s' % self.id) if default_STLStream.fields['action_count'] != self.fields[ 'action_count']: stream_params_list.append('action_count = %s' % self.fields['action_count']) if 'random_seed' in self.fields: stream_params_list.append('random_seed = %s' % self.fields.get('random_seed', 0)) if default_STLStream.mac_src_override_by_pkt != self.mac_src_override_by_pkt: stream_params_list.append('mac_src_override_by_pkt = %s' % self.mac_src_override_by_pkt) if default_STLStream.mac_dst_override_mode != self.mac_dst_override_mode: stream_params_list.append('mac_dst_override_mode = %s' % self.mac_dst_override_mode) mode_args = '' for key, value in list(self.fields['mode'].items()): if key not in ('rate', 'type'): mode_args += '%s = %s, ' % (key, value) mode_args += '%s = %s' % (self.fields['mode']['rate']['type'], self.fields['mode']['rate']['value']) if self.mode_desc == STLTXCont.__str__(): stream_params_list.append('mode = STLTXCont(%s)' % mode_args) elif self.mode_desc == STLTXSingleBurst().__str__(): stream_params_list.append('mode = STLTXSingleBurst(%s)' % mode_args) elif self.mode_desc == STLTXMultiBurst().__str__(): stream_params_list.append('mode = STLTXMultiBurst(%s)' % mode_args) else: raise STLError('Could not determine mode: %s' % self.mode_desc) stream = "stream = STLStream(" + \ ',\n '.join(stream_params_list) + ')' return '\n'.join([imports, packet_code, vm_code, stream])