Skip to content

Commit 266f93e

Browse files
committed
Merge branch 'master' of https://github.com/rev112/stepctf-2015
2 parents 3f0ccaa + 9e786cc commit 266f93e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+922
-1
lines changed

ASR/create/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
1. Generate N E D using tool rsa-wiener-attack (https://github.com/pablocelayes/rsa-wiener-attack) and save selected in "Selected_n_e_d.txt" file.
2+
3+
```bash
4+
python RSAvulnerableKeyGenerator.py
5+
```
6+
7+
2. Generate private.key PEM file using rsatool (https://github.com/ius/rsatool).
8+
9+
```bash
10+
python rsatool.py `cat Selected_n_e_d.txt` -o private.key
11+
```
12+
13+
3. Generate public.key.
14+
15+
```bash
16+
openssl rsa -in private.key -out public.key -outform PEM -pubout
17+
```
18+
19+
4. Encode message.
20+
21+
```bash
22+
openssl rsautl -encrypt -inkey public.key -pubin -in message -out secret
23+
```
24+
5. Test on decoding.
25+
26+
```bash
27+
openssl rsautl -decrypt -inkey private.key -in secret -out test && md5sum test message
28+
```
29+
30+
6. Deploy "secret" and public.key files.

ASR/create/Selected_n_e_d.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-n 505425768438331379583477586793149859779016357536173445727601368429083506892946939712080149243185374889149712883669777241472138976122128303833013626951967984174860289009798083861065343256637740820678289846106861235730545954153000328476846659202260440502052387534350213826713370874369664454639519512880131587979 -e 406337194415078969135388012199768851534892405717049052243569601877767720306971351962115455780742226903789914852797732475115620696182173145519244169134462893981827754911556903443954791096825007180380141420831124481112731502488517569927478504486615667200169344800534739661939136312208398465567840505121573367837 -d 5505607512671258899404366558275649879039721916551771524402852924475941011773
2+

ASR/create/message

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
STCTF#Dang3r0u51R5AMay83Vu1n3ra813#

ASR/create/private.key

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICOQIBAAKBgQLPwAwnXFZ/EvKE1IZLFSokmZYuJVBtfhhPor4aDVDdyQXd6XncJ3U8kGC7Aez7
3+
tUBtHXjOjcjKKWsZwyBKoYzmlIWovJpXoXFQhRCaUBgbN+SM0mJ7jaOAoVLpcfJG5auJ4MtedvVR
4+
/ZklEF+IetObLYZ3Mxq2aAK15Kx0Zte/iwKBgQJCpLQKCrtWDlDkfOYZJvwq8hbHatbcUubpp46b
5+
QmCdV18w3W2sOsMjPoYlFNie7jicukMPYB+BpX8bfpFY3G86AHDe43kfnL2zS53hsSy7faRusOnm
6+
Bal1oJc7GCVSOaJ5iCWTEvFEaIY4i+Mg0ol+HPmEGJjZMXdsODEwYGLwHQIgDCwQPH4iUFihfz18
7+
b8epCYLzO2r9l8thz3zOdX47QT0CQQFeBr2+s8lKeGiG0FsWa55NDi0h6ShCTLBfUM8BkBNmMbqz
8+
5VybaKajeDTGwHpZYBTgVf8hmloh/geJcuBfwJu/AkECDmf/q3wc9yzgKUNtaWuGf+krVnmSCXeS
9+
7Lhrf2SbygyjjT7emWpgpzw/EoJ2wCg4JBZBmmZ82d8F8imYYTy/NQIgDCwQPH4iUFihfz18b8ep
10+
CYLzO2r9l8thz3zOdX47QT0CIAwsEDx+IlBYoX89fG/HqQmC8ztq/ZfLYc98znV+O0E9AkAoVQGA
11+
5pcRKjARucSUlUIq/+XiZZ7qh9Ip7nSm+XCz90UyHEHBTsQ15YgfEfELeZATXLC2BXma+XZU4IIk
12+
TMRw
13+
-----END RSA PRIVATE KEY-----

ASR/create/public.key

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKBgQLPwAwnXFZ/EvKE1IZLFSok
3+
mZYuJVBtfhhPor4aDVDdyQXd6XncJ3U8kGC7Aez7tUBtHXjOjcjKKWsZwyBKoYzm
4+
lIWovJpXoXFQhRCaUBgbN+SM0mJ7jaOAoVLpcfJG5auJ4MtedvVR/ZklEF+IetOb
5+
LYZ3Mxq2aAK15Kx0Zte/iwKBgQJCpLQKCrtWDlDkfOYZJvwq8hbHatbcUubpp46b
6+
QmCdV18w3W2sOsMjPoYlFNie7jicukMPYB+BpX8bfpFY3G86AHDe43kfnL2zS53h
7+
sSy7faRusOnmBal1oJc7GCVSOaJ5iCWTEvFEaIY4i+Mg0ol+HPmEGJjZMXdsODEw
8+
YGLwHQ==
9+
-----END PUBLIC KEY-----

ASR/create/secret

129 Bytes
Binary file not shown.

ASR/solution/nepair

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-n 0x02cfc00c275c567f12f284d4864b152a2499962e25506d7e184fa2be1a0d50ddc905dde979dc27753c9060bb01ecfbb5406d1d78ce8dc8ca296b19c3204aa18ce69485a8bc9a57a1715085109a50181b37e48cd2627b8da380a152e971f246e5ab89e0cb5e76f551fd9925105f887ad39b2d8677331ab66802b5e4ac7466d7bf8b -e 0x0242a4b40a0abb560e50e47ce61926fc2af216c76ad6dc52e6e9a78e9b42609d575f30dd6dac3ac3233e862514d89eee389cba430f601f81a57f1b7e9158dc6f3a0070dee3791f9cbdb34b9de1b12cbb7da46eb0e9e605a975a0973b18255239a27988259312f1446886388be320d2897e1cf9841898d931776c3831306062f01d

ASR/solution/pqetuple

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-p 18332366563702812004827960767382231259638895239413282702812009894882946745998553209194850588719103121402937914114124705492672432051453957400966421943458751 -q 27570132131170104824343411575743642180767006452664381672813278738573428245458845155699724266932275812596878966520171881476555627563245277293094684982034229 -e 406337194415078969135388012199768851534892405717049052243569601877767720306971351962115455780742226903789914852797732475115620696182173145519244169134462893981827754911556903443954791096825007180380141420831124481112731502488517569927478504486615667200169344800534739661939136312208398465567840505121573367837

ASR/solution/private.key

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICOQIBAAKBgQLPwAwnXFZ/EvKE1IZLFSokmZYuJVBtfhhPor4aDVDdyQXd6XncJ3U8kGC7Aez7
3+
tUBtHXjOjcjKKWsZwyBKoYzmlIWovJpXoXFQhRCaUBgbN+SM0mJ7jaOAoVLpcfJG5auJ4MtedvVR
4+
/ZklEF+IetObLYZ3Mxq2aAK15Kx0Zte/iwKBgQJCpLQKCrtWDlDkfOYZJvwq8hbHatbcUubpp46b
5+
QmCdV18w3W2sOsMjPoYlFNie7jicukMPYB+BpX8bfpFY3G86AHDe43kfnL2zS53hsSy7faRusOnm
6+
Bal1oJc7GCVSOaJ5iCWTEvFEaIY4i+Mg0ol+HPmEGJjZMXdsODEwYGLwHQIgDCwQPH4iUFihfz18
7+
b8epCYLzO2r9l8thz3zOdX47QT0CQQFeBr2+s8lKeGiG0FsWa55NDi0h6ShCTLBfUM8BkBNmMbqz
8+
5VybaKajeDTGwHpZYBTgVf8hmloh/geJcuBfwJu/AkECDmf/q3wc9yzgKUNtaWuGf+krVnmSCXeS
9+
7Lhrf2SbygyjjT7emWpgpzw/EoJ2wCg4JBZBmmZ82d8F8imYYTy/NQIgDCwQPH4iUFihfz18b8ep
10+
CYLzO2r9l8thz3zOdX47QT0CIAwsEDx+IlBYoX89fG/HqQmC8ztq/ZfLYc98znV+O0E9AkAoVQGA
11+
5pcRKjARucSUlUIq/+XiZZ7qh9Ip7nSm+XCz90UyHEHBTsQ15YgfEfELeZATXLC2BXma+XZU4IIk
12+
TMRw
13+
-----END RSA PRIVATE KEY-----

ASR/solution/rsatool.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#!/usr/bin/python2
2+
import base64, fractions, optparse, random
3+
import gmpy
4+
5+
from pyasn1.codec.der import encoder
6+
from pyasn1.type.univ import *
7+
8+
PEM_TEMPLATE = '-----BEGIN RSA PRIVATE KEY-----\n%s-----END RSA PRIVATE KEY-----\n'
9+
DEFAULT_EXP = 65537
10+
11+
def factor_modulus(n, d, e):
12+
"""
13+
Efficiently recover non-trivial factors of n
14+
15+
See: Handbook of Applied Cryptography
16+
8.2.2 Security of RSA -> (i) Relation to factoring (p.287)
17+
18+
http://www.cacr.math.uwaterloo.ca/hac/
19+
"""
20+
t = (e * d - 1)
21+
s = 0
22+
23+
while True:
24+
quotient, remainder = divmod(t, 2)
25+
26+
if remainder != 0:
27+
break
28+
29+
s += 1
30+
t = quotient
31+
32+
found = False
33+
34+
while not found:
35+
i = 1
36+
a = random.randint(1,n-1)
37+
38+
while i <= s and not found:
39+
c1 = pow(a, pow(2, i-1, n) * t, n)
40+
c2 = pow(a, pow(2, i, n) * t, n)
41+
42+
found = c1 != 1 and c1 != (-1 % n) and c2 == 1
43+
44+
i += 1
45+
46+
p = fractions.gcd(c1-1, n)
47+
q = (n / p)
48+
49+
return p, q
50+
51+
class RSA:
52+
def __init__(self, p=None, q=None, n=None, d=None, e=DEFAULT_EXP):
53+
"""
54+
Initialize RSA instance using primes (p, q)
55+
or modulus and private exponent (n, d)
56+
"""
57+
58+
self.e = e
59+
60+
if p and q:
61+
assert gmpy.is_prime(p), 'p is not prime'
62+
assert gmpy.is_prime(q), 'q is not prime'
63+
64+
self.p = p
65+
self.q = q
66+
elif n and d:
67+
self.p, self.q = factor_modulus(n, d, e)
68+
else:
69+
raise ArgumentError('Either (p, q) or (n, d) must be provided')
70+
71+
self._calc_values()
72+
73+
def _calc_values(self):
74+
self.n = self.p * self.q
75+
76+
phi = (self.p - 1) * (self.q - 1)
77+
self.d = gmpy.invert(self.e, phi)
78+
79+
# CRT-RSA precomputation
80+
self.dP = self.d % (self.p - 1)
81+
self.dQ = self.d % (self.q - 1)
82+
self.qInv = gmpy.invert(self.q, self.p)
83+
84+
def to_pem(self):
85+
"""
86+
Return OpenSSL-compatible PEM encoded key
87+
"""
88+
return PEM_TEMPLATE % base64.encodestring(self.to_der())
89+
90+
def to_der(self):
91+
"""
92+
Return parameters as OpenSSL compatible DER encoded key
93+
"""
94+
seq = Sequence()
95+
96+
for x in [0, self.n, self.e, self.d, self.p, self.q, self.dP, self.dQ, self.qInv]:
97+
seq.setComponentByPosition(len(seq), Integer(x))
98+
99+
return encoder.encode(seq)
100+
101+
def dump(self, verbose):
102+
vars = ['n', 'e', 'd', 'p', 'q']
103+
104+
if verbose:
105+
vars += ['dP', 'dQ', 'qInv']
106+
107+
for v in vars:
108+
self._dumpvar(v)
109+
110+
def _dumpvar(self, var):
111+
val = getattr(self, var)
112+
113+
parts = lambda s, l: '\n'.join([s[i:i+l] for i in xrange(0, len(s), l)])
114+
115+
if len(str(val)) <= 40:
116+
print '%s = %d (%#x)\n' % (var, val, val)
117+
else:
118+
print '%s =' % var
119+
print parts('%x' % val, 80) + '\n'
120+
121+
122+
if __name__ == '__main__':
123+
parser = optparse.OptionParser()
124+
125+
parser.add_option('-p', dest='p', help='prime', type='int')
126+
parser.add_option('-q', dest='q', help='prime', type='int')
127+
parser.add_option('-n', dest='n', help='modulus', type='int')
128+
parser.add_option('-d', dest='d', help='private exponent', type='int')
129+
parser.add_option('-e', dest='e', help='public exponent (default: %d)' % DEFAULT_EXP, type='int', default=DEFAULT_EXP)
130+
parser.add_option('-o', dest='filename', help='output filname')
131+
parser.add_option('-f', dest='format', help='output format (DER, PEM) (default: PEM)', type='choice', choices=['DER', 'PEM'], default='PEM')
132+
parser.add_option('-v', dest='verbose', help='also display CRT-RSA representation', action='store_true', default=False)
133+
134+
try:
135+
(options, args) = parser.parse_args()
136+
137+
if options.p and options.q:
138+
print 'Using (p, q) to initialise RSA instance\n'
139+
rsa = RSA(p=options.p, q=options.q, e=options.e)
140+
elif options.n and options.d:
141+
print 'Using (n, d) to initialise RSA instance\n'
142+
rsa = RSA(n=options.n, d=options.d, e=options.e)
143+
else:
144+
parser.print_help()
145+
parser.error('Either (p, q) or (n, d) needs to be specified')
146+
147+
rsa.dump(options.verbose)
148+
149+
if options.filename:
150+
print 'Saving %s as %s' % (options.format, options.filename)
151+
152+
153+
if options.format == 'PEM':
154+
data = rsa.to_pem()
155+
elif options.format == 'DER':
156+
data = rsa.to_der()
157+
158+
fp = open(options.filename, 'wb')
159+
fp.write(data)
160+
fp.close()
161+
162+
except optparse.OptionValueError, e:
163+
parser.print_help()
164+
parser.error(e.msg)
165+

0 commit comments

Comments
 (0)