简单的Elf逆向Writeup

时间:2021-07-02 18:14:40

ElfCrackMe1

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

简单的Elf逆向Writeup

题目来源:IDF实验室 CTF训练营;题目链接http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=39
题目下载:http://pan.baidu.com/s/1kTl5wxD

解法1:

IDA查看伪代码法:

  1. 下载文件,现在Linux环境下运行,可以看到关键字符串u r wrongplz enter the flag:
    简单的Elf逆向Writeup
  2. 把文件拖到IDA中,shift+F12查找字符串,双击u r right跳转到相应位置简单的Elf逆向Writeup
  3. 如下图,双击调用关键字符串u r right的函数,简单的Elf逆向Writeup
    跳转后F5查看伪代码如下
     int __cdecl main(int argc, const char **argv, const char **envp)
    {
    int v3; // ebx@6
    const char **v4; // rdx@22
    __int64 v6; // [sp+0h] [bp-C0h]@1
    __int64 v7; // [sp+8h] [bp-B8h]@1
    __int64 v8; // [sp+10h] [bp-B0h]@1
    __int64 v9; // [sp+18h] [bp-A8h]@1
    __int64 v10; // [sp+20h] [bp-A0h]@1
    __int64 v11; // [sp+28h] [bp-98h]@1
    __int64 v12; // [sp+30h] [bp-90h]@1
    __int64 v13; // [sp+38h] [bp-88h]@1
    int v14; // [sp+40h] [bp-80h]@1
    char v15[]; // [sp+80h] [bp-40h]@2
    char v16; // [sp+91h] [bp-2Fh]@14
    char v17; // [sp+92h] [bp-2Eh]@15
    char v18; // [sp+93h] [bp-2Dh]@16
    char v19; // [sp+94h] [bp-2Ch]@17
    char v20; // [sp+95h] [bp-2Bh]@18
    int v21; // [sp+A4h] [bp-1Ch]@1
    int v22; // [sp+A8h] [bp-18h]@7
    int i; // [sp+ACh] [bp-14h]@9 v21 = ;
    memset(&v6, , 0x58uLL);
    v6 = 854698492143LL;
    v7 = 880468295913LL;
    v8 = 597000454391LL;
    v9 = 605590388953LL;
    v10 = 932007903423LL;
    v11 = 760209211613LL;
    v12 = 579820585151LL;
    v13 = 940597838039LL;
    v14 = ;
    printf(
    "plz enter the flag:",
    argv,
    11LL,
    854698492143LL,
    880468295913LL,
    597000454391LL,
    605590388953LL,
    932007903423LL,
    760209211613LL,
    579820585151LL,
    940597838039LL,
    *(_QWORD *)&v14);
    while ( )
    {
    v3 = v21;
    v15[v3] = getch();
    if ( !v15[v3] || v15[v21] == )
    break;
    if ( v15[v21] == )
    {
    printf("\b\b", v6, v7, v8, v9, v10, v11, v12, v13, *(_QWORD *)&v14);
    --v21;
    }
    else
    {
    putchar(v15[v21++]);
    }
    }
    v22 = ;
    if ( v21 != )
    v22 = ;
    for ( i = ; i <= ; ++i )
    {
    if ( v15[i] != (*((_DWORD *)&v6 + i) - ) / )
    {
    v22 = ;
    argv = (const char **)((*((_DWORD *)&v6 + i) - ) / );
    printf("%d", argv, v6, v7, v8, v9, v10, v11, v12, v13, *(_QWORD *)&v14);
    }
    }
    if ( v16 != || v17 != || v18 != || v19 != || v20 != )
    v22 = ;
    v15[v21] = ;
    puts("\r");
    if ( v22 )
    {
    puts("u r wrong\r\n\r");
    main((unsigned __int64)"u r wrong\r\n\r", argv, v4);
    }
    else
    {
    puts("u r right!\r");
    }
    return ;
    }

分析这段伪代码,可以得到关键部分如下:

简单的Elf逆向Writeup

  • v22需为0,按'/'随手注释
    简单的Elf逆向Writeup
  • 如上图中所示,要使v22==0,所有v22=1的语句均不能运行,则需要:v21==22,69行判断均不进入,既要 v15[i] != (*((_DWORD *)&v6 + i) - 1) / 2,同时,v16~v20依次等于48,56,50,51,125,即字符0823}(在相应数字上按r键把相应的ASCII码转换为字符)
    同样按'/'键随手注释
    简单的Elf逆向Writeup
  • 找到定义v15的代码处,则根据上述的关键条件,初步猜测需要输入22位字符,其中前17位存入字符串v15中,后5位覆盖v16~v20的取值,使v16~v20分别等于0823}
    简单的Elf逆向Writeup
  • 继续向下分析,找到输入的代码块,经过分析可得到关键信息如上图,其中v21即为输入的长度,通过关键条件v21==22验证了猜测输入字符串长度为22,同时需要保证输入的后五位为0823}
    简单的Elf逆向Writeup
  • 向下分析v15需要满足的条件,着重分析v15[i] != (*((_DWORD *)&v6 + i) - 1) / 2

    &v6为取v6的地址;(_DWORD *)&v6强制转化为_DWORD型指针,即两个字节;((_DWORD *)&v6 + i)为从&v6向后取sizeof(_DWORD)*i个字节;*((_DWORD *)&v6 + i) - 1)为取从&v6向后取4i个字节的值
    关于地址与指针加减问题http://www.cnblogs.com/WangAoBo/p/6365114.html

    简单的Elf逆向Writeup

  • 如上,分析v6的赋值段代码,v6~v13为_int64型,占8个字节,int型为4个字节,化为16进制如上图
    简单的Elf逆向Writeup
  • v6~v14通过小端存储方式在内存中的存储情况如上

则可写出python脚本解得flag: wctf{ElF_lnX_Ckm_0823}

简单的Elf逆向Writeup
python脚本如下:

 v6=[0x0EF, 0x0C7, 0x0E9, 0x0CD, 0x0F7, 0x8B, 0x0D9,
0x8D, 0x0BF, 0x0D9, 0x0DD, 0x0B1, 0x0BF, 0x87,
0x0D7, 0x0DB, 0x0BF] L=[]
for i in range(17):
num = (v6[i] - 1)/2
ans = chr(int(num))
L.append(ans) flag = ''.join(L)
flag+='0823}' print(flag)