Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenPLC / libmodbus not supporting "Read Write Register" (0x17) function #18

Open
HomeSen opened this issue Nov 16, 2023 · 1 comment
Open

Comments

@HomeSen
Copy link

HomeSen commented Nov 16, 2023

According to the Usenix paper, a libmodbus version had been used for OpenPLC that is vulnerable to a buffer overflow. The buffer overflow can be verified by issuing an according "Read Registers" command that results in a service crash (or SIGSEGV under gdb). As the paper further mentions, the lesser-known "Read Write Register" function (code 0x17) has to be used to gain actual Remote Code Execution.

Unfortuantely, the Modbus service on the PLC returns an "Illegal Function" error, when using that function code, as can be seen from the pymodbus.console output:
image

and also under Wireshark:
image
image

Looking at the libmodbus code inside this repo (and also on the pre-built PLC VM), one can see that this function code is defined:
https://github.com/Fortiphyd/GRFICSv2/blob/master/plc_vm/OpenPLC_v2-master/libmodbus-3.0.4/src/modbus-private.h#L65

and that it should also be properly handled:
https://github.com/Fortiphyd/GRFICSv2/blob/master/plc_vm/OpenPLC_v2-master/libmodbus-3.0.4/src/modbus.c#L870

So, the code path for responding with an "Illegal Function" error should not be taken.

EDIT:
Taking another look at the PLC code base, I realized that the openplc gets linked against libmodbus, but actually uses its own Modbus implementation. The modbus.cpp does not contain a definition for function code 0x17 and also does not have the code implemented for handling it. Thus the message handling function defaults to the "Illegal Function" response.

The overflow still triggers for read requests, due to the modbus.cpp having been modified, accordingly: https://github.com/Fortiphyd/GRFICSv2/blob/master/plc_vm/OpenPLC_v2-master/core/modbus.cpp#L292

So, apparently, it would be required to also re-implement the "Read and Write Registers" function inside the modbus.cpp to gain code executioin.

@hackingprofessional
Copy link

Hi bro!
I am also studying on the topic....
Did you manage to exploit the BOF?
Could you give me some hint ....
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants