forked from RsaCtfTool/RsaCtfTool
-
Notifications
You must be signed in to change notification settings - Fork 29
/
siqs.py
87 lines (71 loc) · 2.51 KB
/
siqs.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
#
# Implements a class which simply interfaces to Yafu
#
# We implement SIQS in this but this can be extended to
# other factorisation methods supported by Yafu very
# simply.
#
# @CTFKris - https://github.com/sourcekris/RsaCtfTool/
#
import os
import subprocess
import re
class SiqsAttack(object):
def __init__(self, args, n):
# Configuration
self.yafubin = "./yafu" # where the binary is
self.threads = 2 # number of threads
self.maxtime = 180 # max time to try the sieve
self.n = n
self.p = None
self.q = None
self.verbose = args.verbose
def testyafu(self):
with open('/dev/null') as DN:
try:
yafutest = subprocess.check_output([self.yafubin,'siqs(1549388302999519)'], stderr=DN)
except:
yafutest = ""
if '48670331' in yafutest:
# yafu is working
if self.verbose:
print "[*] Yafu SIQS is working."
return True
else:
if self.verbose:
print "[*] Yafu SIQS is not working."
return False
def checkyafu(self):
# check if yafu exists and we can execute it
if os.path.isfile(self.yafubin) and os.access(self.yafubin, os.X_OK):
return True
else:
return False
def benchmarksiqs(self):
# NYI
# return the time to factor a 256 bit RSA modulus
return
def doattack(self):
with open('/dev/null') as DN:
yafurun = subprocess.check_output(
[self.yafubin,'siqs('+str(self.n)+')',
'-siqsT', str(self.maxtime),
'-threads',str(self.threads)], stderr=DN)
primesfound = []
if 'input too big for SIQS' in yafurun:
if self.verbose:
print "[-] Modulus too big for SIQS method."
return
for line in yafurun.splitlines():
if re.search('^P[0-9]+\ =\ [0-9]+$',line):
primesfound.append(int(line.split('=')[1]))
if len(primesfound) == 2:
self.p = primesfound[0]
self.q = primesfound[1]
if len(primesfound) > 2:
if self.verbose:
print "[*] > 2 primes found. Is key multiprime?"
if len(primesfound) < 2:
if self.verbose:
print "[*] SIQS did not factor modulus."
return