Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Self extracting archives */
- /* Proof of concept, didn't try compiling or testing */
- /* Author: Krzysztof Szewczyk (a.k.a. Palaiologos) */
- /* Creation date: 08-17-2018*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/stat.h>
- static int parseoct(const char *p, size_t n) {
- int i = 0;
- while ((*p < '0' || *p > '7') && n > 0) {
- ++p;
- --n;
- }
- while (*p >= '0' && *p <= '7' && n > 0) {
- i *= 8;
- i += *p - '0';
- ++p;
- --n;
- }
- return i;
- }
- static int is_end_of_archive(const char *p) {
- int n;
- for (n = 511; n >= 0; --n)
- if (p[n] != '\0')
- return 0;
- return (1);
- }
- static void create_dir(char *pathname, int mode) {
- char *p;
- int r;
- if (pathname[strlen(pathname) - 1] == '/')
- pathname[strlen(pathname) - 1] = '\0';
- r = mkdir(pathname, mode);
- if (r != 0) {
- p = strrchr(pathname, '/');
- if (p != NULL) {
- *p = '\0';
- create_dir(pathname, 0755);
- *p = '/';
- r = mkdir(pathname, mode);
- }
- }
- if (r != 0)
- fprintf(stderr, "Could not create directory %s\n", pathname);
- else printf("Created: %s", pathname);
- }
- static FILE * create_file(char *pathname, int mode) {
- FILE *f;
- f = fopen(pathname, "wb+");
- if (f == NULL) {
- char *p = strrchr(pathname, '/');
- if (p != NULL) {
- *p = '\0';
- create_dir(pathname, 0755);
- *p = '/';
- f = fopen(pathname, "wb+");
- }
- }
- return f;
- }
- static int verify_checksum(const char *p) {
- int n, u = 0;
- for (n = 0; n < 512; ++n) {
- if (n < 148 || n > 155)
- u += ((unsigned char *)p)[n];
- else
- u += 0x20;
- }
- return (u == parseoct(p + 148, 8));
- }
- static void untar(FILE *a, const char *path) {
- char buff[512];
- FILE *f = NULL;
- size_t bytes_read;
- int filesize;
- printf("Extracting from %s\n", path);
- for (;;) {
- bytes_read = fread(buff, 1, 512, a);
- if (bytes_read < 512) {
- fprintf(stderr, "Short read on %s: expected 512, got %d\n", path, (int)bytes_read);
- return;
- }
- if (is_end_of_archive(buff)) {
- printf("End of %s\n", path);
- return;
- }
- if (!verify_checksum(buff)) {
- fprintf(stderr, "Checksum failure\n");
- return;
- }
- filesize = parseoct(buff + 124, 12);
- switch (buff[156]) {
- case '1':
- printf(" Ignoring hardlink %s\n", buff);
- break;
- case '2':
- printf(" Ignoring symlink %s\n", buff);
- break;
- case '3':
- printf(" Ignoring character device %s\n", buff);
- break;
- case '4':
- printf(" Ignoring block device %s\n", buff);
- break;
- case '5':
- printf(" Extracting subdirectory %s\n", buff);
- create_dir(buff, parseoct(buff + 100, 8));
- filesize = 0;
- break;
- case '6':
- printf(" Ignoring FIFO %s\n", buff);
- break;
- default:
- printf(" Extracting file %s\n", buff);
- f = create_file(buff, parseoct(buff + 100, 8));
- break;
- }
- while (filesize > 0) {
- bytes_read = fread(buff, 1, 512, a);
- if (bytes_read < 512) {
- fprintf(stderr, "Short read on %s: Expected 512, got %d\n", path, (int)bytes_read);
- return;
- }
- if (filesize < 512)
- bytes_read = filesize;
- if (f != NULL) {
- if (fwrite(buff, 1, bytes_read, f) != bytes_read) {
- fprintf(stderr, "Failed write\n");
- fclose(f);
- f = NULL;
- }
- }
- filesize -= bytes_read;
- }
- if (f != NULL) {
- fclose(f);
- f = NULL;
- }
- }
- }
- int main(int argc, char **argv) {
- char template[] = "/tmp/palapack.XXXXXX";
- char dest[256];
- char path[256];
- char comment[512];
- struct stat info;
- FILE * self;
- char *dir_name = mkdtemp(template);
- pid_t pid = getpid();
- if(dir_name == NULL) {
- perror("mkdtemp");
- return 0;
- }
- memset(dest,0,sizeof(dest));
- sprintf(path, "/proc/%d/exe", pid);
- if (readlink(path, dest, 256) == -1)
- perror("readlink");
- else {
- printf("Exe path: %s\n", dest);
- printf("Temp dir name: %s", dir_name);
- self = fopen(dest, "rb");
- if(self) {
- fseek(self, 512, SEEK_SET); //TODO: SET TO EXECUTABLE SIZE!
- untar(self, dir_name);
- fclose(self);
- sprintf(comment, "%sstart.sh", dir_name);
- if(access(comment, F_OK) != -1) {
- execl(comment,"start.sh",NULL);
- } else {
- printf("No comment found, exiting.");
- }
- } else {
- perror("fopen");
- }
- }
- if(rmdir(dir_name) == -1) {
- perror("rmdir");
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement