I needed a way to make a CLI based script a bit secure. So, I thought of the following workflow:
- the client will get the MAC of the user, sha256 it and put it in a file at a specific location
- then in my program, I'm also doing the same thing, check to see if the sha256 of the MAC is present in the file (and if it matches) then let the user use the script.
- more, that text file will have multiple lines in it (but the only one that maters is the first one)
I'm aware that this might not be the best option for securing a script, but the client said ("do this to make it a bit harder for somebody else to use it").
import hashlib
import sys
from uuid import getnode
def get_mac():
"""return the MAC address of a specific machine"""
return bytes(str(getnode()), 'utf-8')
def hash_mac():
"""hash the MAC using sha256 algorithm"""
return hashlib.sha256(get_mac()).hexdigest()
def main():
with open('uid_file.cfg') as uid_file:
first_line = uid_file.readline().replace('\n', '')
if str(hash_mac()) in first_line and len(str(hash_mac())) == len(first_line):
print('Match MAC and execute program here\n')
else:
print('MAC does not match! Exiting...')
sys.exit(0)
if __name__ == '__main__':
main()
Any advice on what could go wrong or alternatives / good practices are welcome.
uid_file.cfg
isn't present you're a bit buggered \$\endgroup\$print('Match MAC and execute program here\n')
. Most likely in a function, sayfoo()
. What could possibly prevent any user to open this file and modifyif __name__ == '__main__': main()
intoif __name__ == '__main__': foo()
? \$\endgroup\$