Information

Model: MELSEC iQ-F FX5UJ 24M

Product ID của GX Works 3 trial:

904-999559933

Default FTP password:

FXCPU

Web werver credentials

UsernamePassword
adminadmin123
quanlequanle123
thaidothaido123
minhphamminhpham123

Giải thích các thư mục có trong firmware:

Tham khảo: [Procedure to Update FX5U-PLC CPU Firmware – My Mitsubishi Support](https://www.mymitsubishisupport.co.za/procedure-to-update-fx5u-plc-cpu-firmware/)

Checklist

- [x] Tải và cài đặt GX Works 3 (bản trial)
- [x] Tìm hiểu về CGI scripts
	- [x] Trích xuất các HTTP request gửi đến các CGI scripts trong source code của trang web
	- [x] Thử dùng các giá trị invalid trong các request dùng để đọc hoặc ghi device
- [ ] Thử dùng product ID của bản trial cho bản commercial
- [ ] Thử sửa response của login request
- [x] Thử gửi request trực tiếp đến server do login request không trả vể bất cứ thông tin nào để thể hiện session. Nếu có thì có thể là cookie.
	- [x] Thử khai thác CSRF

Resources

Knowlegde Base

Devices

Statuses of equipment connected to the unit are saved in the ro rammable controller memo area called "devices".

Là các vùng nhớ chứa dữ liệu của các cổng nhận tín hiệu đầu vào hoặc đầu ra. Nếu vùng nhớ có kiểu là bit (mang giá trị 1 hoặc 0) thì nó được gọi là bit device. Tương tự, word device là vùng nhớ chứa 16-bit dữ liệu.

Write to/Read From PLC Function

Sequence program (chương trình dùng để điều khiển các I/O devices) có thể được lưu vào PLC khi CPU module đang chạy:

title: GX Works3 Operating Manual - Write to/Read from PLC function
Created sequence programs can be written to or read from a CPU module with the 'Write to PLC' function or the 'Read from PLC' function. 
 
In addition, by using the online change function, sequence programs can be modified even when a CPU module is running.

Program Simulation

Chúng ta có thể giả lập PLC để test sequence program:

title: GX Works3 Operating Manual - Program Simulation
 
This chapter explains the method for debugging a program offline by using the simulation function.
Đã có các research tập trung vào việc reverse engineer simulator của GX Works3:
- [Презентация PowerPoint](https://goa2023.nullcon.net/doc/goa-2023/Hacking-Mitsubishi-PLC-Without-Access-To-Firmware.pdf)
- [Nullcon Goa 2023 | Hacking Mitsubishi PLC Without Access To Firmware by Anton Dorfman](https://www.youtube.com/watch?v=kG3C2zqdTUk)
- [From DoS to RCE: digging into various Mitsubishi PLC series](https://www.youtube.com/watch?v=O8lSuV0GJNQ)

CGI (Common Gateway Interface)

CGI script giống PHP script trong việc nhận vào các tham số và trả về HTML response hoặc JSON response cho người dùng. Các script này thường nằm trong thư mục /cgi-bin ở trên server.

Tham khảo:

Commands

Port Forwarding

Turn on SSH server: Get started with OpenSSH for Windows | Microsoft Learn

Create a new user for authentication with SSH:

New-LocalUser -Name "sshuser" -Password (ConvertTo-SecureString "Pentest@1337#" -AsPlainText -Force) -FullName "SSH User" -Description "User for SSH login"

Add user to remote access group:

Add-LocalGroupMember -Group "Remote Desktop Users" -Member "sshuser"

Add the following directive to the C:\ProgramData\ssh\sshd_config file:

AllowUsers sshuser

Finally, remote forward with this command:

ssh -f -N -R localhost:1337:192.168.93.85:80 sshuser@localhost

Create a firewall rule for port 1337:

New-NetFirewallRule -DisplayName "PLC webserver" -Direction Inbound -Protocol TCP -LocalPort 1337 -Action Allow -Description "PLC webserver"
References:
- [Key-based authentication in OpenSSH for Windows | Microsoft Learn](https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement)
- [OpenSSH Server configuration for Windows | Microsoft Learn](https://learn.microsoft.com/en-us/windows-server/administration/OpenSSH/openssh-server-configuration)

Make a proxy between IPs:

netsh interface portproxy add v4tov4 listenaddress=10.40.61.35 listenport=80 connectaddress=192.168.93.85 connectport=80

Findings & Behaviour

Cookie được cấp dùng cho việc xác thực là SESSIONID:

HTTP/1.0 200 OK
Set-Cookie: SESSIONID=D760ED183D41FDBF8323DB1A98DE2F23; path=/; httponly;

Tuy nhiên, cookie này có SameSite attribute là None.

Khi thử tấn công CSRF:

<html>
  <!-- CSRF PoC - generated by Burp Suite Professional -->
  <body>
    <form action="http://localhost:1337/cgi/logout.cgi" method="POST">
      <input type="submit" value="Submit request" />
    </form>
    <script>
      history.pushState('', '', '/');
      document.forms[0].submit();
    </script>
  </body>
</html>

Ta thấy server thực hiện chống lại bằng cách validate Referer header và trả về response như sau:

{
  "RET" : "0005"
}
Khi tra trong file JavaScript thì mã lỗi này tương ứng với: `"The source of request is incorrect."`

Referer có giá trị giống Origin cũng không được. Khi thử giá trị sau thì được:

Referer: http://localhost:1337/system/Main.html

Khi bỏ Main.html và thay system thành SYSTEM thì vẫn được:

Referer: http://localhost:1337/SYSTEM/

Điều này cho thấy giá trị này là case-insensitive và nó chỉ match từ đầu cho đến SYSTEM/.

Ngoài ra, nếu header Host bị thay đổi thì phần host trong Referer cũng phải bị thay đổi theo. Ví dụ, request sau sẽ bị từ chối:

Host: 10.40.61.34
Referer: http://10.40.61.35/SYSTEM/
Header `Referer` bị validate trong hầu hết các request.

Malformed HTTP Request

Khi gửi một gói tin HTTP không hợp lệ, server sẽ không phản hồi và khi hết timeout thì Burp Suite sẽ gửi gói tin RST để ngắt.

Ví dụ về gói tin không hợp lệ:

POST/cgi/GetRndNum.cgi HTTP/1.1
Host: 10.40.61.35
Chú ý không có khoảng trắng giữa request method và path.

Trong quá trình nhận được request, server có mở một kết nối TCP và khi vượt quá số lượng kết nối TCP, các request mới sẽ bị từ chối:

title: MELSEC iQ-R/MELSEC iQ-F Web Server Function Guide Book - PRECAUTIONS
When a new HTTP client tries to connect the CPU module while the CPU module is connected by the maximum number of HTTP clients that can simultaneously log on, the response performance may deteriorate.

Footnotes

  1. Xem thêm Shellshock