openc2lib.types.targets.ipv4_net

 1import ipaddress
 2
 3import openc2lib.types.data
 4from openc2lib.core.target import target
 5
 6@target('ipv4_net')
 7class IPv4Net:
 8	"""OpenC2 IPv4 Address Range
 9		
10		IPv4 Address Range as defined in Sec. 3.4.1.9.
11
12		The Standard is not clear on this part. The 
13		IPv4Net Target is defined as "Array /ipv4-net"
14		(where ipv4-net --lowercase!-- is never defined!)
15		However, the json serialization requirements explicitely
16		define:
17		Array /ipv4-net: JSON string containing the text representation 
18		 						of an IPv4 address range as specified in 
19		 						[RFC4632], Section 3.1.
20		According to this definition, I assume a single network address
21		should be managed. Extension to an array of IP network addresses
22		is rather straightforward by using a list for ipv4_net attribute.
23		Note that I have to keep both the string representation of the
24		network address as well as the IPv4Network object to easily 
25		manage the code and to automate the creation of the dictionary.
26		
27	"""
28#ipv4_net: str
29	
30	def __init__(self, ipv4_net=None, prefix=None):
31		""" Initialize IPv4 Address Range
32
33			Initialize `IPv4Net with IPv4 address and prefix.
34			If no IPv4 address is given, initialize to null address.
35			If no prefix is given, assume /32 (iPv4 address only).
36			:param ipv4_net: IPv4 Network Address.
37			:param prefix: IPv4 Network Adress Prefix.
38		"""
39		if ipv4_net is None:
40		    net = ipaddress.IPv4Network("0.0.0.0/0")
41		elif prefix is None:
42		    net = ipaddress.IPv4Network(ipv4_net)
43		else:
44		    tmp = ipv4_net + "/" + str(prefix)
45		    net = ipaddress.IPv4Network(tmp)
46
47		self.__ipv4_net = net.exploded
48	
49	def addr(self):
50		""" Returns address part only (no prefix) """
51		return ipaddress.IPv4Network(self.__ipv4_net).network_address.exploded
52	
53	def prefix(self):
54		""" Returns prefix only """
55		return ipaddress.IPv4Network(self.__ipv4_net).prefixlen
56	
57	def __str__(self):
58	    return ipaddress.IPv4Network(self.__ipv4_net).exploded
59	
60	def __repr__(self):
61	    return ipaddress.IPv4Network(self.__ipv4_net).exploded
@target('ipv4_net')
class IPv4Net:
 7@target('ipv4_net')
 8class IPv4Net:
 9	"""OpenC2 IPv4 Address Range
10		
11		IPv4 Address Range as defined in Sec. 3.4.1.9.
12
13		The Standard is not clear on this part. The 
14		IPv4Net Target is defined as "Array /ipv4-net"
15		(where ipv4-net --lowercase!-- is never defined!)
16		However, the json serialization requirements explicitely
17		define:
18		Array /ipv4-net: JSON string containing the text representation 
19		 						of an IPv4 address range as specified in 
20		 						[RFC4632], Section 3.1.
21		According to this definition, I assume a single network address
22		should be managed. Extension to an array of IP network addresses
23		is rather straightforward by using a list for ipv4_net attribute.
24		Note that I have to keep both the string representation of the
25		network address as well as the IPv4Network object to easily 
26		manage the code and to automate the creation of the dictionary.
27		
28	"""
29#ipv4_net: str
30	
31	def __init__(self, ipv4_net=None, prefix=None):
32		""" Initialize IPv4 Address Range
33
34			Initialize `IPv4Net with IPv4 address and prefix.
35			If no IPv4 address is given, initialize to null address.
36			If no prefix is given, assume /32 (iPv4 address only).
37			:param ipv4_net: IPv4 Network Address.
38			:param prefix: IPv4 Network Adress Prefix.
39		"""
40		if ipv4_net is None:
41		    net = ipaddress.IPv4Network("0.0.0.0/0")
42		elif prefix is None:
43		    net = ipaddress.IPv4Network(ipv4_net)
44		else:
45		    tmp = ipv4_net + "/" + str(prefix)
46		    net = ipaddress.IPv4Network(tmp)
47
48		self.__ipv4_net = net.exploded
49	
50	def addr(self):
51		""" Returns address part only (no prefix) """
52		return ipaddress.IPv4Network(self.__ipv4_net).network_address.exploded
53	
54	def prefix(self):
55		""" Returns prefix only """
56		return ipaddress.IPv4Network(self.__ipv4_net).prefixlen
57	
58	def __str__(self):
59	    return ipaddress.IPv4Network(self.__ipv4_net).exploded
60	
61	def __repr__(self):
62	    return ipaddress.IPv4Network(self.__ipv4_net).exploded

OpenC2 IPv4 Address Range

IPv4 Address Range as defined in Sec. 3.4.1.9.

The Standard is not clear on this part. The IPv4Net Target is defined as "Array /ipv4-net" (where ipv4-net --lowercase!-- is never defined!) However, the json serialization requirements explicitely define: Array /ipv4-net: JSON string containing the text representation of an IPv4 address range as specified in [RFC4632], Section 3.1. According to this definition, I assume a single network address should be managed. Extension to an array of IP network addresses is rather straightforward by using a list for ipv4_net attribute. Note that I have to keep both the string representation of the network address as well as the IPv4Network object to easily manage the code and to automate the creation of the dictionary.

IPv4Net(ipv4_net=None, prefix=None)
31	def __init__(self, ipv4_net=None, prefix=None):
32		""" Initialize IPv4 Address Range
33
34			Initialize `IPv4Net with IPv4 address and prefix.
35			If no IPv4 address is given, initialize to null address.
36			If no prefix is given, assume /32 (iPv4 address only).
37			:param ipv4_net: IPv4 Network Address.
38			:param prefix: IPv4 Network Adress Prefix.
39		"""
40		if ipv4_net is None:
41		    net = ipaddress.IPv4Network("0.0.0.0/0")
42		elif prefix is None:
43		    net = ipaddress.IPv4Network(ipv4_net)
44		else:
45		    tmp = ipv4_net + "/" + str(prefix)
46		    net = ipaddress.IPv4Network(tmp)
47
48		self.__ipv4_net = net.exploded

Initialize IPv4 Address Range

Initialize `IPv4Net with IPv4 address and prefix. If no IPv4 address is given, initialize to null address. If no prefix is given, assume /32 (iPv4 address only).

Parameters
  • ipv4_net: IPv4 Network Address.
  • prefix: IPv4 Network Adress Prefix.
def addr(self):
50	def addr(self):
51		""" Returns address part only (no prefix) """
52		return ipaddress.IPv4Network(self.__ipv4_net).network_address.exploded

Returns address part only (no prefix)

def prefix(self):
54	def prefix(self):
55		""" Returns prefix only """
56		return ipaddress.IPv4Network(self.__ipv4_net).prefixlen

Returns prefix only