• 代码还原之 函数


    指令堆里逆向出来的代码有歧义,有三处返回,有嵌套IF语句,故推断出是个函数;

    1. #if 0
    2. /*27ec: 48 8d 3d 58 39 00 00 lea 0x3958(%rip),%rdi # 614b <_IO_stdin_used@@Base+0x14b> // rdi="COLUMNS"
    3. 27f3: e8 e8 fb ff ff callq 23e0 <getenv@plt>*/ // getenv(rdi)
    4. char *ptr_strtol;
    5. struct winsize p_0xa8;
    6. /*struct winsize
    7. {
    8. unsigned short ws_row; // 窗口字符行数。
    9. unsigned short ws_col; // 窗口字符列数。
    10. unsigned short ws_xpixel; // 窗口宽度,象素值。
    11. unsigned short ws_ypixel; // 窗口高度,象素值。
    12. };*/
    13. char *p_getenv = getenv("COLUMNS");
    14. /*27f8: 48 85 c0 test %rax,%rax // rax="113"
    15. 27fb: 74 09 je 2806 <__sprintf_chk@plt+0x96> // no
    16. 27fd: 80 38 00 cmpb $0x0,(%rax) // 0-0x31, so zf=0
    17. 2800: 0f 85 1e 07 00 00 jne 2f24 <__sprintf_chk@plt+0x7b4>*/ // if zf=0 than jump
    18. if ((p_getenv != NULL) && (*p_getenv != 0)) {
    19. // goto 2f24 --> 283a
    20. col = strtol(p_getenv, &ptr_strtol, 0);
    21. printf("col = %d\n", col);
    22. if ( (!*ptr_strtol) && (col > 0) && (col < 0x7ffffffd))
    23. } else {
    24. /*2806: 48 8d 94 24 a8 00 00 lea 0xa8(%rsp),%rdx // rdx=(rsp+0xa8)=struct winsize p_0xa8
    25. 280d: 00
    26. 280e: be 13 54 00 00 mov $0x5413,%esi
    27. 2813: bf 01 00 00 00 mov $0x1,%edi
    28. 2818: 31 c0 xor %eax,%eax
    29. 281a: e8 51 fd ff ff callq 2570 <ioctl@plt>
    30. 281f: 41 89 c0 mov %eax,%r8d*/ // r8d=ret,把返回值存起来, 此处有可能是一个函数调用的返回。
    31. // include/uapi/asm-generic/ioctls.h:38:#define TIOCGWINSZ 0x5413
    32. ret = ioctl(1, 0x5413, &p_0xa8);
    33. printf("ret = %d, 0x%x\n", ret, p_0xa8.ws_row);
    34. col = 0x84;
    35. /*2822: b8 84 00 00 00 mov $0x84,%eax // 0x84=132
    36. 2827: 45 85 c0 test %r8d,%r8d // r8d=ret
    37. 282a: 78 0e js 283a <__sprintf_chk@plt+0xca> SF = 1 负数, jump
    38. 282c: 0f b7 8c 24 aa 00 00 movzwl 0xaa(%rsp),%ecx // (rsp+0xaa)=p_0xa8.ws_col,通过查看内存值可以判断结构的成员及大小
    39. 2833: 00
    40. 2834: 66 85 c9 test %cx,%cx // cx=cx&0xff
    41. 2837: 0f 45 c1 cmovne %ecx,%eax */ // cmovne 不等传送
    42. if (ret >= 0) {
    43. if (p_0xa8.ws_col != 0) {
    44. col = p_0xa8.ws_col;
    45. }
    46. }
    47. }
    48. #endif
    49. col = get_ws_col();
    50. /*283a: 48 8d 35 2b 3a 00 00 lea 0x3a2b(%rip),%rsi # 626c <_IO_stdin_used@@Base+0x26c> // ""
    51. 2841: bf 06 00 00 00 mov $0x6,%edi
    52. 2846: 89 05 78 6a 00 00 mov %eax,0x6a78(%rip) # 92c4 <_IO_stdin_used@@Base+0x32c4> // 0x55555555684c+0x6a78=0x55555555d2c4=113
    53. 284c: e8 4f fe ff ff callq 26a0 <setlocale@plt>*/

    int get_ws_col()
    {
        char *ptr_strtol;
        struct winsize p_0xa8 = {0};
        int col = 0;

        char *p_getenv = getenv("COLUMNS");

        if ((p_getenv != NULL) && (*p_getenv != 0)) {
            col = strtol(p_getenv, &ptr_strtol, 0);
            printf("col = %d\n", col);

            if ( (!*ptr_strtol) && (col > 0) && (col < 0x7ffffffd)) {
                return col;
            }
        }

        if (ioctl(1, 0x5413, &p_0xa8) >= 0) {
            if (p_0xa8.ws_col != 0) {
                return p_0xa8.ws_col;
            }
        }

        reutrn 0x84;
    }

  • 相关阅读:
    MySQL事务隔离级别
    《凤凰架构》第三章——事务处理
    python基础项目实战-俄罗斯方块
    2022 OceanBase 年度发布会:发布四大策略,迈入4.0时代
    k8s-pod管理 3
    云计算和大数据处理
    基于 MediaPipe 的图像去背
    成集云 | 多维表自动查询快递100信息 | 解决方案
    Python调用oracle存储过程返回游标结果
    三维数字沙盘大数据人工智能模拟对抗推演系统开发教程第一课
  • 原文地址:https://blog.csdn.net/xiaozhiwise/article/details/136477529