Created
October 23, 2018 16:39
-
-
Save sgqy/244287803079f3c2341d397f613e94fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 64-bit only | |
// sizeof(long): 8 byte | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
#include <sys/mman.h> | |
#include <fcntl.h> | |
typedef struct | |
{ | |
char name[100]; /* 0 */ | |
char mode[8]; /* 100 */ | |
char uid[8]; /* 108 */ | |
char gid[8]; /* 116 */ | |
char size[12]; /* 124 */ | |
char mtime[12]; /* 136 */ | |
char chksum[8]; /* 148 */ | |
char typeflag; /* 156 */ | |
char linkname[100]; /* 157 */ | |
char magic[6]; /* 257 */ | |
char version[2]; /* 263 */ | |
char uname[32]; /* 265 */ | |
char gname[32]; /* 297 */ | |
char devmajor[8]; /* 329 */ | |
char devminor[8]; /* 337 */ | |
char prefix[155]; /* 345 */ | |
char preserve[12]; /* 500 */ | |
/* 512 */ | |
} tar_hdr_t; | |
// convert oct string to long int | |
long parse_tar_oct(const char *oct) | |
{ | |
long ret = 0; | |
for(int i = 0; i < 11; ++i) | |
{ | |
ret += (oct[i] - '0') * (1 << (3 * (11 - i - 1))); | |
} | |
return ret; | |
} | |
int list_entry(const char *buf, const long sz) | |
{ | |
if(sz < 512) | |
{ | |
printf("[-] not a tar (size)\n"); | |
exit(1); | |
} | |
for(const char *p = buf; p - buf < sz; /* */) | |
{ | |
tar_hdr_t *info = (tar_hdr_t *)p; | |
if(memcmp("ustar", info->magic, 5) != 0) // end of file will pad blocks filled with zero | |
{ | |
printf("[+] end of file\n"); | |
break; | |
} | |
long len = parse_tar_oct(info->size); | |
printf("[+] [%p] %s\n", p, info->name); | |
p += sizeof(tar_hdr_t) + len; | |
if(len % 512 != 0) // padding zeros at the end of content file | |
{ | |
p += 512 - len % 512; | |
} | |
} | |
} | |
int main(int argc, char **argv) | |
{ | |
if(argc != 2) | |
{ | |
printf("%s <file>\n", argv[0]); | |
return 1; | |
} | |
int fd = open(argv[1], O_RDONLY); | |
off_t size = lseek(fd, 0, SEEK_END); | |
char *p = (char *)mmap(0, size, PROT_READ, MAP_SHARED, fd, 0); | |
printf("[+] [%s] is on [%p]-[%p]\n", argv[1], p, p+size); | |
list_entry(p, size); | |
munmap(p, size); | |
close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment