thread.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. module Msf
  2. class Plugin::ThreadTest < Msf::Plugin
  3. class ConsoleCommandDispatcher
  4. include Msf::Ui::Console::CommandDispatcher
  5. def name
  6. 'ThreadTest'
  7. end
  8. def commands
  9. {
  10. 'start_thread' => 'Start a background thread that writes to the console',
  11. 'stop_thread' => 'Stop a background thread',
  12. 'list_thread' => 'List running threads'
  13. }
  14. end
  15. def cmd_start_thread(*_args)
  16. if @mythread
  17. print_line('Test thread is already running')
  18. return
  19. end
  20. @mythread = ::Thread.new do
  21. loop do
  22. print_line('--- test thread ---')
  23. select(nil, nil, nil, 5)
  24. end
  25. end
  26. print_line('Test thread created')
  27. end
  28. def cmd_stop_thread(*_args)
  29. if !@mythread
  30. print_line('No test thread is running')
  31. return
  32. end
  33. @mythread.kill
  34. @mythread = nil
  35. print_line('Test thread stopped')
  36. end
  37. def cmd_list_thread(*_args)
  38. Thread.list.each do |t|
  39. print_line(format('Thread: 0x%.8x (%s/%d) (%s)', t.object_id, t.status, t.priority, t.tsource))
  40. print_line('')
  41. end
  42. end
  43. end
  44. #
  45. # The constructor is called when an instance of the plugin is created. The
  46. # framework instance that the plugin is being associated with is passed in
  47. # the framework parameter. Plugins should call the parent constructor when
  48. # inheriting from Msf::Plugin to ensure that the framework attribute on
  49. # their instance gets set.
  50. #
  51. def initialize(framework, opts)
  52. super
  53. # If this plugin is being loaded in the context of a console application
  54. # that uses the framework's console user interface driver, register
  55. # console dispatcher commands.
  56. add_console_dispatcher(ConsoleCommandDispatcher)
  57. # Extend the thread to track the calling source
  58. Thread.class_eval("
  59. attr_accessor :tsource
  60. alias initialize_old initialize
  61. def initialize(&block)
  62. self.tsource = caller(1)
  63. initialize_old(&block)
  64. end
  65. ", __FILE__, __LINE__ - 9)
  66. print_status('ThreadTest plugin loaded.')
  67. end
  68. #
  69. # The cleanup routine for plugins gives them a chance to undo any actions
  70. # they may have done to the framework. For instance, if a console
  71. # dispatcher was added, then it should be removed in the cleanup routine.
  72. #
  73. def cleanup
  74. # If we had previously registered a console dispatcher with the console,
  75. # deregister it now.
  76. remove_console_dispatcher('ThreadTest')
  77. end
  78. #
  79. # This method returns a short, friendly name for the plugin.
  80. #
  81. def name
  82. 'threadtest'
  83. end
  84. #
  85. # This method returns a brief description of the plugin. It should be no
  86. # more than 60 characters, but there are no hard limits.
  87. #
  88. def desc
  89. 'Internal test tool for testing thread usage in Metasploit'
  90. end
  91. end
  92. end