openc2lib.types.targettypes

OpenC2 target types

Definition of the target types in the OpenC2 (Sec. 3.4.1). The naming strictly follows the definition of the Language Specification as close as possible. The relevant exception is represented by hyphens that are always dropped.

  1""" OpenC2 target types
  2
  3	Definition of the target types in the OpenC2 (Sec. 3.4.1).
  4	The naming strictly follows the definition of the Language Specification
  5	as close as possible. The relevant exception is represented by hyphens
  6	that are always dropped.
  7"""
  8
  9import dataclasses
 10import ipaddress
 11
 12import openc2lib.types.basetypes
 13import openc2lib.types.datatypes
 14
 15from openc2lib.core.target import Targets
 16
 17
 18class IPv4Net:
 19	"""OpenC2 IPv4 Address Range
 20		
 21		IPv4 Address Range as defined in Sec. 3.4.1.9.
 22
 23		The Standard is not clear on this part. The 
 24		IPv4Net Target is defined as "Array /ipv4-net"
 25		(where ipv4-net --lowercase!-- is never defined!)
 26		However, the json serialization requirements explicitely
 27		define:
 28		Array /ipv4-net: JSON string containing the text representation 
 29		 						of an IPv4 address range as specified in 
 30		 						[RFC4632], Section 3.1.
 31		According to this definition, I assume a single network address
 32		should be managed. Extension to an array of IP network addresses
 33		is rather straightforward by using a list for ipv4_net attribute.
 34		Note that I have to keep both the string representation of the
 35		network address as well as the IPv4Network object to easily 
 36		manage the code and to automate the creation of the dictionary.
 37		
 38	"""
 39#ipv4_net: str
 40	
 41	def __init__(self, ipv4_net=None, prefix=None):
 42		""" Initialize IPv4 Address Range
 43
 44			Initialize `IPv4Net with IPv4 address and prefix.
 45			If no IPv4 address is given, initialize to null address.
 46			If no prefix is given, assume /32 (iPv4 address only).
 47			:param ipv4_net: IPv4 Network Address.
 48			:param prefix: IPv4 Network Adress Prefix.
 49		"""
 50		if ipv4_net is None:
 51		    net = ipaddress.IPv4Network("0.0.0.0/0")
 52		elif prefix is None:
 53		    net = ipaddress.IPv4Network(ipv4_net)
 54		else:
 55		    tmp = ipv4_net + "/" + str(prefix)
 56		    net = ipaddress.IPv4Network(tmp)
 57
 58		self.ipv4_net = net.exploded
 59	
 60	def addr(self):
 61		""" Returns address part only (no prefix) """
 62		return ipaddress.IPv4Network(self.ipv4_net).network_address.exploded
 63	
 64	def prefix(self):
 65		""" Returns prefix only """
 66		return ipaddress.IPv4Network(self.ipv4_net).prefixlen
 67	
 68	def __str__(self):
 69	    return ipaddress.IPv4Network(self.ipv4_net).exploded
 70	
 71	def __repr__(self):
 72	    return ipaddress.IPv4Network(self.ipv4_net).exploded
 73
 74
 75@dataclasses.dataclass
 76class IPv4Connection(openc2lib.types.basetypes.Record):
 77	"""OpenC2 IPv4 Connection
 78		
 79		IPv4 Connection including IPv4 addressed, protocol, and port numbers, as defined in Sec. 3.4.1.10.
 80	"""
 81	src_addr: IPv4Net = None
 82	""" Source address """
 83	src_port: int = None
 84	""" Source port """
 85	dst_addr: IPv4Net = None
 86	""" Destination address """
 87	dst_port: int = None
 88	""" Destination port """
 89	protocol: openc2lib.types.datatypes.L4Protocol = None
 90	""" L4 protocol """
 91
 92	def __repr__(self):
 93		return (f"IPv4Connection(src='{self.src_addr}', sport={self.src_port}, "
 94	             f"dst='{self.dst_addr}', dport={self.dst_port}, protocol='{self.protocol}')")
 95	
 96	def __str__(self):
 97		return f"IPv4Connection(" \
 98	            f"src={self.src_addr}, " \
 99	            f"dst={self.dst_addr}, " \
100	            f"protocol={self.protocol}, " \
101	            f"src_port={self.src_port}, " \
102	            f"st_port={self.dst_port})"
103
104class Features(openc2lib.types.basetypes.ArrayOf(openc2lib.types.datatypes.Feature)):
105	""" OpenC2 Features
106
107		Implements the Features target (Section 3.4.1.5).
108		Just defines an `ArrayOf` `Feature`.
109	"""
110# TODO: implmement control on the max number of elements
111	pass
112
113
114
115# Register the list of available Targets
116Targets.add('features', Features, 9)
117Targets.add('ipv4_net', IPv4Net, 13)
118Targets.add('ipv4_connection', IPv4Connection, 15)
class IPv4Net:
19class IPv4Net:
20	"""OpenC2 IPv4 Address Range
21		
22		IPv4 Address Range as defined in Sec. 3.4.1.9.
23
24		The Standard is not clear on this part. The 
25		IPv4Net Target is defined as "Array /ipv4-net"
26		(where ipv4-net --lowercase!-- is never defined!)
27		However, the json serialization requirements explicitely
28		define:
29		Array /ipv4-net: JSON string containing the text representation 
30		 						of an IPv4 address range as specified in 
31		 						[RFC4632], Section 3.1.
32		According to this definition, I assume a single network address
33		should be managed. Extension to an array of IP network addresses
34		is rather straightforward by using a list for ipv4_net attribute.
35		Note that I have to keep both the string representation of the
36		network address as well as the IPv4Network object to easily 
37		manage the code and to automate the creation of the dictionary.
38		
39	"""
40#ipv4_net: str
41	
42	def __init__(self, ipv4_net=None, prefix=None):
43		""" Initialize IPv4 Address Range
44
45			Initialize `IPv4Net with IPv4 address and prefix.
46			If no IPv4 address is given, initialize to null address.
47			If no prefix is given, assume /32 (iPv4 address only).
48			:param ipv4_net: IPv4 Network Address.
49			:param prefix: IPv4 Network Adress Prefix.
50		"""
51		if ipv4_net is None:
52		    net = ipaddress.IPv4Network("0.0.0.0/0")
53		elif prefix is None:
54		    net = ipaddress.IPv4Network(ipv4_net)
55		else:
56		    tmp = ipv4_net + "/" + str(prefix)
57		    net = ipaddress.IPv4Network(tmp)
58
59		self.ipv4_net = net.exploded
60	
61	def addr(self):
62		""" Returns address part only (no prefix) """
63		return ipaddress.IPv4Network(self.ipv4_net).network_address.exploded
64	
65	def prefix(self):
66		""" Returns prefix only """
67		return ipaddress.IPv4Network(self.ipv4_net).prefixlen
68	
69	def __str__(self):
70	    return ipaddress.IPv4Network(self.ipv4_net).exploded
71	
72	def __repr__(self):
73	    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)
42	def __init__(self, ipv4_net=None, prefix=None):
43		""" Initialize IPv4 Address Range
44
45			Initialize `IPv4Net with IPv4 address and prefix.
46			If no IPv4 address is given, initialize to null address.
47			If no prefix is given, assume /32 (iPv4 address only).
48			:param ipv4_net: IPv4 Network Address.
49			:param prefix: IPv4 Network Adress Prefix.
50		"""
51		if ipv4_net is None:
52		    net = ipaddress.IPv4Network("0.0.0.0/0")
53		elif prefix is None:
54		    net = ipaddress.IPv4Network(ipv4_net)
55		else:
56		    tmp = ipv4_net + "/" + str(prefix)
57		    net = ipaddress.IPv4Network(tmp)
58
59		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.
ipv4_net
def addr(self):
61	def addr(self):
62		""" Returns address part only (no prefix) """
63		return ipaddress.IPv4Network(self.ipv4_net).network_address.exploded

Returns address part only (no prefix)

def prefix(self):
65	def prefix(self):
66		""" Returns prefix only """
67		return ipaddress.IPv4Network(self.ipv4_net).prefixlen

Returns prefix only

@dataclasses.dataclass
class IPv4Connection(openc2lib.types.basetypes.Record):
 76@dataclasses.dataclass
 77class IPv4Connection(openc2lib.types.basetypes.Record):
 78	"""OpenC2 IPv4 Connection
 79		
 80		IPv4 Connection including IPv4 addressed, protocol, and port numbers, as defined in Sec. 3.4.1.10.
 81	"""
 82	src_addr: IPv4Net = None
 83	""" Source address """
 84	src_port: int = None
 85	""" Source port """
 86	dst_addr: IPv4Net = None
 87	""" Destination address """
 88	dst_port: int = None
 89	""" Destination port """
 90	protocol: openc2lib.types.datatypes.L4Protocol = None
 91	""" L4 protocol """
 92
 93	def __repr__(self):
 94		return (f"IPv4Connection(src='{self.src_addr}', sport={self.src_port}, "
 95	             f"dst='{self.dst_addr}', dport={self.dst_port}, protocol='{self.protocol}')")
 96	
 97	def __str__(self):
 98		return f"IPv4Connection(" \
 99	            f"src={self.src_addr}, " \
100	            f"dst={self.dst_addr}, " \
101	            f"protocol={self.protocol}, " \
102	            f"src_port={self.src_port}, " \
103	            f"st_port={self.dst_port})"

OpenC2 IPv4 Connection

IPv4 Connection including IPv4 addressed, protocol, and port numbers, as defined in Sec. 3.4.1.10.

IPv4Connection( src_addr: IPv4Net = None, src_port: int = None, dst_addr: IPv4Net = None, dst_port: int = None, protocol: openc2lib.types.datatypes.L4Protocol = None)
src_addr: IPv4Net = None

Source address

src_port: int = None

Source port

dst_addr: IPv4Net = None

Destination address

dst_port: int = None

Destination port

L4 protocol

105class Features(openc2lib.types.basetypes.ArrayOf(openc2lib.types.datatypes.Feature)):
106	""" OpenC2 Features
107
108		Implements the Features target (Section 3.4.1.5).
109		Just defines an `ArrayOf` `Feature`.
110	"""
111# TODO: implmement control on the max number of elements
112	pass

OpenC2 Features

Implements the Features target (Section 3.4.1.5). Just defines an ArrayOf Feature.

Inherited Members
builtins.list
list
clear
copy
append
insert
extend
pop
remove
index
count
reverse
sort
openc2lib.types.basetypes.ArrayOf.__new__..ArrayOf
fieldtype
fromdict
openc2lib.types.basetypes.Array
fieldtypes
todict