51 rs_modbus_register_parser();
78 #define MODBUS_CONFIG_DEFAULT_STREAM_DEPTH 0
81 static uint8_t invalidFunctionCode[] = {
91 static uint8_t readCoilsReq[] = { 0x00, 0x00,
99 static uint8_t readCoilsRsp[] = { 0x00, 0x00,
107 static uint8_t readCoilsErrorRsp[] = {
119 static uint8_t writeSingleRegisterReq[] = { 0x00, 0x0A,
127 static uint8_t invalidWriteSingleRegisterReq[] = { 0x00, 0x0A,
134 static uint8_t writeSingleRegisterRsp[] = { 0x00, 0x0A,
144 static uint8_t writeMultipleRegistersReq[] = { 0x00, 0x0A,
155 static uint8_t writeMultipleRegistersRsp[] = { 0x00, 0x0A,
165 static uint8_t maskWriteRegisterReq[] = { 0x00, 0x0A,
174 static uint8_t invalidMaskWriteRegisterReq[] = { 0x00, 0x0A,
182 static uint8_t maskWriteRegisterRsp[] = { 0x00, 0x0A,
194 static uint8_t readWriteMultipleRegistersReq[] = { 0x12, 0x34,
209 static uint8_t readWriteMultipleRegistersRsp[] = { 0x12, 0x34,
224 static uint8_t forceListenOnlyMode[] = { 0x0A, 0x00,
232 static uint8_t invalidProtocolIdReq[] = { 0x00, 0x00,
240 static uint8_t invalidLengthWriteMultipleRegistersReq[] = {
252 static uint8_t exceededLengthWriteMultipleRegistersReq[] = {
262 static uint8_t invalidLengthPDUWriteMultipleRegistersReq[] = {
270 static int ModbusParserTest01(
void) {
277 memset(&f, 0,
sizeof(f));
278 memset(&ssn, 0,
sizeof(ssn));
282 f.
proto = IPPROTO_TCP;
288 STREAM_TOSERVER, readCoilsReq,
289 sizeof(readCoilsReq));
292 ModbusState *modbus_state = f.
alstate;
295 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 0);
297 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 1);
298 FAIL_IF_NOT(rs_modbus_message_get_read_request_address(&request) == 0x7890);
299 FAIL_IF_NOT(rs_modbus_message_get_read_request_quantity(&request) == 19);
302 STREAM_TOCLIENT, readCoilsRsp,
303 sizeof(readCoilsRsp));
306 FAIL_IF_NOT(rs_modbus_state_get_tx_count(modbus_state) == 1);
315 static int ModbusParserTest02(
void) {
322 memset(&f, 0,
sizeof(f));
323 memset(&ssn, 0,
sizeof(ssn));
327 f.
proto = IPPROTO_TCP;
333 STREAM_TOSERVER, writeMultipleRegistersReq,
334 sizeof(writeMultipleRegistersReq));
337 ModbusState *modbus_state = f.
alstate;
340 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 0);
342 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 16);
343 FAIL_IF_NOT(rs_modbus_message_get_write_multreq_address(&request) == 0x01);
344 FAIL_IF_NOT(rs_modbus_message_get_write_multreq_quantity(&request) == 2);
347 const uint8_t *data = rs_modbus_message_get_write_multreq_data(&request, &data_len);
355 STREAM_TOCLIENT, writeMultipleRegistersRsp,
356 sizeof(writeMultipleRegistersRsp));
359 FAIL_IF_NOT(rs_modbus_state_get_tx_count(modbus_state) == 1);
368 static int ModbusParserTest03(
void) {
380 memset(&f, 0,
sizeof(
Flow));
388 f.
proto = IPPROTO_TCP;
403 "(msg:\"Modbus Data mismatch\"; "
405 "modbus.value_mismatch; "
414 readWriteMultipleRegistersReq,
415 sizeof(readWriteMultipleRegistersReq));
418 ModbusState *modbus_state = f.
alstate;
421 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 0);
424 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 23);
425 FAIL_IF_NOT(rs_modbus_message_get_rw_multreq_read_address(&request) == 0x03);
426 FAIL_IF_NOT(rs_modbus_message_get_rw_multreq_read_quantity(&request) == 6);
427 FAIL_IF_NOT(rs_modbus_message_get_rw_multreq_write_address(&request) == 0x0E);
428 FAIL_IF_NOT(rs_modbus_message_get_rw_multreq_write_quantity(&request) == 3);
431 uint8_t
const *data = rs_modbus_message_get_rw_multreq_write_data(&request, &data_len);
441 STREAM_TOCLIENT, readWriteMultipleRegistersRsp,
442 sizeof(readWriteMultipleRegistersRsp));
445 FAIL_IF_NOT(rs_modbus_state_get_tx_count(modbus_state) == 1);
466 static int ModbusParserTest04(
void) {
473 memset(&f, 0,
sizeof(f));
474 memset(&ssn, 0,
sizeof(ssn));
478 f.
proto = IPPROTO_TCP;
484 STREAM_TOSERVER, forceListenOnlyMode,
485 sizeof(forceListenOnlyMode));
488 ModbusState *modbus_state = f.
alstate;
491 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 0);
494 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 8);
495 FAIL_IF_NOT(rs_modbus_message_get_subfunction(&request) == 4);
504 static int ModbusParserTest05(
void) {
516 memset(&f, 0,
sizeof(
Flow));
524 f.
proto = IPPROTO_TCP;
539 "(msg:\"Modbus invalid Protocol version\"; "
541 "modbus.invalid_protocol_id; "
549 STREAM_TOSERVER, invalidProtocolIdReq,
550 sizeof(invalidProtocolIdReq));
553 ModbusState *modbus_state = f.
alstate;
575 static int ModbusParserTest06(
void) {
587 memset(&f, 0,
sizeof(
Flow));
595 f.
proto = IPPROTO_TCP;
610 "(msg:\"Modbus unsolicited response\"; "
612 "modbus.unsolicited_response; "
620 STREAM_TOCLIENT, readCoilsRsp,
621 sizeof(readCoilsRsp));
624 ModbusState *modbus_state = f.
alstate;
646 static int ModbusParserTest07(
void) {
658 memset(&f, 0,
sizeof(
Flow));
666 f.
proto = IPPROTO_TCP;
681 "(msg:\"Modbus invalid Length\"; "
683 "modbus.invalid_length; "
692 invalidLengthWriteMultipleRegistersReq,
693 sizeof(invalidLengthWriteMultipleRegistersReq));
696 ModbusState *modbus_state = f.
alstate;
718 static int ModbusParserTest08(
void) {
730 memset(&f, 0,
sizeof(
Flow));
738 f.
proto = IPPROTO_TCP;
753 "(msg:\"Modbus Exception code invalid\"; "
755 "modbus.invalid_exception_code; "
763 STREAM_TOSERVER, readCoilsReq,
764 sizeof(readCoilsReq));
767 ModbusState *modbus_state = f.
alstate;
770 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 0);
773 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 1);
774 FAIL_IF_NOT(rs_modbus_message_get_read_request_address(&request) == 0x7890);
775 FAIL_IF_NOT(rs_modbus_message_get_read_request_quantity(&request) == 19);
778 STREAM_TOCLIENT, readCoilsErrorRsp,
779 sizeof(readCoilsErrorRsp));
782 FAIL_IF_NOT(rs_modbus_state_get_tx_count(modbus_state) == 1);
803 static int ModbusParserTest09(
void) {
808 uint32_t input_len =
sizeof(readCoilsReq), part2_len = 3;
809 uint8_t *input = readCoilsReq;
813 memset(&f, 0,
sizeof(f));
814 memset(&ssn, 0,
sizeof(ssn));
818 f.
proto = IPPROTO_TCP;
824 STREAM_TOSERVER, input, input_len - part2_len);
828 STREAM_TOSERVER, input, input_len);
831 ModbusState *modbus_state = f.
alstate;
834 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 0);
837 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 1);
838 FAIL_IF_NOT(rs_modbus_message_get_read_request_address(&request) == 0x7890);
839 FAIL_IF_NOT(rs_modbus_message_get_read_request_quantity(&request) == 19);
841 input_len =
sizeof(readCoilsRsp);
843 input = readCoilsRsp;
846 STREAM_TOCLIENT, input, input_len - part2_len);
850 STREAM_TOCLIENT, input, input_len);
853 FAIL_IF_NOT(rs_modbus_state_get_tx_count(modbus_state) == 1);
862 static int ModbusParserTest10(
void) {
863 uint32_t input_len =
sizeof(readCoilsReq) +
sizeof(writeMultipleRegistersReq);
864 uint8_t *input, *ptr;
872 input = (uint8_t *)
SCMalloc (input_len *
sizeof(uint8_t));
875 memcpy(input, readCoilsReq,
sizeof(readCoilsReq));
876 memcpy(input +
sizeof(readCoilsReq), writeMultipleRegistersReq,
sizeof(writeMultipleRegistersReq));
878 memset(&f, 0,
sizeof(f));
879 memset(&ssn, 0,
sizeof(ssn));
883 f.
proto = IPPROTO_TCP;
889 STREAM_TOSERVER, input, input_len);
892 ModbusState *modbus_state = f.
alstate;
895 FAIL_IF_NOT(rs_modbus_state_get_tx_count(modbus_state) == 2);
897 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 1);
900 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 16);
901 FAIL_IF_NOT(rs_modbus_message_get_write_multreq_address(&request) == 0x01);
902 FAIL_IF_NOT(rs_modbus_message_get_write_multreq_quantity(&request) == 2);
905 uint8_t
const *data = rs_modbus_message_get_write_multreq_data(&request, &data_len);
912 input_len =
sizeof(readCoilsRsp) +
sizeof(writeMultipleRegistersRsp);
914 ptr = (uint8_t *)
SCRealloc (input, input_len *
sizeof(uint8_t));
918 memcpy(input, readCoilsRsp,
sizeof(readCoilsRsp));
919 memcpy(input +
sizeof(readCoilsRsp), writeMultipleRegistersRsp,
sizeof(writeMultipleRegistersRsp));
932 static int ModbusParserTest11(
void) {
941 size_t input_len = 65536;
942 uint8_t *input =
SCCalloc(1, input_len);
946 memcpy(input, exceededLengthWriteMultipleRegistersReq,
947 sizeof(exceededLengthWriteMultipleRegistersReq));
952 memset(&f, 0,
sizeof(
Flow));
960 f.
proto = IPPROTO_TCP;
975 "(msg:\"Modbus invalid Length\"; "
977 "modbus.invalid_length; "
988 ModbusState *modbus_state = f.
alstate;
1010 static int ModbusParserTest12(
void) {
1022 memset(&f, 0,
sizeof(
Flow));
1030 f.
proto = IPPROTO_TCP;
1045 "(msg:\"Modbus invalid Length\"; "
1047 "modbus.invalid_length; "
1056 invalidLengthPDUWriteMultipleRegistersReq,
1057 sizeof(invalidLengthPDUWriteMultipleRegistersReq));
1060 ModbusState *modbus_state = f.
alstate;
1082 static int ModbusParserTest13(
void) {
1089 memset(&f, 0,
sizeof(f));
1090 memset(&ssn, 0,
sizeof(ssn));
1094 f.
proto = IPPROTO_TCP;
1100 STREAM_TOSERVER, maskWriteRegisterReq,
1101 sizeof(maskWriteRegisterReq));
1104 ModbusState *modbus_state = f.
alstate;
1107 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 0);
1110 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 22);
1111 FAIL_IF_NOT(rs_modbus_message_get_and_mask(&request) == 0x00F2);
1112 FAIL_IF_NOT(rs_modbus_message_get_or_mask(&request) == 0x0025);
1115 STREAM_TOCLIENT, maskWriteRegisterRsp,
1116 sizeof(maskWriteRegisterRsp));
1119 FAIL_IF_NOT(rs_modbus_state_get_tx_count(modbus_state) == 1);
1128 static int ModbusParserTest14(
void) {
1135 memset(&f, 0,
sizeof(f));
1136 memset(&ssn, 0,
sizeof(ssn));
1140 f.
proto = IPPROTO_TCP;
1146 STREAM_TOSERVER, writeSingleRegisterReq,
1147 sizeof(writeSingleRegisterReq));
1150 ModbusState *modbus_state = f.
alstate;
1153 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 0);
1156 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 6);
1157 FAIL_IF_NOT(rs_modbus_message_get_write_address(&request) == 0x0001);
1158 FAIL_IF_NOT(rs_modbus_message_get_write_data(&request) == 0x0003);
1161 STREAM_TOCLIENT, writeSingleRegisterRsp,
1162 sizeof(writeSingleRegisterRsp));
1165 FAIL_IF_NOT(rs_modbus_state_get_tx_count(modbus_state) == 1);
1174 static int ModbusParserTest15(
void) {
1186 memset(&f, 0,
sizeof(f));
1187 memset(&ssn, 0,
sizeof(ssn));
1194 f.
proto = IPPROTO_TCP;
1209 "(msg:\"Modbus invalid Length\"; "
1211 "modbus.invalid_length; "
1219 STREAM_TOSERVER, invalidMaskWriteRegisterReq,
1220 sizeof(invalidMaskWriteRegisterReq));
1223 ModbusState *modbus_state = f.
alstate;
1226 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 0);
1229 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 22);
1237 STREAM_TOCLIENT, maskWriteRegisterRsp,
1238 sizeof(maskWriteRegisterRsp));
1241 FAIL_IF_NOT(rs_modbus_state_get_tx_count(modbus_state) == 1);
1242 ModbusMessage response = rs_modbus_state_get_tx_response(modbus_state, 0);
1245 FAIL_IF_NOT(rs_modbus_message_get_function(&response) == 22);
1261 static int ModbusParserTest16(
void) {
1273 memset(&f, 0,
sizeof(f));
1274 memset(&ssn, 0,
sizeof(ssn));
1281 f.
proto = IPPROTO_TCP;
1296 "(msg:\"Modbus invalid Length\"; "
1298 "modbus.invalid_length; "
1307 invalidWriteSingleRegisterReq,
1308 sizeof(invalidWriteSingleRegisterReq));
1311 ModbusState *modbus_state = f.
alstate;
1314 ModbusMessage request = rs_modbus_state_get_tx_request(modbus_state, 0);
1317 FAIL_IF_NOT(rs_modbus_message_get_function(&request) == 6);
1319 const uint8_t *data = rs_modbus_message_get_bytevec_data(&request, &data_len);
1330 STREAM_TOCLIENT, writeSingleRegisterRsp,
1331 sizeof(writeSingleRegisterRsp));
1334 FAIL_IF_NOT(rs_modbus_state_get_tx_count(modbus_state) == 1);
1335 ModbusMessage response = rs_modbus_state_get_tx_response(modbus_state, 0);
1338 FAIL_IF_NOT(rs_modbus_message_get_function(&response) == 6);
1339 FAIL_IF_NOT(rs_modbus_message_get_write_address(&response) == 0x0001);
1354 static int ModbusParserTest17(
void) {
1361 memset(&f, 0,
sizeof(f));
1362 memset(&ssn, 0,
sizeof(ssn));
1366 f.
proto = IPPROTO_TCP;
1372 readCoilsReq,
sizeof(readCoilsReq));
1380 readCoilsRsp,
sizeof(readCoilsRsp));
1392 static int ModbusParserTest18(
void) {
1397 uint32_t input_len =
sizeof(readCoilsReq), part2_len = 3;
1398 uint8_t *input = readCoilsReq;
1402 memset(&f, 0,
sizeof(f));
1403 memset(&ssn, 0,
sizeof(ssn));
1407 f.
proto = IPPROTO_TCP;
1413 input, input_len - part2_len);
1426 input_len =
sizeof(readCoilsRsp);
1428 input = readCoilsRsp;
1431 input, input_len - part2_len);
1449 static int ModbusParserTest19(
void) {
1461 memset(&f, 0,
sizeof(
Flow));
1469 f.
proto = IPPROTO_TCP;
1484 "(msg:\"Modbus invalid Function code\"; "
1486 "modbus.invalid_function_code; "
1495 invalidFunctionCode,
1496 sizeof(invalidFunctionCode));
1499 ModbusState *modbus_state = f.
alstate;
1524 ModbusParserTest01);
1525 UtRegisterTest(
"ModbusParserTest02 - Modbus Write Multiple registers request",
1526 ModbusParserTest02);
1527 UtRegisterTest(
"ModbusParserTest03 - Modbus Read/Write Multiple registers request",
1528 ModbusParserTest03);
1529 UtRegisterTest(
"ModbusParserTest04 - Modbus Force Listen Only Mode request",
1530 ModbusParserTest04);
1531 UtRegisterTest(
"ModbusParserTest05 - Modbus invalid Protocol version",
1532 ModbusParserTest05);
1533 UtRegisterTest(
"ModbusParserTest06 - Modbus unsolicited response",
1534 ModbusParserTest06);
1535 UtRegisterTest(
"ModbusParserTest07 - Modbus invalid Length request",
1536 ModbusParserTest07);
1537 UtRegisterTest(
"ModbusParserTest08 - Modbus Exception code invalid",
1538 ModbusParserTest08);
1539 UtRegisterTest(
"ModbusParserTest09 - Modbus fragmentation - 1 ADU in 2 TCP packets",
1540 ModbusParserTest09);
1541 UtRegisterTest(
"ModbusParserTest10 - Modbus fragmentation - 2 ADU in 1 TCP packet",
1542 ModbusParserTest10);
1543 UtRegisterTest(
"ModbusParserTest11 - Modbus exceeded Length request",
1544 ModbusParserTest11);
1546 ModbusParserTest12);
1547 UtRegisterTest(
"ModbusParserTest13 - Modbus Mask Write register request",
1548 ModbusParserTest13);
1549 UtRegisterTest(
"ModbusParserTest14 - Modbus Write single register request",
1550 ModbusParserTest14);
1551 UtRegisterTest(
"ModbusParserTest15 - Modbus invalid Mask Write register request",
1552 ModbusParserTest15);
1553 UtRegisterTest(
"ModbusParserTest16 - Modbus invalid Write single register request",
1554 ModbusParserTest16);
1556 ModbusParserTest17);
1557 UtRegisterTest(
"ModbusParserTest18 - Modbus stream depth in 2 TCP packets",
1558 ModbusParserTest18);
1559 UtRegisterTest(
"ModbusParserTest19 - Modbus invalid Function code",
1560 ModbusParserTest19);