-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjunosdecode.py
177 lines (136 loc) · 4.72 KB
/
junosdecode.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
#!/usr/bin/python
#############################################################################
## this lovely script was painfully ported
## by matt hite ([email protected]), who knows
## very little perl
##
## original: http://search.cpan.org/dist/Crypt-Juniper/lib/Crypt/Juniper.pm
## requires python 2.7 due to use of dict comprehension
##
## version 1.01 - works with python 3
##
##
#############################################################################
## added juniper_encrypt and the necessary functions
## by Minsuk Song <[email protected]>, who also knows
## very little perl
##
from __future__ import print_function
import sys
import argparse
import random
#################################################################
## globals
MAGIC = "$9$"
###################################
## letter families
FAMILY = ["QzF3n6/9CAtpu0O", "B1IREhcSyrleKvMW8LXx", "7N-dVbwsY2g4oaJZGUDj", "iHkq.mPf5T"]
EXTRA = dict()
for x, item in enumerate(FAMILY):
for c in item:
EXTRA[c] = 3 - x
###################################
## forward and reverse dictionaries
NUM_ALPHA = [x for x in "".join(FAMILY)]
ALPHA_NUM = {NUM_ALPHA[x]: x for x in range(0, len(NUM_ALPHA))}
###################################
## encoding moduli by position
ENCODING = [[1, 4, 32], [1, 16, 32], [1, 8, 32], [1, 64], [1, 32], [1, 4, 16, 128], [1, 32, 64]]
def _nibble(cref, length):
nib = cref[0:length]
rest = cref[length:]
if len(nib) != length:
print("Ran out of characters: hit '%s', expecting %s chars" % (nib, length))
sys.exit(1)
return nib, rest
def _gap(c1, c2):
return (ALPHA_NUM[str(c2)] - ALPHA_NUM[str(c1)]) % (len(NUM_ALPHA)) - 1
def _gap_decode(gaps, dec):
num = 0
if len(gaps) != len(dec):
print("Nibble and decode size not the same!")
sys.exit(1)
for x in range(0, len(gaps)):
num += gaps[x] * dec[x]
return chr(num % 256)
def juniper_decrypt(crypt):
chars = crypt.split("$9$", 1)[1]
first, chars = _nibble(chars, 1)
toss, chars = _nibble(chars, EXTRA[first])
prev = first
decrypt = ""
while chars:
decode = ENCODING[len(decrypt) % len(ENCODING)]
nibble, chars = _nibble(chars, len(decode))
gaps = []
for i in nibble:
g = _gap(prev, i)
prev = i
gaps += [g]
decrypt += _gap_decode(gaps, decode)
return decrypt
def _reverse(my_list):
new_list = list(my_list)
new_list.reverse()
return new_list
def _gap_encode(pc, prev, encode):
_ord = ord(pc)
crypt = ''
gaps = []
for mod in _reverse(encode):
gaps.insert(0, int(_ord/mod))
_ord %= mod
for gap in gaps:
gap += ALPHA_NUM[prev] + 1
prev = NUM_ALPHA[gap % len(NUM_ALPHA)]
crypt += prev
return crypt
def _randc(cnt = 0):
ret = ""
for _ in range(cnt):
ret += NUM_ALPHA[random.randrange(len(NUM_ALPHA))]
return ret
def juniper_encrypt(plaintext, salt = None):
if salt is None:
salt = _randc(1)
rand = _randc(EXTRA[salt])
pos = 0
prev = salt
crypt = MAGIC + salt + rand
for x in plaintext:
encode = ENCODING[pos % len(ENCODING)]
crypt += _gap_encode(x, prev, encode)
prev = crypt[-1]
pos += 1
return crypt
def main():
parser = argparse.ArgumentParser(description="Junos $9$ password en/decrypt script")
parser.add_argument('-v','--version', action='version', version='%(prog)s 1.01')
parser.add_argument("-r","--result-only", action="store_true", dest="resultOnly", help="Output resulting string only")
group = parser.add_mutually_exclusive_group()
group.add_argument("-e", "--encrypt", dest="plaintext", help="encrypt plaintext")
group.add_argument("-d", "--decrypt", dest="secret", help="decrypt secret")
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
if not args.resultOnly:
print("Junos $9$ secrets en/decrypter")
print("python version by matt hite/min song")
print("original perl version by kevin brintnall\n")
if args.secret:
encrypted_string = args.secret
if args.resultOnly:
print(juniper_decrypt(encrypted_string), end='')
else:
print("encrypted version: %s" % encrypted_string)
print("decrypted version: %s" % juniper_decrypt(encrypted_string))
elif args.plaintext:
plaintext_string = args.plaintext
if args.resultOnly:
print(juniper_encrypt(plaintext_string), end='')
else:
print("plaintext version: %s" % plaintext_string)
print("encrypted version: %s" % juniper_encrypt(plaintext_string))
if __name__ == "__main__":
main()