Sunday, January 9, 2011

Buffer Overflow ให้โปรแกรม spawn shell (โจทย์)

หลังจากทำไป 2 แบบฝึกหัด ผมยังมีโจทย์ให้อีก 2 โจทย์ โดยไม่มีเฉลยนะครับ (ยังไงผมก็ไม่เฉลยนะ) ถ้าทำไม่ได้ แสดงว่ายังไม่เข้าใจเรื่องนี้จริงๆ

ข้อ1 (ex_06_5.c) ข้อนี้น่าจะทำได้ คล้ายๆกับแบบฝึกหัดเลย แต่อยากให้ลองใช้ gdb ดูตำแหน่งของตัวแปรต่างๆ ใน stack ด้วย

/*
gcc -fno-pie -fno-stack-protector -z norelro -z execstack -o ex_06_5 ex_06_5.c
sudo su -c "chown root: ex_06_5;chmod 4755 ex_06_5"
*/
#include <stdio.h>
#include <string.h>

#define MAX_CPY 530

void vuln(char *d, char *s)
{
  unsigned long n = MAX_CPY;
  strncpy(d, s, n);
}

int main(int argc, char **argv)
{
  int i = 0;
  char c = 1;
  short s = -1;
  char buf[1];
  
  vuln(buf, argv[1]);

  return 0;
}

ข้อ2 (ex_06_6.c) ข้อนี้ต้องใช้ความรู้ Linux นิดหน่อย ถ้าใครเคยทำตามที่ผมให้ลอง คงไม่มีอะไรยาก

/*
gcc -fno-pie -fno-stack-protector -z norelro -z execstack -o ex_06_6 ex_06_6.c
sudo su -c "chown root: ex_06_6;chmod 4755 ex_06_6"
*/
#include <stdio.h>
#include <string.h>

#define MAX_CPY 24

int main(int argc, char **argv)
{
  char buf[1];

  strncpy(buf, argv[1], MAX_CPY);

  return 0;
}

สำหรับข้อ2 ผมมี Hint ให้ สำหรับคนที่ลองทำแล้วทำไม่ได้จริงๆ เพียงแค่ drag mouse คลุมบรรทัดข้างล่าง
Hint: ใช้ env หรือ argv ช่วย

1 comment:

  1. What are the best online casino site?
    If you want to learn about the best online casinos, you will learn how to register and play at one of the most popular online casinos 카지노사이트luckclub in the world.

    ReplyDelete