5
5
import hashlib
6
6
import argparse
7
7
import logging
8
+
9
+ import case_utils .local_uuid
8
10
import exifread
9
11
import rdflib
10
12
import rdflib .plugins .sparql
11
13
14
+ from case_utils .namespace import NS_RDF , \
15
+ NS_RDFS , NS_UCO_CORE , NS_UCO_OBSERVABLE , \
16
+ NS_UCO_TYPES , NS_UCO_VOCABULARY , NS_XSD
12
17
13
- __version__ = "0.1.1 "
18
+ __version__ = "0.1.2 "
14
19
15
20
_logger = logging .getLogger (os .path .basename (__file__ ))
16
21
17
- parser = argparse .ArgumentParser ()
18
- parser .add_argument ("file" , help = "file to extract exif data from" )
19
- args = parser .parse_args ()
20
22
21
- NS_RDF = rdflib .RDF
22
- NS_RDFS = rdflib .RDFS
23
- NS_UCO_CORE = rdflib .Namespace ("https://unifiedcyberontology.org/ontology/uco/core#" )
24
- NS_UCO_LOCATION = rdflib .Namespace ("https://unifiedcyberontology.org/ontology/uco/location#" )
25
- NS_UCO_OBSERVABLE = rdflib .Namespace ("https://unifiedcyberontology.org/ontology/uco/observable#" )
26
- NS_UCO_TYPES = rdflib .Namespace ("https://unifiedcyberontology.org/ontology/uco/types#" )
27
- NS_UCO_VOCABULARY = rdflib .Namespace ("https://unifiedcyberontology.org/ontology/uco/vocabulary#" )
28
- NS_XSD = rdflib .namespace .XSD
23
+ ns_kb = rdflib .Namespace ("http://example.org/kb/" )
24
+
25
+
26
+ def get_node_iri (ns : rdflib .Namespace , prefix : str ) -> rdflib .URIRef :
27
+ node_id = rdflib .URIRef (f"{ prefix } { case_utils .local_uuid .demo_uuid ()} " , ns )
28
+ return node_id
29
29
30
30
31
31
def get_file_info (filepath ):
32
32
"""
33
33
A function to get some basic information about the file application being run against
34
34
:param filepath: The relative path to the image
35
- :return: A Dictinary with some information about the file
35
+ :return: A Dictionary with some information about the file
36
36
"""
37
37
file_information = {}
38
38
try :
@@ -54,9 +54,8 @@ def get_exif(file):
54
54
:param file: The image file
55
55
:return: Dictionary with the exif from the image
56
56
"""
57
- file = open (file , 'rb' )
58
- exif_tags = exifread .process_file (file )
59
- file .close ()
57
+ with open (file , 'rb' ) as file :
58
+ exif_tags = exifread .process_file (file )
60
59
return exif_tags
61
60
62
61
@@ -75,15 +74,15 @@ def create_exif_dict(tags):
75
74
76
75
def n_cyber_object_to_node (graph ):
77
76
"""
78
- Initial function to create the blank nodes for each of the file's facet nodes
77
+ Initial function to create nodes for each of the file's facet nodes
79
78
:param graph: rdflib graph object for adding nodes to
80
- :return: The four blank nodes for each fo the other functions to fill
79
+ :return: The four nodes for each fo the other functions to fill
81
80
"""
82
- cyber_object_facet = rdflib .BNode ( )
83
- n_raster_facets = rdflib .BNode ( )
84
- n_file_facets = rdflib .BNode ( )
85
- n_content_facets = rdflib .BNode ( )
86
- n_exif_facets = rdflib .BNode ( )
81
+ cyber_object_facet = rdflib .URIRef ( get_node_iri ( ns = ns_kb , prefix = "observableobject-" ) )
82
+ n_raster_facets = rdflib .URIRef ( get_node_iri ( ns = ns_kb , prefix = "rasterpicture-" ) )
83
+ n_file_facets = rdflib .URIRef ( get_node_iri ( ns = ns_kb , prefix = "filefacet-" ) )
84
+ n_content_facets = rdflib .URIRef ( get_node_iri ( ns = ns_kb , prefix = "contentfacet-" ) )
85
+ n_exif_facets = rdflib .URIRef ( get_node_iri ( ns = ns_kb , prefix = "exiffacet-" ) )
87
86
graph .add ((
88
87
cyber_object_facet ,
89
88
NS_RDF .type ,
@@ -116,12 +115,12 @@ def filecontent_object_to_node(graph, n_content_facets, file_information):
116
115
"""
117
116
Unused: Create a node that will add the file content facet node to the graph
118
117
:param graph: rdflib graph object for adding nodes to
119
- :param n_content_facets: Blank node to contain all of the content facet information
118
+ :param n_content_facets: Blank node to contain all content facet information
120
119
:param file_information: Dictionary containing information about file being analysed
121
120
:return: None
122
121
"""
123
- byte_order_facet = rdflib .BNode ( )
124
- file_hash_facet = rdflib .BNode ( )
122
+ byte_order_facet = rdflib .URIRef ( get_node_iri ( ns = ns_kb , prefix = "byteorder-" ) )
123
+ file_hash_facet = rdflib .URIRef ( get_node_iri ( ns = ns_kb , prefix = "hash-" ) )
125
124
graph .add ((
126
125
n_content_facets ,
127
126
NS_RDF .type ,
@@ -259,13 +258,13 @@ def raster_object_to_node(graph, controlled_dict, n_raster_facets, file_informat
259
258
260
259
def controlled_dictionary_object_to_node (graph , controlled_dict , n_exif_facet ):
261
260
"""
262
- Add controlled dictionary object to accept all of the Values in the extracted exif
261
+ Add controlled dictionary object to accept all Values in the extracted exif
263
262
:param graph: rdflib graph object for adding nodes to
264
263
:param controlled_dict: Dictionary containing the EXIF information from image
265
- :param n_controlled_dictionary :
264
+ :param n_exif_facet :
266
265
:return: None
267
266
"""
268
- n_controlled_dictionary = rdflib .BNode ( )
267
+ n_controlled_dictionary = rdflib .URIRef ( get_node_iri ( ns = ns_kb , prefix = "controlleddict-" ) )
269
268
graph .add ((
270
269
n_exif_facet ,
271
270
NS_RDF .type ,
@@ -289,9 +288,9 @@ def controlled_dictionary_object_to_node(graph, controlled_dict, n_exif_facet):
289
288
try :
290
289
assert isinstance (v_value , rdflib .Literal )
291
290
except AssertionError :
292
- _logger .info ("v_value = %r." % v_value )
291
+ _logger .info (f "v_value = { v_value } " )
293
292
raise
294
- n_entry = rdflib .BNode ( )
293
+ n_entry = rdflib .URIRef ( get_node_iri ( ns = ns_kb , prefix = "controlleddictionaryentry-" ) )
295
294
graph .add ((
296
295
n_controlled_dictionary ,
297
296
NS_UCO_TYPES .entry ,
@@ -319,16 +318,15 @@ def main():
319
318
Main function to run the application
320
319
:return: prints out the case file - TODO: write to file instead
321
320
"""
321
+ parser = argparse .ArgumentParser ()
322
+ parser .add_argument ("file" , help = "file to extract exif data from" )
323
+ args = parser .parse_args ()
322
324
local_file = args .file
323
325
file_info = get_file_info (local_file )
324
326
tags = get_exif (local_file )
325
327
tag_dict = create_exif_dict (tags )
328
+ case_utils .local_uuid .configure ()
326
329
out_graph = rdflib .Graph ()
327
- out_graph .namespace_manager .bind ("uco-core" , NS_UCO_CORE )
328
- out_graph .namespace_manager .bind ("uco-location" , NS_UCO_LOCATION )
329
- out_graph .namespace_manager .bind ("uco-observable" , NS_UCO_OBSERVABLE )
330
- out_graph .namespace_manager .bind ("uco-types" , NS_UCO_TYPES )
331
- out_graph .namespace_manager .bind ("uco-vocabulary" , NS_UCO_VOCABULARY )
332
330
exif_facet_node , raster_facets_node , file_facets_node , content_facets \
333
331
= n_cyber_object_to_node (out_graph )
334
332
controlled_dictionary_object_to_node (out_graph , tag_dict , exif_facet_node )
@@ -338,17 +336,14 @@ def main():
338
336
context = {"kb" : "http://example.org/kb/" ,
339
337
"rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ,
340
338
"rdfs" : "http://www.w3.org/2000/01/rdf-schema#" ,
341
- "uco-core" : "https://unifiedcyberontology.org/ontology/uco/core#" ,
342
- "uco-location" : "https://unifiedcyberontology.org/ontology/uco/location#" ,
343
- "uco-observable" : "https://unifiedcyberontology.org/ontology/uco/observable#" ,
344
- "uco-types" : "https://unifiedcyberontology.org/ontology/uco/types#" ,
339
+ "uco-core" : "https://ontology.unifiedcyberontology.org/uco/core/" ,
340
+ "uco-observable" : "https://ontology.unifiedcyberontology.org/uco/observable/" ,
341
+ "uco-types" : "https://ontology.unifiedcyberontology.org/uco/types/" ,
345
342
"xsd" : "http://www.w3.org/2001/XMLSchema#" }
346
-
347
343
graphed = out_graph .serialize (format = 'json-ld' , context = context )
348
344
349
- graph = json .dumps (graphed , indent = 4 )
350
- case_json = json .loads (graph .encode ('utf-8' ))
351
- print (case_json )
345
+ parsed = json .loads (graphed )
346
+ print (json .dumps (parsed , indent = 4 ))
352
347
353
348
354
349
if __name__ == "__main__" :
0 commit comments