From da470e7064ffd462891823d8532ef542919fc346 Mon Sep 17 00:00:00 2001 From: devStorm <59678453+developStorm@users.noreply.github.com> Date: Wed, 17 Apr 2024 16:00:23 -0700 Subject: [PATCH] refactor: removes number literals --- modules/siemens/s7.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/modules/siemens/s7.go b/modules/siemens/s7.go index 2e8a4948..d0b8b011 100644 --- a/modules/siemens/s7.go +++ b/modules/siemens/s7.go @@ -9,12 +9,15 @@ import ( "github.com/zmap/zgrab2" ) +const s7ModuleIdRecordSize = 28 const ( - S7_MODULE_ID_MODULE_INDEX = 0x1 - S7_MODULE_ID_HARDWARE_INDEX = 0x6 - S7_MODULE_ID_FIRMWARE_INDEX = 0x7 + s7ModuleIdModuleIndex = 0x1 + s7ModuleIdHardwareIndex = 0x6 + s7ModuleIdFirmwareIndex = 0x7 ) +const uint16Size = 2 + // ReconnectFunction is used to re-connect to the target to re-try the scan with a different TSAP destination. type ReconnectFunction func() (net.Conn, error) @@ -292,7 +295,7 @@ func parseModuleIDDataRecord(data []byte) (*moduleIDData, error) { // Constructs the version number from a moduleIDData record. func getVersionNumber(record *moduleIDData) string { - lastByteAusbg1 := record.Ausbg1 & 0xFF + lastByteAusbg1 := record.Ausbg1 & 0xFF return fmt.Sprintf("V%d.%d", lastByteAusbg1, record.Ausbg2) } @@ -307,13 +310,13 @@ func parseModuleIdentificationRequest(logStruct *S7Log, s7Packet *S7Packet) erro // Parse LENTHDR and N_DR from the header recordLen := int(binary.BigEndian.Uint16(s7Packet.Data[offset : offset+2])) - offset += 2 + offset += uint16Size numRecords := int(binary.BigEndian.Uint16(s7Packet.Data[offset : offset+2])) - offset += 2 + offset += uint16Size // Check if the data record length and number of data records are valid - if recordLen != 28 || numRecords*recordLen > len(s7Packet.Data)-offset { + if recordLen != s7ModuleIdRecordSize || numRecords*recordLen > len(s7Packet.Data)-offset { return fmt.Errorf("invalid data record length or number of data records") } @@ -325,11 +328,11 @@ func parseModuleIdentificationRequest(logStruct *S7Log, s7Packet *S7Packet) erro } switch record.Index { - case S7_MODULE_ID_MODULE_INDEX: + case s7ModuleIdModuleIndex: logStruct.ModuleId = record.MIFB - case S7_MODULE_ID_HARDWARE_INDEX: + case s7ModuleIdHardwareIndex: logStruct.Hardware = getVersionNumber(record) - case S7_MODULE_ID_FIRMWARE_INDEX: + case s7ModuleIdFirmwareIndex: logStruct.Firmware = getVersionNumber(record) }