Created
January 7, 2019 04:32
-
-
Save aaronyoo/63f6ee2fe453c182fa16d23499f65587 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/stat.h> | |
#define PARASITE_SIZE 8824 | |
int main(int argc, char **argv) { | |
/* Execute parasite code */ | |
printf("Parasite code is running!\n"); | |
/* Move to the beginning of the host executable */ | |
FILE *fp = fopen(argv[0], "r"); | |
fseek(fp, 0L, SEEK_END); | |
int file_size = ftell(fp); | |
fseek(fp, PARASITE_SIZE, SEEK_SET); | |
/* Read host executable into buffer */ | |
int host_size = file_size - PARASITE_SIZE; | |
char *host_buffer = calloc(host_size, sizeof(char)); | |
fread(host_buffer, sizeof(char), host_size, fp); | |
/* Write host executable into a temp file */ | |
FILE *temp_fp = fopen("temp.out", "w"); | |
fwrite(host_buffer, sizeof(char), host_size, temp_fp); | |
/* Clean up */ | |
fclose(temp_fp); | |
fclose(fp); | |
free(host_buffer); | |
chmod("temp.out", S_IXOTH | S_IXGRP | S_IXUSR); | |
/* Execute the host executable */ | |
int ret = execl("temp.out", "temp.out", (char *) 0); | |
if (ret == -1) { | |
perror("execl"); | |
exit(1); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment