forked from henrytwo/BlockChainChain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeychain.py
153 lines (111 loc) · 4.01 KB
/
keychain.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
import os
import sys
from console import *
import bcc_main
import time
import glob
import traceback
import dataparsing
import sha256frompubkey
import KeyScraper
from os.path import expanduser
home = expanduser("~")
def check_key(primed):
with open('/var/log/auth.log') as file:
log = file.read().strip().split('\n')[::-1]
for line in log:
if 'RSA SHA256' in line:
raw_key = line.split()[-1]
Console.print('SSH KEY FINGERPRINT: %s' % raw_key, Colors.RED_BOLD)
authorized = key_exists(raw_key)
break
if not primed:
if authorized:
dataparsing.log(raw_key, 'LOGIN')
else:
dataparsing.log(raw_key, 'KICK')
return authorized
def list_keys():
valid_keys = []
if os.path.isfile(home + '/.ssh/authorized_keys'):
with open(home + '/.ssh/authorized_keys', 'r') as file:
keys = file.read().strip().split('\n')
for i in range(len(keys)):
key = keys[i]
if len(key.split()) == 6:
valid_keys.append(' '.join(key.split()[3:]))
else:
open(home + '/.ssh/authorized_keys', 'w').close()
return valid_keys
def key_exists(k):
keys = list_keys()
for i in range(len(keys)):
key = keys[i]
try:
reference_key = sha256frompubkey.sha256_fingerprint_from_pub_key(key)
if 'ssh-rsa' in k:
k = sha256frompubkey.sha256_fingerprint_from_pub_key(k)
if k == reference_key:
return [keys, i]
except:
traceback.print_exc()
else:
return False
def add_key(key):
if not key_exists(key):
dataparsing.log(sha256frompubkey.sha256_fingerprint_from_pub_key(key), 'ADD-KEY')
os.system(
'echo \'command="python3.7 BlockChainChain/bcc_main.py $SSH_ORIGINAL_COMMAND",no-port-forwarding,no-x11-forwarding,no-agent-forwarding %s\' >> %s/.ssh/authorized_keys' % (
key, home))
return True
else:
return False
def revoke_key(key):
k = key_exists(key)
if k:
del k[0][k[1]]
dataparsing.log(sha256frompubkey.sha256_fingerprint_from_pub_key(key), 'REVOKE-KEY')
with open(home + '/.ssh/authorized_keys', 'w') as file:
file.write('\n'.join(k[0]))
return True
else:
return False
def get_name_from_key(key):
keys = glob.glob("/keybase/public/" + bcc_main.user + "/gatekeeper/*")
for f in keys:
k = open(f).read().strip()
if key.strip() == sha256frompubkey.sha256_fingerprint_from_pub_key(k):
return f.split('/')[-1]
def load_key(persist=False):
old_time = time.time()
Console.print('Retrieving keys...\n', Colors.BLACK_BOLD)
keys = KeyScraper.get_key(bcc_main.user)
current_keys = set(list_keys())
Console.print('Loading keys...', Colors.BLUE_BOLD)
Console.print('%i key(s) loaded.\n' % len(keys), Colors.CYAN_BOLD)
for key in keys:
if key not in current_keys:
Console.print('[+] ' + sha256frompubkey.sha256_fingerprint_from_pub_key(key), Colors.GREEN_BOLD)
add_key(key)
# So revokes are verbose
if not persist:
for r in current_keys - keys:
Console.print('[-] ' + sha256frompubkey.sha256_fingerprint_from_pub_key(r), Colors.RED)
revoke_key(r)
Console.print('Keys updated!\n\nCompleted update in %5.5f seconds\n' % (time.time() - old_time), Colors.BOLD)
if __name__ == '__main__':
commands = sys.argv
try:
if commands[1] == 'add':
print(add_key(' '.join(commands[2:])))
elif commands[1] == 'revoke':
print(revoke_key(commands[2]))
elif commands[1] == 'list':
print(list_keys())
elif commands[1] == 'check':
print(key_exists(commands[2]))
else:
print('Invalid arguments!')
except:
print('Something went wrong.')
traceback.print_exc()