File tree Expand file tree Collapse file tree 2 files changed +37
-2
lines changed
ui/console/command_dispatcher Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -33,4 +33,37 @@ def self.type
33
33
def desc
34
34
"Powershell session"
35
35
end
36
+
37
+ #
38
+ # Takes over the shell_command of the parent
39
+ #
40
+ def shell_command ( cmd )
41
+ # insert random marker
42
+ strm = Rex ::Text . rand_text_alpha ( 15 )
43
+ endm = Rex ::Text . rand_text_alpha ( 15 )
44
+
45
+ # Send the shell channel's stdin.
46
+ shell_write ( ";'#{ strm } '\n " + cmd + "\n '#{ endm } ';\n " )
47
+
48
+ timeout = 1800 # 30 minute timeout
49
+ etime = ::Time . now . to_f + timeout
50
+
51
+ buff = ""
52
+ # Keep reading data until the marker has been received or the 30 minture timeout has occured
53
+ while ( ::Time . now . to_f < etime )
54
+ res = shell_read ( -1 , timeout )
55
+ break unless res
56
+ timeout = etime - ::Time . now . to_f
57
+
58
+ buff << res
59
+ if buff . match ( /#{ endm } / )
60
+ # if you see the end marker, read the buffer from the start marker to the end and then display back to screen
61
+ buff = buff . split ( /#{ strm } / ) [ -1 ]
62
+ buff . gsub! ( /PS .*>/ , '' )
63
+ buff . gsub! ( /#{ endm } / , '' )
64
+ return buff
65
+ end
66
+ end
67
+ buff
68
+ end
36
69
end
Original file line number Diff line number Diff line change @@ -1772,13 +1772,15 @@ def cmd_sessions(*args)
1772
1772
rescue Rex ::TimeoutError
1773
1773
print_error ( "Operation timed out" )
1774
1774
end
1775
- elsif session . type == 'shell'
1775
+ elsif session . type == 'shell' || session . type == 'powershell'
1776
1776
output = session . shell_command ( cmd )
1777
1777
print_line ( output ) if output
1778
1778
end
1779
1779
ensure
1780
1780
# Restore timeout for each session
1781
- session . response_timeout = last_known_timeout if last_known_timeout
1781
+ if session . respond_to? ( :response_timeout )
1782
+ session . response_timeout = last_known_timeout if last_known_timeout
1783
+ end
1782
1784
end
1783
1785
# If the session isn't a meterpreter or shell type, it
1784
1786
# could be a VNC session (which can't run commands) or
You can’t perform that action at this time.
0 commit comments