Grehack 2015 - Paper - Industrial Control Systems Dynamic Code Injection
Grehack 2015 - Paper - Industrial Control Systems Dynamic Code Injection
Grehack 2015 - Paper - Industrial Control Systems Dynamic Code Injection
Abstract. Since the day of the virus Stuxnet, the world has discovered the im-
portance of securing Industrial Control Systems, more commonly known as
SCADA, and their potential impacts on Critical Infrastructure Protection (CIP).
The Stuxnet Malware [14] uses a specific exploit (CVE-2012-3015) which con-
sists of Step 7 Insecure Library Loading. In this paper, we propose to demon-
strate how easy it is to make a dynamic code injection in a S7-300 PLC without
shutting down or restarting the equipment. We developed a program in C lan-
guage, using Snap7 library, to push a new Organization Bloc (OB) inside the
CPU. We developed a small HMI to illustrate the dynamic modification on the
execution flow. In conclusion, we suggest some countermeasures or protection
strategies against the dynamic code injection.
1 Introduction
1
Industrial Control System (ICS): is a general term that encompasses several types of con-
trol systems used in industrial production, including supervisory control and data acquisition
(SCADA) systems, Distributed Control Systems (DCS), and other smaller control system
configurations such as Programmable Logic Controllers (PLC) often found in the industrial
sectors and critical infrastructures.
2
Programmable Logic Controller (PLC): is a digital computer used for automation of typi-
cally industrial electromechanical processes, such as control of machinery on factory assem-
bly lines, amusement rides, or light fixtures.
propose to use a S7-300 PLC from Siemens to this paper. But, we point out that all
these weaknesses may be valid on the other PLC manufacturers. While these attacks
are known to function against Siemens PLC, the vulnerabilities the attacks exploit are
general enough to also likely be found in other PLC manufacturers used by ICS.
Firstly we will introduce the main internal operating of the S7-300 PLC [6]. Sec-
ondly, we will detail the block oriented approach, the CPU flow execution and the
basic programming concepts. The third part on this paper will describe a dynamic
code injection attack. And finally, we will finish by a practical demonstration before
concluding.
2 Operating principle
3
Since the S7-1500 model and the following firmware version, an authentication session is
needed, and in some case you need a password to upload (write into memory area) a new
bloc into PLC.
4
“O.B. one” not like Obi-Wan Kenobi in Star Wars movie (joke).
curring functions (ie. Regulations task). SFB are identical to SFC with their
own specific memory area (used to save context or parameters)
- DB (Data Block): are user program data areas in which the user data are han-
dled in a structured way.
- SZL (System Zone List): memory area containing the diagnostic data and
system state. It is in this area that we can find, for example, the level of CPU
protection CPU [7].
The Fig. 3 shows the global block structure (OB, FB, DB, etc.).
A Header containing main block info such as MC7 size, date and time, etc.
A Body containing the data.
A Footer containing info about structure such as number and type of elements of a
DB. Additional block info such as author, etc.
To avoid drawing attention, block load order is very important. If a DB calls a FC not
yet loaded, this can result in a PLC crash. We should always start by loading the rank
N-1 blocks before rank N blocks (cf. Fig. 2).
2.3 CPU flow execution
The CPU checks the status of inputs and outputs at each cycle (around a few milli-
seconds). Specific memory areas are used to store binary data modules: Inputs-
Memory Picture (IMP) and Outputs-Memory Picture (OMP). The program (MC7
code) accesses these registers during treatment.
Inputs-Memory Picture (IMP) is in the memory area of the PLC CPU. It contains
the logic state of all inputs. IMP is read at the beginning of each cycle.
Outputs-Memory Picture (OMP) contains the output values obtained after process-
ing program. At the end of the cycle, these values are sent to physical outputs of the
PLC.
The figure Fig. 4 summarizes the reading process, the loading and processing of
inputs and outputs.
2.4 Programming
We will use the TIA Portal Software [9] to code a Step 7 program. Siemens offers
three different kinds of programming languages to use [8]:
In our demonstration, we will use the CONT language to code the corrupted OB1, but
you can also use LIST or LOG.
After coding, the source code is converted automatically to LIST language. Thereby,
this first transformation standardizes the code and makes it compatible to all Siemens
product ranges. The end build process generates a MC7 file which is a binary code
machine that the PLC CPU can understand and execute (cf. Fig. 5).
Each generated block file is a MC7 file uploaded to the memory using TIA Portal
Software. Historically, the small memory size of PLC requires the use of small file
sizes (LIST hexadecimal code).
Fig. 5. Analogy between java and step 7 languages build process.
All these variables must have a type depending on their use. The most common, in
our case, is the bit (Boolean). Indeed, we use the digital inputs and outputs (all or
nothing so) for our demonstration. Many states will therefore be represented by 0 (if
nothing happens) or 1 (there is something going on). However, many other types are
available, such as byte (byte), the word (double byte), etc.
These variables must be declared in a static way in source code, that is to say, they
are affected a name but also a position (offset) in the memory (for example M2.4
indicates a memento that will be located on the second byte and will start from the 4th
bit).
If the real user sends an authentication packet to the PLC, first the device makes a
comparison to verify that date and time packet are correct. If this previous verification
is ready, the device checks if the password or hash (CRC code), from the user’s
packet, matches the one inside of the project file stored in the PLC’s memory.
If both conditions are true, the device flips a bit and allows a read/write/execute
permission to the PLC’s memory.
In case of false conditions, the device rejects the request then aborts the TCP con-
nection.
Snap 7 library ensures the good execution of data exchange.
To do this, we simply include the Snap7 wrapper in our source code and use the
Client class as follow:
After obtaining the original OB1, we open it under TIA Portal then we modify it as
we like (by coding the payload part: for example showing a false statements).
Industrial control systems commonly use polling techniques to continuously monitor
the state of remote process (as was in Natanz nuclear Iranian plant). Polling takes the
form of a query transmitted from the PLC to the HMI supervision followed by a re-
sponse packet transmitted from the HMI supervision to the PLC. The state informa-
tion is used to provide a human machine interface (HMI) to monitor the process and
take requisite control actions based upon process state. An example of paylod consists
of modifying the original source code to send false information to the operator.
Finally we build the MC7 file by compiling the code source. Note that a legitimate
station of development checks the correct CRC between the header of block and the
body of the same block (no integrity problem with the CPU S7-300).
As for the Upload operation, conversely we made the Download of the corrupted OB1
from hacker station to PLC by using the following code source:
Parameters
The PLC CPU uses a hard-coded password to give the users access. D. Beresford
[15] has shown that it is possible to read and write data to the PLC’s memory even
when the password protection is enabled5. We have two other possibilities to bypass
the password: extracting the hash of password then replaying it again or brute forcing
the password offline. To do this, we use a funny SCADA tools composed by five
Python scripts [.
s7_password_hash_extractor() to extract the password hash from the
PEData.plf file. You will find the *.plf file into the PLC configuration or in Simatic
environment.
s7_brute_offline() to crack the password by brute forcing the connection to
the PLC. We recall that the number of connections attempts is unlimited. We need
only 5 minutes to find the right password.
5
This is particularly true for previous models of Siemens PLC. We do not test it yet on S7-
1500 PLC.
At the end, we connect to the PLC and disable the password protection to be able
to use the Snap 7 Library without authentication session.
There is another confidential technique which allow to broke the password due to
the poorness of cipher algorithm used, but we can not talking about it in this paper6.
As showing in the red box of the second capture (Fig. 7), we can read two characters
kh for Know-How encoded file. Theses bytes are used by the CPU (and Simatic
framework) to open or not the code source of the block.
6
Actually we just have done this exploit only on S7-300, so we can not say the same for other
later PLC models.
4 Demonstration
In our Cybersecurity Labs we use a real S7-300 PLC with associated Power- mod-
ule and Inputs/Outputs card to proof our cyber-attack. To simplify the demonstration7,
we will use a virtualized instance of S7-300 PLC. This simulated PLC has the same
functionalities as a real PLC.
To do this, we use a PLCSIM program (proposed by Siemens Company) and a
very friendly tool NetToPLCsim S7online [14] to communicate from the LAN to the
simulated PLC.
The Fig. 8 show the network interconnection of demonstration components.
7
This also reduces the equipment needed the day of conference.
5 Conclusion
In this paper, we saw that it’s very easy to load a corrupted code in the PLC mem-
ory with the capability sometimes to turn off security flag (depending on model of
PLC). The main reasons for insecure Industrial Control System is: CIP has been built
from the very beginning without security in mind.
The potential impacts of malicious code injections include interruption process
control, interruption of device communication, unauthorized modification of process
set points or destroy the actuators (depending of the payload). Typically actuators,
such as switches or valves, connected to physical processes are connected to a digital
or analog output connected to intelligent electronic device. Each output connects to
the cyber system by modeling it as a digital point in a register of the memory. Chang-
ing the state of a bit or a function block in PLC memory has an immediate impact on
the physical actuator. An attacker who understands a device’s implementation specif-
ics including a memory map can introduce a backdoor into the process to alter actua-
tor states.
In order to protect against the dynamic injection arbitrary code through the PLC we
can use a probe as SURICATA [20] which is able to decode S7Com/S7Com Plus
packets and detecting this kinds of actions. We wrote some specifics SURICATA
rules catching S7 Command & Control PDU8. We have also the possibility to config-
ure on the S7-300 PLC the “alarm organization block” to detect immediately each
modification code or downloading blocks. Thus event detection can draw mainte-
nance operator attention to check the PLC code.
8
You just need to catch the hexadecimal value of uploading block from the PROFINET pac-
ket (code function 0x32 offset=7 and code function 0x1d offset=21).
6 References
1. Five myths of industrial control system security (http://www.scmagazineuk.com/five-
myths-of-industrial-control-system-security/article/431387/)
2. Ralph Langner, “To kill a centrifuge. A technical analysis of what Stuxnet’s creators tried”
to achieve, Hamburg, Munich, November 2013.
3. Thomas H. Morris & Wei Gao, “Industrial Control System Cyber Attacks”, 1st Interna-
tional Symposium for ICS & SCADA Cyber Security Research 2013 (ICS-CSR 2013),
Leicester, UK, 16-17 September 2013.
4. https://en.wikipedia.org/wiki/Industrial_control_system
5. Siemens Simatic S7-300 PLC Remote Memory Viewer (https://exploit-
db.com/exploits/19831/)
6. SIMATIC. System Software for S7-300 400 System and Standard Function, March 2006
7. SIMATIC, Structure and memory CPU used, January 2013.
8. Karl-Heinz John and Michael Tiegelkamp. “IEC 61131-3: programming industrial auto-
mation systems: concepts and programming langugages, requirements for programming
systems, decision-making aids”. Springer Science & Business Media, 2010.
9. SIEMENS. “Initiate programming with STEP7 API”, 2005.
10. SIMATIC S7. CONT language for SIMATIC S7-300/400 programming blocs, Siemens.
11. SIMATIC S7. LOG language for SIMATIC S7-300/400 manual reference, Siemens.
12. SIMATIC S7. LIST language for SIMATIC S7-300/400 manuel reference, Siemens.
13. Davide Nardella, SNAP7. Http://snap7.sourceforge.net/
14. Antoine Mignon and David Boucart, “Siemens S7 protocol communication analysis”,
August 2013.
15. Dillon Beresford, “Black hat. In Siemens Simatic S7 PLC Exploitation”, NSS Labs, 2011.
16. Dillon Beresford, “Exploiting Siemens Simatic S7 PLCs”, July 2011.
17. https://github.com/atimorin/scada-tools/blob/master/s7_password_hashes_extractor.py
18. Modbus application protocol specification V1.1b3 (www.modbus.org), April 26, 2012.
19. Modicon Modbus Protocol Reference Guide - PI–MBUS–300 Rev. J.
20. David Diallo and Mathieu Feuillet, “Industrial Systems Detection : Suricata and Modbus
case”, C&ESAR Conference, France, Novembre 2014.
21. http://nettoplcsim.sourceforge.net/