using
System;
using
System.Collections.Generic;
using
System.IO;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
虚拟机
{
class
Program
{
static
int
[] data =
new
int
[]
{
0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x22, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x27, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x21, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x21, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x2D, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x81, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x21, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x26, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x2D, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x5A, 0x00, 0x00, 0x00,
0x2D, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x2F, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x38, 0x62, 0x64, 0x61, 0x65, 0x34, 0x35, 0x36, 0x2D, 0x35, 0x61, 0x63,
0x38, 0x2D, 0x31, 0x31, 0x65, 0x39, 0x2D, 0x61, 0x31, 0x63, 0x31, 0x2D, 0x38, 0x38, 0x65, 0x39,
0x66, 0x65, 0x38, 0x30, 0x66, 0x65, 0x61, 0x66, 0x65, 0x55, 0x63, 0x57, 0x01, 0x04, 0x53, 0x06,
0x49, 0x49, 0x49, 0x1F, 0x1F, 0x07, 0x57, 0x51, 0x57, 0x43, 0x5F, 0x57, 0x57, 0x5E, 0x43, 0x57,
0x0A, 0x02, 0x57, 0x43, 0x5E, 0x03, 0x5E, 0x57, 0x00, 0x00, 0x59, 0x0F, 0x77, 0x72, 0x6F, 0x6E,
0x67, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x63, 0x6F, 0x72, 0x72, 0x65, 0x63, 0x74, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x66, 0x6C, 0x61, 0x67, 0x20, 0x69, 0x73, 0x20, 0x66, 0x6C,
0x61, 0x67, 0x7B, 0x59, 0x6F, 0x75, 0x72, 0x50, 0x61, 0x74, 0x63, 0x68, 0x7D, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
};
static
void
Main(
string
[] args)
{
var
buf =
new
int
[5]{0,0,0,0,0};
var
index = 0;
var
idata =
new
int
[data.Length / 4];
for
(
var
i = 0;i < idata.Length;i++)
{
idata[i] = data[i * 4] | (data[i * 4 + 1] << 8) | (data[i * 4 + 2] << 16) | (data[i * 4 + 3] << 24);
}
var
lastcmd = 0x21;
var
lastindex = 0;
var
cnt = 0;
var
stb =
new
StringBuilder();
while
(
true
)
{
start:
stb.Append(index +
":\r\n"
);
switch
(idata[index])
{
case
0x21:
buf[idata[index + 1]] = idata[index + 2];
stb.Append(
"buf["
+ idata[index + 1] +
"] = "
+ idata[index + 2] +
";\r\n"
);
break
;
case
0x22:
buf[idata[index + 1]] = buf[idata[index + 2]];
stb.Append(
"buf["
+ idata[index + 1] +
"] = buf["
+ idata[index + 2] +
"];\r\n"
);
break
;
case
0x23:
buf[idata[index + 1]] = idata[buf[idata[index + 2]]];
stb.Append(
"buf["
+ idata[index + 1] +
"] = code[buf["
+ idata[index + 2] +
"]];"
);
if
(buf[idata[index + 2]] >= (0x204 + 0x6F) / 4)
{
stb.Append(
"//读Patch"
);
}
else
if
(buf[idata[index + 2]] >= 0x204 / 4)
{
stb.Append(
"//读敏感区域,偏移:"
+ buf[idata[index + 2]]);
}
else
{
stb.Append(
"//"
+ buf[idata[index + 2]] +
""
);
}
stb.Append(
"\r\n"
);
break
;
case
0x24:
idata[buf[idata[index + 1]]] = buf[idata[index + 2]];
stb.Append(
"code[buf["
+ idata[index + 1] +
"]] = buf["
+ idata[index + 2] +
"];"
);
if
(buf[idata[index + 1]] >= (0x204 + 0x6F) / 4)
{
stb.Append(
"//写Patch"
);
}
else
if
(buf[idata[index + 1]] >= 0x204 / 4)
{
stb.Append(
"//写敏感区域,偏移:"
+ buf[idata[index + 1]]);
}
else
{
stb.Append(
"//"
+ buf[idata[index + 1]] +
""
);
}
stb.Append(
"\r\n"
);
break
;
case
0x25:
buf[idata[index + 1]] += buf[idata[index + 2]];
stb.Append(
"buf["
+ idata[index + 1] +
"] += buf["
+ idata[index + 2] +
"];\r\n"
);
break
;
case
0x26:
buf[idata[index + 1]] -= buf[idata[index + 2]];
stb.Append(
"buf["
+ idata[index + 1] +
"] -= buf["
+ idata[index + 2] +
"];\r\n"
);
break
;
case
0x27:
buf[idata[index + 1]] ^= buf[idata[index + 2]];
stb.Append(
"buf["
+ idata[index + 1] +
"] ^= buf["
+ idata[index + 2] +
"];\r\n"
);
break
;
case
0x28:
buf[idata[index + 1]] <<= buf[idata[index + 2]] & 0xFF;
stb.Append(
"buf["
+ idata[index + 1] +
"] <<= buf["
+ idata[index + 2] +
"];\r\n"
);
break
;
case
0x29:
buf[idata[index + 1]] >>= buf[idata[index + 2]] & 0xFF;
stb.Append(
"buf["
+ idata[index + 1] +
"] >>= buf["
+ idata[index + 2] +
"];\r\n"
);
break
;
case
0x2A:
buf[idata[index + 1]] &= buf[idata[index + 2]];
stb.Append(
"buf["
+ idata[index + 1] +
"] &= buf["
+ idata[index + 2] +
"];\r\n"
);
break
;
case
0x2B:
index = buf[idata[index + 1]];
stb.Append(
"jmp "
+ buf[idata[index + 1]] +
";\r\n"
);
goto
start;
break
;
case
0x2C:
stb.Append(
"if(buf["
+ idata[index + 2] +
"] == 0)\r\n{\r\n\tjmp "
+ buf[idata[index + 1]] +
";\r\n}\r\n"
);
if
(buf[idata[index + 2]] == 0)
{
stb.Append(
"//上述跳转已成立\r\n\r\n"
);
index = buf[idata[index + 1]];
goto
start;
}
break
;
case
0x2D:
stb.Append(
"if(buf["
+ idata[index + 2] +
"] != 0)\r\n{\r\n\tjmp "
+ buf[idata[index + 1]] +
";\r\n}\r\n"
);
if
(buf[idata[index + 2]] != 0)
{
{
stb.Append(
"//上述跳转已成立\r\n\r\n"
);
index = buf[idata[index + 1]];
goto
start;
}
}
break
;
case
0x30:
Console.WriteLine(
"wrong"
);
goto
exit;
break
;
case
0x2F:
Console.WriteLine(
"correct"
);
Console.WriteLine(
"flag is flag{YourPatch}"
);
goto
exit;
break
;
case
0x2E:
Console.WriteLine(
"CPU已终止运行"
);
goto
exit;
break
;
}
index += 3;
}
exit:
Console.WriteLine(
"代码:"
);
Console.WriteLine(stb.ToString());
File.WriteAllText(
@"D:\exercise\CTF\20190421\strange_int_740dfa57eab289ada9d30380ef4393d5\code.txt"
,stb.ToString());
while
(
true
);
}
}
}