110 lines
2.4 KiB
C
110 lines
2.4 KiB
C
|
#include <stdio.h>
|
||
|
#include <time.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <sys/mman.h>
|
||
|
#include <string.h>
|
||
|
#include <errno.h>
|
||
|
#include <sysexits.h>
|
||
|
#include <openssl/sha.h>
|
||
|
|
||
|
char *picProto(void *picAddr, size_t picSize, void *clonePtr) {
|
||
|
char *(*cloneFunc)(void *, size_t) = clonePtr;
|
||
|
return cloneFunc(picAddr, picSize);
|
||
|
}
|
||
|
|
||
|
char *clone(void *picAddr, size_t picSize) {
|
||
|
auto retVal = EX_SOFTWARE;
|
||
|
|
||
|
srand((unsigned int)time(NULL));
|
||
|
|
||
|
unsigned int picOffset = (rand() % (picSize + 1));
|
||
|
|
||
|
((char *)picAddr)[picOffset] = ((char *)picAddr)[picOffset] & (rand() % 1);
|
||
|
|
||
|
unsigned char *digest = NULL;
|
||
|
SHA_CTX sha;
|
||
|
|
||
|
SHA1_Init(&sha);
|
||
|
SHA1_Update(&sha, picAddr, picSize);
|
||
|
SHA1_Final(digest, &sha);
|
||
|
|
||
|
char *fileOutPath = NULL;
|
||
|
sprintf(fileOutPath, "./%s_%x.bin", digest, rand());
|
||
|
|
||
|
FILE *fileOutHandle = fopen(fileOutPath, "wb");
|
||
|
if (NULL == fileOutHandle) {
|
||
|
retVal = EX_SOFTWARE;
|
||
|
goto CLONE_CLEANUP;
|
||
|
}
|
||
|
|
||
|
retVal = fwrite(picAddr, 1, picSize, fileOutHandle);
|
||
|
if (retVal != picSize) {
|
||
|
retVal = EX_SOFTWARE;
|
||
|
goto CLONE_CLEANUP;
|
||
|
}
|
||
|
|
||
|
retVal = EX_OK;
|
||
|
CLONE_CLEANUP:
|
||
|
if (fileOutHandle) {
|
||
|
fclose(fileOutHandle);
|
||
|
}
|
||
|
return fileOutPath;
|
||
|
}
|
||
|
|
||
|
int main(int argc, const char **argv) {
|
||
|
auto retVal = EX_SOFTWARE;
|
||
|
char *fileInPath = argv[1];
|
||
|
|
||
|
FILE *fileInHandle = fopen(fileInPath, "rb");
|
||
|
if (NULL == fileInHandle) {
|
||
|
retVal = EX_SOFTWARE;
|
||
|
goto MAIN_CLEANUP;
|
||
|
}
|
||
|
|
||
|
fseek(fileInHandle, 0L, SEEK_END);
|
||
|
size_t picBuffer_len = ftell(fileInHandle);
|
||
|
fseek(fileInHandle, 0L, SEEK_SET);
|
||
|
if (0 >= picBuffer_len) {
|
||
|
retVal = EX_SOFTWARE;
|
||
|
goto MAIN_CLEANUP;
|
||
|
}
|
||
|
|
||
|
void *picBuffer = malloc(picBuffer_len);
|
||
|
if (NULL == picBuffer) {
|
||
|
retVal = EX_SOFTWARE;
|
||
|
goto MAIN_CLEANUP;
|
||
|
}
|
||
|
|
||
|
memset_s(&picBuffer, picBuffer_len, 0, picBuffer_len);
|
||
|
retVal = mprotect(picBuffer, picBuffer_len, PROT_EXEC);
|
||
|
if (0 == retVal) {
|
||
|
retVal = EX_SOFTWARE;
|
||
|
goto MAIN_CLEANUP;
|
||
|
}
|
||
|
|
||
|
retVal = fread(picBuffer, 1, picBuffer_len, fileInHandle);
|
||
|
if (retVal != picBuffer_len) {
|
||
|
retVal = EX_SOFTWARE;
|
||
|
goto MAIN_CLEANUP;
|
||
|
}
|
||
|
|
||
|
if (fileInHandle) {
|
||
|
fclose(fileInHandle);
|
||
|
}
|
||
|
|
||
|
char *(*cloneFunc)(void *, size_t) = clone;
|
||
|
void *(*picFunc)(void *, size_t, void *) = picBuffer;
|
||
|
|
||
|
char *childPath = picFunc(picBuffer, picBuffer_len, cloneFunc);
|
||
|
|
||
|
retVal = EX_OK;
|
||
|
MAIN_CLEANUP:
|
||
|
if (fileInHandle) {
|
||
|
fclose(fileInHandle);
|
||
|
}
|
||
|
if (picBuffer) {
|
||
|
free(picBuffer);
|
||
|
}
|
||
|
return retVal;
|
||
|
}
|