BUUCTF 逆向题目 xor
题目地址:
https://buuoj.cn/challenges
https://files.buuoj.cn/files/caa0fdad8f67a3115e11dc722bb9bba7/7ea34089-68ff-4bb7-8e96-92094285dfe9.zip
首先,查壳
信息:
文件名: H:
大小: 8884(8.68 KiB)
操作系统: macOS(10.13.0)
架构: X86_64
模式: 64 位
类型: EXECUTE
字节序: LE
使用IDA64打开文件
int __fastcall main(int argc, const char **argv, const char **envp)
{
int i;
char flag[264];
memset(flag, 0, 0x100uLL);
printf("Input your flag:\n");
get_line(flag, 256LL);
if ( strlen(flag) != 33 )
goto LABEL_7;
for ( i = 1; i < 33; ++i )
flag[i] ^= flag[i - 1];
if ( !strncmp(flag, global, 0x21uLL) )
printf("Success");
else
LABEL_7:
printf("Failed");
return 0;
}
分析代码,flag为33长度字符串,flag的字符每一位与前一位异或后得到新字符串global
异或的特点:
如果A^B=C,则有A^C=B和B^C=A
def xor_encrypt(input_str):
result_list = [input_str[0]]
for i in range(1, len(input_str)):
result_char = chr(ord(result_list[i - 1]) ^ ord(input_str[i]))
result_list.append(result_char)
return result_list
def format_output(result_list):
formatted_output = []
for char in result_list:
if char.isprintable():
formatted_output.append(char)
else:
hex_representation = "0x{:02x}".format(ord(char))
formatted_output.append(hex_representation)
return formatted_output
user_input = input("请输入字符串: ")
encrypted_list = xor_encrypt(user_input)
formatted_output = format_output(encrypted_list)
print("异或后的结果数组:", formatted_output)
请输入字符串: flag{QianQiuWanDai_YiTongJiangHu}
异或后的结果数组: ['f', '0x0a', 'k', '0x0c', 'w', '&', 'O', '.', '@', '0x11', 'x', '0x0d', 'Z', ';', 'U', '0x11', 'p', '0x19', 'F', '0x1f', 'v', '"', 'M', '#', 'D', '0x0e', 'g', '0x06', 'h', '0x0f', 'G', '2', 'O']
global为
'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'
要想获得flag则需要global的每一位与前一位异或获得flag