-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhips_cipher.py
More file actions
executable file
·996 lines (939 loc) · 35 KB
/
Copy pathhips_cipher.py
File metadata and controls
executable file
·996 lines (939 loc) · 35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
#!/usr/bin/python3
#
# Excellent Regards, the Alveare Solutions #!/Society -x
#
# HIPS Cipher Enryptor/Decryptor
import optparse
import os
import glob
import json
import pysnooper
import piexif
import shutil
from typing import List
from subprocess import Popen, PIPE
from PIL import Image, ExifTags
SCRIPT_NAME = 'HIPSCipher'
VERSION = '1.0'
VERSION_NAME = 'Portal'
CURRENT_DIR = os.getcwd()
CONFIG = {
'config_file': '',
'current_dir': CURRENT_DIR,
'batch_dir': '%s/batch' % CURRENT_DIR,
'tmp_file': '%s/hc_tmp.txt' % CURRENT_DIR,
'report_file': '%s/hc_report.dump' % CURRENT_DIR,
'image_file': '%s/dta/Regards.jpg' % CURRENT_DIR,
'cleartext_file': '%s/hc_clear.txt' % CURRENT_DIR,
'running_mode': 'decrypt', # <decrypt|encrypt|write-exif|read-exif|dump-exif|clean-exif>
'data_source': 'terminal', # <file|terminal>
'exif_data': '#!/',
'exif_tag': piexif.ExifIFD.UserComment,
'keycode': 'HIPS', # Encryption password
'cleanup': ['tmp_file'], # CONFIG keys containing file paths
'full_cleanup': [
'tmp_file', 'cleartext_file', 'report_file', 'batch_dir'
],
'in_place': False,
'batch': False,
'report': True,
'silent': False,
}
action_result = {'input': [], 'output': [], 'msg': '', 'exit': 0, 'errors': []}
# FETCHERS
def fetch_running_mode_from_user(prompt: str = 'Action') -> str:
global CONFIG
stdout_msg('Specify action or (.back)...', info=True)
if CONFIG.get('running_mode'):
prompt = prompt + '[' + CONFIG['running_mode'] + ']> '
print(
'[ INFO ]: Leave blank to keep current '\
'(%s)' % CONFIG['running_mode']
)
stdout_msg(
'1) Encrypt cleartext 5) Write EXIF tag\n' + \
'2) Decrypt ciphertext 6) Read EXIF tag\n' + \
'3) Dump all EXIF tags 7) Clear all EXIF tags\n' + \
'4) Disk Cleanup\n'
)
selection_map = {
'1': 'encrypt', '2': 'decrypt', '3': 'dump-exif', '4': 'cleanup',
'5': 'write-exif', '6': 'read-exif', '7': 'clean-exif'
}
while True:
selection = input(prompt)
if not selection:
if not CONFIG.get('running_mode'):
continue
selection = [k for k, v in selection_map.items() \
if v == CONFIG.get('running_mode')][0]
if selection == '.back':
return
if selection not in ('1', '2', '3', '4', '5', '6', '7'):
print('[ ERROR ]: Invalid selection (%s)' % selection)
CONFIG['running_mode'] = selection_map[selection]
break
print()
return selection_map[selection]
def fetch_data_from_user(prompt='Data'):
stdout_msg(
'Specify input data for action '\
'(%s) or (.back)...' % CONFIG.get('running_mode', ''), info=True
)
if CONFIG.get('running_mode') in (
'encrypt', 'decrypt', 'cleanup', 'dump-exif', 'read-exif',
'write-exif', 'clean-exif'
):
prompt = prompt + '[' + CONFIG['running_mode'] + ']'
while True:
data = input(prompt + '> ')
if not data:
continue
if data == '.back':
return
break
print()
return data
#@pysnooper.snoop()
def fetch_exif_tag_from_user(prompt='ExifTag'):
global CONFIG
stdout_msg(
'Specify EXIF tag ID or (.back)...', info=True,
silence=CONFIG.get('silent')
)
if CONFIG.get('exif_tag'):
prompt = prompt + '[' + str(CONFIG['exif_tag']) + ']> '
stdout_msg(
'Leave blank to keep current '\
'(%s)' % CONFIG['exif_tag'], info=True, silence=CONFIG.get('silent')
)
while True:
tag_id = input(prompt)
if not tag_id:
if not CONFIG.get('exif_tag'):
continue
tag_id = CONFIG.get('exif_tag')
if tag_id == '.back':
return
try:
tag_id = int(tag_id)
except Exception as e:
stdout_msg(
'EXIF tag must be a number, not (%s)' % (tag_id), warn=True,
silence=CONFIG.get('silent')
)
continue
CONFIG.update({'exif_tag': tag_id})
break
print()
return tag_id
def fetch_image_file_path_from_user(prompt='IMGPath'):
global CONFIG
stdout_msg(
'Specify image file path or (.back)...', info=True,
silence=CONFIG.get('silent')
)
if CONFIG.get('image_file'):
prompt = prompt + '[' + os.path.basename(CONFIG['image_file']) + ']> '
stdout_msg(
'Leave blank to keep current '\
'(%s)' % CONFIG['image_file'], info=True, silence=CONFIG.get('silent')
)
while True:
img_path = input(prompt)
if not img_path:
if not CONFIG.get('image_file'):
continue
img_path = CONFIG.get('image_file')
if img_path == '.back':
return
CONFIG.update({'image_file': img_path})
break
print()
return img_path
def fetch_replay_confirmation_from_user(prompt='Replay', qa_msg='Do you want to go again?'):
stdout_msg('[ Q/A ]: %s' % qa_msg, silence=CONFIG.get('silent'))
while True:
answer = input(prompt + '[Y/N]> ')
if not answer:
continue
if answer.lower() not in ('y', 'n', 'yes', 'no', 'yeah', 'nah'):
print(); stdout_msg(
'Invalid answer (%s)\n' % answer, err=True,
silence=CONFIG.get('silent')
)
continue
break
print()
return True if answer in ('y', 'yes', 'yeah') else False
def fetch_keycode_from_user(prompt='KeyCode'):
global CONFIG
stdout_msg(
'Specify encryption keycode sequence or (.back)...', info=True,
silence=CONFIG.get('silent')
)
if CONFIG.get('keycode'):
prompt = prompt + '[' + CONFIG['keycode'] + ']> '
stdout_msg(
'Leave blank to keep current '\
'(%s)' % CONFIG['keycode'], info=True, silence=CONFIG.get('silent')
)
while True:
code = input(prompt)
if not code:
if not CONFIG.get('keycode'):
continue
code = CONFIG.get('keycode')
if code == '.back':
return
CONFIG.update({'keycode': code})
break
print()
return code
# CHECKERS
#@pysnooper.snoop()
def check_preconditions(**conf):
errors = []
file_paths = ['image_file']
requirements = ['running_mode', 'data_source']
for fl in file_paths + requirements:
if not conf.get(fl):
errors.append('Attribute (%s) not set' % fl)
if conf.get('running_mode', '').lower() in ('encrypt') \
and conf.get('data_source') == 'file':
if not os.path.exists(conf.get('cleartext_file')):
errors.append(
'Cleartext file (%s) not found' % conf.get('cleartext_file')
)
if conf.get('running_mode', '').lower() not in (
'encrypt', 'decrypt', 'cleanup',
'write-exif', 'read-exif', 'dump-exif', 'clean-exif'
):
errors.append(
'Invalid running mode specified (%s)' % conf.get('running_mode')
)
if conf.get('data_source') not in ('file', 'terminal'):
errors.append(
'Invalid data source specified (%s)' % conf.get('data_source')
)
if conf.get('batch') and not os.path.exists(conf.get('batch_dir')):
errors.append(
'Batch action specified but batch directory (%s) not found'
% conf.get('batch_dir')
)
action_result.update({'exit': len(errors) + 10, 'msg': '\n'.join(errors)})
return False if errors else True
# GENERAL
def reset_action_result():
global action_result
action_result = {'input': [], 'output': [], 'msg': '', 'exit': 0, 'errors': []}
return action_result
def batch_dir2list(directory):
image_paths, image_types = [], ['*.jpg', '*.jpeg', '*.png']
for image_type in image_types:
pattern = os.path.join(directory, image_type)
image_paths.extend(glob.glob(pattern))
return image_paths
def shell_cmd(command, user=None):
if user:
command = "su %s -c \'%s\'" % (str(user), str(command))
process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
output, errors = process.communicate()
return str(output).rstrip('\n'), str(errors).rstrip('\n'), process.returncode
def message_to_bin(message):
return ''.join(format(ord(char), '08b') for char in message)
#@pysnooper.snoop()
def load_config(file_path):
global CONFIG
if not file_path or not os.path.exists(file_path):
return
with open(file_path, 'r') as fl:
CONFIG.update(json.load(fl))
return CONFIG
#@pysnooper.snoop()
def file2list(file_path):
if not file_path or not os.path.exists(file_path):
return {}
with open(file_path, 'r') as fl:
converted = fl.readlines()
return converted
#@pysnooper.snoop()
def write2file(*args, file_path=str(), mode='w', **kwargs):
with open(file_path, mode, encoding='utf-8', errors='ignore') \
as active_file:
content = ''
for line in args:
content = content + (
str(line) if '\n' in line else str(line) + '\n'
)
for line_key in kwargs:
content = content + \
str(line_key) + '=' + str(kwargs[line_key]) + '\n'
try:
active_file.write(content)
except UnicodeError as e:
return False
return True
def clear_screen():
return os.system('cls' if os.name == 'nt' else 'clear')
def stdout_msg(message, silence=False, red=False, info=False, warn=False,
err=False, done=False, bold=False, green=False, ok=False, nok=False):
if red:
display_line = '\033[91m' + str(message) + '\033[0m'
elif green:
display_line = '\033[1;32m' + str(message) + '\033[0m'
elif ok:
display_line = '[ ' + '\033[1;32m' + 'OK' + '\033[0m' + ' ]: ' \
+ '\033[92m' + str(message) + '\033[0m'
elif nok:
display_line = '[ ' + '\033[91m' + 'NOK' + '\033[0m' + ' ]: ' \
+ '\033[91m' + str(message) + '\033[0m'
elif info:
display_line = '[ INFO ]: ' + str(message)
elif warn:
display_line = '[ ' + '\033[91m' + 'WARNING' + '\033[0m' + ' ]: ' \
+ '\033[91m' + str(message) + '\x1b[0m'
elif err:
display_line = '[ ' + '\033[91m' + 'ERROR' + '\033[0m' + ' ]: ' \
+ '\033[91m' + str(message) + '\033[0m'
elif done:
display_line = '[ ' + '\x1b[1;34m' + 'DONE' + '\033[0m' + ' ]: ' \
+ str(message)
elif bold:
display_line = '\x1b[1;37m' + str(message) + '\x1b[0m'
else:
display_line = message
if silence:
return False
print(display_line)
return True
# ACTIONS
#@pysnooper.snoop()
def clean_exif(**context) -> str:
global action_result
failures = 0
if context.get('in_place'):
out_img_path = context['image_file']
else:
img_dir = os.path.dirname(context['image_file'])
img_name = os.path.basename(context['image_file'])
out_img_path = img_dir + '/hips.' + img_name
try:
image = Image.open(context['image_file'])
# Check if the image has EXIF data
if hasattr(image, '_getexif'):
exif_dict = piexif.load(image.info["exif"])
# Remove all EXIF data
exif_dict = {}
# Save the image without EXIF data to the specified output path
image.save(out_img_path, exif=piexif.dump(exif_dict))
# If the image doesn't have EXIF data attribute
else:
failures += 1
action_result['errors'].append(
f"Image {context['image_file']} does not have EXIF data."
)
except Exception as e:
failures += 1
action_result['errors'].append(str(e))
action_result.update({
'input': action_result['input'] + [context['image_file']],
'output': action_result['output'] + [out_img_path]
if os.path.exists(out_img_path) else action_result['output'],
'msg': 'OK: EXIF clean successful' if not failures \
else 'NOK: EXIF clean failures detected (%s)' % failures,
'exit': 0 if os.path.exists(out_img_path) and not failures else 9,
})
return out_img_path if not failures else None
#@pysnooper.snoop()
def write_exif(**context) -> str:
global action_result
failures, tag_id = 0, int(context.get('exif_tag', piexif.ExifIFD.UserComment))
if context.get('in_place'):
out_img_path = context['image_file']
else:
img_dir = os.path.dirname(context['image_file'])
img_name = os.path.basename(context['image_file'])
out_img_path = img_dir + '/hips.' + img_name
try:
image = Image.open(context['image_file'])
# Check if the image has EXIF data
if hasattr(image, '_getexif') and image.info.get('exif'):
exif_dict = piexif.load(image.info["exif"])
# Set the specified tag ID with the provided value
exif_dict['Exif'][tag_id] = context['exif_data'].encode('utf-8')
else:
exif_dict = {'Exif': {tag_id: context['exif_data'].encode('utf-8')}}
action_result['msg'] = f"Image {context['image_file']} does not have EXIF data."
exif_bytes = piexif.dump(exif_dict)
# Save the image with the updated EXIF data
image.save(out_img_path, exif=exif_bytes)
except Exception as e:
failures += 1
action_result['errors'].append(str(e))
action_result.update({
'input': action_result['input'] + [context['image_file'], f'EXIF Tag: {tag_id}'],
'output': action_result['output'] + [out_img_path, context['exif_data']]
if os.path.exists(out_img_path) else action_result['output'],
'msg': 'OK: EXIF write successful' if not failures \
else 'NOK: EXIF write failures detected (%s)' % failures,
'exit': 0 if os.path.exists(out_img_path) and not failures else 9,
})
return out_img_path if not failures else None
#@pysnooper.snoop()
def read_exif(**context) -> str:
global action_result
failures = 0
tag_id, tag_data = int(context.get('exif_tag', piexif.ExifIFD.UserComment)), ''
try:
# Open the image using PIL
image = Image.open(context['image_file'])
# Check if the image has EXIF data
if hasattr(image, '_getexif'):
exif_dict = piexif.load(image.info["exif"])
# Check if the specified tag ID is present in the EXIF data
if tag_id in exif_dict['Exif']:
tag_data = exif_dict['Exif'][tag_id].decode('utf-8')
else:
failures += 1
action_result['errors'].append(
f"Tag {tag_id} not found in the EXIF data."
)
# If the image doesn't have EXIF data attribute
else:
failures += 1
action_result['errors'].append(
f"Image {context['image_file']} does not have EXIF data."
)
except Exception as e:
failures += 1
action_result['errors'].append(str(e))
action_result.update({
'input': action_result['input'] + [context['image_file'], f'EXIF Tag: {tag_id}'],
'output': action_result['output'] + [tag_data]
if tag_data else action_result['output'],
'msg': 'OK: EXIF read successful' if not failures \
else 'NOK: EXIF read failures detected (%s)' % failures,
'exit': 0 if not failures else 9,
})
return tag_data if not failures else None
#@pysnooper.snoop()
def dump_exif(**context) -> dict:
global action_result
failures, exif_data = 0, {}
try:
# Open the image using PIL
image = Image.open(context['image_file'])
# Check if the image has EXIF data
if hasattr(image, '_getexif'):
exif_details = piexif.load(image.info["exif"])
for ifd, sub_dict in exif_details.items():
if not sub_dict:
continue
for tag, value in sub_dict.items():
exif_data[str(tag)] = str(value)
# If the image doesn't have EXIF data attribute
else:
failures += 1
action_result['errors'].append("The image does not have EXIF data.")
except Exception as e:
failures += 1
action_result['errors'].append(str(e))
action_result.update({
'input': action_result['input'] + [context['image_file']],
'output': action_result['output'] + [exif_data]
if exif_data else action_result['output'],
'msg': 'OK: EXIF dump successful' if not failures \
else 'NOK: EXIF dump failures detected (%s)' % failures,
'exit': 0 if not failures else 9,
})
return exif_data if not failures else None
#@pysnooper.snoop()
def encrypt(*data: List[str], **context) -> str:
global action_result
failures = 0
if context.get('in_place'):
out_img_path = context['image_file']
else:
img_dir = os.path.dirname(context['image_file'])
img_name = os.path.basename(context['image_file'])
out_img_path = img_dir + '/hips.' + img_name
if os.path.exists(out_img_path):
os.remove(out_img_path)
# If data is not a file that exists, it will be written to tmp_file
if not os.path.exists(data[0]):
with open(context['tmp_file'], 'w') as fl:
fl.write(';'.join(data))
secret_file = context['tmp_file']
else:
secret_file = data[0]
stdout, stderr, exit = shell_cmd(
'steghide embed -ef %s -cf %s -sf %s -p %s' % (
secret_file, context['image_file'], out_img_path, context['keycode']
)
)
if exit != 0:
action_result['errors'] += [stdout, stderr]
failures += 1
secret_content = ''.join(file2list(secret_file))
action_result.update({
'input': action_result['input'] + [context['image_file']],
'output': action_result['output'] + [out_img_path, secret_content],
'msg': 'OK: Encryption successful' if os.path.exists(out_img_path) and \
not failures else 'NOK: Encryption failures detected (%s)' % failures,
'exit': 0 if os.path.exists(out_img_path) and not failures else 9,
})
return out_img_path if not failures else None
#@pysnooper.snoop()
def decrypt(**context) -> str:
global action_result
failures = 0
stdout, stderr, exit = shell_cmd(
'steghide extract -sf %s -p %s' % (
context['image_file'], context['keycode']
)
)
if exit != 0:
action_result['errors'] += [stdout, stderr]
failures += 1
sanitized_stderr = stderr.lstrip('"b\'').rstrip('.\\n\'"').replace('"', '')
action_result.update({
'input': action_result['input'] + [context['image_file']],
'output': action_result['output'] + [stdout if exit != 0 else sanitized_stderr],
'msg': 'OK: Decryption successful' if not failures \
else 'NOK: Decryption failures detected (%s)' % failures,
'exit': 0 if not failures else 9,
})
revealed_file = sanitized_stderr.split(' ')[-1]
if os.path.exists(revealed_file):
content = '\n'.join(file2list(revealed_file))
action_result['output'].append(content)
return context['image_file'] if not failures else None
# FORMATTERS
def format_header():
header = '''
_______________________________________________________________________________
* * * %s''' % SCRIPT_NAME + ''' * * *
________________________________________________________v%s%s_____________''' % (VERSION, VERSION_NAME) + '''
Excellent Regards, the Alveare Solutions #!/Society -x
'''
return header
# DISPLAY
#@pysnooper.snoop()
def display2terminal(*lines, result=False, **context):
if (not lines and not result) or context.get('silent'):
return True
if result:
result2display = action_result.copy()
if isinstance(action_result.get('errors'), list) and not action_result['errors']:
del result2display['errors']
stdout_msg(
'[ %s ]: %s Action Result' % (
CONFIG.get('running_mode', '').upper(), SCRIPT_NAME
), silence=context.get('silent')
)
stdout_msg(
json.dumps(result2display, indent=4), silence=context.get('silent')
)
else:
stdout_msg('\n'.join(lines) + '\n', silence=context.get('silent'))
print()
return True
#@pysnooper.snoop()
def display_header(**context):
if context.get('silent'):
return False
stdout_msg(format_header())
return True
# CREATORS
#@pysnooper.snoop()
def create_command_line_parser():
parser = optparse.OptionParser(
format_header() + '\n[ DESCRIPTION ]: HIPSCipher Encryption/Decryption -\n\n'
' [ Ex ]: Terminal based running mode with default settings\n'
' ~$ %prog \n\n'
' [ Ex ]: File based running mode decryption\n'
' ~$ %prog \\ \n'
' --action decrypt \\ \n'
' --image-file ./dta/Regards.jpg \\ \n'
' --key-code HIPS1234\n\n'
' [ Ex ]: File based running mode encryption with no STDOUT\n'
' ~$ %prog \\ \n'
' --action encrypt \\ \n'
' --image-file ./dta/Regards.jpg \\ \n'
' --key-code HIPS1234 \\ \n'
' --cleartext-file hc_cleartext.txt \\ \n'
' --in-place \\ \n'
' --silent\n\n'
' [ Ex ]: File based running mode batch encryption with STDOUT\n'
' ~$ %prog \\ \n'
' --action encrypt \\ \n'
' --key-code HIPS1234 \\ \n'
' --batch \\ \n'
' --batch-dir files2encrypt \\ \n'
' --cleartext-file hc_cleartext.txt\n\n'
' [ Ex ]: File based EXIF dump saved to non-default report file\n'
' ~$ %prog \\ \n'
' --action dump-exif \\ \n'
' --image-file ./dta/Regards.jpg \\ \n'
' --report \\ \n'
' --report-file hc_custom.report\n\n'
' [ Ex ]: File based EXIF write\n'
' ~$ %prog \\ \n'
' --action write-exif \\ \n'
' --exif-tag 37510 \\ \n'
' --exif-data #!/ \\ \n'
' --image-file ./dta/Regards.jpg\n\n'
' [ Ex ]: File based EXIF tag read\n'
' ~$ %prog \\ \n'
' --action read-exif \\ \n'
' --exif-tag 37510 \\ \n'
' --image-file ./dta/Regards.jpg\n\n'
' [ Ex ]: File based EXIF cleanup\n'
' ~$ %prog \\ \n'
' --action clean-exif \\ \n'
' --image-file ./dta/Regards.jpg\n\n'
' [ Ex ]: Run with context data from JSON config file\n'
' ~$ %prog \\ \n'
' --konfig-file conf/hips_cipher.conf.json\n\n'
' [ Ex ]: Cleanup all generated files from disk\n'
' ~$ %prog \\ \n'
' --action cleanup'
)
return parser
# PARSERS
#@pysnooper.snoop()
def process_command_line_options(parser, **context):
global CONFIG
(options, args) = parser.parse_args()
if options.config_file:
return load_config(options.config_file)
to_update = {key: val for key, val in options.__dict__.items() if val}
CONFIG.update(to_update)
return to_update
#@pysnooper.snoop()
def add_command_line_parser_options(parser):
parser.add_option(
'-a', '--action', dest='running_mode', type='string',
help='Specify the desired action. Options: <encrypt|decrypt|cleanup>',
)
parser.add_option(
'-i', '--image-file', dest='image_file', type='string',
help='Path to the image file to operate on.'
)
parser.add_option(
'-c', '--cleartext-file', dest='cleartext_file', type='string',
help='Cleartext file path for IO operations during file running mode.'
)
parser.add_option(
'-d', '--batch-dir', dest='batch_dir', type='string',
help='Specify location to patch dirs of files.'
)
parser.add_option(
'-x', '--exif-data', dest='exif_data', type='string',
help='The exif data to write. (Implies --action write-exif, Default: #!/)'
)
parser.add_option(
'-X', '--exif-tag', dest='exif_tag', type='string',
help='The exif tag to write. (Implies --action (write-exif|read-exif), '
'Default: [37510]UserComment)'
)
parser.add_option(
'-s', '--data-src', dest='data_source', type='string',
help='Specify if the input data source. Options: <file|terminal>, '
'Default: file'
)
parser.add_option(
'-R', '--report-file', dest='report_file', type='string',
help='Specify path of report file. (Implies --report)'
)
parser.add_option(
'-k', '--key-code', dest='keycode', type=str,
help='Cryptographic password.'
)
parser.add_option(
'-K', '--konfig-file', dest='config_file', type=str,
help='Path to the %s configuration file.' % SCRIPT_NAME
)
parser.add_option(
'-I', '--in-place', dest='in_place', action='store_true',
help='Modify target image in place without creating copy.'
)
parser.add_option(
'-b', '--batch', dest='batch', action='store_true',
help='Perform actions on all files in the batch directory.'
)
parser.add_option(
'-S', '--silent', dest='silent', action='store_true',
help='Run with no STDOUT output. Implies a file data source.'
)
parser.add_option(
'-r', '--report', dest='report', action='store_true',
help='Save action results to report file.'
)
return parser
#@pysnooper.snoop()
def parse_cli_args(**context):
parser = create_command_line_parser()
add_command_line_parser_options(parser)
return process_command_line_options(parser, **context)
# REPORTERS
def report_action_result(result, **context):
return write2file(
json.dumps(action_result, indent=4),
file_path=context.get('report_file')
)
# CLEANERS
#@pysnooper.snoop()
def cleanup(full=False, **context):
global CONFIG
global action_result
to_remove = [
context.get(label, '')
for label in context['cleanup' if not full else 'full_cleanup']
]
try:
for file_path in to_remove:
if not os.path.exists(file_path):
continue
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
else:
os.remove(file_path)
if full:
CONFIG.update({'report': False})
except OSError as e:
action_result.update({
'msg': 'Cleanup error! Details: %s' % str(e),
'exit': 8,
})
return False
return True
# SETUP
#@pysnooper.snoop()
def setup(**context):
global action_result
file_paths = []
dir_paths = ['batch_dir']
for fl_label in file_paths:
if fl_label not in context or os.path.isfile(context[fl_label]):
continue
try:
create = write2file('', mode='a', file_path=context[fl_label])
except Exception as e:
action_result['errors'].append(str(e))
for dir_label in dir_paths:
if dir_label not in context or os.path.isdir(context[dir_label]):
continue
try:
create = os.makedirs(context[dir_label])
except Exception as e:
action_result['errors'].append(str(e))
if action_result['errors']:
action_result.update({
'msg': '%s Setup failed ' % SCRIPT_NAME +
'with (%d) errors! Details: ' % len(action_result['errors']) \
+ ','.join(action_result['errors']),
'exit': 11,
})
return True if not action_result['errors'] else False
# INIT
#@pysnooper.snoop()
def init_terminal_running_mode(**conf):
global action_result
while True:
reset_action_result()
if os.path.exists(conf['tmp_file']):
os.remove(conf['tmp_file'])
action = fetch_running_mode_from_user()
if not action:
action_result.update({
'exit': 0,
'msg': 'Action aborted at running mode prompt'
})
break
if action == 'cleanup':
clean = cleanup(full=True, **conf)
clear = clear_screen()
display_header(**conf)
continue
elif action not in (
'dump-exif', 'read-exif', 'write-exif', 'cleanup', 'clean-exif'
):
keycode = fetch_keycode_from_user()
if not keycode:
action_result.update({
'exit': 0,
'msg': 'Action aborted at keycode prompt'
})
break
img_file = fetch_image_file_path_from_user()
if not img_file:
action_result.update({
'exit': 0,
'msg': 'Action aborted at image file input prompt'
})
break
if action in ('encrypt', 'write-exif'):
data = fetch_data_from_user()
if not data:
action_result.update({
'exit': 0,
'msg': 'Action aborted at data input prompt'
})
break
if action == 'write-exif':
CONFIG['exif_data'] = data
if action in ('write-exif', 'read-exif'):
exif_tag = fetch_exif_tag_from_user()
if not exif_tag:
action_result.update({
'exit': 0,
'msg': 'Action aborted at EXIF tag input prompt'
})
break
handlers = {
'encrypt': encrypt,
'decrypt': decrypt,
'write-exif': write_exif,
'read-exif': read_exif,
'dump-exif': dump_exif,
'clean-exif': clean_exif,
}
if CONFIG.get('running_mode') not in handlers:
action_result.update({
'exit': 4,
'msg': 'Invalid running mode %s' % CONFIG.get('running_mode')
})
return action_result['exit']
if action == 'encrypt':
action = handlers[CONFIG['running_mode']](data, **CONFIG)
else:
action = handlers[CONFIG['running_mode']](**CONFIG)
if action is None:
action_result.update({
'exit': 5,
'msg': 'Action %s failed' % CONFIG.get('running_mode')
})
display = display2terminal(result=True, **CONFIG)
if not display:
action_result.update({
'exit': 7,
'msg': 'Could not display action result'
})
replay = fetch_replay_confirmation_from_user()
if not replay:
break
clear = clear_screen()
display_header(**CONFIG)
return action_result['exit']
#@pysnooper.snoop()
def init_file_running_mode(**conf):
global action_result
if not conf.get('keycode'):
keycode = fetch_keycode_from_user()
img_file = conf.get('image_file')
if not os.path.exists(img_file):
action_result.update({
'exit': 2,
'msg': 'Could not find image file %s' % img_file
})
return action_result['exit']
if conf.get('running_mode') == 'encrypt':
data = ''.join(
file2list(conf.get('cleartext_file', 'hc_clear.txt'))
)
handlers = {
'encrypt': encrypt,
'decrypt': decrypt,
'write-exif': write_exif,
'read-exif': read_exif,
'dump-exif': dump_exif,
'clean-exif': clean_exif,
}
if conf.get('running_mode') not in handlers:
action_result.update({
'exit': 4,
'msg': 'Invalid running mode %s' % conf.get('running_mode')
})
return action_result['exit']
args = [] if conf['running_mode'] != 'encrypt' else [conf['cleartext_file']]
if conf.get('batch'):
image_paths = batch_dir2list(conf['batch_dir'])
for img_path in image_paths:
conf['image_file'] = img_path
action = handlers[conf['running_mode']](*args, **conf)
else:
action = handlers[conf['running_mode']](*args, **conf)
if action is None:
action_result.update({
'exit': 5,
'msg': 'Action %s failed' % conf.get('running_mode')
})
display = display2terminal(result=True, **conf)
if not display:
action_result.update({
'exit': 7,
'msg': 'Could not display action result'
})
return action_result['exit']
#@pysnooper.snoop()
def init():
global CONFIG
global action_result
cli_parse = parse_cli_args(**CONFIG)
CONFIG['data_source'] = 'terminal' if not cli_parse \
and not CONFIG['data_source'] else 'file'
display_header(**CONFIG)
stdout_msg(
"[ INIT ]: Those who see beyond the Realm Of Forms leaped into the Jump Gate...\n",
silence=CONFIG.get('silent')
)
try:
lock_n_load = setup(**CONFIG)
if CONFIG.get('running_mode', '').lower() == 'cleanup':
stdout_msg(
'[ ACTION ]: Cleaning up files from disk...',
silence=CONFIG.get('silent')
)
clean = cleanup(full=True, **CONFIG)
stdout_msg(
'Terminating with exit code (%s)' % str(action_result['exit']),
silence=CONFIG.get('silent'), done=True
)
exit(action_result['exit'])
check = check_preconditions(**CONFIG)
if not check:
details = action_result.get('msg', '')
action_result.update({
'msg': 'Action preconditions check failed for running mode '\
'%s. Details: %s' % (CONFIG.get('running_mode'), details),
'exit': 1,
})
exit(action_result['exit'])
if not cli_parse or CONFIG.get('data_source').lower() == 'terminal':
CONFIG['batch'] = False
run = init_terminal_running_mode(**CONFIG)
else:
run = init_file_running_mode(**CONFIG)
except Exception as e:
action_result.update({'msg': str(e), 'exit': 10})
finally:
if CONFIG.get('cleanup'):
clean = cleanup(**CONFIG)
if CONFIG.get('report'):
report = report_action_result(action_result, **CONFIG)
if not report:
action_result.update({
'msg': 'Failed to generate report %s'
% CONFIG.get('report_file'),
'exit': 20,
})
print(); stdout_msg(
'Terminating with exit code (%s)' % str(action_result['exit']),
silence=CONFIG.get('silent'), done=True
)
exit(action_result['exit'])
if __name__ == '__main__':
init()
# CODE DUMP