Skip to content

Commit

Permalink
InvokeCommand return output
Browse files Browse the repository at this point in the history
Modify PowerShellRemoting.InvokeCommand to return a string.
  • Loading branch information
rasta-mouse committed Dec 9, 2019
1 parent 33d538b commit a41b22d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
Expand Up @@ -2,7 +2,6 @@
// Project: SharpSploit (https://github.com/cobbr/SharpSploit)
// License: BSD 3-Clause

using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SharpSploit.LateralMovement;
Expand All @@ -15,13 +14,15 @@ public class PowerShellRemotingTests
[TestMethod]
public void TestInvokeCommand()
{
string FileName = Path.GetTempFileName();
bool result = PowerShellRemoting.InvokeCommand("localhost", $@"'test' | Out-File '{FileName}'");
Assert.IsTrue(result);
System.Threading.Thread.Sleep(2000);
string text = File.ReadAllText(FileName);
Assert.AreEqual("test", text);
File.Delete(FileName);
var result = PowerShellRemoting.InvokeCommand("dc1", "whoami; hostname");
Assert.IsTrue(!string.IsNullOrEmpty(result));
}

[TestMethod]
public void TestInvokeCommandWCredentials()
{
var result = PowerShellRemoting.InvokeCommand("dc1", "whoami; hostname", "DEV", "rasta", "Passw0rd!");
Assert.IsTrue(!string.IsNullOrEmpty(result));
}
}
}
}
15 changes: 6 additions & 9 deletions SharpSploit/LateralMovement/PowerShellRemoting.cs
Expand Up @@ -15,19 +15,17 @@ public class PowerShellRemoting
/// <param name="Domain">Domain for explicit credentials.</param>
/// <param name="Username">Username for explicit credentials.</param>
/// <param name="Password">Password for explicit credentials.</param>
/// <returns>Bool. True if execution succeeds, false otherwise.</returns>
/// <returns>String. Results of the PowerShell command.</returns>
/// <author>Daniel Duggan (@_RastaMouse)</author>
/// <remarks>
/// The return value is a little ambigious as the function won't return as long
/// as the command is still running on the remote target. Also, if execution fails
/// (e.g. because bad creds), it doesn't throw an error and it returns true regardless.
/// The function won't return as long as the command is still running on the remote target.
/// </remarks>
public static bool InvokeCommand(string ComputerName, string Command, string Domain = "", string Username = "", string Password = "")
public static string InvokeCommand(string ComputerName, string Command, string Domain = "", string Username = "", string Password = "")
{
string command = string.Empty;
bool useCredentials = Domain != "" && Username != "" && Password != "";

if(useCredentials)
if (useCredentials)
{
command += $@"$Credential = New-Object System.Management.Automation.PSCredential(""{Domain}\{Username}"", (ConvertTo-SecureString ""{Password}"" -AsPlainText -Force)); ";
}
Expand All @@ -36,9 +34,8 @@ public static bool InvokeCommand(string ComputerName, string Command, string Dom
{
command += $" -Credential $Credential";
}

Shell.PowerShellExecute(command, false);
return true;

return Shell.PowerShellExecute(command);
}
}
}
6 changes: 2 additions & 4 deletions SharpSploit/SharpSploit.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a41b22d

Please sign in to comment.