Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os.path
- from OpenSSL import *
- from struct import *
- from Crypto.Hash import SHA512
- from Crypto.Protocol import KDF
- from Crypto.Cipher import AES
- from Crypto.Util import Counter
- from sys import *
- def main():
- if not (os.path.isfile("passwd_file") and os.path.isfile("master_passwd")):
- master_registration()
- else:
- if len(argv)==1:
- print("Enter your master password in command line!")
- else:
- check_match(argv[1])
- user_key=get_key(argv[1])
- text_byte_content=extract(user_key,"INTEGRITY CHECK OF PASSWORD FILE FAILED!\n")
- check_integrity(text_byte_content[1],"INTEGRITY CHECK OF PASSWORD FILE FAILED!\n")
- user_cmd(user_key,text_byte_content)
- def master_registration():
- pff=open("passwd_file","wb")
- mpf=open("master_passwd","wb")
- mp=input("Please enter a master password:\n")
- IV=rand.bytes(256)
- mpf.write(IV)
- mpf.write(hash_mp(mp,IV))
- mpf.close()
- pff.close()
- def hash_mp(mp,IV):
- pwIV=b''.join([pack("B",ord(mp[i])) for i in range(len(mp))])
- pwIV+=IV
- h = SHA512.new()
- h.update(pwIV)
- return h.digest()
- def check_match(pw):
- mpf=open("master_passwd","rb")
- IVhash=mpf.read()
- IV,Hash=IVhash[:256],IVhash[256:]
- if hash_mp(pw,IV)!=Hash:
- print("WRONG MASTER PASSWORD!\n")
- mpf.close()
- exit()
- def extract(key,words):
- pff=open("passwd_file","rb")
- contents=pff.read()
- if len(contents)>=76:
- root,num,IV,curpos=contents[:64],contents[64:68],contents[68:76],76
- num_users=unpack("I",num)[0]
- text_list,byte_list=[num_users,IV],[root,num,IV]
- cipher=initialize_cipher(IV,key)
- for i in range(num_users):
- unlen_byte,curpos=contents[curpos:curpos+4],curpos+4
- check_len(unlen_byte,4,words)
- unlen=unpack("I",unlen_byte)[0]
- un_ct,curpos=contents[curpos:curpos+unlen],curpos+unlen
- check_len(un_ct,unlen,words)
- pwlen_byte,curpos=contents[curpos:curpos+4],curpos+4
- check_len(pwlen_byte,4,words)
- pwlen=unpack("I",pwlen_byte)[0]
- pw_ct,curpos=contents[curpos:curpos+pwlen],curpos+pwlen
- check_len(pw_ct,pwlen,words)
- dnlen_byte,curpos=contents[curpos:curpos+4],curpos+4
- check_len(dnlen_byte,4,words)
- dnlen=unpack("I",dnlen_byte)[0]
- dn_ct,curpos=contents[curpos:curpos+dnlen],curpos+dnlen
- check_len(dn_ct,dnlen,words)
- byte_list+=[unlen_byte,un_ct,pwlen_byte,pw_ct,dnlen_byte,dn_ct]
- un_byte,pw_byte,dn_byte = cipher.decrypt(un_ct),cipher.decrypt(pw_ct),cipher.decrypt(dn_ct)
- un=''.join([chr(x) for x in un_byte])
- pw=''.join([chr(x) for x in pw_byte])
- dn=''.join([chr(x) for x in dn_byte])
- text_list.append([un,pw,dn])
- check_len(contents,curpos,words)
- pff.close()
- return [text_list,byte_list]
- elif len(contents)==0:
- pff.close()
- return [[0,b''],[]]
- else:
- check_integrity([],words,True)
- def check_len(item,length,words):
- if len(item)!=length:
- check_integrity([],words,True)
- def initialize_cipher(IV,key):
- l=list(unpack("B"*8,IV))
- initial=sum([l[i]*(256**(len(l)-i-1)) for i in range(len(l))])
- ctr = Counter.new(128,initial_value=initial)
- return AES.new(key, AES.MODE_CTR, counter=ctr)
- def user_input(mode="all"):
- un,pw,dn="","",""
- while True and mode=="all":
- un=input("Please enter a username less than 80 characters without space character and '!':\n")
- if '!' not in un and ' ' not in un and len(un)<80:
- break
- elif len(un)>=80:
- print("The username should be less than 80 characters!\n")
- else:
- print("The username should not contain space character or '!'\n")
- while True and (mode=="all" or mode=="chg_acct"):
- word=" "
- if mode=="chg_acct":
- word=" new "
- pw=input("Please enter a"+word+"password less than 80 characters without space character and '!':\n")
- if '!' not in pw and ' ' not in pw and len(pw)<80:
- break
- elif len(pw)>=80:
- print("The password should be less than 80 characters!\n")
- else:
- print("The password should not contain space character or '!'\n")
- while True and (mode=="all" or mode=="get_pw"):
- dn=input("Please enter a domain name less than 80 characters without space character and '!':\n")
- if '!' not in dn and ' ' not in dn and len(dn)<80:
- break
- elif len(dn)>=80:
- print("The domain name should be less than 80 characters!\n")
- else:
- print("The domain name should not contain space character or '!'\n")
- return un,pw,dn
- def check_exist(un,dn,pw,content,command):
- print(content)
- if command=="reg_acct":
- return True in [user[0]==un or user[2]==dn for user in content[2:]]
- elif command=="del_acct" or command=="chg_acct":
- return True in [user[0]==un and user[1]==pw and user[2]==dn for user in content[2:]]
- else:
- unpw=[(user[0],user[1]) for user in content[2:] if user[2]==dn]
- if unpw==[]:
- return False
- else:
- return unpw[0][0],unpw[0][1]
- def re_encrypt(key,content):
- pff=open("passwd_file","wb")
- IV,num=rand.bytes(8),pack("I",content[0])
- encrypt,users_list=num+IV,[num,IV]
- cipher=initialize_cipher(IV,key)
- for i in range(content[0]):
- user=content[2+i]
- un,pw,dn = cipher.encrypt(user[0]),cipher.encrypt(user[1]),cipher.encrypt(user[2])
- unlen,pwlen,dnlen=pack('I',len(un)),pack('I',len(pw)),pack('I',len(dn))
- encrypt+=unlen+un+pwlen+pw+dnlen+dn
- users_list+=[unlen,un,pwlen,pw,dnlen,dn]
- print (users_list)
- root=MHT_construct(users_list,SHA512.new())
- print (root)
- pff.write(root+encrypt)
- pff.close()
- return [root]+users_list
- def MHT_construct(nodes,h):
- if len(nodes)==1:
- return nodes[0]
- L=[]
- for i in range(len(nodes)//2):
- h.update(nodes[i*2]+nodes[i*2+1])
- L.append(h.digest())
- if len(nodes)%2==0:
- return MHT_construct(L,h)
- else:
- L.append(nodes[-1])
- return MHT_construct(L,h)
- def check_integrity(content,words,len_inconsist=False):
- if len_inconsist:
- print(words)
- exit()
- if content!=[]:
- if not content[0]==MHT_construct(content[1:],SHA512.new()):
- print(words)
- exit()
- else:
- return True
- def reg_acct(key,content):
- un,pw,dn=user_input()
- if check_exist(un,dn,pw,content[0],"reg_acct"):
- return False
- content[0].append([un,pw,dn])
- content[0][0]+=1
- byte=re_encrypt(key,content[0])
- content[0][1]=byte[2]
- return [content[0],byte]
- def del_acct(key,content):
- un,pw,dn=user_input()
- if not check_exist(un,dn,pw,content[0],"del_acct"):
- return False
- content[0].remove([un,pw,dn])
- content[0][0]=content[0][0]-1
- byte=re_encrypt(key,content[0])
- content[0][1]=byte[2]
- return [content[0],byte]
- def chg_acct(key,content):
- un,pw,dn=user_input("all")
- if not check_exist(un,dn,pw,content[0],"chg_acct"):
- return False
- _,newpw,_=user_input("chg_acct")
- content[0].remove([un,pw,dn])
- content[0].append([un,newpw,dn])
- byte=re_encrypt(key,content[0])
- content[0][1]=byte[2]
- return [content[0],byte]
- def get_pw(key,content):
- un,pw,dn=user_input("get_pw")
- result=check_exist(un,dn,pw,content,"get_pw")
- if result==False:
- print("USER ACCOUNT DOES NOT EXIST!\n")
- else:
- un,pw=result
- print("username "+un+" password "+pw+"\n")
- def get_key(mpw):
- return KDF.PBKDF2(mpw,b"mulalamulala",dkLen=32)
- def user_cmd(key,content):
- print(content[0])
- cmd=input("\nPlease enter an input command below:\n0.exit\n1.check integrity\n2.register account\n3.delete account\n4.change account:\n5.get password\n")
- if cmd not in "012345":
- print("Enter a legal number of user commands!\n")
- user_cmd(key,content)
- else:
- if cmd=='1':
- content=extract(key,"FAILED!\n")
- check_integrity(content[1],"FAILED!\n")
- print("PASSED!\n")
- elif cmd=='2':
- result=reg_acct(key,content)
- if result==False:
- print("\nUSER ACCOUNT ALREADY EXISTS!")
- else:
- content=result
- elif cmd=='3':
- result=del_acct(key,content)
- if result==False:
- print("USER ACCOUNT DOES NOT EXIST!\n")
- else:
- content=result
- elif cmd=='4':
- result=chg_acct(key,content)
- if result==False:
- print("USER ACCOUNT DOES NOT EXIST!\n")
- else:
- content=result
- elif cmd=='5':
- get_pw(key,content[0])
- else:
- exit()
- user_cmd(key,content)
- main()
Add Comment
Please, Sign In to add comment