08.03
1、看之前的2篇文章
08.04
1、換服務器并配置

阿里云服務器創(chuàng)建鏡像備份
登陸控制臺--->實例--->鏡像創(chuàng)建--->導出
需要開通OSS,暫未完成
2、安裝做題需要的環(huán)境等
centOS安裝apt-get, 比較麻煩
告辭,退了
3、換成Ubuntu
但是,字體沒有配色,看起來很難受。
08.05
1、解決沒有高亮顯示
https://blog.csdn.net/wzy1551368/article/details/79394001
sudo apt install gedit
發(fā)現(xiàn)是因為普通賬戶登陸了,而不是管理員登陸。
sudo vim /etc/ssh/sshd_config
找到 #PermitRootLogin prohibit-password, 改為yes
成功修改為root權限登陸!
2、安裝所必須的工具
pwngdb
pwntool
3、題目:fastbin_attack-examples
32位文件,IDA打開
參考文章 https://www.anquanke.com/post/id/86286
其中fd,bk指針只用到了fd
攻擊:覆蓋堆頭,fd指向任意地址。
除此之外對于fastbin還存在double free,house of spirit等攻擊方式。
輸入bins,即可查看bins的內容。

delete之后

使用unsortedbin來泄漏libc的指針。
tcachebins機制是新引入的。
08.06
1、CTF特訓營
-
程序保護機制
D595E9B0-26E4-41FC-8C67-3716B8409375.png
,重定位,重定位信息可寫,full relro 重定位信息不可寫。
free的規(guī)則
tcache
重要結構體:entry 和 pertheread_struct運行代碼
malloc需要包含頭文件 stdlib.h
08.07
1、示例運行
有strcpy函數,需要用到 <string.h> 頭文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void target_func(){
printf("Hacked\n");
}
void show_info_A(char *info){
printf("%s\n",info);
}
struct struct_A{
int type;
int size;
char A_info[0x20];
void (*show_info_ptr)(char *);
};
struct struct_B{
int type;
int size;
char B_info[0x40];
};
void show_info(void *data, int type){
printf("in show_info:%d\n",type);
if(type == 0){
printf("in 0\n");
struct struct_A *struct_A_ptr = (struct struct_A*)data;
struct_A_ptr -> show_info_ptr(struct_A_ptr->A_info);
}
else if(type == 1){
printf("in 1\n");
struct struct_B *struct_B_ptr = (struct struct_B*)data;
printf("%s\n",struct_B_ptr->B_info);
}
}
int main(){
struct struct_A *var_a;
struct struct_B *var_b;
var_a = malloc(sizeof(struct struct_A));
var_a->type = 0;
strcpy(var_a->A_info,"A_info");
var_a->show_info_ptr = show_info_A;
var_a->size = strlen(var_a -> A_info);
var_b = malloc(sizeof(struct struct_B));
scanf("%d",&var_a ->type);
getchar();
gets(var_b->B_info);
var_b->size = strlen(var_b -> B_info);
show_info(var_a,var_a->type);
show_info(var_b,var_b->type);
return 0;
}
gdb中查看函數地址的方法:x 函數名

得到target_func函數的地址為 0x55555555481a
書中有一個錯誤,B對應的是1,如果想有錯誤,應該輸入0.
終端中,雙擊g,回到函數最開始的位置。
思想:填充A_info部分,添加返回地址即可。但是沒成功。
2、unlink
有檢查,突破手段 freenote 或 stkof
源碼:修改指針。如果P的尺寸不在small_bin,并且P前一塊用來指示的尺寸不為NULL,assert兩次。
freenote
3、視頻b站
08.08
1、b站視頻
objdump -d hacknote | less
cat /proc/26666 maps

ldd 程序
可以查看使用的libc是哪一個。

LD_LIBRARY_PATH
真實地址在 .got.plt里面
lay asm可以查看匯編代碼。
libcdb.com可以查找函數的對應地址(概率成功)
08.09-08.10
1、棧
64位,控制rax
rax = rdi

輸出

控制rcx
(1)strcpy, ecx = 輸入字符串(2)ecx為syscall的下一行地址(可以用來泄漏libc基址)
lay reg查看寄存器的值
08.11
1、編寫帶debug symbol 的libc
下載太慢,本機下載再上傳。
CFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" CXXFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" ../configure --prefix=/root/test/glibc-2.19/64
make報錯

編譯命令改為:
CFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -Wno-error=maybe-uninitialized -Wno-error" CXXFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -Wno-error=maybe-uninitialized -Wno-error" ../configure --prefix=/root/test/glibc-2.19/64
還是報錯

/usr/bin/ld: /root/test/glibc-2.19/build/csu/crt1.o: relocation R_X86_64_32S against symbol `__libc_csu_fini' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/crtbegin.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a PIE object
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
../Rules:140: recipe for target '/root/test/glibc-2.19/build/iconv/iconvconfig' failed
make[2]: *** [/root/test/glibc-2.19/build/iconv/iconvconfig] Error 1
make[2]: Leaving directory '/root/test/glibc-2.19/iconv'
Makefile:213: recipe for target 'iconv/others' failed
make[1]: *** [iconv/others] Error 2
make[1]: Leaving directory '/root/test/glibc-2.19'
Makefile:9: recipe for target 'all' failed
make: *** [all] Error 2
參考 https://sourceware.org/bugzilla/show_bug.cgi?id=20845
CFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" CXXFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" ../configure --prefix=/root/test/glibc-2.19/64 --disable-shared
還是報錯,但是錯誤比剛才少了一些

不編譯2.19了,編譯2.23版本
參考視頻和文章 http://www.itdecent.cn/p/1a966b62b3d4
CFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" CXXFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" ../configure --prefix=/root/glibc/glibc-2.23/64 --disable-shared
不行,改為
CFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -Wno-error=maybe-uninitialized -Wno-error" \ CXXFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -Wno-error=maybe-uninitialized -Wno-error" \ ../configure --prefix=/root/glibc/glibc-2.23/64 --disable-shared
還是錯誤,可能是版本太低,參考
https://developer.aliyun.com/article/620956
**中途安裝 patchelf **
參考 https://blog.csdn.net/qq_33873431/article/details/97751520
編譯環(huán)境比當前系統(tǒng)版本要高。
/root/glibc/glibc-2.23/build/nss/getent.o: In function `rpc_keys':
/root/glibc/glibc-2.23/nss/getent.c:685: undefined reference to `setrpcent'
/root/glibc/glibc-2.23/nss/getent.c:686: undefined reference to `getrpcent'
/root/glibc/glibc-2.23/nss/getent.c:688: undefined reference to `endrpcent'
/root/glibc/glibc-2.23/nss/getent.c:697: undefined reference to `getrpcbyname'
/root/glibc/glibc-2.23/nss/getent.c:695: undefined reference to `getrpcbynumber'
collect2: error: ld returned 1 exit status
../Rules:147: recipe for target '/root/glibc/glibc-2.23/build/nss/getent' failed
make[2]: *** [/root/glibc/glibc-2.23/build/nss/getent] Error 1
make[2]: Leaving directory '/root/glibc/glibc-2.23/nss'
Makefile:214: recipe for target 'nss/others' failed
make[1]: *** [nss/others] Error 2
make[1]: Leaving directory '/root/glibc/glibc-2.23'
Makefile:9: recipe for target 'all' failed
make: *** [all] Error 2
查看版本root@VM-0-3-ubuntu:/lib64# strings ld-linux-x86-64.so.2 | grep GLIBC

可能只能下載相應的版本。。。。
下載2.4版本的應該沒錯
CFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -Wno-error=maybe-uninitialized -Wno-error" CXXFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -Wno-error=maybe-uninitialized -Wno-error" ../configure --prefix=/root/glibc/glibc-2.4/64 --disable-shared
CFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" CXXFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" ../configure --prefix=/root/glibc/glibc-2.4/64 --disable-shared

/root/glibc/glibc-2.23/64 --disable-shared
還是版本沒有,下載2.5試試
CFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" CXXFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" ../configure --prefix=/root/glibc/glibc-2.5/64 --disable-shared

下載2.3版本試試
CFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" CXXFLAGS="-g -g3 -ggdb -gdwarf-4 -Og -w" ../configure --prefix=/root/glibc/glibc-2.3/64 --disable-shared
沒了原來版本2.3的提示了,但是說gcc的版本可能太低了。
安裝最新版gcc的方法
需要gcc的版本是7.4.0,make的版本為4.1
不行不行不行
08.12
1、下載glibc源碼
官網下載較慢,github上地址 https://github.com/jorinzou/glibc
2、分析
查看ldd版本

安裝docker
3、hacknote
編譯時加入-g,就會帶上debuging symbol

chunk比malloc的內存要大一點
lay src
參考 下圖,得到堆塊的內容


top chunk


用來記錄可以使用,但是沒有分配的內存 0x20d71
再malloc一些塊

地址頭部由290變?yōu)?d0,+0x40
內存d71變?yōu)閐31 , -0x40
ltrace 會trace libc的function call
ltrace -e 'malloc+free' ./hacknote

chunk在使用的時候,fd bk沒有作用,可以存放data,free之后才有作用
p/x *(struct malloc_chunk*)0x555555756250將內存轉為chunk來查看

08.13
1、b站視頻
使用malloc需要包含 <stdlib.h>


free掉p之后,按理說size的P位應該為0,但是測試發(fā)現(xiàn)還是1
查看最開始free的chunk, fd指向main_arean部分

實驗結果:main_arean是一個環(huán)狀
-
malloc會把剛剛釋放的給重新分配回來
7114B132-6170-4B5E-915C-0F158CA2D17D.png
08.14
1、b站視頻

發(fā)現(xiàn)和視頻講解不一樣,新malloc的是靠后的0x...4e0
查看bin結構如下

這是由于新引入的tcachebins的結構,新free的被放在最開頭的位置
可能是單鏈表的結構,bk的值都是0.
- 如果一個chunk后面是top chunk,在free的時候會和后面的合并。如果它前面或者后面的沒有使用(not inuse),free的時候也會合并
2、UAF
free掉的東西有機會再被利用,因為指針沒有清空。
函數如下
#include <cstdio>
#include <cstdlib>
#include <cstring>
class A{
public:
virtual void print(){
puts("class A");
}
};
class B: public A{
public:
void print(){
puts("class B");
}
};
void sh(){
system("sh");
}
char buf[1024];
int main(){
setbuf(stdout,0,_IONBF,0);
A *p = new B(); //malloc
delete p; //free
fgets(buf,sizeof(buf),stdin);
char *q = strndup(buf); // malloc
p->print();
return 0;
}
08.17
1、b站視頻
上個程序分析
p首先地址是 0x602010
fd原本指向的是B的print
free掉之后原來指向的內容就會清零
08.18
1、b站視頻---uaf
strdup在復制的時候,64位很有可能00截斷。
call函數的時候,自己可以使用傳遞的第一個參數,是object本身。
想查看一個函數的地址,直接p 函數名即可。
如果沒有直接的后門函數,自己構造printf,p->print會把p傳進來
2、Fastbin corruption
尺寸小于0x80
單鏈表,只用fd,fd是指向下一個塊的指針。
重新malloc取塊時,也是從頭部開始拿的,因為是單鏈表
自己也有一些檢查
關于double free:只檢查第一個chunk是否和要free的相同,容易繞過。

08.19
???
08.20
???
08.21
1、fastbin
取出的chunk的size要正確
繞過:
- stack上的變數當作size
- GOT上,如果函數沒有call過,通常地址為0x40,把0x40當作size
接下來就會對任意地址進行讀寫
08.23
1、fastbin -- double free
程序如下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sh(char *cmd){
system(cmd);
}
int main(){
// setbuf(stdout, 0, _IONBF, 0);
int cmd,idx,sz;
char *ptr[10];
memset(ptr,0,sizeof(ptr));
puts("1.malloc + gets\n2.free\n3.puts");
while(1){
printf("> ");
scanf("%d %d", &cmd, &idx);
idx %= 10;
if (cmd == 1){
scanf("%d%*c", &sz);
ptr[idx] = malloc(sz);
gets(ptr[idx]);
}
else if (cmd == 2){
free(ptr[idx]);
}
else if (cmd == 3){
puts(ptr[idx]);
}
else exit(0);
}
return 0;
}
輸入的是cmd,idx
-
如果free的部分是空的,就什么都不做。
image.png
檢查不太好
objdump -d fastbin > d1
ps au 查看進程id

08.23
1、fastbin
heap起始的位置是 0x555555756000

結束的位置是

heap2- 32min
參考視頻 https://www.bilibili.com/video/BV1op4y1D7e9?from=search&seid=9463446650216238443
向后合并是 把前一個塊與此塊合并。
- 理解glibc malloc
參考 https://sploitfun.wordpress.com/2015/02/10/understanding-glibc-malloc/
1、b站視頻 heap
需要glibc,下載
wget https://ftp.gnu.org/gnu/libc/glibc-2.19.tar.gz
強網先鋒AP
步驟大概就是:覆蓋,泄漏puts地址,找到基址,
參考
https://prowes5.github.io/2019/05/27/2019強網杯部分writeup/
https://www.anquanke.com/post/id/179386#h2-10
http://blog.leanote.com/post/xp0int/[Pwn]-強網先鋒-AP-mf
1、搭建CTF-PWN環(huán)境
CTF特訓營
做題網址
http://pwnable.kr/play.php
https://www.jarvisoj.com/login
https://www.ctfhub.com/#/index


