def mkDevice(screen, x, y, id): if id.isdigit(): subnet = Subnet(screen, x, y) subnet.IP = IP(int(id)) return subnet elif id == "N": dns = DNS(screen, x, y) dns.IP = str(ord(list(id)[0])) return dns elif id.isupper(): router = Router(screen, x, y) router.IP = str(ord(list(id)[0])) router.selected = router.IP == "66" return router elif id.islower(): if id == "h": host = Client(screen, x, y) host.name = "Alice" host.corespondent = "Bob" elif id == "x": host = Client(screen, x, y) host.name = "Bob" host.corespondent = "Alice" else: host = Host(screen, x ,y) host.IP = str(ord(list(id)[0])) return host else: print "Unrecognized unique identifier in sprite map" return None
def parseSubnets(subnet_list): """ <method internal="yes"> Helper function to convert a string-based subnet list to two tuples consisting of InetSubnet and InetSubnet6 instances. </method> """ import socket subnets = {socket.AF_INET: [], socket.AF_INET6: []} subnet_list = makeSequence(subnet_list) for item in subnet_list: if isinstance(item, basestring): subnet = Subnet.create(item) elif isinstance(item, Subnet): subnet = item else: raise ValueError, "Invalid subnet specification: value='%s'" % ( item, ) subnets[subnet.get_family()].append( (subnet.addr_packed(), subnet.netmask_packed())) return (tuple(subnets[socket.AF_INET]), tuple(subnets[socket.AF_INET6]))
def setAvailableSubnetAddress(self, subnetSpace, numSubnets, host, targetport, honeyPort, Strategy): if host.portNum != targetport: #normal host if Strategy == "even_dist": subnet = self.getAvaiSubnet(numSubnets) if Strategy == "crowded_dist": subnet = self.subnetList[1] #use same subnet for all hosts if Strategy == "random_dist": idx = random.randint(1, len(self.subnetList)) subnet = self.subnetList[idx] hostNum = self.getAvaiHostforSubnet(subnet) addr = subnetSpace[:-1] + str(subnet.number) + "." + str(hostNum) host.setDecIPAddr(addr) self.subnetList[subnet.number].hosts.append(host) elif host.portNum == honeyPort: #honeypot subnet = self.getAvaiSubnet(numSubnets) hostNum = self.getAvaiHostforSubnet(subnet) addr = subnetSpace[:-1] + str(subnet.number) + "." + str(hostNum) host.setDecIPAddr(addr) self.subnetList[subnet.number].hosts.append(host) else: subnet = Subnet(self.targetsubnet) self.subnetList[self.targetsubnet] = subnet hostNum = self.getAvaiHostforSubnet(subnet) addr = subnetSpace[:-1] + str(subnet.number) + "." + str(hostNum) host.setDecIPAddr(addr) return host
def getAvaiSubnet(self, numSubnets): ns = len(self.subnetList.keys()) if ns < numSubnets: self.upperSubnet += 1 subnet = self.upperSubnet self.up = False sub = Subnet(subnet) self.subnetList[subnet] = sub else: subnetId = -1 minHosts = 99 for k in self.subnetList.keys(): subnetHosts = self.subnetList[k].hosts if len(subnetHosts) < minHosts: minHosts = len(subnetHosts) subnetId = k sub = self.subnetList[subnetId] return sub
def setAvailableSubnetAddress(self, subnetSpace, numSubnets, host, targetport): if host.portNum != targetport: #normal host subnet = self.getAvaiSubnet(numSubnets) #subnet=self.subnetList[1] #use same subnet for all hosts hostNum = self.getAvaiHostforSubnet(subnet) addr = subnetSpace[:-1] + str(subnet.number) + "." + str(hostNum) host.setDecIPAddr(addr) self.subnetList[subnet.number].hosts.append(host) elif host.portNum == 5: #honeypot subnet = self.getAvaiSubnet(numSubnets) hostNum = self.getAvaiHostforSubnet(subnet) addr = subnetSpace[:-1] + str(subnet.number) + "." + str(hostNum) host.setDecIPAddr(addr) self.subnetList[subnet.number].hosts.append(host) else: subnet = Subnet(self.targetsubnet) self.subnetList[self.targetsubnet] = subnet hostNum = self.getAvaiHostforSubnet(subnet) addr = subnetSpace[:-1] + str(subnet.number) + "." + str(hostNum) host.setDecIPAddr(addr) return host
def parseSubnets(subnet_list): """ Helper function to convert a string-based subnet list to two tuples consisting of InetSubnet and InetSubnet6 instances. """ import socket subnets = { socket.AF_INET: [], socket.AF_INET6: [] } subnet_list = makeSequence(subnet_list) for item in subnet_list: if isinstance(item, basestring): subnet = Subnet.create(item) elif isinstance(item, Subnet): subnet = item else: raise ValueError, "Invalid subnet specification: value='%s'" % (item,) subnets[subnet.get_family()].append((subnet.addr_packed(), subnet.netmask_packed())) return (tuple(subnets[socket.AF_INET]), tuple(subnets[socket.AF_INET6]))
from Subnet import Subnet from IpAddress import AzureIpAddress from ApplicationSecurityGroup import ApplicationSecurityGroup from NetworkSecurityRule import NetworkSecurityRule from NetworkEnvironment import NetworkEnvironment, Env if __name__ == '__main__': dev_env = NetworkEnvironment(Env.DEV) print( f"Subscription {dev_env.subscription_name} [id: {dev_env.subscription_id}]" ) app_subnet = Subnet(dev_env, "app-subnet") an_ip_addr = AzureIpAddress(dev_env, "10.0.0.10") an_asg = ApplicationSecurityGroup(dev_env, "asg-rg", "some-asg") if (an_ip_addr.is_in_range("10.0.0.0/24")): print("IP Address is in range") else: print("IP Address not in range!") print(app_subnet.ip_range) nsg_in = NetworkSecurityRule("allow-ssh", "Just a test security rule", 100, an_asg, app_subnet, "TCP", 22, "Inbound") print(f"Network Security Rule:\n {nsg_in.to_json_object()}")
def __init__(self,name, config): self.subnets = [Subnet(spec) for spec in config['subnets']] self.current_subnet = None self.name = name self.config = config info(self)
def __init__(self, para): Subnet.__init__(self, para) self.layerList = [] self.fork = Fork2({'instanceName': para['instanceName'] + '_fork'}) self.layerList.append(self.fork) self.skipMode = para['skipMode'] if self.skipMode == 'conv': convPara4 = { 'instanceName': para['instanceName'] + '_skipConv1', 'padding': False, 'padShape': (0, 0), 'stride': para['skipStride'], 'outChannel': para['outChannel3'], 'kernelShape': (1, 1), 'bias': False } self.skipConv = Conv2D(convPara4) self.skipNorm = Normalize( {'instanceName': para['instanceName'] + '_skipNorm'}) self.skipScale = Scale( {'instanceName': para['instanceName'] + '_skipScale'}) self.layerList.append(self.skipConv) self.layerList.append(self.skipNorm) self.layerList.append(self.skipScale) convPara1 = { 'instanceName': para['instanceName'] + '_mainConv1', 'padding': False, 'padShape': (0, 0), 'stride': para['stride1'], 'outChannel': para['outChannel1'], 'kernelShape': (1, 1), 'bias': False } convPara2 = { 'instanceName': para['instanceName'] + '_mainConv2', 'padding': True, 'padShape': (1, 1), 'stride': 1, 'outChannel': para['outChannel2'], 'kernelShape': (3, 3), 'bias': False } convPara3 = { 'instanceName': para['instanceName'] + '_mainConv3', 'padding': False, 'padShape': (0, 0), 'stride': 1, 'outChannel': para['outChannel3'], 'kernelShape': (1, 1), 'bias': False } self.mainConv1 = Conv2D(convPara1) self.mainNorm1 = Normalize( {'instanceName': para['instanceName'] + '_mainNorm1'}) self.mainScale1 = Scale( {'instanceName': para['instanceName'] + '_mainScale1'}) self.mainActivation1 = Activation({ 'instanceName': para['instanceName'] + '_mainReLU1', 'activationType': para['activationType'] }) self.layerList.append(self.mainConv1) self.layerList.append(self.mainNorm1) self.layerList.append(self.mainScale1) self.layerList.append(self.mainActivation1) self.mainConv2 = Conv2D(convPara2) self.mainNorm2 = Normalize( {'instanceName': para['instanceName'] + '_mainNorm2'}) self.mainScale2 = Scale( {'instanceName': para['instanceName'] + '_mainScale2'}) self.mainActivation2 = Activation({ 'instanceName': para['instanceName'] + '_mainReLU2', 'activationType': para['activationType'] }) self.layerList.append(self.mainConv2) self.layerList.append(self.mainNorm2) self.layerList.append(self.mainScale2) self.layerList.append(self.mainActivation2) self.mainConv3 = Conv2D(convPara3) self.mainNorm3 = Normalize( {'instanceName': para['instanceName'] + '_mainNorm3'}) self.mainScale3 = Scale( {'instanceName': para['instanceName'] + '_mainScale3'}) self.layerList.append(self.mainConv3) self.layerList.append(self.mainNorm3) self.layerList.append(self.mainScale3) self.sum = Sum2({'instanceName': para['instanceName'] + '_sum'}) self.activation3 = Activation({ 'instanceName': para['instanceName'] + '_outReLU3', 'activationType': para['activationType'] }) self.layerList.append(self.sum) self.layerList.append(self.activation3) self.bottomInterface = self.fork self.topInterface = self.activation3
def calculate(ip, sm): subnet = Subnet() ip_parts = ip.split('.') sm_parts = sm.split('.') for i in range(len(ip_parts)): octet_ip = int(ip_parts[i]) octet_sm = int(sm_parts[i]) """Converting dec to bin""" bin_octet_ip = bin(octet_ip) bin_octet_sm = bin(octet_sm) """Delete 0b which is set before binary value""" str_bin_octet_ip = str(bin_octet_ip).replace('0b', '') str_bin_octet_sm = str(bin_octet_sm).replace('0b', '') """filling bin becomes 8 bits""" str_bin_octet_ip = fill_byte(str_bin_octet_ip) str_bin_octet_sm = fill_byte(str_bin_octet_sm) """Calculate network address""" real_ip = compare_bits(str_bin_octet_ip, str_bin_octet_sm) bin_octet_ip = convert_binstr_to_dec(real_ip) str_dec_octet_ip = str(bin_octet_ip).replace(".0", "") subnet.bin_network = subnet.bin_network + real_ip subnet.dec_network = subnet.dec_network + str_dec_octet_ip """Calculate broadcast address""" bin_broadcast = '' if str_bin_octet_sm != '11111111': if str_bin_octet_sm != '00000000': tail_part = str_bin_octet_sm.replace('1', '') bin_broadcast = turn_to_highest(tail_part, real_ip) else: bin_broadcast = '11111111' else: bin_broadcast = real_ip subnet.bin_broadcast += bin_broadcast subnet.dec_broadcast += str( convert_binstr_to_dec(bin_broadcast)).replace(".0", "") """Calculate first address""" if i == len(ip_parts) - 1: subnet.bin_first_host += bin_plus_one(real_ip) subnet.dec_first_host += str( convert_binstr_to_dec(bin_plus_one(real_ip))).replace( ".0", "") else: subnet.bin_first_host += real_ip subnet.dec_first_host += str( convert_binstr_to_dec(real_ip)).replace(".0", "") """Calculate last address""" if i == len(ip_parts) - 1: subnet.bin_last_host += bin_nega_one(bin_broadcast) subnet.dec_last_host += str( convert_binstr_to_dec(bin_nega_one(bin_broadcast))).replace( ".0", "") else: subnet.bin_last_host += bin_broadcast subnet.dec_last_host += str( convert_binstr_to_dec(bin_broadcast)).replace(".0", "") if i < len(ip_parts) - 1: subnet.bin_network = subnet.bin_network + '.' subnet.dec_network = subnet.dec_network + '.' subnet.bin_broadcast = subnet.bin_broadcast + '.' subnet.dec_broadcast += '.' subnet.bin_first_host += '.' subnet.dec_first_host += '.' subnet.bin_last_host += '.' subnet.dec_last_host += '.' print('Network: ' + subnet.dec_network + ' , ' + subnet.bin_network) print('Broadcast: ' + subnet.dec_broadcast + ' , ' + subnet.bin_broadcast) print('First host: ' + subnet.dec_first_host + ' , ' + subnet.bin_first_host) print('Last host: ' + subnet.dec_last_host + ' , ' + subnet.bin_last_host)