阿里云服务器 个人网站,做美食网站视频下载,营销型的物流网站模板,河北邢台学院REVERSE-COMPETITION-HGAME2022-Week4( WOW )serverezvmhardasm( WOW )
32位exe#xff0c;ida打开 观察伪代码逻辑#xff0c;上面的红框中#xff0c;输入经过加密#xff0c;密文放入Buf2中#xff0c;然后Buf2和已知的密文res比较 比较完#xff0c;打印winida打开 观察伪代码逻辑上面的红框中输入经过加密密文放入Buf2中然后Buf2和已知的密文res比较 比较完打印win或者error 发现下面还有一部分对Buf2处理的代码逻辑而且几乎和上面对输入加密的结构是一样的 于是猜测下面的红框中就是解密逻辑 起调试随便构造一个长度为32的输入 在调用memcmp前下断将内存中Buf2的数据patch成已知的密文res 进行下面对Buf2的解密解密后的input即为flag
server
64位exe用ida7.7打开 52pojie上有最新的全插件绿色版ida7.7可供下载 从main函数中知道监听本地的9090端口 回调函数为main_HttpHandleFunc调试发现输入经过main_encrypt处理后与已知的res比较 进入main_encrypt函数伪代码看不出什么玄机在汇编指令 可以发现main_encrypt的前半部分实际上是个RSA加密并且已知pqe 输入经rsa加密后将十进制密文的每一个数字转成字符存入rsa_cipher中 然后进行后半部分的两轮异或运算异或后的结果与已知的res比较 这里举一个例子来理解后半部分的两轮异或运算以便能够更好地逆向
# -*- coding:utf-8 -*-
import gmpy2
num192582184765240663364795767694262273105045150785272129481762171937885924776597#p
num2107310528658039985708896636559112400334262005367649176746429531274300859498993#q
num1_mul_num2num1*num2#n
num3950501#e
my_in_shgame{abcdefghijklmnopqrstuvwxy}#构造的输入
my_in0x6867616d657b6162636465666768696a6b6c6d6e6f707172737475767778797d
my_in_ciphergmpy2.powmod(my_in,num3,num1_mul_num2)#rsa加密
my_in_cipher_sstr(my_in_cipher)
print(RSA加密后的十进制密文)
print(my_in_cipher_s)
print(RSA加密后的十进制密文长度)
print(len(my_in_cipher_s))
#模拟main_encrypt中后半部分的两轮异或运算
res[]
for i in range(len(my_in_cipher_s)-1):res.append(ord(my_in_cipher_s[i]))
print(十进制密文的每一个数字转成字符)
print(res)
v22102
index0
while index153:tmpres[index]res[index]^v22v22tmpindex1
print(第一轮异或运算结果)
print(res)
index0
while index153:tmpres[index]res[index]^v22v22tmpindex1
print(第二轮异或运算结果)
print(res)
# RSA加密后的十进制密文
# 2315576653393559402200063691755666172531457200527539170715551199511112051176056764826673635703056993058028383465485142593456001504496707046294726739445446
# RSA加密后的十进制密文长度
# 154
# 十进制密文的每一个数字转成字符(后面用cipher表示)
# [50, 51, 49, 53, 53, 55, 54, 54, 53, 51, 51, 57, 51, 53, 53, 57, 52, 48, 50, 50, 48, 48, 48, 54, 51, 54, 57, 49, 55, 53, 53, 54, 54, 54, 49, 55, 50, 53, 51, 49, 52, 53, 55, 50, 48, 48, 53, 50, 55, 53, 51, 57, 49, 55, 48, 55, 49, 53, 53, 53, 49, 49, 57, 57, 53, 49, 49, 49, 49, 50, 48, 53, 49, 49, 55, 54, 48, 53, 54, 55, 54, 52, 56, 50, 54, 54, 55, 51, 54, 51, 53, 55, 48, 51, 48, 53, 54, 57, 57, 51, 48, 53, 56, 48, 50, 56, 51, 56, 51, 52, 54, 53, 52, 56, 53, 49, 52, 50, 53, 57, 51, 52, 53, 54, 48, 48, 49, 53, 48, 52, 52, 57, 54, 55, 48, 55, 48, 52, 54, 50, 57, 52, 55, 50, 54, 55, 51, 57, 52, 52, 53, 52, 52]
# 第一轮异或运算结果(后面用round_1表示)
# [84, 1, 2, 4, 0, 2, 1, 0, 3, 6, 0, 10, 10, 6, 0, 12, 13, 4, 2, 0, 2, 0, 0, 6, 5, 5, 15, 8, 6, 2, 0, 3, 0, 0, 7, 6, 5, 7, 6, 2, 5, 1, 2, 5, 2, 0, 5, 7, 5, 2, 6, 10, 8, 6, 7, 7, 6, 4, 0, 0, 4, 0, 8, 0, 12, 4, 0, 0, 0, 3, 2, 5, 4, 0, 6, 1, 6, 5, 3, 1, 1, 2, 12, 10, 4, 0, 1, 4, 5, 5, 6, 2, 7, 3, 3, 5, 3, 15, 0, 10, 3, 5, 13, 8, 2, 10, 11, 11, 11, 7, 2, 3, 1, 12, 13, 4, 5, 6, 7, 12, 10, 7, 1, 3, 6, 0, 1, 4, 5, 4, 0, 13, 15, 1, 7, 7, 7, 4, 2, 4, 11, 13, 3, 5, 4, 1, 4, 10, 13, 0, 1, 1, 0]
# 第二轮异或运算结果(后面用round_2表示)
# [96, 85, 3, 6, 4, 2, 3, 1, 3, 5, 6, 10, 0, 12, 6, 12, 1, 9, 6, 2, 2, 2, 0, 6, 3, 0, 10, 7, 14, 4, 2, 3, 3, 0, 7, 1, 3, 2, 1, 4, 7, 4, 3, 7, 7, 2, 5, 2, 2, 7, 4, 12, 2, 14, 1, 0, 1, 2, 4, 0, 4, 4, 8, 8, 12, 8, 4, 0, 0, 3, 1, 7, 1, 4, 6, 7, 7, 3, 6, 2, 0, 3, 14, 6, 14, 4, 1, 5, 1, 0, 3, 4, 5, 4, 0, 6, 6, 12, 15, 10, 9, 6, 8, 5, 10, 8, 1, 0, 0, 12, 5, 1, 2, 13, 1, 9, 1, 3, 1, 11, 6, 13, 6, 2, 5, 6, 1, 5, 1, 1, 4, 13, 2, 14, 6, 0, 0, 3, 6, 6, 15, 6, 14, 6, 1, 5, 5, 14, 7, 13, 1, 0, 1]我们可以发现 round_1[0]102 xor cipher[0]round_1[1]cipher[0] xor cipher[1]依次类推这里的102是已知的 round_2[0]52 xor round_1[0]round_2[1]round_1[0] xor round_1[1]依次类推 这里的52实际上等于cipher[len(cipher)-1]也就是cipher的最后一个值 但是由于cipher是RSA加密后的密文所以其实cipher[len(cipher)-1]是未知的 想要逆向两轮异或运算从而得到正确的RSA密文 需要爆破cipher[len(cipher)-1]范围是十进制数字字符
# -*- coding:utf-8 -*-
import gmpy2
from Crypto.Util.number import long_to_bytes
# res 相当于 round_2
res[0x63,0x55,0x4,0x3,0x5,0x5,0x5,0x3,0x7,0x7,0x2,0x8,0x8,0xb,0x1,0x2,0xa,0x4,0x2,0xd,0x8,0x9,0xc,0x9,0x4,0xd,0x8,0x0,0xe,0x0,0xf,0xd,0xe,0xa,0x2,0x2,0x1,0x7,0x3,0x5,0x6,0x4,0x6,0x7,0x6,0x2,0x2,0x5,0x3,0x3,0x9,0x6,0x0,0xb,0xd,0xb,0x0,0x2,0x3,0x8,0x3,0xb,0x7,0x1,0xb,0x5,0xe,0x5,0x0,0xa,0xe,0xf,0xd,0x7,0xd,0x7,0xe,0x1,0xf,0x1,0xb,0x5,0x6,0x2,0xc,0x6,0xa,0x4,0x1,0x7,0x4,0x2,0x6,0x3,0x6,0xc,0x5,0xc,0x3,0xc,0x6,0x0,0x4,0xf,0x2,0xe,0x7,0x0,0xe,0xe,0xc,0x4,0x3,0x4,0x2,0x0,0x0,0x2,0x6,0x2,0x3,0x6,0x4,0x4,0x4,0x7,0x1,0x2,0x3,0x9,0x2,0xc,0x8,0x1,0xc,0x3,0xc,0x2,0x0,0x3,0xe,0x3,0xe,0xc,0x9,0x1,0x7,0xf,0x5,0x7,0x2,0x2,0x4]
p92582184765240663364795767694262273105045150785272129481762171937885924776597
q107310528658039985708896636559112400334262005367649176746429531274300859498993
np*q
e950501
phin(p-1)*(q-1)
dgmpy2.invert(e,phin)
# i 相当于 cipher[len(cipher)-1]爆破的目标
for i in range(0x30,0x3a):tmp1[res[0]^i] # tmp1 相当于 round_1for j in range(1,len(res)):tmp1.append(res[j]^tmp1[j-1])tmp2[tmp1[0]^102]# tmp2 相当于 cipherfor j in range(1,len(tmp1)):tmp2.append(tmp1[j]^tmp2[j-1])wrong0for m in tmp2: # cipher 中每个元素都必须是十进制数字字符if m0x30 or m0x39:wrong1breakif wrong:continueif itmp2[len(tmp2)-1]:# 验证爆破的目标和还原出来的cipher[len(cipher)-1]是否相同cipherfor k in tmp2:cipherchr(k)cipher_numint(cipher)mgmpy2.powmod(cipher_num,d,n)# RSA解密print(long_to_bytes(m))
# hgame{g0_and_g0_http_5erv3r_nb}ezvm
vm调试确定输入的长度应为32 对输入的处理逻辑为输入的每个字符左移1位和一个值异或然后比较 构造一个长度为32的输入得到经程序处理后的结果 逆向对输入的处理逻辑得到输入的每个字符左移1位后要去异或的值的集合 最后由真正的密文爆破出flag
# -*- coding:utf-8 -*-
shgame{abcdefghijklmnopqrstuvwxy}# 对输入的处理逻辑输入的每个字符左移1位和一个值异或然后比较print(hex((ord(h)1)^0x5e))# 0x8e
print(hex((ord(g)1)^0x46))# 0x88# my_res: 构造的输入s经过程序处理后的结果
my_res[0x8e,0x88,0xa3,0x99,0xc4,0xa5,0x8b,0xdb,0x97,0x96,0xfc,0xfb,0xe7,0x91,0xb1,0xef,0xb2,0xe3,0xcf,0xc4,0x85,0xde,0xc0,0xb4,0xa0,0xb6,0xdf,0xa2,0xad,0xd3,0x92,0xc1]
# 逆向对输入的处理逻辑得到输入的每个字符左移1位后要去异或的值的集合
xor_box[]
for i in range(len(s)):tmp(ord(s[i])1)0xffxor_box.append(tmp^my_res[i])# res: 真正的密文
res[0x8e,0x88,0xa3,0x99,0xc4,0xa5,0xc3,0xdd,0x19,0xec,0x6c,0x9b,0xf3,0x1b,0x8b,0x5b,0x3e,0x9b,0xf1,0x86,0xf3,0xf4,0xa4,0xf8,0xf8,0x98,0xab,0x86,0x89,0x61,0x22,0xc1]
flag
for i in range(len(res)):for j in range(32,128):if (j1)^xor_box[i]res[i]:flagchr(j)
print(flag)
# hgame{Ea$Y-Vm-t0-PrOTeCT_cOde!!}hardasm
近7000行的汇编指令应该是有什么地方设计得很巧妙 起调试构造一个长度为32的输入hgame{abcdefghijklmnopqrstuvwxy} 在如下位置下断点 程序断下后观察此时[rsp70hvar_50]上的数据 可以发现前6个数据均为非0的0xFF而后面的数据均为0 这是因为我们构造的输入的前6个字符hgame{“是正确的而后面凑长度的字符是错误的 于是我们可以知道 可以通过[rsp70hvar_50]上连续的0xFF的个数n确定我们的输入前n个字符是正确的 那么怎么知道[rsp70hvar_50]上连续的0xFF的个数n呢我们需要能够打印的函数 程序最后会打印success或者error”我们可以利用这个打印函数 从框住的汇编指令处可以观察到我们的目标数据地址是借rcx寄存器传入打印函数的 于是我们patch程序将[rsp70hvar_50]借rcx传入打印函数如下 这里不知道怎么能直接实现lea rcx,[rsp70hvar_50]如果有师傅知道请在评论区留言谢谢 这样程序在最后就不会打印success或者error而会打印[rsp70hvar_50]上的数据 将patch应用到程序后再起调试同样的输入在调用打印函数printf后下断观察打印出的内容 可以看到虽然没有打印出具体的字符因为0xFF不可见但是长度是正确的为6 或者可以用python来确认
import subprocess
flaghgame{abcdefghijklmnopqrstuvwxy}
p subprocess.Popen([D:\\ctfdownloadfiles\\hardasm.exe], stdinsubprocess.PIPE, stdoutsubprocess.PIPE, stderrsubprocess.PIPE)
p.stdin.write(flag.encode())
p.stdin.close()
out p.stdout.read()
print(len(out))
for c in out:print(hex(c))
# 6
# 0xff
# 0xff
# 0xff
# 0xff
# 0xff
# 0xff最后就是依次爆破每个位置上的字符 根据程序打印出的连续的0xFF的个数来确定某位置上的字符是否正确
# -*- coding:utf-8 -*-
import subprocess
real_flaghgame{#绝对正确的前6个字符
cur_index6#当前爆破的位置
while cur_index32:for i in range(32,128):#当前爆破的位置上的字符real_flag_arr [0] * 32for j in range(len(real_flag)):#正确的先复制一下real_flag_arr[j]ord(real_flag[j])real_flag_arr[len(real_flag_arr)-1]ord(})#最后一个字符}固定for j in range(len(real_flag_arr)-2,cur_index,-1):#除了当前爆破的位置其他位置上都设置为32real_flag_arr[j]32real_flag_arr[cur_index]i#设置当前爆破的位置上的字符real_flag_arr_s.join(chr(k) for k in real_flag_arr)#输入到程序中的字符串p subprocess.Popen([D:\\ctfdownloadfiles\\hardasm.exe], stdinsubprocess.PIPE, stdoutsubprocess.PIPE, stderrsubprocess.PIPE)p.stdin.write(real_flag_arr_s.encode())p.stdin.close()out p.stdout.read()if len(out)cur_index:#判断程序打印出的0xFF的个数是否增加增加则说明当前爆破的位置上的字符设置的是正确的real_flagchr(i)cur_index1print(real_flag)break
# hgame{r
# hgame{ri
# hgame{rig
# hgame{righ
# hgame{right
# hgame{right_
# hgame{right_y
# hgame{right_yo
# hgame{right_you
# hgame{right_your
# hgame{right_your_
# hgame{right_your_a
# hgame{right_your_as
# hgame{right_your_asm
# hgame{right_your_asm_
# hgame{right_your_asm_i
# hgame{right_your_asm_is
# hgame{right_your_asm_is_
# hgame{right_your_asm_is_g
# hgame{right_your_asm_is_go
# hgame{right_your_asm_is_goo
# hgame{right_your_asm_is_good
# hgame{right_your_asm_is_good!
# hgame{right_your_asm_is_good!!
# hgame{right_your_asm_is_good!!}
# hgame{right_your_asm_is_good!!}