FlyFar

DoubleAgent/main.c

Jan 2nd, 2024
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.04 KB | Cybersecurity | 0 0
  1. /* Includes ******************************************************************/
  2. #include <Windows.h>
  3. #include <stdio.h>
  4. #include <crtdbg.h>
  5. #include "Path.h"
  6. #include "Verifier.h"
  7.  
  8. /* Macros ********************************************************************/
  9. #define DOUBLEAGENT_ACTION_INSTALL (L"install")
  10. #define DOUBLEAGENT_ACTION_REPAIR (L"repair")
  11. #define DOUBLEAGENT_ACTION_UNINSTALL (L"uninstall")
  12. #define DOUBLEAGENT_VERIFIER_DLL_NAME L"DoubleAgentDll.dll"
  13. #define DOUBLEAGENT_VERIFIER_DLL_RELATIVE_PATH_X86 (L".\\x86\\" ## DOUBLEAGENT_VERIFIER_DLL_NAME)
  14. #define DOUBLEAGENT_VERIFIER_DLL_RELATIVE_PATH_X64 (L".\\x64\\" ## DOUBLEAGENT_VERIFIER_DLL_NAME)
  15.  
  16. /* Types *********************************************************************/
  17. typedef enum _DOUBLEAGENT_ARGUMENTS
  18. {
  19.     DOUBLEAGENT_ARGUMENTS_INVALID_VALUE = -1,
  20.     DOUBLEAGENT_ARGUMENTS_SELF_PATH,
  21.     DOUBLEAGENT_ARGUMENTS_ACTION_TYPE,
  22.     DOUBLEAGENT_ARGUMENTS_PROCESS_NAME,
  23.  
  24.     /* Must be last */
  25.     DOUBLEAGENT_ARGUMENTS_COUNT
  26. } DOUBLEAGENT_ARGUMENTS, *PDOUBLEAGENT_ARGUMENTS;
  27.  
  28. /* Function Declarations *****************************************************/
  29. /*
  30.  * Handles the install action
  31.  */
  32. static DOUBLEAGENT_STATUS main_Install(IN PCWSTR *ppcwszArgv);
  33. /*
  34.  * Handles the repair action
  35.  */
  36. static DOUBLEAGENT_STATUS main_Repair(IN PCWSTR *ppcwszArgv);
  37. /*
  38.  * Handles the uninstall action
  39.  */
  40. static DOUBLEAGENT_STATUS main_Uninstall(IN PCWSTR *ppcwszArgv);
  41.  
  42. /* Function Definitions ******************************************************/
  43. INT wmain(IN SIZE_T nArgc, IN PCWSTR *ppcwszArgv)
  44. {
  45.     DOUBLEAGENT_STATUS eStatus = DOUBLEAGENT_STATUS_INVALID_VALUE;
  46.  
  47.     /* Prints usage */
  48.     if (1 == nArgc)
  49.     {
  50.         (VOID)wprintf(L"Usage:\tDoubleAgent.exe install\\uninstall\\repair process_name\n");
  51.         (VOID)wprintf(L"e.g.\tDoubleAgent.exe install cmd.exe\n");
  52.         return 0;
  53.     }
  54.  
  55.     /* Validates the arguments */
  56.     if (DOUBLEAGENT_ARGUMENTS_COUNT != nArgc)
  57.     {
  58.         DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_DOUBLEAGENT_WMAIN_INVALID_ARGS_COUNT);
  59.         goto lbl_cleanup;
  60.     }
  61.  
  62.     /* Install action */
  63.     if (0 == _wcsicmp(ppcwszArgv[DOUBLEAGENT_ARGUMENTS_ACTION_TYPE], DOUBLEAGENT_ACTION_INSTALL))
  64.     {
  65.         eStatus = main_Install(ppcwszArgv);
  66.         if (FALSE == DOUBLEAGENT_SUCCESS(eStatus))
  67.         {
  68.             goto lbl_cleanup;
  69.         }
  70.     }
  71.     /* Repair action */
  72.     else if (0 == _wcsicmp(ppcwszArgv[DOUBLEAGENT_ARGUMENTS_ACTION_TYPE], DOUBLEAGENT_ACTION_REPAIR))
  73.     {
  74.         eStatus = main_Repair(ppcwszArgv);
  75.         if (FALSE == DOUBLEAGENT_SUCCESS(eStatus))
  76.         {
  77.             goto lbl_cleanup;
  78.         }
  79.     }
  80.     /* Uninstall action */
  81.     else if (0 == _wcsicmp(ppcwszArgv[DOUBLEAGENT_ARGUMENTS_ACTION_TYPE], DOUBLEAGENT_ACTION_UNINSTALL))
  82.     {
  83.         eStatus = main_Uninstall(ppcwszArgv);
  84.         if (FALSE == DOUBLEAGENT_SUCCESS(eStatus))
  85.         {
  86.             goto lbl_cleanup;
  87.         }
  88.     }
  89.     /* Unsupported action */
  90.     else
  91.     {
  92.         DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_DOUBLEAGENT_WMAIN_UNSUPPORTED_ACTION);
  93.         goto lbl_cleanup;
  94.     }
  95.  
  96.     /* Succeeded */
  97.     DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_SUCCESS);
  98.  
  99. lbl_cleanup:
  100.     if (FALSE != DOUBLEAGENT_SUCCESS(eStatus))
  101.     {
  102.         (VOID)wprintf(L"Succeeded");
  103.     }
  104.     else
  105.     {
  106.         (VOID)wprintf(L"Failed (error code %lu)", eStatus);
  107.     }
  108.     /* Returns status */
  109.     return eStatus;
  110. }
  111.  
  112. static DOUBLEAGENT_STATUS main_Install(IN PCWSTR *ppcwszArgv)
  113. {
  114.     DOUBLEAGENT_STATUS eStatus = DOUBLEAGENT_STATUS_INVALID_VALUE;
  115.     PWSTR pwszExeDirPath = NULL;
  116.     PWSTR pwszVerifierDllPathX86 = NULL;
  117.     PWSTR pwszVerifierDllPathX64 = NULL;
  118.  
  119.     /* Validates the parameters */
  120.     _ASSERT(NULL != ppcwszArgv);
  121.  
  122.     /* Gets the executable directory */
  123.     eStatus = PATH_GetDirectory(ppcwszArgv[DOUBLEAGENT_ARGUMENTS_SELF_PATH], &pwszExeDirPath);
  124.     if (FALSE == DOUBLEAGENT_SUCCESS(eStatus))
  125.     {
  126.         goto lbl_cleanup;
  127.     }
  128.  
  129.     /* Gets the x86 verifier dll path */
  130.     eStatus = PATH_Combine(pwszExeDirPath, DOUBLEAGENT_VERIFIER_DLL_RELATIVE_PATH_X86, &pwszVerifierDllPathX86);
  131.     if (FALSE == DOUBLEAGENT_SUCCESS(eStatus))
  132.     {
  133.         goto lbl_cleanup;
  134.     }
  135.  
  136.     /* Gets the x64 verifier dll path */
  137.     eStatus = PATH_Combine(pwszExeDirPath, DOUBLEAGENT_VERIFIER_DLL_RELATIVE_PATH_X64, &pwszVerifierDllPathX64);
  138.     if (FALSE == DOUBLEAGENT_SUCCESS(eStatus))
  139.     {
  140.         goto lbl_cleanup;
  141.     }
  142.  
  143.     /* Installs the application verifier for the process */
  144.     eStatus = VERIFIER_Install(ppcwszArgv[DOUBLEAGENT_ARGUMENTS_PROCESS_NAME], DOUBLEAGENT_VERIFIER_DLL_NAME, pwszVerifierDllPathX86, pwszVerifierDllPathX64);
  145.     if (FALSE == DOUBLEAGENT_SUCCESS(eStatus))
  146.     {
  147.         goto lbl_cleanup;
  148.     }
  149.  
  150.     /* Succeeded */
  151.     DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_SUCCESS);
  152.  
  153. lbl_cleanup:
  154.     /* Frees the x64 verifier dll path */
  155.     if (NULL != pwszVerifierDllPathX64)
  156.     {
  157.         (VOID)HeapFree(GetProcessHeap(), 0, pwszVerifierDllPathX64);
  158.         pwszVerifierDllPathX64 = NULL;
  159.     }
  160.  
  161.     /* Frees the x86 verifier dll path */
  162.     if (NULL != pwszVerifierDllPathX86)
  163.     {
  164.         (VOID)HeapFree(GetProcessHeap(), 0, pwszVerifierDllPathX86);
  165.         pwszVerifierDllPathX86 = NULL;
  166.     }
  167.  
  168.     /* Frees the executable directory */
  169.     if (NULL != pwszExeDirPath)
  170.     {
  171.         (VOID)HeapFree(GetProcessHeap(), 0, pwszExeDirPath);
  172.         pwszExeDirPath = NULL;
  173.     }
  174.  
  175.     /* Returns status */
  176.     return eStatus;
  177. }
  178.  
  179. static DOUBLEAGENT_STATUS main_Repair(IN PCWSTR *ppcwszArgv)
  180. {
  181.     UNREFERENCED_PARAMETER(ppcwszArgv);
  182.     DOUBLEAGENT_STATUS eStatus = DOUBLEAGENT_STATUS_INVALID_VALUE;
  183.  
  184.     /* Validates the parameters */
  185.     _ASSERT(NULL != ppcwszArgv);
  186.  
  187.     /* Repairs the machine to its original state */
  188.     eStatus = VERIFIER_Repair();
  189.     if (FALSE == DOUBLEAGENT_SUCCESS(eStatus))
  190.     {
  191.         goto lbl_cleanup;
  192.     }
  193.  
  194.     /* Succeeded */
  195.     DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_SUCCESS);
  196.  
  197. lbl_cleanup:
  198.     /* Returns status */
  199.     return eStatus;
  200. }
  201.  
  202. static DOUBLEAGENT_STATUS main_Uninstall(IN PCWSTR *ppcwszArgv)
  203. {
  204.     DOUBLEAGENT_STATUS eStatus = DOUBLEAGENT_STATUS_INVALID_VALUE;
  205.  
  206.     /* Validates the parameters */
  207.     _ASSERT(NULL != ppcwszArgv);
  208.  
  209.     /* Uninstalls the application verifier from the process */
  210.     VERIFIER_Uninstall(ppcwszArgv[DOUBLEAGENT_ARGUMENTS_PROCESS_NAME], DOUBLEAGENT_VERIFIER_DLL_NAME);
  211.  
  212.     /* Succeeded */
  213.     DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_SUCCESS);
  214.  
  215.     /* Returns status */
  216.     return eStatus;
  217. }
Add Comment
Please, Sign In to add comment