[파일 자동 삭제]

2017. 9. 19. 03:190x02 Reverse Engineer/0x03. Etc

728x90

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <Windows.h>

void Kill(char *thisFile)

{

FILE *fp = NULL;

// batch파일명

char* killer = "killfile.bat";

// 실행파일명

char* thisFileReal = thisFile;

char szBatFile[256];


// batch파일을 생성합니다.

fp = fopen(killer, "wt");

if (fp == NULL)

{

puts("파일생성 실패");

return;

}


// batch파일에 실행할 내용 작성

wsprintfA(szBatFile,

":Repeat      \r\n"

"del /f /s /q %s    \r\n"

"if exist \"%s\" goto Repeat \r\n"

"del /s /q %s     \r\n",

thisFile, thisFile, killer);


fwrite(szBatFile, strlen(szBatFile), 1, fp);

fclose(fp);


// batch파일을 실행합니다.

ShellExecuteA(NULL, "open", killer, NULL, NULL, 0);

}




int main(int argc, char*argv[])

{

// F:\OPENSECURELAB_바이너리작업실\asm_version Let's puzzle game\Debug\asm_version Let's puzzle game.exe

char *tmp=0;

char *confirm = "Fn";

char *fileext=0;

int slash_cnt = 0;

static int j = 0;

char temp = 0;

static int index = 0;

char *newPath = 0;

char *endPath = 0;

printf("%s\n", argv[0]);

int pathLength = strlen(argv[0]);

tmp = (char *)malloc(pathLength);

memset(tmp, 0, pathLength-4);

memcpy(tmp, argv[0], strlen(argv[0])+1);

printf("경로 길이 : %d\n", strlen(confirm));

printf("rename return value = %d\n", rename(argv[0], "H4C_CTF.exe"));

/*if (rename(tmp, "H4C_CTF.exe") == 0)

{

'

tmp = (char *)realloc(tmp, sizeof(tmp));

printf("argv = %s\n", tmp);

}*/

printf("복사 %s\n", tmp);

newPath = (char*)malloc((strlen(tmp)));

memset(newPath, 0, strlen(tmp) + 1);

for (int i = pathLength-1; i >= 0; i--)

{

if (tmp[i] == '\\')

{

slash_cnt++;

}

else if(tmp[i] != '\\')

{

if (newPath[j] == '\\') {

i--;

break;

}

else

{

newPath[j] = tmp[i];

index++;

j++;

}

}

if (slash_cnt == 1)

{

break;

}

}

printf("문자열 : %s\n", newPath);

printf("길이 : %d\n", strlen(newPath));


for (int i=0; i< (strlen(newPath))/2 ; i++)

{

temp = newPath[i];

newPath[i] = newPath[strlen(newPath) - i - 1];

newPath[strlen(newPath) - i - 1] = temp;

}

printf("변경 된 문자열 : %s\n", newPath);

Kill(newPath);

}