stix2arango 1.1.3__py3-none-any.whl → 1.1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of stix2arango might be problematic. Click here for more details.

stix2arango/__main__.py CHANGED
@@ -1,27 +1,81 @@
1
1
  import argparse
2
2
  from stix2arango.stix2arango import Stix2Arango
3
3
 
4
+
4
5
  def parse_bool(value: str):
5
6
  value = value.lower()
6
7
  # ["false", "no", "n"]
7
8
  return value in ["yes", "y", "true", "1"]
8
9
 
10
+ def parse_ref(value: str):
11
+ if not (value.endswith('_ref') or value.endswith('_refs')):
12
+ raise argparse.ArgumentTypeError('value must end with _ref or _refs')
13
+ return value
14
+
15
+
9
16
  def parse_arguments():
10
17
  parser = argparse.ArgumentParser(description="Import STIX JSON into ArangoDB")
11
18
  parser.add_argument("--file", required=True, help="Path to STIX JSON file")
12
- parser.add_argument("--is_large_file", action="store_true", help="Use large file mode [Use this mode when the bundle is very large, this will enable you stix2arango to chunk before loading into memory]")
19
+ parser.add_argument(
20
+ "--is_large_file",
21
+ action="store_true",
22
+ help="Use large file mode [Use this mode when the bundle is very large, this will enable you stix2arango to chunk before loading into memory]",
23
+ )
13
24
  parser.add_argument("--database", required=True, help="ArangoDB database name")
14
- parser.add_argument("--create_db", default=True, type=parse_bool, help="whether or not to skip the creation of database, requires admin permission")
25
+ parser.add_argument(
26
+ "--create_db",
27
+ default=True,
28
+ type=parse_bool,
29
+ help="whether or not to skip the creation of database, requires admin permission",
30
+ )
15
31
  parser.add_argument("--collection", required=True, help="ArangoDB collection name")
16
- parser.add_argument("--stix2arango_note", required=False, help="Note for the import", default="")
17
- parser.add_argument("--ignore_embedded_relationships", required=False, help="Ignore Embedded Relationship for the import", type=parse_bool, default=False)
18
- parser.add_argument("--ignore_embedded_relationships_sro", required=False, help="Ignore Embedded Relationship for imported SROs", type=parse_bool, default=False)
19
- parser.add_argument("--ignore_embedded_relationships_smo", required=False, help="Ignore Embedded Relationship for imported SMOs", type=parse_bool, default=False)
20
-
32
+ parser.add_argument(
33
+ "--stix2arango_note", required=False, help="Note for the import", default=""
34
+ )
35
+ parser.add_argument(
36
+ "--ignore_embedded_relationships",
37
+ required=False,
38
+ help="Ignore Embedded Relationship for the import",
39
+ type=parse_bool,
40
+ default=False,
41
+ )
42
+ parser.add_argument(
43
+ "--ignore_embedded_relationships_sro",
44
+ required=False,
45
+ help="Ignore Embedded Relationship for imported SROs",
46
+ type=parse_bool,
47
+ default=False,
48
+ )
49
+ parser.add_argument(
50
+ "--ignore_embedded_relationships_smo",
51
+ required=False,
52
+ help="Ignore Embedded Relationship for imported SMOs",
53
+ type=parse_bool,
54
+ default=False,
55
+ )
56
+ parser.add_argument(
57
+ "--include_embedded_relationships_attributes",
58
+ required=False,
59
+ help="Only create embedded relationships for keys",
60
+ action="extend",
61
+ nargs="+",
62
+ type=parse_ref
63
+ )
21
64
  return parser.parse_args()
22
65
 
23
66
 
24
67
  def main():
25
68
  args = parse_arguments()
26
- stix_obj = Stix2Arango(args.database, args.collection, file=args.file, create_db=args.create_db, stix2arango_note=args.stix2arango_note, ignore_embedded_relationships=args.ignore_embedded_relationships, ignore_embedded_relationships_sro=args.ignore_embedded_relationships_sro, ignore_embedded_relationships_smo=args.ignore_embedded_relationships_smo, is_large_file=args.is_large_file)
27
- stix_obj.run()
69
+ stix_obj = Stix2Arango(
70
+ database=args.database,
71
+ collection=args.collection,
72
+ file=args.file,
73
+ create_db=args.create_db,
74
+ stix2arango_note=args.stix2arango_note,
75
+ ignore_embedded_relationships=args.ignore_embedded_relationships,
76
+ ignore_embedded_relationships_sro=args.ignore_embedded_relationships_sro,
77
+ ignore_embedded_relationships_smo=args.ignore_embedded_relationships_smo,
78
+ is_large_file=args.is_large_file,
79
+ include_embedded_relationships_attributes=args.include_embedded_relationships_attributes,
80
+ )
81
+ stix_obj.run()
@@ -42,6 +42,7 @@ class Stix2Arango:
42
42
  ignore_embedded_relationships=False,
43
43
  ignore_embedded_relationships_sro=True,
44
44
  ignore_embedded_relationships_smo=True,
45
+ include_embedded_relationships_attributes=None,
45
46
  bundle_id=None,
46
47
  username=config.ARANGODB_USERNAME,
47
48
  password=config.ARANGODB_PASSWORD,
@@ -89,6 +90,7 @@ class Stix2Arango:
89
90
  self.ignore_embedded_relationships = ignore_embedded_relationships
90
91
  self.ignore_embedded_relationships_smo = ignore_embedded_relationships_smo
91
92
  self.ignore_embedded_relationships_sro = ignore_embedded_relationships_sro
93
+ self.include_embedded_relationships_attributes = include_embedded_relationships_attributes
92
94
  self.object_key_mapping = {}
93
95
  if create_collection:
94
96
  self.create_s2a_indexes()
@@ -472,14 +474,16 @@ class Stix2Arango:
472
474
  for obj in tqdm(bundle_objects, desc="upload_embedded_edges"):
473
475
  if obj["id"] not in inserted_object_ids:
474
476
  continue
475
- if (
477
+ if self.include_embedded_relationships_attributes:
478
+ pass
479
+ elif (
476
480
  self.ignore_embedded_relationships_smo and obj["type"] in SMO_TYPES
477
481
  ) or (
478
482
  self.ignore_embedded_relationships_sro and obj["type"] == "relationship"
479
483
  ):
480
484
  continue
481
485
 
482
- for ref_type, targets in utils.get_embedded_refs(obj):
486
+ for ref_type, targets in utils.get_embedded_refs(obj, attributes=self.include_embedded_relationships_attributes):
483
487
  utils.create_relationship_obj(
484
488
  obj=obj,
485
489
  source=obj.get("id"),
@@ -578,7 +582,7 @@ class Stix2Arango:
578
582
  self.filename, all_objects
579
583
  )
580
584
 
581
- if not self.ignore_embedded_relationships:
585
+ if (not self.ignore_embedded_relationships) or self.include_embedded_relationships_attributes:
582
586
  module_logger.info(
583
587
  "Creating new embedded relationships using _refs and _ref"
584
588
  )
stix2arango/utils.py CHANGED
@@ -116,7 +116,7 @@ def remove_duplicates(objects):
116
116
  return list(objects_hashmap.values())
117
117
 
118
118
 
119
- def get_embedded_refs(object: list | dict, xpath: list = []):
119
+ def get_embedded_refs(object: list | dict, xpath: list = [], attributes=None):
120
120
  embedded_refs = []
121
121
  if isinstance(object, dict):
122
122
  for key, value in object.items():
@@ -125,11 +125,13 @@ def get_embedded_refs(object: list | dict, xpath: list = []):
125
125
  if match := EMBEDDED_RELATIONSHIP_RE.fullmatch(key):
126
126
  relationship_type = "-".join(xpath + match.group(1).split("_"))
127
127
  targets = value if isinstance(value, list) else [value]
128
+ if attributes and key not in attributes:
129
+ continue
128
130
  embedded_refs.append((relationship_type, targets))
129
131
  elif isinstance(value, list):
130
- embedded_refs.extend(get_embedded_refs(value, xpath + [key]))
132
+ embedded_refs.extend(get_embedded_refs(value, xpath + [key], attributes=attributes))
131
133
  elif isinstance(object, list):
132
134
  for obj in object:
133
135
  if isinstance(obj, dict):
134
- embedded_refs.extend(get_embedded_refs(obj, xpath))
136
+ embedded_refs.extend(get_embedded_refs(obj, xpath, attributes=attributes))
135
137
  return embedded_refs
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: stix2arango
3
- Version: 1.1.3
3
+ Version: 1.1.4
4
4
  Summary: stix2arango is a command line tool that takes a group of STIX 2.1 objects in a bundle and inserts them into ArangoDB. It can also handle updates to existing objects in ArangoDB imported in a bundle.
5
5
  Project-URL: Homepage, https://github.com/muchdogesec/stix2arango
6
6
  Project-URL: Issues, https://github.com/muchdogesec/stix2arango/issues
@@ -60,20 +60,6 @@ Note, the installation assumes ArangoDB is already installed locally.
60
60
 
61
61
  [You can install ArangoDB here](https://arangodb.com/download/). stix2arango is compatible with both the Enterprise and Community versions.
62
62
 
63
- #### A note for Mac users
64
-
65
- Fellow Mac users, ArangoDB can be installed and run using homebrew as follows;
66
-
67
- ```shell
68
- ## Install
69
- brew install arangodb
70
- ## Run
71
- brew services start arangodb
72
- ## will now be accessible in a browser at: http://127.0.0.1:8529 . Default username is root with no password set (leave blank)
73
- ## Stop
74
- brew services stop arangodb
75
- ```
76
-
77
63
  ### Configuration options
78
64
 
79
65
  stix2arango has various settings that are defined in an `.env` file.
@@ -100,12 +86,14 @@ python3 stix2arango.py \
100
86
  Where;
101
87
 
102
88
  * `--file` (required): is the path to the valid STIX 2.1 bundle .json file
103
- * `--database` (required): is the name of the Arango database the objects should be stored in. If database does not exist, stix2arango will create it
89
+ * `--database` (required): is the name of the Arango database the objects should be stored in.
90
+ * `--create_db` (default `true`): If database does not exist, stix2arango will create it. You can set to `false` to stop this behaviour (and avoid the risk of incorrect DBs being created). Generally setting to `false` is a good idea if you know the databases exist. This setting will only work if the Arango user being used to authenticate has permissions to create new databases.
104
91
  * `--collection` (required): is the name of the Arango collection in the database specified the objects should be stored in. If the collection does not exist, stix2arango will create it
105
92
  * `--stix2arango_note` (optional): Will be stored under the `_stix2arango_note` custom attribute in ArangoDB. Useful as can be used in AQL. `a-z` characters only. Max 24 chars.
106
93
  * `--ignore_embedded_relationships` (optional, boolean): if `true` passed, this will stop ANY embedded relationships from being generated. This applies for all object types (SDO, SCO, SRO, SMO). If you want to target certain object types see `ignore_embedded_relationships_sro` and `ignore_embedded_relationships_sro` flags. ` Default is `false`
107
94
  * `--ignore_embedded_relationships_sro` (optional, boolean): if `true` passed, will stop any embedded relationships from being generated from SRO objects (`type` = `relationship`). Default is `false`
108
- * `--ignore_embedded_relationships_smo` (optional, boolean): if `true` passed, will stop any embedded relationships from being generated from SMO objects (`type` = `marking-definition`, `extension-definition`, `language-content`). Default is `false`
95
+ * `--ignore_embedded_relationships_smo` (optional, boolean): if `true` passed, will stop any embedded relationships from being generated from SMO objects (`type` = `marking-defirnition`, `extension-definition`, `language-content`). Default is `false`
96
+ * `--include_embedded_relationships_attributes` (optional, stix `_ref` or `_refs` attribute): if you only want to create embedded relationships from certain keys (attributes) in a STIX object you can pass a list of attributes here. e.g. `object_refs created_by_ref` . In this example, embedded relationships to all objects listed in `object_refs` and objects in `created_by_ref` will be created between source (the objects that house these attibutes) and destinations (the objects listed as values for these attributes)
109
97
  * `--is_large_file` (pass flag): Use this mode when the bundle is very large (>100mb), this will chunk the input into multiple files before loading into memory.
110
98
 
111
99
  For example, [using the MITRE ATT&CK Enterprise bundle](https://raw.githubusercontent.com/mitre/cti/master/enterprise-attack/enterprise-attack.json);
@@ -132,6 +120,18 @@ python3 stix2arango.py \
132
120
  --is_large_file
133
121
  ```
134
122
 
123
+ If you want to include embedded relationships for `created_by_ref` and `object_marking_refs` attibutes collection, you would run;
124
+
125
+ ```shell
126
+ python3 stix2arango.py \
127
+ --file cti_knowledge_base_store/mitre-attack-enterprise/enterprise-attack-15_1.json \
128
+ --database stix2arango_demo \
129
+ --collection demo_2 \
130
+ --stix2arango_note v15.1 \
131
+ --include_embedded_relationships_attributes object_refs created_by_ref \
132
+ --is_large_file
133
+ ```
134
+
135
135
  #### A note on embedded relationships
136
136
 
137
137
  stix2arango can handle all embedded references to other STIX objects under `_ref` and `_refs` properties in a STIX object when `--ignore_embedded_relationships` is set to false.
@@ -1,16 +1,16 @@
1
1
  stix2arango/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- stix2arango/__main__.py,sha256=wbR_iO70Vld2NYiml6Kz4rH396uOiNwTtjNBl4AHZEg,1987
2
+ stix2arango/__main__.py,sha256=zsCi_bfDULLDkqlRwXyGhFuLvSRcvESEc4MMN7h1lbQ,2835
3
3
  stix2arango/config.py,sha256=NZFrcnEfz-0QBrut2Rh7xMF78v0bk6U6y2TY_7mHxSs,1407
4
- stix2arango/utils.py,sha256=eVAMvXZVylM2RXzi2ph0RVW__eoSjAHWSWSG3900yjk,4487
4
+ stix2arango/utils.py,sha256=bUKJBQ2owbCQKWs_m-VYjYCHuQLykizabE4D3LPspW8,4636
5
5
  stix2arango/services/__init__.py,sha256=E87fB-dxI4mPxMVs00jdLhjp9jFhkVfjhMKIqGLRJlY,45
6
6
  stix2arango/services/arangodb_service.py,sha256=jr6zXFueluCU60WOJy7XuA9Ty0zW5FzGNBJGtJzq0PY,11964
7
7
  stix2arango/services/version_annotator.py,sha256=Sd1MIaXzK0fpNopNxRoB_3etodzAjX5D_p3uGQSWzOI,2946
8
8
  stix2arango/stix2arango/__init__.py,sha256=OqxWEEsHqR1QQpznM5DbFJ5bO5numKYtoYhjXYJMEyg,36
9
9
  stix2arango/stix2arango/bundle_loader.py,sha256=qi-0E_bMIMPZXzISvjhrWX8K-f7iFv9vOekldOGVczU,4603
10
- stix2arango/stix2arango/stix2arango.py,sha256=sC-br0nptUtZMzNza6v3s6rjdgJk-EG0_KErN9JN9qQ,22060
10
+ stix2arango/stix2arango/stix2arango.py,sha256=HJXDqA9NWxXVQSHPmbpkEKurpWEbZmy5bng5SQ1OsjE,22412
11
11
  stix2arango/templates/marking-definition.json,sha256=0q9y35mUmiF6xIWSLpkATL4JTHGSCNyLbejqZiQ0AuE,3113
12
- stix2arango-1.1.3.dist-info/METADATA,sha256=z9lCPIr6WmDFUpxzg4CQhEt_hSlBpGFjqdignPw0mSw,6873
13
- stix2arango-1.1.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
- stix2arango-1.1.3.dist-info/entry_points.txt,sha256=k2WnxMsHFLoyC6rqfvjhIMS1zwtWin51-MbNCGmSMYE,58
15
- stix2arango-1.1.3.dist-info/licenses/LICENSE,sha256=BK8Ppqlc4pdgnNzIxnxde0taoQ1BgicdyqmBvMiNYgY,11364
16
- stix2arango-1.1.3.dist-info/RECORD,,
12
+ stix2arango-1.1.4.dist-info/METADATA,sha256=Hre8CoZ_6ic_jNFQ1ONrA8jTr3wLL54wFT-_nLYGsmY,7797
13
+ stix2arango-1.1.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
+ stix2arango-1.1.4.dist-info/entry_points.txt,sha256=k2WnxMsHFLoyC6rqfvjhIMS1zwtWin51-MbNCGmSMYE,58
15
+ stix2arango-1.1.4.dist-info/licenses/LICENSE,sha256=BK8Ppqlc4pdgnNzIxnxde0taoQ1BgicdyqmBvMiNYgY,11364
16
+ stix2arango-1.1.4.dist-info/RECORD,,