/PurpleSharp/Simulations/Execution.cs

https://github.com/mvelazc0/PurpleSharp · C# · 146 lines · 127 code · 19 blank · 0 comment · 0 complexity · 9f13c01bd08577bed6ddaaeec8574977 MD5 · raw file

  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Management.Automation;
  4. namespace PurpleSharp.Simulations
  5. {
  6. class Execution
  7. {
  8. static public void ExecuteWmiCmd(string log)
  9. {
  10. string currentPath = AppDomain.CurrentDomain.BaseDirectory;
  11. Lib.Logger logger = new Lib.Logger(currentPath + log);
  12. logger.SimulationHeader("T1047");
  13. logger.TimestampInfo("Using the command line to execute the technique");
  14. try
  15. {
  16. ExecutionHelper.StartProcessNET("wmic.exe", String.Format(@"process call create ""powershell.exe"""), logger);
  17. logger.SimulationFinished();
  18. }
  19. catch (Exception ex)
  20. {
  21. logger.SimulationFailed(ex);
  22. }
  23. }
  24. static public void ExecutePowershellCmd(string log)
  25. {
  26. string currentPath = AppDomain.CurrentDomain.BaseDirectory;
  27. Lib.Logger logger = new Lib.Logger(currentPath + log);
  28. logger.SimulationHeader("T1059.001");
  29. logger.TimestampInfo("Using the command line to execute the technique");
  30. try
  31. {
  32. string encodedPwd = "UwB0AGEAcgB0AC0AUwBsAGUAZQBwACAALQBzACAAMgAwAA==";
  33. ExecutionHelper.StartProcessApi("", String.Format("powershell.exe -enc {0}", encodedPwd), logger);
  34. logger.SimulationFinished();
  35. }
  36. catch(Exception ex)
  37. {
  38. logger.SimulationFailed(ex);
  39. }
  40. }
  41. static public void ExecutePowershellNET(string log)
  42. {
  43. string currentPath = AppDomain.CurrentDomain.BaseDirectory;
  44. Lib.Logger logger = new Lib.Logger(currentPath + log);
  45. logger.SimulationHeader("T1059.001");
  46. logger.TimestampInfo("Using the System.Management.Automation .NET namespace to execute the technique");
  47. try
  48. {
  49. PowerShell pstest = PowerShell.Create();
  50. String script = "UwB0AGEAcgB0AC0AUwBsAGUAZQBwACAALQBzACAAMgAwAA==";
  51. script = System.Text.Encoding.Unicode.GetString(System.Convert.FromBase64String(script));
  52. pstest.AddScript(script);
  53. Collection<PSObject> output = null;
  54. output = pstest.Invoke();
  55. logger.TimestampInfo("Succesfully invoked a PowerShell script using .NET");
  56. logger.SimulationFinished();
  57. }
  58. catch (Exception ex)
  59. {
  60. logger.SimulationFailed(ex);
  61. }
  62. }
  63. static public void WindowsCommandShell(string log)
  64. {
  65. string currentPath = AppDomain.CurrentDomain.BaseDirectory;
  66. Lib.Logger logger = new Lib.Logger(currentPath + log);
  67. logger.SimulationHeader("T1059.003");
  68. try
  69. {
  70. ExecutionHelper.StartProcessApi("", "cmd.exe /C whoami", logger);
  71. logger.SimulationFinished();
  72. }
  73. catch (Exception ex)
  74. {
  75. logger.SimulationFailed(ex);
  76. }
  77. }
  78. static public void ServiceExecution(string log)
  79. {
  80. string currentPath = AppDomain.CurrentDomain.BaseDirectory;
  81. Lib.Logger logger = new Lib.Logger(currentPath + log);
  82. logger.SimulationHeader("T1569.002");
  83. try
  84. {
  85. ExecutionHelper.StartProcessApi("", "net start UpdaterService", logger);
  86. ExecutionHelper.StartProcessApi("", "sc start UpdaterService", logger);
  87. logger.SimulationFinished();
  88. }
  89. catch (Exception ex)
  90. {
  91. logger.SimulationFailed(ex);
  92. }
  93. }
  94. static public void VisualBasic(string log)
  95. {
  96. string currentPath = AppDomain.CurrentDomain.BaseDirectory;
  97. Lib.Logger logger = new Lib.Logger(currentPath + log);
  98. logger.SimulationHeader("T1059.005");
  99. try
  100. {
  101. string file = "invoice0420.vbs";
  102. ExecutionHelper.StartProcessApi("", String.Format("wscript.exe {0}", file), logger);
  103. logger.SimulationFinished();
  104. }
  105. catch (Exception ex)
  106. {
  107. logger.SimulationFailed(ex);
  108. }
  109. }
  110. static public void JScript(string log)
  111. {
  112. string currentPath = AppDomain.CurrentDomain.BaseDirectory;
  113. Lib.Logger logger = new Lib.Logger(currentPath + log);
  114. logger.SimulationHeader("T1059.007");
  115. try
  116. {
  117. string file = "invoice0420.js";
  118. ExecutionHelper.StartProcessApi("", String.Format("wscript.exe {0}", file), logger);
  119. logger.SimulationFinished();
  120. }
  121. catch (Exception ex)
  122. {
  123. logger.SimulationFailed(ex);
  124. }
  125. }
  126. }
  127. }