-
Notifications
You must be signed in to change notification settings - Fork 26
/
firmware_password_manager.py
executable file
·1168 lines (988 loc) · 46.5 KB
/
firmware_password_manager.py
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
996
997
998
999
1000
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
"""
This should not be blank.
"""
# Copyright (c) 2020 University of Utah Student Computing Labs. ################
# All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appears in all copies and
# that both that copyright notice and this permission notice appear
# in supporting documentation, and that the name of The University
# of Utah not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission. This software is supplied as is without expressed or
# implied warranties of any kind.
################################################################################
# firmware_password_manager.py #################################################
#
# A Python script to help Macintosh administrators manage the firmware passwords
# of their computers.
#
#
# 2.0.0 2015.11.05 Initial python rewrite. tjm
#
# 2.1.0 2016.03.07 "Now with spinning rims"
# bug fixes, obfuscation features,
# additional tools and examples. tjm
#
# 2.1.1 2016.03.16 slack identifier customization,
# logic clarifications. tjm
#
# 2.1.2 2016.03.16 cleaned up argparse. tjm
#
# 2.1.3 2016.04.04 remove obsolete flag logic. tjm
#
# 2.1.4 2017.10.23 using rm -P for secure delete,
# added additional alerting, additional pylint cleanup. tjm
#
# 2.5.0 2017.11.14 removed flags, uses configuration file,
# reintroduced setregproptool functionality,
# removed management_tools, ported to
# python3, added testing fuctionality. tjm
#
# 2.5.0 2020.01.23 2.5 actually finished and committed. tjm
#
#
#
# keyfile format:
#
# | comment:passwords <-- comments are ignored, except for new.
# | new:newpassword <-- the new password to be installed.
#
################################################################################
# notes: #######################################################################
#
# ./firmware_password_manager_cfg_v2.5b3.py -c private.INI -t
#
#
# sudo pyinstaller --onefile firmware_password_manager.py
#
#
#
################################################################################
# external tool documentation ##################################################
#
# firmwarepasswd v 1.0
# Copyright (C) 2014 Apple Inc. All Rights Reserved.
#
#
# Usage: firmwarepasswd [OPTION]
#
# ? Show usage
# -h Show usage
# -setpasswd Set a firmware password. You will be promted for passwords as needed.
# NOTE: if this is the first password set, and no mode is
# in place, the mode will automatically be set to "command"
# -setmode [mode] Set mode to:
# "command" - password required to change boot disk
# "full" - password required on all startups
# NOTE: cannot set a mode without having set a password
# -mode Prints out the current mode setting
# -check Prints out whether there is / isn't a firmware password is set
# -delete Delete current firmware password and mode setting
# -verify Verify current firmware password
# -unlockseed Generates a firmware password recovery key
# NOTE: Machine must be stable for this command to generate
# a valid seed. No pending changes that need a restart.
# NOTE: Seed is only valid until the next time a firmware password
# command occurs.
#
#
#
# setregproptool v 2.0 (9) Aug 24 2013
# Copyright (C) 2001-2010 Apple Inc.
# All Rights Reserved.
#
# Usage: setregproptool [-c] [-d [-o <old password>]] [[-m <mode> -p <password>] -o <old password>]
#
# -c Check whether password is enabled.
# Sets return status of 0 if set, 1 otherwise.
# -d Delete current password/mode.
# Requires current password on some machines.
# -p Set password.
# Requires current password on some machines.
# -m Set security mode.
# Requires current password on some machines.
# Mode can be either "full" or "command".
# Full mode requires entry of the password on
# every boot, command mode only requires entry
# of the password if the boot picker is invoked
# to select a different boot device.
#
# When enabling the Firmware Password for the first
# time, both the password and mode must be provided.
# Once the firmware password has been enabled, providing
# the mode or password alone will change that parameter
# only.
#
# -o Old password.
# Only required on certain machines to disable
# or change password or mode. Optional, if not
# provided the tool will prompt for the password.
#
################################################################################
#
# imports
from argparse import RawTextHelpFormatter
import argparse
import base64
import configparser
import hashlib
import inspect
import json
import logging
import os
import platform
import plistlib
import re
import socket
import subprocess
import sys
import pexpect
import requests
class FWPM_Object(object):
"""
This should not be blank.
"""
def __init__(self, args, logger, master_version):
"""
This should not be blank.
"""
self.args = args
self.logger = logger
self.master_version = master_version
self.srp_path = None
self.fwpwd_path = None
self.config_options = {}
self.local_identifier = None
self.passwords_raw = None
self.fwpw_managed_string = None
self.new_password = None
self.other_password_list = []
self.current_fwpw_state = False
self.current_fwpm_hash = None
self.clean_exit = False
self.read_config = False
self.read_keyfile = False
self.modify_fwpw = False
self.modify_nvram = False
self.matching_hashes = False
self.matching_passwords = False
self.configuration_path = None
self.system_version = platform.mac_ver()[0].split(".")
self.srp_check()
self.fwpwd_check()
if self.fwpwd_path:
self.current_fwpw_state = self.fwpwd_current_state()
elif self.srp_path:
self.current_fwpw_state = self.srp_current_state()
self.injest_config()
if self.config_options["slack"]["use_slack"]:
self.slack_optionator()
self.injest_keyfile()
self.hash_current_state()
self.hash_incoming()
#
# What if the string isn't a hash?!?
if (self.current_fwpm_hash == self.fwpw_managed_string) and self.config_options["flags"]["management_string_type"] == 'hash':
self.matching_hashes = True
self.master_control()
def master_control(self):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
if self.current_fwpm_hash == self.fwpw_managed_string:
if self.logger:
self.logger.info("Hashes match. No change required.")
else:
if self.logger:
self.logger.info("Hashes DO NOT match. Change required.")
if self.fwpwd_path:
self.fwpwd_change()
self.secure_delete()
elif self.srp_path:
self.srp_change()
self.secure_delete()
else:
print("No FW tool found.")
quit()
#
# nvram maintenance
#
self.nvram_manager()
#
# some kind of post action reporting.
# handle reboot flag here?
#
self.exit_manager()
def hash_current_state(self):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
existing_keyfile_hash = None
if self.logger:
self.logger.info("Checking existing hash.")
try:
existing_keyfile_hash_raw = subprocess.check_output(["/usr/sbin/nvram", "-p"]).decode('utf-8')
existing_keyfile_hash_raw = existing_keyfile_hash_raw.split('\n')
for item in existing_keyfile_hash_raw:
if "fwpw-hash" in item:
existing_keyfile_hash = item
else:
self.current_fwpm_hash = None
self.current_fwpm_hash = existing_keyfile_hash.split("\t")[1]
if self.args.testmode:
print("Existing hash: %s" % self.current_fwpm_hash)
except:
pass
def hash_incoming(self):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
if self.logger:
self.logger.info("Checking incoming hash.")
if self.config_options["flags"]["management_string_type"] == "custom":
#
# ?!?!?!?!?!?!?
#
self.fwpw_managed_string = self.config_options["flags"]["management_string_type"]
elif self.config_options["flags"]["management_string_type"] == "hash":
hashed_key = hashlib.new('sha256')
# hashed_key.update(self.passwords_raw.encode('utf-8'))
hashed_key.update(self.new_password.encode('utf-8'))
for entry in sorted(self.other_password_list):
hashed_key.update(entry.encode('utf-8'))
self.fwpw_managed_string = hashed_key.hexdigest()
# prepend '2:' to denote hash created with v2 of script, will force a password change from v1
self.fwpw_managed_string = '2:' + self.fwpw_managed_string
else:
self.fwpw_managed_string = None
if self.args.testmode:
print("Incoming hash: %s" % self.fwpw_managed_string)
def secure_delete(self):
"""
attempts to securely delete the keyfile with medium overwrite and zeroing settings
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
if self.logger:
self.logger.info("Deleting keyfile")
use_srm = bool(os.path.exists("/usr/bin/srm"))
if self.args.testmode:
if self.logger:
self.logger.info("Test mode, keyfile not deleted.")
return
if use_srm:
try:
subprocess.call(["/usr/bin/srm", "-mz", self.config_options["keyfile"]["path"]])
if self.logger:
self.logger.info("keyfile deleted successfuly.")
except Exception as exception_message:
if self.logger:
self.logger.critical("Issue with attempt to remove keyfile. %s" % exception_message)
else:
try:
deleted_keyfile = subprocess.call(["/bin/rm", "-Pf", self.config_options["keyfile"]["path"]])
print("return: %r" % deleted_keyfile)
if self.logger:
self.logger.info("keyfile deleted successfuly.")
except Exception as exception_message:
if self.logger:
self.logger.critical("Issue with attempt to remove keyfile. %s" % exception_message)
# is this really needed?
if os.path.exists(self.config_options["keyfile"]["path"]):
if self.logger:
self.logger.critical("Failure to remove keyfile.")
else:
if self.logger:
self.logger.info("Keyfile removed.")
return
def injest_config(self):
"""
attempts to consume and format configuration file
"""
# handle parsing errors in cfg?!?
# where to handle looking for cfg in specific locations?!?
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
try:
if os.path.exists(self.args.configfile):
# firmware_password_manager_cfg_v2.5b8.py:434: DeprecationWarning: The SafeConfigParser class has been renamed to ConfigParser in Python 3.2. This alias will be removed in future versions. Use ConfigParser directly instead.
config = configparser.ConfigParser(allow_no_value=True)
config.read(self.args.configfile)
self.config_options["flags"] = {}
self.config_options["keyfile"] = {}
self.config_options["logging"] = {}
self.config_options["slack"] = {}
self.config_options["os"] = {}
self.config_options["fwpm"] = {}
for section in ["flags", "keyfile", "logging", "slack"]:
for item in config.options(section):
if "use_" in item:
try:
self.config_options[section][item] = config.getboolean(section, item)
except:
self.config_options[section][item] = False
elif "path" in item:
self.config_options[section][item] = config.get(section, item)
else:
self.config_options[section][item] = config.get(section, item)
if self.args.testmode:
print("Configuration file variables:")
for key, value in self.config_options.items():
print(key)
for sub_key, sub_value in value.items():
print("\t%s %r" % (sub_key, sub_value))
else:
if self.logger:
self.logger.critical("Issue locating configuration file, exiting.")
sys.exit()
except Exception as exception_message:
if self.logger:
self.logger.critical("Issue reading configuration file, exiting. %s" % exception_message)
sys.exit()
self.read_config = True
def sanity_check(self):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
def srp_check(self):
"""
full setregproptool support later, if ever.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
if os.path.exists('/usr/local/bin/setregproptool'):
self.srp_path = '/usr/local/bin/setregproptool'
elif os.path.exists(os.path.dirname(os.path.abspath(__file__)) + '/setregproptool'):
self.srp_path = os.path.dirname(os.path.abspath(__file__)) + '/setregproptool'
else:
print("SRP #3a")
if self.logger:
self.logger.info("SRP path: %s" % self.srp_path)
def srp_current_state(self):
"""
full setregproptool support later, if ever.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
try:
existing_fw_pw = subprocess.call([self.srp_path, "-c"])
if self.logger:
self.logger.info("srp says %r" % existing_fw_pw)
if existing_fw_pw:
return False
# it's weird, I know. Blame Apple.
else:
return True
except:
if self.logger:
self.logger.info("ERROR srp says %r" % existing_fw_pw)
return False
#
# # E:451,15: Undefined variable 'CalledProcessError' (undefined-variable)
# except CalledProcessError:
# if self.logger:
# self.logger.info("ERROR srp says %r" % existing_fw_pw)
# return False
def srp_change(self):
"""
full setregproptool support later, if ever.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
print("Using srp tool!")
print("%r" % self.current_fwpw_state)
def fwpwd_check(self):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
if os.path.exists('/usr/sbin/firmwarepasswd'):
self.fwpwd_path = '/usr/sbin/firmwarepasswd'
else:
print("FWPWD #2b")
if self.logger:
self.logger.info("FWPWD path: %s" % self.fwpwd_path)
def fwpwd_current_state(self):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
existing_fw_pw = subprocess.check_output([self.fwpwd_path, "-check"])
# R:484, 8: The if statement can be replaced with 'return bool(test)' (simplifiable-if-statement)
# return bool('Yes' in existing_fw_pw)
if b'Yes' in existing_fw_pw:
return True
else:
return False
def fwpwd_change(self):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
known_current_password = False
current_password = ''
# is this really needed?!?
new_fw_tool_cmd = [self.fwpwd_path, '-verify']
if self.current_fwpw_state:
if self.logger:
self.logger.info("Verifying current FW password")
for index in reversed(range(len(self.other_password_list))):
child = pexpect.spawn(' '.join(new_fw_tool_cmd))
child.expect('Enter password:')
child.sendline(self.other_password_list[index])
result = child.expect(['Correct', 'Incorrect'])
if result == 0:
#
# correct password, exit loop
current_password = self.other_password_list[index]
known_current_password = True
break
else:
#
# wrong password, keep going
continue
#
# We've discovered the currently set firmware password
if known_current_password:
#
# Deleting firmware password
if not self.config_options["flags"]["use_fwpw"]:
if self.logger:
self.logger.info("Deleting FW password")
new_fw_tool_cmd = [self.fwpwd_path, '-delete']
if self.logger:
self.logger.info(' '.join(new_fw_tool_cmd))
child = pexpect.spawn(' '.join(new_fw_tool_cmd))
child.expect('Enter password:')
child.sendline(current_password)
result = child.expect(['removed', 'incorrect'])
if result == 0:
#
# password accepted, log result and exit
if self.logger:
self.logger.info("Finished. Password should be removed. Restart required. [%i]" % (index + 1))
self.clean_exit = True
else:
if self.logger:
self.logger.critical("Asked to delete, current password not accepted. Exiting.")
# secure_delete_keyfile(logger, args, config_options)
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Asked to delete, current password not accepted.", '', 'error')
# self.error_bot.send_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Asked to delete, current password not accepted.")
sys.exit(1)
#
# Current and new password are identical
#
#
# WAIT. How (is/would) this possible, clearly the hashes don't match!!! What if they aren't using hashes?
#
#
elif current_password == self.new_password:
self.matching_passwords = True
self.clean_exit = True
#
# Change current firmware password to new password
else:
if self.logger:
self.logger.info("Updating FW password")
new_fw_tool_cmd = [self.fwpwd_path, '-setpasswd']
if self.logger:
self.logger.info(' '.join(new_fw_tool_cmd))
child = pexpect.spawn(' '.join(new_fw_tool_cmd))
result = child.expect('Enter password:')
if result == 0:
pass
else:
if self.logger:
self.logger.error("bad response from firmwarepasswd. Exiting.")
self.secure_delete()
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Bad response from firmwarepasswd.", '', 'error')
sys.exit(1)
child.sendline(current_password)
result = child.expect('Enter new password:')
if result == 0:
pass
else:
if self.logger:
self.logger.error("bad response from firmwarepasswd. Exiting.")
self.secure_delete()
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Bad response from firmwarepasswd.", '', 'error')
sys.exit(1)
child.sendline(self.new_password)
result = child.expect('Re-enter new password:')
if result == 0:
pass
else:
if self.logger:
self.logger.error("bad response from firmwarepasswd. Exiting.")
self.secure_delete()
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Bad response from firmwarepasswd.", '', 'error')
sys.exit(1)
child.sendline(self.new_password)
child.expect(pexpect.EOF)
child.close()
if self.logger:
self.logger.info("Updated FW Password.")
self.clean_exit = True
#
# Unable to match current password with contents of keyfile
else:
if self.logger:
self.logger.critical("Current FW password not in keyfile. Quitting.")
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Current FW password not in keyfile.", '', 'error')
self.secure_delete()
sys.exit(1)
#
# No current firmware password, setting it
else:
new_fw_tool_cmd = [self.fwpwd_path, '-setpasswd']
if self.logger:
self.logger.info(' '.join(new_fw_tool_cmd))
child = pexpect.spawn(' '.join(new_fw_tool_cmd))
result = child.expect('Enter new password:')
print(child.before)
if result == 0:
pass
else:
if self.logger:
self.logger.error("bad response from firmwarepasswd. Exiting.")
self.secure_delete()
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Bad response from firmwarepasswd.", '', 'error')
sys.exit(1)
child.sendline(self.new_password)
result = child.expect('Re-enter new password:')
if result == 0:
pass
else:
if self.logger:
self.logger.error("bad response from firmwarepasswd. Exiting.")
self.secure_delete()
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Bad response from firmwarepasswd.", '', 'error')
sys.exit(1)
child.sendline(self.new_password)
child.expect(pexpect.EOF)
child.close()
if self.logger:
self.logger.info("Added FW Password.")
self.clean_exit = True
def slack_optionator(self):
"""
ip, mac, hostname
computername
serial
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
if self.verify_network():
try:
full_ioreg = subprocess.check_output(['ioreg', '-l']).decode('utf-8')
serial_number_raw = re.findall('\"IOPlatformSerialNumber\" = \"(.*)\"', full_ioreg)
serial_number = serial_number_raw[0]
if self.args.testmode:
print("Serial number: %r" % serial_number)
if self.config_options["slack"]["slack_identifier"].lower() == 'ip' or self.config_options["slack"]["slack_identifier"].lower() == 'mac' or self.config_options["slack"]["slack_identifier"].lower() == 'hostname':
processed_device_list = []
# Get ordered list of network devices
base_network_list = subprocess.check_output(["/usr/sbin/networksetup", "-listnetworkserviceorder"]).decode('utf-8')
network_device_list = re.findall(r'\) (.*)\n\(.*Device: (.*)\)', base_network_list)
ether_up_list = subprocess.check_output(["/sbin/ifconfig", "-au", "ether"]).decode('utf-8')
for device in network_device_list:
device_name = device[0]
port_name = device[1]
try:
if self.args.testmode:
print(device_name, port_name)
if port_name in ether_up_list:
device_info_raw = subprocess.check_output(["/sbin/ifconfig", port_name]).decode('utf-8')
mac_address = re.findall('ether (.*) \n', device_info_raw)
if self.args.testmode:
print("%r" % mac_address)
ether_address = re.findall('inet (.*) netmask', device_info_raw)
if self.args.testmode:
print("%r" % ether_address)
if len(ether_address) and len(mac_address):
processed_device_list.append([device_name, port_name, ether_address[0], mac_address[0]])
except Exception as this_exception:
print(this_exception)
if processed_device_list:
if self.logger:
self.logger.info("1 or more active IP addresses. Choosing primary.")
if self.args.testmode:
print("Processed devices: ", processed_device_list)
if self.config_options["slack"]["slack_identifier"].lower() == 'ip':
self.local_identifier = processed_device_list[0][2] + " (" + processed_device_list[0][0] + ":" + processed_device_list[0][1] + ")"
elif self.config_options["slack"]["slack_identifier"].lower() == 'mac':
self.local_identifier = processed_device_list[0][3] + " (" + processed_device_list[0][0] + ":" + processed_device_list[0][1] + ")"
elif self.config_options["slack"]["slack_identifier"].lower() == 'hostname':
try:
self.local_identifier = socket.getfqdn()
except:
if self.logger:
self.logger.error("error discovering hostname info.")
self.local_identifier = serial_number
else:
if self.logger:
self.logger.error("error discovering IP info.")
self.local_identifier = serial_number
elif self.config_options["slack"]["slack_identifier"].lower() == 'computername':
try:
cname_identifier_raw = subprocess.check_output(['/usr/sbin/scutil', '--get', 'ComputerName'])
self.local_identifier = cname_identifier_raw.split('\n')[0]
if self.logger:
self.logger.info("Computername: %r" % self.local_identifier)
except:
if self.logger:
self.logger.info("error discovering computername.")
self.local_identifier = serial_number
elif self.config_options["slack"]["slack_identifier"].lower() == 'serial':
self.local_identifier = serial_number
if self.logger:
self.logger.info("Serial number: %r" % self.local_identifier)
else:
if self.logger:
self.logger.info("bad or no identifier flag, defaulting to serial number.")
self.local_identifier = serial_number
if self.args.testmode:
print("Local identifier: %r" % self.local_identifier)
except Exception as this_exception:
print(this_exception)
self.config_options["slack"]["use_slack"] = False
else:
self.config_options["slack"]["use_slack"] = False
if self.logger:
self.logger.info("No network detected.")
def slack_message(self, message, icon, type):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
slack_info_channel = False
slack_error_channel = False
if self.config_options["slack"]["use_slack"] and self.config_options["slack"]["slack_info_url"]:
slack_info_channel = True
if self.config_options["slack"]["use_slack"] and self.config_options["slack"]["slack_error_url"]:
slack_error_channel = True
if slack_error_channel and type == 'error':
slack_url = self.config_options["slack"]["slack_error_url"]
elif slack_info_channel:
slack_url = self.config_options["slack"]["slack_info_url"]
else:
return
payload = {'text': message, 'username': 'FWPM ' + self.master_version, 'icon_emoji': ':key:'}
response = requests.post(slack_url, data=json.dumps(payload), headers={'Content-Type': 'application/json'})
self.logger.info('Response: ' + str(response.text))
self.logger.info('Response code: ' + str(response.status_code))
def reboot_exit(self):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
def injest_keyfile(self):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
path_to_keyfile_exists = os.path.exists(self.config_options["keyfile"]["path"])
if not path_to_keyfile_exists:
if self.logger:
self.logger.critical("%r does not exist. Exiting." % self.config_options["keyfile"]["path"])
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Keyfile does not exist.", '', 'error')
sys.exit(2)
if self.logger:
self.logger.info("Reading password file")
if self.config_options["keyfile"]["use_obfuscation"]:
#
# unobfuscate plist
if self.logger:
self.logger.info("Reading plist")
passwords = []
if "plist" in self.config_options["keyfile"]["path"]:
try:
keyfile_plist = plistlib.readPlist(self.config_options["keyfile"]["path"])
content_raw = keyfile_plist["data"]
except:
if self.logger:
self.logger.critical("Error reading plist. Exiting.")
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Error reading plist.", '', 'error')
sys.exit(1)
else:
try:
with open(self.config_options["keyfile"]["path"], 'r') as reader:
content_raw = reader.read()
except:
if self.logger:
self.logger.critical("Error reading plist. Exiting.")
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Error reading plist.", '', 'error')
sys.exit(1)
content_raw = base64.b64decode(content_raw)
content_raw = content_raw.decode('utf-8').split(",")
content_raw = [x for x in content_raw if x]
output_string = ""
for item in content_raw:
label, pword = item.split(':')
pword = base64.b64decode(pword)
try:
commented = label.split('#')[1]
commented = base64.b64decode(commented)
is_commented = True
except:
is_commented = False
if is_commented:
output_string = "#" + commented.decode('utf-8') + ":" + pword.decode('utf-8')
passwords.append(output_string)
else:
uncommented = base64.b64decode(label)
output_string = uncommented.decode('utf-8') + ":" + pword.decode('utf-8')
passwords.append(output_string)
else:
#
# read keyfile
if self.logger:
self.logger.info("Reading plain text")
try:
with open(self.config_options["keyfile"]["path"], "r") as keyfile:
self.passwords_raw = keyfile.read()
passwords = self.passwords_raw.splitlines()
except:
if self.logger:
self.logger.critical("Error reading keyfile. Exiting.")
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Error reading keyfile.", '', 'error')
sys.exit(1)
if self.logger:
self.logger.info("Closed password file")
# new_password = None
# other_password_list = []
#
# parse data from keyfile and build list of passwords
for entry in passwords:
try:
key, value = entry.split(":", 1)
except Exception as this_exception:
if self.logger:
self.logger.critical("Malformed keyfile, key:value format required. %r. Quitting." % this_exception)
self.secure_delete()
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Malformed keyfile.", '', 'error')
sys.exit(1)
if key.lower() == 'new':
if self.new_password is not None:
if self.logger:
self.logger.critical("Malformed keyfile, multiple new keys. Quitting.")
self.secure_delete()
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Malformed keyfile.", '', 'error')
sys.exit(1)
else:
self.new_password = value
self.other_password_list.append(value)
else:
self.other_password_list.append(value)
if self.logger:
self.logger.info("Sanity checking password file contents")
if self.new_password is None and self.config_options["flags"]["use_fwpw"]:
if self.logger:
self.logger.critical("Malformed keyfile, no \'new\' key. Quitting.")
self.secure_delete()
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :no_entry:\n" + "Malformed keyfile.", '', 'error')
sys.exit(1)
self.read_keyfile = True
try:
self.other_password_list.remove(self.new_password)
except:
pass
def nvram_manager(self):
"""
This should not be blank.
"""
if self.logger:
self.logger.info("%s: activated" % inspect.stack()[0][3])
if self.clean_exit:
if not self.config_options["flags"]["use_fwpw"]:
try:
subprocess.call(["/usr/sbin/nvram", "-d", "fwpw-hash"])
if self.logger:
self.logger.info("nvram entry pruned.")
if self.config_options["slack"]["use_slack"]:
self.slack_message("_*" + self.local_identifier + "*_ :unlock:\n" + "FWPW and nvram entry removed.", '', 'info')
#
# Should we return here?
#
except Exception as exception_message:
if self.logger:
self.logger.warning("nvram reported error attempting to remove hash. Exiting. %s" % exception_message)
#
# Slack?
#