Skip to content

Commit f87d158

Browse files
committed
Sharif CTF 2016
1 parent 8ce942b commit f87d158

File tree

307 files changed

+1298
-0
lines changed

Some content is hidden

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

307 files changed

+1298
-0
lines changed

SharifCTF/2016/RSA-Keygen/ciphertext

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
64A3F710E3CB9B114FD112B45AC4845292D0B1FEE1468147E80FABA3CD56B1206F5C59E5D400A7F20C9BCD5B42C7197A0D07FBBA48BFBDA550C5CAFB562BEC1B1CB301D131E13233F2BD1C80EEB48956FF0BC8DB6AE2CD727FB1DAC62822331B15A6044F825D01D81036DA3CC8A3575165E813051036715CDF5F7865676DC2513AAD08C5113DFFDC4E6B13E6FFCA2FAD1AA6986D3ED9F1896C109F641074DA7DBFE62CCAD3CACE4B80332475FE3C9EC4869FCA31EE2860D45959F8583C2AEC7A00FC2FD63DBF6CBEB1C604D60CF780FE028ED0AD65DC74BC5335F96EE7CEDEA292F76B427E5F402BCC609B39302CD4A51F405C6ACF8B8A7569AAD9A9318F060B
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
from random import randrange
3+
import fractions
4+
5+
6+
def get_primes(n):
7+
numbers = set(range(n, 1, -1))
8+
primes = []
9+
while numbers:
10+
p = numbers.pop()
11+
primes.append(p)
12+
numbers.difference_update(set(range(p*2, n+1, p)))
13+
return primes
14+
15+
def egcd(a, b):
16+
if a == 0:
17+
return (b, 0, 1)
18+
else:
19+
g, y, x = egcd(b % a, a)
20+
return (g, x - (b // a) * y, y)
21+
22+
def modinv(a, m):
23+
g, x, y = egcd(a, m)
24+
if g != 1:
25+
raise Exception('modular inverse does not exist')
26+
else:
27+
return x % m
28+
29+
def miller_rabin(n, k):
30+
r, s = 0, n - 1
31+
while s % 2 == 0:
32+
r += 1
33+
s //= 2
34+
for _ in range(k):
35+
a = randrange(2, n - 1)
36+
x = pow(a, s, n)
37+
if x == 1 or x == n - 1:
38+
continue
39+
for _ in range(r - 1):
40+
x = pow(x, 2, n)
41+
if x == n - 1:
42+
break
43+
else:
44+
return False
45+
return True
46+
47+
48+
### main #################
49+
primes = get_primes(443)
50+
primes.sort()
51+
del primes[0]
52+
#print primes
53+
54+
pi = 1;
55+
for x in primes:
56+
pi *= x
57+
print "pi=%X" % pi
58+
59+
while True:
60+
kp = randrange(1, 2**12) + 2**12 + 2**13 + 2**14 + \
61+
2**15 + 2**16 + 2**17 + 2**18 + 2**19
62+
print "kp=%X" % kp
63+
64+
tp = 0
65+
while fractions.gcd(tp, pi) != 1:
66+
print "trying..."
67+
tp = randrange(1, 2**399);
68+
print "tp=%X" % tp
69+
70+
p = kp * pi * 2**400 + tp
71+
print "p=%X" % p
72+
print "bitlength(p)=", len(bin(p))-2
73+
74+
if miller_rabin(p, 40) == True:
75+
break
76+
77+
while True:
78+
kq = randrange(1, 2**12) + 2**12 + 2**13 + 2**14 + \
79+
2**15 + 2**16 + 2**17 + 2**18 + 2**19
80+
print "kq=%X" % kq
81+
82+
tq = 0
83+
while fractions.gcd(tq, pi) != 1:
84+
print "trying..."
85+
tq = randrange(1, 2**399);
86+
print "tq=%X" % tq
87+
88+
q = kq * pi * 2**400 + tq
89+
print "q=%X" % q
90+
print "bitlength(q)=", len(bin(q))-2
91+
92+
if miller_rabin(q, 40) == True:
93+
break
94+
95+
print "p=%X" % p
96+
print "q=%X" % q
97+
98+
n = p * q
99+
print "n=%X" % n
100+
print "bitlength(n)=", len(bin(n))-2
101+
102+
e = 2**16 + 1
103+
print "e=%X" % e
104+
#print "bitlength(e)=", len(bin(e))-2
105+
106+
d = modinv(e, (p-1)*(q-1))
107+
print "d=%X" % d
108+
#print "bitlength(d)=", len(bin(d))-2
109+
110+
m = 12354178254918274687189741234123412398461982374619827346981756309845712384198076
111+
print "m=%X" % m
112+
print "bitlength(m)=", len(bin(m))-2
113+
114+
c = pow(m, e, n)
115+
print "c=%X" % c
116+
print "bitlength(c)=", len(bin(c))-2
117+
118+
m2 = pow(c, d, n)
119+
print "m2=%X" % m2
120+
print "bitlength(m2)=", len(bin(m2))-2

SharifCTF/2016/RSA-Keygen/publickey

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
n=A4E20DDB854955794E7ABF4AE40051C0FC30488C82AB93B7DD046C1CC094A54334C97E84B523BD3F79331EBEAF5249200D729A483D5B8D944D58DF18D2CA9401B1A1A6CDA8A3AC5C234A501794B76886C426FAC35AD9615ADAB5C94B58C03CCFFA891CE0156CBC14255F019617E40DE9124FBBE70D64CD823DCA870FF76B649320927628250D47DB8DFA9BBCE9964CB3FE3D1B69845BD6FA2E6938DDA1F109E5F4E4170C845B976BBD5121107642FC00606208F9BC83322532739BCFEAF706FB2AF985EBD9769C7FBD50ECBF55566BD44FB241F9FD2DE25069AA8C744F0558514F1E9C8E4297A4D4B25D9F2B7494B466C2E6E2834BA68C5C824215018368B4FB
2+
e=10001

SharifCTF/2016/RSA-Keygen/solver.py

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
2+
from random import randrange
3+
import math
4+
import fractions
5+
6+
7+
def get_primes(n):
8+
numbers = set(range(n, 1, -1))
9+
primes = []
10+
while numbers:
11+
p = numbers.pop()
12+
primes.append(p)
13+
numbers.difference_update(set(range(p*2, n+1, p)))
14+
return primes
15+
16+
def egcd(a, b):
17+
if a == 0:
18+
return (b, 0, 1)
19+
else:
20+
g, y, x = egcd(b % a, a)
21+
return (g, x - (b // a) * y, y)
22+
23+
def modinv(a, m):
24+
g, x, y = egcd(a, m)
25+
if g != 1:
26+
raise Exception('modular inverse does not exist')
27+
else:
28+
return x % m
29+
30+
def miller_rabin(n, k):
31+
r, s = 0, n - 1
32+
while s % 2 == 0:
33+
r += 1
34+
s //= 2
35+
for _ in range(k):
36+
a = randrange(2, n - 1)
37+
x = pow(a, s, n)
38+
if x == 1 or x == n - 1:
39+
continue
40+
for _ in range(r - 1):
41+
x = pow(x, 2, n)
42+
if x == n - 1:
43+
break
44+
else:
45+
return False
46+
return True
47+
48+
primes = get_primes(443)
49+
primes.sort()
50+
del primes[0]
51+
52+
pi = 1
53+
for x in primes:
54+
pi *= x
55+
56+
x = 2**400 * pi
57+
58+
n = 0xA4E20DDB854955794E7ABF4AE40051C0FC30488C82AB93B7DD046C1CC094A54334C97E84B523BD3F79331EBEAF5249200D729A483D5B8D944D58DF18D2CA9401B1A1A6CDA8A3AC5C234A501794B76886C426FAC35AD9615ADAB5C94B58C03CCFFA891CE0156CBC14255F019617E40DE9124FBBE70D64CD823DCA870FF76B649320927628250D47DB8DFA9BBCE9964CB3FE3D1B69845BD6FA2E6938DDA1F109E5F4E4170C845B976BBD5121107642FC00606208F9BC83322532739BCFEAF706FB2AF985EBD9769C7FBD50ECBF55566BD44FB241F9FD2DE25069AA8C744F0558514F1E9C8E4297A4D4B25D9F2B7494B466C2E6E2834BA68C5C824215018368B4FB
59+
e = 0x10001
60+
61+
ac = n//(x**2)
62+
ad_bc = (n - ac*(x**2))//x
63+
bd = n%x
64+
65+
print 'AC\t%x\n\nAD_BC\t%x\n\nBD\t%x\n' % (ac, ad_bc, bd)
66+
67+
for i in range(2**12):
68+
a_ = (2**8 - 1) * (2**12) + i
69+
if ac % a_ == 0:
70+
a = a_
71+
c = ac/a
72+
ta = i
73+
tc = c % (2**12)
74+
print 'a: %x / c: %x\nta: %x / tc: %x\n' % (a, c, ta, tc)
75+
76+
# we have a and c!
77+
g = fractions.gcd(a, c)
78+
a_g = a/g
79+
c_g = c/g
80+
81+
ld = ad_bc // a
82+
lb = 0
83+
84+
tt = a * ld + lb * c
85+
while tt != ad_bc:
86+
if tt < ad_bc:
87+
lb += 1
88+
if tt > ad_bc:
89+
ld -= 1
90+
tt = a * ld + lb * c
91+
92+
print 'ld: %x / lb: %x' % (ld, lb)
93+
94+
rd = 0
95+
rb = ad_bc // c
96+
97+
tt = a * rd + rb * c
98+
while tt != ad_bc:
99+
if tt < ad_bc:
100+
rd += 1
101+
if tt > ad_bc:
102+
rb -= 1
103+
tt = a * rd + rb * c
104+
105+
print 'rd: %x / rb: %x' % (rd, rb)
106+
107+
def getBD(k):
108+
td = ld - k * (c_g)
109+
tb = lb + k * (a_g)
110+
return tb * td
111+
112+
# td - c/g / tb + a/g
113+
l = 0
114+
r = (rb - lb) // (a_g) - 1
115+
while l <= r:
116+
m = (l + r) >> 1
117+
if getBD(m) <= getBD(m+1):
118+
l = m+1
119+
else:
120+
r = m-1
121+
mid = l
122+
123+
l = 0
124+
r = mid
125+
while l <= r:
126+
m = (l + r) >> 1
127+
if getBD(m) < bd:
128+
l = m+1
129+
else:
130+
r = m-1
131+
print '%x\n%x\n%x\n' % (getBD(l-1)-bd, getBD(l)-bd, getBD(l+1)-bd)
132+
if getBD(l) == bd:
133+
print 'SOLVED!'
134+
print 'p: %x\nq: %x\n' % (a*x + b, c*x + d)
135+
136+
137+
l = mid
138+
r = (rb - lb) // (a_g)
139+
while l <= r:
140+
m = (l + r) >> 1
141+
if getBD(m) > bd:
142+
l = m+1
143+
else:
144+
r = m-1
145+
print '%x\n%x\n%x\n' % (getBD(l-1)-bd, getBD(l)-bd, getBD(l+1)-bd)
146+
if getBD(l) == bd:
147+
print 'SOLVED!'
148+
d = ld - l * (c_g)
149+
b = lb + l * (a_g)
150+
print ad_bc == a*d + b*c, bd == b*d, ac == a*c, n == ac * x * x + ad_bc * x + bd
151+
152+
p = a*x + b
153+
q = c*x + d
154+
print 'p: %x\nq: %x\n' % (p, q)
155+
156+
cipher = 0x64A3F710E3CB9B114FD112B45AC4845292D0B1FEE1468147E80FABA3CD56B1206F5C59E5D400A7F20C9BCD5B42C7197A0D07FBBA48BFBDA550C5CAFB562BEC1B1CB301D131E13233F2BD1C80EEB48956FF0BC8DB6AE2CD727FB1DAC62822331B15A6044F825D01D81036DA3CC8A3575165E813051036715CDF5F7865676DC2513AAD08C5113DFFDC4E6B13E6FFCA2FAD1AA6986D3ED9F1896C109F641074DA7DBFE62CCAD3CACE4B80332475FE3C9EC4869FCA31EE2860D45959F8583C2AEC7A00FC2FD63DBF6CBEB1C604D60CF780FE028ED0AD65DC74BC5335F96EE7CEDEA292F76B427E5F402BCC609B39302CD4A51F405C6ACF8B8A7569AAD9A9318F060B
157+
d = modinv(e, (p-1)*(q-1))
158+
159+
m = pow(cipher, d, n)
160+
print "m=%X" % m
161+
print ("%x" % m).decode('hex')
162+
print "bitlength(m)=", len(bin(m))-2
741 KB
Loading

SharifCTF/2016/cheetah/Hide.jar

2.14 KB
Binary file not shown.

SharifCTF/2016/cheetah/Hide.java

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Decompiled with CFR 0_110.
3+
*/
4+
import java.awt.Point;
5+
import java.awt.image.BufferedImage;
6+
import java.awt.image.RenderedImage;
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.PrintStream;
10+
import javax.imageio.ImageIO;
11+
12+
public class Hide {
13+
protected void steg(String string, String string2) {
14+
BufferedImage bufferedImage = this.loadImage(string2 + ".png");
15+
BufferedImage bufferedImage2 = this.steg(string, bufferedImage);
16+
try {
17+
ImageIO.write((RenderedImage)bufferedImage2, "png", new File(string2 + "_out.png"));
18+
}
19+
catch (IOException var5_5) {
20+
throw new RuntimeException("Unable to write image!");
21+
}
22+
}
23+
24+
protected BufferedImage steg(String string, BufferedImage bufferedImage) {
25+
if ((string = "" + string.length() + ":" + string).length() * 8 > bufferedImage.getWidth() * bufferedImage.getHeight()) {
26+
System.out.println("There won't be enough space to store this message!");
27+
System.out.println("Message length: " + string.length() + " bytes. " + "Image can hold a maximum of " + bufferedImage.getWidth() * bufferedImage.getHeight() / 8);
28+
throw new RuntimeException("There won't be enough space to store this message!");
29+
}
30+
byte[] arrby = string.getBytes();
31+
Point point = new Point(0, 0);
32+
for (int n : arrby) {
33+
for (int i = 0; i < 8; ++i) {
34+
if ((n & 128) == 128) {
35+
bufferedImage.setRGB(point.x, point.y, this.setLeastSignificantBit(bufferedImage.getRGB(point.x, point.y), true));
36+
} else {
37+
bufferedImage.setRGB(point.x, point.y, this.setLeastSignificantBit(bufferedImage.getRGB(point.x, point.y), false));
38+
}
39+
n <<= 1;
40+
this.movePointer(point, bufferedImage);
41+
}
42+
}
43+
return bufferedImage;
44+
}
45+
46+
protected int setLeastSignificantBit(int n, boolean bl) {
47+
n >>= 1;
48+
n <<= 1;
49+
if (bl) {
50+
++n;
51+
}
52+
return n;
53+
}
54+
55+
protected void movePointer(Point point, BufferedImage bufferedImage) {
56+
if (point.x == bufferedImage.getWidth() - 1) {
57+
point.x = -1;
58+
++point.y;
59+
}
60+
++point.x;
61+
if (point.y == bufferedImage.getHeight()) {
62+
throw new RuntimeException("Pointer moved beyond the end of the image");
63+
}
64+
}
65+
66+
private BufferedImage loadImage(String string) {
67+
try {
68+
BufferedImage bufferedImage = ImageIO.read(new File(string));
69+
return bufferedImage;
70+
}
71+
catch (IOException var2_3) {
72+
System.out.println("Unable to load \"" + string + "\"");
73+
System.exit(0);
74+
return null;
75+
}
76+
}
77+
78+
public static void main(String[] arrstring) {
79+
if (arrstring.length < 2) {
80+
System.out.println("Input Arguments Is Not Valid.");
81+
System.out.println("Run By 'java -jar Hide.jar `file-path` `message`'");
82+
return;
83+
}
84+
System.out.println("Welcome!\nDecrypt The Image And Capture The Flag!");
85+
Hide hide = new Hide();
86+
hide.steg(arrstring[1], arrstring[0]);
87+
}
88+
}
89+

0 commit comments

Comments
 (0)