代码还原之 函数

指令堆里逆向出来的代码有歧义,有三处返回,有嵌套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;

}

相关推荐
姓刘的哦10 分钟前
ubuntu中使用docker
linux·ubuntu·docker
代码程序猿RIP19 分钟前
【Linux】(1)—进程概念-⑤进程调度
linux·运维
_lizhiqiang34 分钟前
联想拯救者R9000P 网卡 Realtek 8852CE Ubuntu/Mint linux 系统睡眠后,无线网卡失效
linux·运维·ubuntu·r9000p·无线网卡·8852ce
心随_风动1 小时前
SUSE Linux 发行版全面解析:从开源先驱到企业级支柱
linux·运维·开源
西北大程序猿3 小时前
单例模式与锁(死锁)
linux·开发语言·c++·单例模式
酷爱码3 小时前
在 Linux 中修改 Apache HTTP Server(httpd)默认端口的完整指南
linux·http·apache
jiunian_cn3 小时前
【Linux】Linux权限
linux·服务器·mysql
betazhou4 小时前
有没有 MariaDB 5.5.56 对应 MySQL CONNECTION_CONTROL 插件
linux·数据库·mysql·oracle·mariadb
烟雨书信4 小时前
Linux中MySQL的逻辑备份与恢复
linux·运维·mysql
wkj0014 小时前
QuaggaJS 配置参数详解
java·linux·服务器·javascript·quaggajs