-
Notifications
You must be signed in to change notification settings - Fork 0
/
RsaCtfTool.py
executable file
·818 lines (706 loc) · 32.4 KB
/
RsaCtfTool.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
----------------------------------------------------------------------------
"THE BEER-WARE LICENSE" (Revision 42):
ganapati (@G4N4P4T1) wrote this file. As long as you retain this notice you
can do whatever you want with this stuff. If we meet some day, and you think
this stuff is worth it, you can buy me a beer in return.
----------------------------------------------------------------------------
Additionnal contributors :
@CTFKris (sourcekris)
"""
from Crypto.PublicKey import RSA
import signal
import gmpy2
from rsalibnum import *
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import re
import argparse
import os
import subprocess
from glob import glob
import tempfile
import sys
import base64
import binascii
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
sys.setrecursionlimit(2000)
if sys.version_info < (3, 0):
int = long
class FactorizationError(Exception):
pass
class PublicKey(object):
def __init__(self, key):
"""Create RSA key from input content
:param key: public key file content
:type key: string
"""
try:
pub = RSA.importKey(key)
except ValueError as e:
print(e)
sys.exit(1)
self.n = pub.n
self.e = pub.e
self.key = key
def __str__(self):
# Print armored public key
return self.key
class PrivateKey(object):
def __init__(self, p, q, e, n):
"""Create private key from base components
:param p: extracted from n
:param q: extracted from n
:param e: exponent
:param n: n from public key
"""
t = (p-1)*(q-1)
d = invmod(e, t)
self.key = RSA.construct((n, e, d, p, q))
def decrypt(self, cipher):
"""Uncipher data with private key
:param cipher: input cipher
:type cipher: string
"""
try:
tmp_priv_key = tempfile.NamedTemporaryFile()
with open(tmp_priv_key.name, "wb") as tmpfd:
tmpfd.write(str(self).encode('utf8'))
tmp_priv_key_name = tmp_priv_key.name
tmp_cipher = tempfile.NamedTemporaryFile()
with open(tmp_cipher.name, "wb") as tmpfd:
tmpfd.write(cipher)
tmp_cipher_name = tmp_cipher.name
with open('/dev/null') as DN:
openssl_result = subprocess.check_output(['openssl',
'rsautl',
'-raw',
'-decrypt',
'-in',
tmp_cipher_name,
'-inkey',
tmp_priv_key_name],
stderr=DN)
return openssl_result
except:
return self.key.decrypt(cipher)
def __str__(self):
# Print armored private key
return self.key.exportKey().decode("utf-8")
class RSAAttack(object):
def __init__(self, args):
if '*' in args.publickey or '?' in args.publickey:
# get list of public keys from wildcard expression
self.pubkeyfilelist = glob(args.publickey)
self.pubkeyfilelist.sort()
self.args = args
self.attackobjs = []
if args.verbose:
print("[*] Multikey mode using keys: " + repr(self.pubkeyfilelist))
# Try to build new key if e is huge (so d is small) and after try wiener on this new key
self.same_n_huge_e_attack()
if args.uncipherfile is not None and any(i in 'abs*' for i in ['*','?']):
# get list of ciphers from wildcard expression
self.uncipherlist = glob(args.uncipherfile)
self.uncipherlist.sort()
if len(self.uncipherlist) != len(self.pubkeyfilelist):
if args.verbose:
print("It has to be the same number of keys and ciphers")
quit()
self.args = args
if args.verbose:
print("[*] Multiciphers mode using ciphers: " + repr(self.uncipherlist))
# Initialize a list of objects by recursively calling this on each key and cipher
for i in range(len(self.uncipherlist)):
args.publickey = self.pubkeyfilelist[i]
cipher = open(self.uncipherlist[i], "rb").read()
args.uncipher = cipher
self.attackobjs.append(RSAAttack(args))
else:
# Initialize a list of objects by recursively calling this on each key
for pub in self.pubkeyfilelist:
args.publickey = pub # is this a kludge or is this elegant?
self.attackobjs.append(RSAAttack(args))
else:
# Load single public key
if not isinstance(args.publickey, str):
args.publickey = args.publickey.name
key = open(args.publickey, 'rb').read()
self.pubkeyfile = args.publickey
self.pub_key = PublicKey(key)
self.priv_key = None
self.displayed = False # have we already spammed the user with this private key?
self.args = args
self.unciphered = None
self.attackobjs = None # This is how we'll know this object represents 1 key
self.plaintext = None
# Read n/e from publickey file
if not args.n or not args.e:
pkey = PublicKey(key)
if not args.n:
args.n = pkey.n
if not args.e:
args.e = pkey.e
# Test if sage is working and if so, load additional sage based attacks
if args.sageworks:
self.implemented_attacks.append(self.smallfraction)
self.implemented_attacks.append(self.boneh_durfee)
self.implemented_attacks.append(self.ecm) # make sure ECM always comes last!
# Load ciphertext
if args.uncipher is not None:
self.cipher = args.uncipher
else:
self.cipher = None
return
def same_n_huge_e_attack(self):
parsed_keys = []
for k in self.pubkeyfilelist:
key = open(k, 'rb').read()
parsed_keys.append(PublicKey(key))
if len(set([_.n for _ in parsed_keys])) == 1:
new_e = 1
for k in parsed_keys:
new_e = new_e * k.e
tmpfile = tempfile.NamedTemporaryFile()
with open(tmpfile.name, "wb") as tmpfd:
tmpfd.write(RSA.construct((parsed_keys[0].n, new_e)).publickey().exportKey())
self.same_n_huge_e = tmpfile
return True
else:
return None
def hastads(self):
# Hastad attack for low public exponent, this has found success for e = 3, and e = 5 previously
if self.pub_key.e <= 11 and self.cipher is not None:
orig = s2n(self.cipher)
c = orig
while True:
m = gmpy2.iroot(c, self.pub_key.e)[0]
if pow(m, self.pub_key.e, self.pub_key.n) == orig:
self.unciphered = n2s(m)
break
c += self.pub_key.n
return
def factordb(self):
# if factordb returns some math to derive the prime, solve for p without using an eval
def solveforp(equation):
try:
if '^' in equation:
k, j = equation.split('^')
if '-' in j:
j, sub = j.split('-')
eq = list(map(int, [k, j, sub]))
return pow(eq[0], eq[1])-eq[2]
except Exception as e:
if self.args.verbose:
print("[*] FactorDB gave something we couldn't parse sorry (%s). Got error: %s" % (equation, e))
raise FactorizationError()
# Factors available online?
try:
url_1 = 'https://factordb.com/index.php?query=%i'
url_2 = 'https://factordb.com/index.php?id=%s'
s = requests.Session()
r = s.get(url_1 % self.pub_key.n, verify=False)
regex = re.compile("index\.php\?id\=([0-9]+)", re.IGNORECASE)
ids = regex.findall(r.text)
p_id = ids[1]
q_id = ids[2]
# bugfix: See https://github.com/sourcekris/RsaCtfTool/commit/16d4bb258ebb4579aba2bfc185b3f717d2d91330#commitcomment-21878835
regex = re.compile("value=\"([0-9\^\-]+)\"", re.IGNORECASE)
r_1 = s.get(url_2 % p_id, verify=False)
r_2 = s.get(url_2 % q_id, verify=False)
key_p = regex.findall(r_1.text)[0]
key_q = regex.findall(r_2.text)[0]
self.pub_key.p = int(key_p) if key_p.isdigit() else solveforp(key_p)
self.pub_key.q = int(key_q) if key_q.isdigit() else solveforp(key_q)
if self.pub_key.p == self.pub_key.q == self.pub_key.n:
raise FactorizationError()
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
except Exception as e:
return
def wiener(self):
# this attack module can be optional based on sympy and wiener_attack.py existing
try:
from wiener_attack import WienerAttack
except ImportError:
if self.args.verbose:
print("[!] Warning: Wiener attack module missing (wiener_attack.py) or SymPy not installed?")
return
# Wiener's attack
wiener = WienerAttack(self.pub_key.n, self.pub_key.e)
if wiener.p is not None and wiener.q is not None:
self.pub_key.p = wiener.p
self.pub_key.q = wiener.q
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
def primefac(self, primefac_timeout=45):
# this attack rely on primefac
try:
from primefac import primefac
except ImportError:
if self.args.verbose:
print("[!] Warning: primefac attack module missing")
return
# use primefac
try:
with timeout(seconds=primefac_timeout):
result = list(primefac(self.pub_key.n, timeout=primefac_timeout))
except FactorizationError :
return
if len(result) == 2:
self.pub_key.p = int(result[0])
self.pub_key.q = int(result[1])
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
def ecm(self):
# use elliptic curve method, may return a prime or may never return
# only works if the sageworks() function returned True
print("[*] ECM Method can run forever and may never succeed. Hit Ctrl-C to bail out.")
try:
if self.args.ecmdigits:
sageresult = int(subprocess.check_output(['sage', 'ecm.sage', str(self.pub_key.n), str(self.args.ecmdigits)]))
else:
sageresult = int(subprocess.check_output(['sage', 'ecm.sage', str(self.pub_key.n)]))
except subprocess.CalledProcessError:
return
if sageresult > 0:
self.pub_key.p = sageresult
self.pub_key.q = self.pub_key.n // self.pub_key.p
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
def boneh_durfee(self):
# use boneh durfee method, should return a d value, else returns 0
# only works if the sageworks() function returned True
# many of these problems will be solved by the wiener attack module but perhaps some will fall through to here
# TODO: get an example public key solvable by boneh_durfee but not wiener
try:
sageresult = int(subprocess.check_output(['sage', 'boneh_durfee.sage',
str(self.pub_key.n), str(self.pub_key.e)]))
except subprocess.CalledProcessError:
return
if sageresult > 0:
# use PyCrypto _slowmath rsa_construct to resolve p and q from d
from Crypto.PublicKey import _slowmath
tmp_priv = _slowmath.rsa_construct(int(self.pub_key.n), int(self.pub_key.e), d=int(sageresult))
self.pub_key.p = tmp_priv.p
self.pub_key.q = tmp_priv.q
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
def smallq(self):
# Try an attack where q < 100,000, from BKPCTF2016 - sourcekris
for prime in primes(100000):
if self.pub_key.n % prime == 0:
self.pub_key.q = prime
self.pub_key.p = self.pub_key.n // self.pub_key.q
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
def smallfraction(self):
# Code/idea from Renaud Lifchitz's talk 15 ways to break RSA security @ OPCDE17
# only works if the sageworks() function returned True
try:
sageresult = int(subprocess.check_output(['sage', 'smallfraction.sage', str(self.pub_key.n)]))
if sageresult > 0:
self.pub_key.p = sageresult
self.pub_key.q = self.pub_key.n // self.pub_key.p
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
except subprocess.CalledProcessError:
return
return
def fermat(self, fermat_timeout=60):
# Try an attack where the primes are too close together from BKPCTF2016 - sourcekris
# this attack module can be optional
try:
from fermat import fermat
except ImportError:
if self.args.verbose:
print("[!] Warning: Fermat factorization module missing (fermat.py)")
return
try:
with timeout(seconds=fermat_timeout):
self.pub_key.p, self.pub_key.q = fermat(self.pub_key.n)
except FactorizationError:
return
if self.pub_key.q is not None:
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
def noveltyprimes(self):
# "primes" of the form 31337 - 313333337 - see ekoparty 2015 "rsa 2070"
# not all numbers in this form are prime but some are (25 digit is prime)
maxlen = 25 # max number of digits in the final integer
for i in range(maxlen-4):
prime = int("3133" + ("3" * i) + "7")
if self.pub_key.n % prime == 0:
self.pub_key.q = prime
self.pub_key.p = self.pub_key.n // self.pub_key.q
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
def comfact_cn(self):
# Try an attack where the public key has a common factor with the ciphertext - sourcekris
if self.cipher:
commonfactor = gcd(self.pub_key.n, s2n(self.cipher))
if commonfactor > 1:
self.pub_key.q = commonfactor
self.pub_key.p = self.pub_key.n // self.pub_key.q
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
unciphered = self.priv_key.decrypt(self.cipher)
return
def commonfactors(self):
# Try to find the gcd between each pair of moduli and resolve the private keys if gcd > 1
for x in self.attackobjs:
for y in self.attackobjs:
if x.pub_key.n != y.pub_key.n:
g = gcd(x.pub_key.n, y.pub_key.n)
if g != 1:
if self.args.verbose and not x.displayed and not y.displayed:
print("[*] Found common factor in modulus for " + x.pubkeyfile + " and " + y.pubkeyfile)
# update each attackobj with a private_key
x.pub_key.p = g
x.pub_key.q = x.pub_key.n // g
y.pub_key.p = g
y.pub_key.q = y.pub_key.n // g
x.priv_key = PrivateKey(int(x.pub_key.p), int(x.pub_key.q),
int(x.pub_key.e), int(x.pub_key.n))
y.priv_key = PrivateKey(int(y.pub_key.p), int(y.pub_key.q),
int(y.pub_key.e), int(y.pub_key.n))
# call attack method to print the private keys at the nullattack step or attack singularly
# depending on the success of the gcd operation
x.attack()
y.attack()
return
def pastctfprimes(self):
path = os.path.dirname(os.path.abspath(__file__))
pastctfprimes_path = os.path.join(path, 'pastctfprimes.txt')
primes = [int(x) for x in open(pastctfprimes_path, 'r').readlines() if not x.startswith('#') and not x.startswith('\n')]
if self.args.verbose:
print("[*] Loaded " + str(len(primes)) + " primes")
for prime in primes:
if self.pub_key.n % prime == 0:
self.pub_key.q = prime
self.pub_key.p = self.pub_key.n // self.pub_key.q
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
def commonmodulus(self):
try:
from commonmodulus_attack import CommonModulusAttack
except ImportError:
if self.args.verbose:
print("[!] Warning: Common modulus attack module.")
return
# Common modulus attack
if self.attackobjs[0].pub_key.n != self.attackobjs[1].pub_key.n:
if self.args.verbose:
print("Modulus are not equal, common modulus attack is impossible.")
return
self.len = len(self.attackobjs)
try:
for i in range(self.len):
self.attackobjs[i].cipherdec = int(binascii.hexlify(
base64.b64decode(self.attackobjs[i].cipher)
),16)
except binascii.Error:
for i in range(self.len):
self.attackobjs[i].cipherdec = int(binascii.hexlify(
self.attackobjs[i].cipher)
,16)
except:
return
CM = CommonModulusAttack(self.attackobjs)
CM.extended_euclidean()
CM.modular_inverse()
args.plaintext=CM.print_value()
print(args.plaintext)
return
def chinese(self):
try:
from chinese_attack import chinese_attack
except ImportError:
if self.args.verbose:
print("[!] Warning: Chinese attack module.")
return
# Chinese attack
self.len = len(self.attackobjs)
if self.len<3:
print("Insuffisant number of arguments")
return
for i in self.attackobjs:
if i.pub_key.e != self.attackobjs[0].pub_key.e:
if self.args.verbose:
print("Exposants are not equal, chinese attack is impossible.")
return
try:
for i in range(self.len):
self.attackobjs[i].cipherdec = int(binascii.hexlify(
base64.b64decode(self.attackobjs[i].cipher)
),16)
except binascii.Error:
for i in range(self.len):
self.attackobjs[i].cipherdec = int(binascii.hexlify(
self.attackobjs[i].cipher)
,16)
CH = chinese_attack(self.attackobjs)
CH.system_solve()
args.plaintext = CH.print_value()
print(args.plaintext)
return
def prime_modulus(self):
# an attack where the modulus is not a composite number, so the math is unique
# NYI
return
def siqs(self):
# attempt a Self-Initializing Quadratic Sieve
# this attack module can be optional
try:
from siqs import SiqsAttack
except ImportError:
if self.args.verbose:
print("[!] Warning: Yafu SIQS attack module missing (siqs.py)")
return
if self.pub_key.n.bit_length() > 1024:
print("[!] Warning: Modulus too large for SIQS attack module")
return
siqsobj = SiqsAttack(self.args, self.pub_key.n)
if siqsobj.checkyafu() and siqsobj.testyafu():
siqsobj.doattack()
if siqsobj.p and siqsobj.q:
self.pub_key.q = siqsobj.q
self.pub_key.p = siqsobj.p
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
def Pollard_p_1(self):
# Pollard P minus 1 factoring, using the algorithm as described by https://math.berkeley.edu/~sagrawal/su14_math55/notes_pollard.pdf
from p_1 import pollard_P_1
if not hasattr(self.pub_key, "p"):
self.pub_key.p = None
if not hasattr(self.pub_key, "q"):
self.pub_key.q = None
# Pollard P-1 attack
poll_res = pollard_P_1(self.pub_key.n)
if poll_res and len(poll_res) > 1:
self.pub_key.p, self.pub_key.q = poll_res
if self.pub_key.q is not None:
self.priv_key = PrivateKey(int(self.pub_key.p), int(self.pub_key.q),
int(self.pub_key.e), int(self.pub_key.n))
return
def nullattack(self):
# do nothing, used for multi-key attacks that succeeded so we just print the
# private key without spending any time factoring
return
def mersenne_primes(self):
p = q = None
mersenne_tab = [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521,
607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941,
11213, 19937, 21701, 23209, 44497, 86243, 110503,
132049, 216091, 756839, 859433, 1257787, 1398269,
2976221, 3021377, 6972593, 13466917, 20336011,
24036583, 25964951, 30402457, 32582657, 37156667,
42643801, 43112609, 57885161, 74207281, 77232917]
for mersenne_prime in mersenne_tab:
if self.pub_key.n % ((2**mersenne_prime)-1) == 0:
p = (2**mersenne_prime)-1
q = self.pub_key.n // ((2**mersenne_prime)-1)
break
if p is not None and q is not None:
self.priv_key = PrivateKey(int(p), int(q),
int(self.pub_key.e),
int(self.pub_key.n))
return
def attack(self):
if self.attackobjs is not None:
if self.args.verbose:
print("[*] Performing common factors attack.")
#self.commonfactors()
if self.args.verbose:
print("[*] Performing common modulus attack.")
self.commonmodulus()
if self.args.verbose:
print("[*] Performing chinese attack.")
self.chinese()
try:
if self.plaintext is not None:
quit()
if self.same_n_huge_e:
args.attackobjs = None
args.publickey = self.same_n_huge_e
RSAAttack(args).attack()
except AttributeError:
pass
else:
# loop through implemented attack methods and conduct attacks
for attack in self.implemented_attacks:
if self.args.attack is not None and self.args.attack == attack.__name__:
if self.args.verbose:
print("[*] Performing " + attack.__name__ + " attack.")
getattr(self, attack.__name__)()
elif self.args.attack is None or (self.args.attack is not None and self.args.attack == "all"):
if self.args.verbose and "nullattack" not in attack.__name__:
print("[*] Performing " + attack.__name__ + " attack.")
getattr(self, attack.__name__)()
# check and print resulting private key
if self.priv_key is not None:
if self.args.private and not self.displayed:
print(self.priv_key)
self.displayed = True
break
if self.unciphered is not None:
break
# If we wanted to decrypt, do it now
if self.cipher and self.priv_key is not None:
self.unciphered = self.priv_key.decrypt(self.cipher)
print("[+] Clear text : %s" % str(self.unciphered))
elif self.unciphered is not None:
print("[+] Clear text : %s" % str(self.unciphered))
else:
if self.cipher is not None and self.args.attack is None:
print("[-] Sorry, cracking failed")
if self.priv_key is None and self.args.private:
print("[-] Sorry, cracking failed")
implemented_attacks = [nullattack, hastads, factordb, pastctfprimes,
mersenne_primes, noveltyprimes, smallq, wiener,
comfact_cn, primefac, fermat, siqs, Pollard_p_1]
# source http://stackoverflow.com/a/22348885
class timeout:
def __init__(self, seconds=10, error_message='[-] Timeout'):
self.seconds = seconds
self.error_message = error_message
def handle_timeout(self, signum, frame):
raise FactorizationError(self.error_message)
def __enter__(self):
signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.seconds)
def __exit__(self, type, value, traceback):
signal.alarm(0)
def sageworks():
# Check if sage is installed and working
try:
sageversion = subprocess.check_output(['sage', '-v'])
except OSError:
return False
if 'SageMath version' in sageversion.decode('utf-8'):
return True
else:
return False
def loadkeys(keys):
""" Load one or more keys
"""
return []
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='RSA CTF Tool')
parser.add_argument('--publickey', help='public key file. You can use wildcards for multiple keys.')
parser.add_argument('--createpub', help='Take n and e from cli and just print a public key then exit', action='store_true')
parser.add_argument('--dumpkey', help='Just dump the RSA variables from a key - n,e,d,p,q', action='store_true')
parser.add_argument('--uncipherfile', help='uncipher a file. You can use wildcards for multiple ciphers.', default=None)
parser.add_argument('--uncipher', help='uncipher a cipher', default=None)
parser.add_argument('--verbose', help='verbose mode (display n, e, p and q)', action='store_true')
parser.add_argument('--private', help='Display private key if recovered', action='store_true')
parser.add_argument('--ecmdigits', type=int, help='Optionally an estimate as to how long one of the primes is for ECM method', default=None)
parser.add_argument('-n', help='Specify the modulus. format : int or 0xhex')
parser.add_argument('-p', help='Specify the first prime number. format : int or 0xhex')
parser.add_argument('-q', help='Specify the second prime number. format : int or 0xhex')
parser.add_argument('-e', help='Specify the public exponent. format : int or 0xhex')
parser.add_argument('--key', help='Specify the input key file in --dumpkey mode.')
attacks_list = [_.__name__ for _ in RSAAttack.implemented_attacks if _.__name__ is not "nullattack"] + ["all"]
parser.add_argument('--attack', help='Specify the attack mode.', default="all", choices=attacks_list)
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
# Parse longs if exists
if args.p is not None:
if args.p.startswith("0x"):
args.p = int(args.p, 16)
else:
args.p = int(args.p)
if args.q is not None:
if args.q.startswith("0x"):
args.q = int(args.q, 16)
else:
args.q = int(args.q)
if args.p and args.q is not None:
args.n = args.p * args.q
elif args.n is not None:
if args.n.startswith("0x"):
args.n = int(args.n, 16)
else:
args.n = int(args.n)
if args.e is not None:
if args.e.startswith("0x"):
args.e = int(args.e, 16)
else:
args.e = int(args.e)
# if we have uncipher but no uncipherfile
if args.uncipher is not None:
if args.uncipher.startswith("0x"):
args.uncipher = int(args.uncipher, 16)
else:
args.uncipher = int(args.uncipher)
args.uncipher = n2s(args.uncipher)
elif args.uncipherfile is not None and not any(i in args.uncipherfile for i in ['*','?']):
cipher = open(args.uncipherfile, 'rb').read()
args.uncipher = cipher
# If we have n and one of p and q, calculated the other
if args.n and (args.p or args.q):
if args.p and not args.q:
args.q = args.n // args.p
if args.q and not args.p:
args.p = args.n // args.q
# If we already have all informations
if args.p is not None and args.q is not None and args.e is not None:
try:
priv_key = PrivateKey(args.p, args.q, args.e, args.n)
except ValueError:
if args.verbose:
print("[!] No invmod for e and t, maybe an error in your args ?")
sys.exit(0)
if args.private:
print(priv_key)
if args.createpub:
print(RSA.construct((args.n, args.e)).publickey().exportKey())
if args.uncipher is not None:
unciphered = priv_key.decrypt(args.uncipher)
print("[+] Clear text : %s" % unciphered)
quit()
# if createpub mode generate public key then quit
if args.createpub:
if (args.n is None and (args.p is None or args.q is None)) or args.e is None:
raise Exception("Specify both a modulus and exponent on the command line. See --help for info.")
print(RSA.construct((args.n, args.e)).publickey().exportKey().decode("utf-8"))
quit()
# if dumpkey mode dump the key components then quit
if args.dumpkey:
if args.key is None:
raise Exception("Specify a key file to dump with --key. See --help for info.")
key_data = open(args.key, 'rb').read()
key = RSA.importKey(key_data)
print("[*] n: " + str(key.n))
print("[*] e: " + str(key.e))
if key.has_private():
print("[*] d: " + str(key.d))
print("[*] p: " + str(key.p))
print("[*] q: " + str(key.q))
quit()
if sageworks():
args.sageworks = True
else:
args.sageworks = False
tmpfile = None
if args.publickey is None and args.e is not None and args.n is not None:
tmpfile = tempfile.NamedTemporaryFile()
with open(tmpfile.name, "wb") as tmpfd:
tmpfd.write(RSA.construct((args.n, args.e)).publickey().exportKey())
args.publickey = tmpfile.name
attackobj = RSAAttack(args)
attackobj.attack()