当前位置: 代码迷 >> 综合 >> 【buuctf】xor
  详细解决方案

【buuctf】xor

热度:93   发布时间:2023-11-04 22:45:47.0

buuctf xor

主函数

int __cdecl main(int argc, const char **argv, const char **envp)
{
    char *v3; // rsi@1__int64 v4; // rax@8signed int i; // [sp+2Ch] [bp-124h]@2char input[264]; // [sp+40h] [bp-110h]@1__int64 v8; // [sp+148h] [bp-8h]@1v8 = *(_QWORD *)__stack_chk_guard_ptr;memset(input, 0, 0x100uLL);v3 = (char *)256;printf("Input your flag:\n", 0LL);get_line(input, 256LL);if ( strlen(input) != 33 )goto LABEL_13;for ( i = 1; i < 33; ++i )input[i] ^= input[i - 1];v3 = global;if ( !strncmp(input, global, 041uLL) )printf("Success", v3);else
LABEL_13:printf("Failed", v3);v4 = *(_QWORD *)__stack_chk_guard_ptr;if ( *(_QWORD *)__stack_chk_guard_ptr == v8 )LODWORD(v4) = 0;return v4;
}
for ( i = 1; i < 33; ++i )input[i] ^= input[i - 1];

可以看到该程序把用户输入进行异或加密操作后与global字符串进行比较,相同的话就通过
加密算法为后一个字符与前一个字符做异或,并把结果赋值给后一个字符。

x2=x2^x1
x3=x3^x2

由于异或运算的特殊性
两次异或相同值后结果不变
故解密思路

a2=x2^x1
a3=x3^x2

写出解密脚本

由于不确定是16进制还是字符,用isinstance函数进行类型遍历

str1 = ['f', 0x0A, 'k', 0x0C, 'w', '&', 'O', '.', '@', 0x11, 'x', 0x0D, 'Z', ';', 'U', 0x11, 'p', 0x19, 'F', 0x1F, 'v','"', 'M', '#', 'D', 0x0E, 'g', 6, 'h', 0x0F, 'G', '2', 'O']
strddd="f"for i in range(1,len(str1)):if isinstance(str1[i],str):if isinstance(str1[i-1],str):strddd+=chr(ord(str1[i])^ord(str1[i-1]))else:strddd+=chr(ord(str1[i])^str1[i-1])else:strddd+=chr(str1[i] ^ ord(str1[i - 1]))
print(''.join(strddd))