Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ The below 3rd party libraries are used in this project.
* `Install-Package TaskScheduler -Version 2.8.11`
* You can now build the project yourself!

* Merge The Dependencies into a Standalone binary using [ILMerge](https://github.com/dotnet/ILMerge)

* `ILMerge.exe" "C:\SharPersist\SharPersist\bin\Release\SharPersist.exe" /out:"C:\SharPersist_Standalone.exe" "C:\SharPersist\SharPersist\bin\Release\Microsoft.Win32.TaskScheduler.dll"`

# Arguments/Options

* <b>-t </b> - persistence technique
Expand Down Expand Up @@ -70,8 +74,9 @@ The below 3rd party libraries are used in this project.

# Optional Add-Ons (-o)
* `env` - optional add-on for env variable obfuscation for registry
* `hourly` - optional add-on for schtask frequency
* `daily` - optional add-on for schtask frequency
* `minute <number> ` - optional add-on for schtask frequency
* `hourly <number> ` - optional add-on for schtask frequency
* `daily <number>` - optional add-on for schtask frequency
* `logon` - optional add-on for schtask frequency


Expand Down Expand Up @@ -128,7 +133,12 @@ The below 3rd party libraries are used in this project.

`SharPersist -t schtask -c "C:\Windows\System32\cmd.exe" -a "/c echo 123 >> c:\123.txt" -n "Some Task" -m add`

`SharPersist -t schtask -c "C:\Windows\System32\cmd.exe" -a "/c echo 123 >> c:\123.txt" -n "Some Task" -m add -o hourly`
##### (Runs Scheduled Task every 2 hours)
`SharPersist -t schtask -c "C:\Windows\System32\cmd.exe" -a "/c echo 123 >> c:\123.txt" -n "Some Task" -m add -o "hourly 2"`
##### (Runs Scheduled Task every 5 minutes)
`SharPersist -t schtask -c "C:\Windows\System32\cmd.exe" -a "/c echo 123 >> c:\123.txt" -n "Some Task" -m add -o "minute 5"`
##### (Runs Scheduled Task every 2 days)
`SharPersist -t schtask -c "C:\Windows\System32\cmd.exe" -a "/c echo 123 >> c:\123.txt" -n "Some Task" -m add -o "daily 2"`


## Removing Persistence Triggers (Remove)
Expand Down Expand Up @@ -215,6 +225,8 @@ The below 3rd party libraries are used in this project.

`SharPersist -t schtask -c "C:\Windows\System32\cmd.exe" -a "/c echo 123 >> c:\123.txt" -n "Some Task" -m check`

##### (List Scheduled Task that runs hourly)

`SharPersist -t schtask -c "C:\Windows\System32\cmd.exe" -a "/c echo 123 >> c:\123.txt" -n "Some Task" -m check -o hourly`


Expand Down
4 changes: 2 additions & 2 deletions SharPersist/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1")]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: AssemblyVersion("1.1")]
[assembly: AssemblyFileVersion("1.1")]
125 changes: 118 additions & 7 deletions SharPersist/SchTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Win32.TaskScheduler;
using Microsoft.Win32.TaskScheduler;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -92,7 +92,8 @@ public void addPersistence(string command, string commandArg, string theName, st
td.RegistrationInfo.Description = theName;

// set trigger time appropriately based on option provided
string triggerTime = option.ToLower();
string[] optional_addon = option.Split(' ');
string triggerTime = optional_addon[0].ToLower();

// daily schtask
if (triggerTime.Equals("daily"))
Expand All @@ -101,7 +102,9 @@ public void addPersistence(string command, string commandArg, string theName, st
// Create a trigger that runs every day and will start randomly between 10 a.m. and 12 p.m.
DailyTrigger dt = new DailyTrigger();
dt.StartBoundary = DateTime.Today + TimeSpan.FromHours(10);
dt.DaysInterval = 1;

int interval = Int32.Parse(optional_addon[1]);
dt.DaysInterval = (short)interval;
dt.RandomDelay = TimeSpan.FromHours(2);

td.Triggers.Add(dt);
Expand All @@ -112,10 +115,26 @@ public void addPersistence(string command, string commandArg, string theName, st
else if (triggerTime.Equals("hourly"))
{
TimeTrigger tt = new TimeTrigger();
tt.Repetition.Interval = TimeSpan.FromMinutes(60);
int interval = Int32.Parse(optional_addon[1]);
tt.Repetition.Interval = TimeSpan.FromMinutes(interval*60);
td.Triggers.Add(tt);

}



// schtask every n minutes

else if (triggerTime.Equals("minute"))
{
TimeTrigger tt = new TimeTrigger();
int interval = Int32.Parse(optional_addon[1]);
tt.Repetition.Interval = TimeSpan.FromSeconds(interval * 60);
td.Triggers.Add(tt);

}


// schtask at logon. this will run as system
else if (triggerTime.Equals("logon"))
{
Expand Down Expand Up @@ -299,6 +318,8 @@ public void listPersistence(string persistMethod, string command, string command
string schtaskName = task.Name;
DateTime runTime = task.NextRunTime;
string theRunTime = runTime.ToString("G", CultureInfo.CurrentCulture);
DateTime lastrunTime = task.LastRunTime;
string theLastRunTime = lastrunTime.ToString("G", CultureInfo.CurrentCulture);

// once we find the schtask, display its details
if (schtaskName.ToLower().Equals(theName.ToLower()))
Expand Down Expand Up @@ -333,6 +354,9 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");
Expand Down Expand Up @@ -398,6 +422,8 @@ public void listPersistence(string persistMethod, string command, string command
string schtaskName = task.Name;
DateTime runTime = task.NextRunTime;
string theRunTime = runTime.ToString("G", CultureInfo.CurrentCulture);
DateTime lastrunTime = task.LastRunTime;
string theLastRunTime = lastrunTime.ToString("G", CultureInfo.CurrentCulture);
bool taskActive = task.IsActive;

// only proceed to list schtask info if it is active
Expand Down Expand Up @@ -427,8 +453,80 @@ public void listPersistence(string persistMethod, string command, string command
if (optionSpecified)
{

if (option.ToLower().Equals("hourly") && triggerType.ToLower().Equals("time"))

int lastrunday = lastrunTime.Day;
int nextrunday = runTime.Day;
int lastrunhour = lastrunTime.Hour;
int nextrunhour = runTime.Hour;
int lastrunminute = lastrunTime.Minute;
int nextrunminute = runTime.Minute;


int timediffday = nextrunday - lastrunday;
int timediffhour = nextrunhour - lastrunhour;
int timediffminute = nextrunminute - lastrunminute;


string[] optional_addon = option.Split(' ');
string triggerTime = optional_addon[0].ToLower();



if (triggerTime.ToLower().Equals("minute") && triggerType.ToLower().Equals("time") && timediffminute > 0 && timediffhour <= 0 && timediffday <= 0)
{


Console.WriteLine("[*] INFO: TASK NAME:");
Console.WriteLine(schtaskName);
Console.WriteLine("");
Console.WriteLine("[*] INFO: TASK PATH:");
Console.WriteLine(schtaskFolder);
Console.WriteLine("");
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");

// get the frequency in which the schtask executes
TriggerCollection theTriggers = task.Definition.Triggers;
string theTriggerType = "";
foreach (Trigger trigger in theTriggers)
{
RepetitionPattern pattern = trigger.Repetition;

theTriggerType = trigger.TriggerType.ToString();
Console.WriteLine("[*] INFO: TASK TRIGGER:");
Console.WriteLine(theTriggerType);
Console.WriteLine("");
}



// get all actions and print
foreach (Microsoft.Win32.TaskScheduler.Action action in allActions)
{
Console.WriteLine("[*] INFO: TASK ACTION:");
Console.WriteLine(action.ToString());
Console.WriteLine("");

}

Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");

}


if (triggerTime.ToLower().Equals("hourly") && triggerType.ToLower().Equals("time") && timediffhour > 0 && timediffday <= 0)
{


Console.WriteLine("[*] INFO: TASK NAME:");
Console.WriteLine(schtaskName);
Console.WriteLine("");
Expand All @@ -438,6 +536,9 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");
Expand Down Expand Up @@ -469,10 +570,11 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");

}


else if (option.ToLower().Equals("daily") && triggerType.ToLower().Equals("daily"))
else if (triggerTime.ToLower().Equals("daily") && triggerType.ToLower().Equals("daily") && timediffday > 0)
{
Console.WriteLine("[*] INFO: TASK NAME:");
Console.WriteLine(schtaskName);
Expand All @@ -483,6 +585,9 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");
Expand Down Expand Up @@ -517,7 +622,7 @@ public void listPersistence(string persistMethod, string command, string command
}


else if ((option.ToLower().Equals("logon") && triggerType.ToLower().Equals("logon")) || (option.ToLower().Equals("boot") && triggerType.ToLower().Equals("boot")))
else if ((triggerTime.ToLower().Equals("logon") && triggerType.ToLower().Equals("logon")) || (option.ToLower().Equals("boot") && triggerType.ToLower().Equals("boot")))
{
Console.WriteLine("[*] INFO: TASK NAME:");
Console.WriteLine(schtaskName);
Expand All @@ -528,6 +633,9 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");
Expand Down Expand Up @@ -576,6 +684,9 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");
Expand Down
4 changes: 2 additions & 2 deletions SharPersist/SharPersist.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
<Reference Include="Microsoft.CSharp">
<HintPath>..\..\..\..\..\..\..\..\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.CSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Win32.TaskScheduler, Version=2.8.11.0, Culture=neutral, PublicKeyToken=c416bc1b32d97233, processorArchitecture=MSIL">
<HintPath>..\packages\TaskScheduler.2.8.11\lib\net40\Microsoft.Win32.TaskScheduler.dll</HintPath>
<Reference Include="Microsoft.Win32.TaskScheduler, Version=2.10.1.0, Culture=neutral, PublicKeyToken=e25603a88b3aa7da, processorArchitecture=MSIL">
<HintPath>..\packages\TaskScheduler.2.10.1\lib\net40\Microsoft.Win32.TaskScheduler.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
Expand Down
7 changes: 4 additions & 3 deletions SharPersist/lib/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public static void PrintHelp()

Console.Write("OPTIONAL ADD-ONS:\n\n");
Console.Write("\tenv: optional add-on for env variable obfuscation for registry\n\n");
Console.Write("\thourly: optional add-on for schtask frequency\n\n");
Console.Write("\tdaily: optional add-on for schtask frequency\n\n");
Console.Write("\tminute <number> : optional add-on for schtask frequency\n\n");
Console.Write("\thourly <number> : optional add-on for schtask frequency\n\n");
Console.Write("\tdaily <number> : optional add-on for schtask frequency\n\n");
Console.Write("\tlogon: optional add-on for schtask frequency\n\n");

Console.Write("REGISTRY KEYS:\n\n");
Expand All @@ -103,7 +104,7 @@ public static void PrintHelp()
Console.Write("\t-t reg -c \"<command>\" -a \"<arg>\" -k \"<pre-determined reg key>\" -m add\n\n");
Console.Write("\t-t schtaskbackdoor -c \"<command>\" -a \"<arg>\" -n \"<schtask name>\" -m add\n\n");
Console.Write("\t-t schtask -c \"<command>\" -a \"<arg>\" -n \"<schtask name>\" -m add\n\n");
Console.Write("\t-t schtask -c \"<command>\" -a \"<arg>\" -n \"<schtask name>\" -m add -o <frequency>\n\n");
Console.Write("\t-t schtask -c \"<command>\" -a \"<arg>\" -n \"<schtask name>\" -m add -o \"<frequency> <number>\"\n\n");
Console.Write("\t-t startupfolder -c \"<command>\" -a \"<arg>\" -f \"<file name>\" -m add\n\n");
Console.Write("\t-t tortoisesvn -c \"<command>\" -a \"<arg>\" -m add\n\n");
Console.Write("\t-t service -c \"<command>\" -a \"<arg>\" -n \"<service name>\" -m add\n\n");
Expand Down
4 changes: 4 additions & 0 deletions SharPersist/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="TaskScheduler" version="2.10.1" targetFramework="net40" />
</packages>