代码还原之 函数

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

复制代码
    #if 0
    /*27ec:	48 8d 3d 58 39 00 00 	lea    0x3958(%rip),%rdi        # 614b <_IO_stdin_used@@Base+0x14b> // rdi="COLUMNS"
    27f3:	e8 e8 fb ff ff       	callq  23e0 <getenv@plt>*/	// getenv(rdi)
    char *ptr_strtol;
    struct winsize p_0xa8;
        /*struct winsize
        {
            unsigned short ws_row; // 窗口字符行数。
            unsigned short ws_col; // 窗口字符列数。
            unsigned short ws_xpixel; // 窗口宽度,象素值。
            unsigned short ws_ypixel; // 窗口高度,象素值。
        };*/
    
	char *p_getenv = getenv("COLUMNS");

    /*27f8:	48 85 c0             	test   %rax,%rax			// rax="113"
    27fb:	74 09                	je     2806 <__sprintf_chk@plt+0x96>	// no
    27fd:	80 38 00             	cmpb   $0x0,(%rax)			// 0-0x31, so zf=0
    2800:	0f 85 1e 07 00 00    	jne    2f24 <__sprintf_chk@plt+0x7b4>*/	// if zf=0 than jump
    
	if ((p_getenv != NULL) && (*p_getenv != 0)) {
			// goto 2f24 --> 283a
            col = strtol(p_getenv, &ptr_strtol, 0);
            printf("col = %d\n", col);

            if ( (!*ptr_strtol) && (col > 0) && (col < 0x7ffffffd))

	} else {

    /*2806:	48 8d 94 24 a8 00 00 	lea    0xa8(%rsp),%rdx  // rdx=(rsp+0xa8)=struct winsize p_0xa8
    280d:	00 
    280e:	be 13 54 00 00       	mov    $0x5413,%esi
    2813:	bf 01 00 00 00       	mov    $0x1,%edi
    2818:	31 c0                	xor    %eax,%eax
    281a:	e8 51 fd ff ff       	callq  2570 <ioctl@plt>
    281f:	41 89 c0             	mov    %eax,%r8d*/          // r8d=ret,把返回值存起来, 此处有可能是一个函数调用的返回。
    
        // include/uapi/asm-generic/ioctls.h:38:#define TIOCGWINSZ 0x5413
        ret = ioctl(1, 0x5413, &p_0xa8);
        printf("ret = %d, 0x%x\n", ret, p_0xa8.ws_row);    
        col = 0x84;
    
    /*2822:	b8 84 00 00 00       	mov    $0x84,%eax   // 0x84=132
    2827:	45 85 c0             	test   %r8d,%r8d    // r8d=ret
    282a:	78 0e                	js     283a <__sprintf_chk@plt+0xca>    SF = 1 负数, jump
    282c:	0f b7 8c 24 aa 00 00 	movzwl 0xaa(%rsp),%ecx  // (rsp+0xaa)=p_0xa8.ws_col,通过查看内存值可以判断结构的成员及大小
    2833:	00 
    2834:	66 85 c9             	test   %cx,%cx          // cx=cx&0xff
    2837:	0f 45 c1             	cmovne %ecx,%eax */     // cmovne 不等传送
        if (ret >= 0) {
            if (p_0xa8.ws_col != 0) {
                col = p_0xa8.ws_col;
            }
        }
    }
    #endif

    col = get_ws_col();

    /*283a:	48 8d 35 2b 3a 00 00 	lea    0x3a2b(%rip),%rsi        # 626c <_IO_stdin_used@@Base+0x26c> // ""
    2841:	bf 06 00 00 00       	mov    $0x6,%edi
    2846:	89 05 78 6a 00 00    	mov    %eax,0x6a78(%rip)        # 92c4 <_IO_stdin_used@@Base+0x32c4> // 0x55555555684c+0x6a78=0x55555555d2c4=113
    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;

}

相关推荐
Benszen2 分钟前
Secret详解
linux·运维·服务器
是星辰吖~7 分钟前
WIN32_线程(下)
汇编
Dlrb121125 分钟前
Linux网络编程-网络基础概念(IP, UDP协议)
linux·服务器·网络·网络基础·端口号·ip协议·udp协议
小易撩挨踢1 小时前
[特殊字符] Linux 7.1 内核正式发布:距 7.0 仅 9 周,新 CPU/GPU/文件系统全面升级
linux·运维
vortex52 小时前
Linux进程权限继承研究:从setuid()到exec()与system()的行为差异
linux·服务器·系统安全·suid
swordbob2 小时前
3 大 I/O 模型BIO / NIO / AIO
java·linux·spring
小小小花儿2 小时前
服务器上修改个人账户权限
linux·服务器
Coisinier2 小时前
RHCE中shell脚本基础(磁盘剩余空间监控,Web 服务状态检查,curl 访问 Web 服务并返回状态)
linux·运维·服务器·前端·nginx·操作系统
暮云星影3 小时前
全志linux开发屏幕适配(二)`HDMI`驱动适配说明
linux·arm开发·驱动开发