Bläddra i källkod

Implemented response mechanism for Modbus Discovery & Detection request from nmap and Metasploit. Also implemented reply mechanism for READ_COIL and WRITE_COIL requests

shreyas.srinivasa 9 år sedan
förälder
incheckning
6582f908c5
1 ändrade filer med 109 tillägg och 9 borttagningar
  1. 109 9
      src/de/tudarmstadt/informatik/hostage/protocol/MODBUS.java

+ 109 - 9
src/de/tudarmstadt/informatik/hostage/protocol/MODBUS.java

@@ -3,10 +3,15 @@ package de.tudarmstadt.informatik.hostage.protocol;
 import android.util.Log;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Random;
 
 import de.tudarmstadt.informatik.hostage.wrapper.Packet;
 
+import static java.lang.System.*;
+
 /**
  * Created by Shreyas Srinivasa on 25.05.15.
  */
@@ -38,20 +43,39 @@ public class MODBUS implements Protocol {
     //Request Codes (keeping the most essential ones)
 
     public static final int READ_COILS = 1;
-    public static final int READ_INPUT_DISCRETES = 2;
+   // public static final int READ_INPUT_DISCRETES = 2; Not sure of these commands
     public static final int READ_INPUT_REGISTERS = 4;
     public static final int WRITE_COIL = 5;
     public static final int WRITE_SINGLE_REGISTER = 6;
-    public static final int WRITE_MULTIPLE_COILS = 15;
-    public static final int WRITE_MULTIPLE_REGISTERS = 16;
+    //public static final int WRITE_MULTIPLE_COILS = 15; Not sure of these commands
+    //public static final int WRITE_MULTIPLE_REGISTERS = 16; Not sure of these commands
     public static final int MODBUS_SERVICE = 17;
 
+    HashMap<Integer,Integer> coil = new HashMap<Integer,Integer>();
+    HashMap<Integer,Integer> register = new HashMap<Integer,Integer>();
 
-    //Reply codes
+    public int readcoil(int address) {
 
+        if (coil.containsKey(address)) {
+            int val = (Integer) coil.get(address);
+            return val;
+        } else {
+            coil.put(address, rand());
+            System.out.println(coil);
 
-    //Device Information
+            int val = (Integer) coil.get(address);
+            System.out.println("Address:" + String.valueOf(address) + "Data:" + String.valueOf(val));
+            return val;
+        }
+    }
+    private int rand() {
+
+        int num =(Math.random()<0.5)?0:1;
+        return num;
+    }
 
+
+    //Device Information
     private String DeviceInfo = getDeviceInfo();
 
     private String getDeviceInfo() {
@@ -64,14 +88,17 @@ public class MODBUS implements Protocol {
     @Override
     public List<Packet> processMessage(Packet requestPacket) {
         List<Packet> responsePackets = new ArrayList<Packet>();
+
         byte[] request = null;
         if (requestPacket != null) {
             request = requestPacket.getBytes();
-            for (byte b : request) {
-                System.out.println(b);
+            //for (byte b : request) {
+            //    System.out.println(b);
 
+            getRequestType(request);
 
-            }
+            responsePackets=processRequest(request,getRequestType(request));
+            System.out.println(responsePackets);
 
 
         }
@@ -80,5 +107,78 @@ public class MODBUS implements Protocol {
         return responsePackets;
     }
 
-}
+    private List<Packet> processRequest(byte[] request,int requestType) {
+
+        List<Packet> responsePackets = new ArrayList<Packet>();
+        switch (requestType){
+
+            case MODBUS_SERVICE:
+                responsePackets.add(new Packet(request,getDeviceInfo()));
+                break;
+
+            case READ_INPUT_REGISTERS:
+                responsePackets.add(new Packet(request,getDeviceInfo()));
+                break;
+
+            case READ_COILS:
+                int address = (request[9]);
+                request[5]=4;
+
+                request[9]=(byte)readcoil(address);
+                responsePackets.add(new Packet(request,getDeviceInfo()));
+                break;
+
+            case WRITE_COIL:
+                int coilAddress = (request[9]);
+                int coilData = (request[10]);
+                writeCoil(coilAddress,coilData);
+                responsePackets.add(new Packet(request,getDeviceInfo()));
+                break;
+
+            default:
+                break;
+
+        }
+    return responsePackets;
+    }
+
+    private int writeCoil(int coilAddress, int coilData) {
+
+        coil.put(coilAddress,coilData);
+
+        int val=(Integer)coil.get(coilAddress);
+        return val;
+
+    }
+
+
+    /* gets the type of request made from the master */
+    private int getRequestType(byte[] request) {
+
+        int requestType=request[7];
+
+        if (requestType == 17) {
+            requestType = MODBUS_SERVICE;
+        } else if (requestType == 5) {
+            requestType = WRITE_COIL;
+        } else if (requestType == 1) {
+            requestType = READ_COILS;
+        } else if (requestType == 6) {
+            requestType = WRITE_SINGLE_REGISTER;
+        } else if (requestType == 4) {
+            requestType = READ_INPUT_REGISTERS;
+        }
+
+        System.out.println(requestType);
+        return requestType;
+
+
+
+    }
+
+
+
+
+    }
+