keyserver-fs/upload-file.py

73 lines
2.7 KiB
Python
Raw Normal View History

2018-05-28 15:08:18 +01:00
import subprocess
import random
import string
import base64
import sys
import os
#check if input is a file
if os.path.exists(sys.argv[1]) != True:
2018-06-10 23:13:36 +01:00
print "you typed something wrong, could not find that file"
2018-05-28 15:08:18 +01:00
else:
2018-06-10 23:13:36 +01:00
file_to_upload = sys.argv[1]
if "." in sys.argv[1]:
domain = sys.argv[1][sys.argv[1].index("."):]
else:
domain = ".com"
2018-05-28 15:08:18 +01:00
#generate random data for credentials
user_name = ''.join(random.choice(string.ascii_uppercase) for _ in range(10))
2018-06-10 23:13:36 +01:00
email = ''.join(random.choice(string.ascii_uppercase) for _ in range(10)) + "@"\
+ ''.join(random.choice(string.ascii_uppercase) for _ in range(5)) + domain
2018-05-28 15:08:18 +01:00
passphrase = ''.join(random.choice(string.ascii_uppercase) for _ in range(5))
key_server = "eu.pool.sks-keyservers.net" #any key server is good as it will propogate world wide
#unattended key generation
p = subprocess.Popen('gpg2 --batch --pinentry-mode=loopback --passphrase ' + passphrase +\
2018-06-10 23:13:36 +01:00
' --quick-gen-key "' + user_name + ' ' + email + '" rsa1024',\
shell=True, stdout=subprocess.PIPE)
2018-05-28 15:08:18 +01:00
out, err = p.communicate()
#get pub key
2018-06-10 23:13:36 +01:00
p = subprocess.Popen('gpg2 --list-key ' + email, shell=True, stdout=subprocess.PIPE)
2018-05-28 15:08:18 +01:00
out, err = p.communicate()
key = out.split()[6] # parse out the key so we can use to send keys to the key servers
#open file in binary and break it up into 1305byte chunks
chunk_list = []
with open(file_to_upload, 'rb') as infile:
2018-06-10 23:13:36 +01:00
while True:
chunk = infile.read(1305)
if not chunk:
break
chunk_list.append(chunk)
2018-05-28 15:08:18 +01:00
#encode binary chunks into base64 strings
2018-06-10 23:13:36 +01:00
i = 0
2018-05-28 15:08:18 +01:00
for x in chunk_list:
2018-06-10 23:13:36 +01:00
new_uid = str(i) + "@" + base64.b64encode(x)
#below you need to use --batch so you only have to type the password in once,
#could not get --passphrase to work for some reason
p = subprocess.Popen("gpg2 --batch --pinentry-mode=loopback --passphrase " + passphrase\
2018-06-10 23:13:36 +01:00
+ " --quick-add-uid " + email + " " + new_uid, shell=True, stdout=subprocess.PIPE)
out, err = p.communicate()
i += 1
2018-05-28 15:08:18 +01:00
#finally send keys to a server
2018-06-10 23:13:36 +01:00
p = subprocess.Popen("gpg2 --keyserver " + key_server + " --send-keys "\
+ key, shell=True, stdout=subprocess.PIPE)
2018-05-28 15:08:18 +01:00
out, err = p.communicate()
#remove keys when done as they are not needed anymore
2018-06-10 23:13:36 +01:00
p = subprocess.Popen("gpg --batch --yes --delete-secret-keys " + key +\
"&& gpg --batch --yes --delete-keys " + key, shell=True, stdout=subprocess.PIPE)
out, err = p.communicate()
2018-06-10 23:13:36 +01:00
if not err:
print "removing temp keys\n"
print "It can take 3-10mins before your key appears on your chosen server\n"
print "http://" + key_server + "/pks/lookup?search=" + email + "&op=index"
else:
2018-06-10 23:13:36 +01:00
print "something went wrong try again"