CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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