Bläddra i källkod

Fixed issues with ModBus communication. Fixed issue with S7Comm attack detection.Added methods in Managed Objects for SNMP

Shreyas Srinivasa 8 år sedan
förälder
incheckning
77047ca728

+ 1 - 1
assets/payload/redirect-ports.sh

@@ -12,7 +12,7 @@ length=${#protocol[@]} # count protocol elements
 # for (( i=0; i<$length; i++ ))
 #for i in `seq 0 9` # fix for android's annoyingly limited bash
 
-for i in 0 1 2 3 4 5 6 7 8 9 10 11 12# another fix for devices missing the seq command
+for i in 1 2 3 4 5 6 7 8 9 10 11 12 13# another fix for devices missing the seq command
 
 do
 	# echo ${protocol[$i]} ${origin[$i]} ${destination[$i]} # debug

+ 25 - 0
src/de/tudarmstadt/informatik/hostage/Listener.java

@@ -23,6 +23,7 @@ import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
 import de.tudarmstadt.informatik.hostage.net.MyServerSocketFactory;
 import de.tudarmstadt.informatik.hostage.protocol.SMB;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol;
+import de.tudarmstadt.informatik.hostage.protocol.SNMP;
 import de.tudarmstadt.informatik.hostage.protocol.SSLProtocol;
 import de.tudarmstadt.informatik.hostage.system.Device;
 
@@ -132,6 +133,7 @@ public class Listener implements Runnable {
 	@Override
 	public void run() {
         if(protocol.toString().equals("SMB")) return;
+		if(protocol.toString().equals("SNMP")) return;
 
 		while (!thread.isInterrupted()) {
 			addHandler();
@@ -166,6 +168,24 @@ public class Listener implements Runnable {
             ((SMB) protocol).initialize(this);
         }
 
+		else {
+			if (protocol.toString().equals("SNMP")) {
+
+				if (!Device.isPortRedirectionAvailable()) {
+					return false;
+				}
+				if (Device.isPorthackInstalled()) {
+				/*
+				Currently the port binder is the preferred method for creating sockets.
+				If it installed, we can't use iptables to create UDP sockets.
+				@see MyServerSocketFactory
+				 */
+					return false;
+				}
+				((SNMP) protocol).start();
+			}
+		}
+
 		try {
 			server = new MyServerSocketFactory().createServerSocket(port);
 			if (server == null)
@@ -188,6 +208,10 @@ public class Listener implements Runnable {
 		try {
             if(protocol.toString().equals("SMB")){
                 ((SMB) protocol).stop();
+
+			if(protocol.toString().equals("SNMP")){
+				((SNMP)protocol).stop();
+			}
             }
 
 			server.close();
@@ -280,6 +304,7 @@ public class Listener implements Runnable {
 	 */
 	private void startHandler(Socket client) throws Exception {
 		handlers.add(newInstance(service, this, protocol.toString().equals("CIFS") ? protocol : protocol.getClass().newInstance(), client));
+		//handlers.add(newInstance(service, this, protocol.toString().equals("SNMP") ? protocol : protocol.getClass().newInstance(), client));
 	}
 
 	/**

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

@@ -105,7 +105,7 @@ public class MODBUS implements Protocol {
             request = requestPacket.getBytes();
 
 
-            getRequestType(request);
+           // getRequestType(request);
 
             responsePackets=processRequest(request,getRequestType(request));
             System.out.println(responsePackets);

+ 15 - 4
src/de/tudarmstadt/informatik/hostage/protocol/S7COMM.java

@@ -1,5 +1,6 @@
 package de.tudarmstadt.informatik.hostage.protocol;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import de.tudarmstadt.informatik.hostage.wrapper.Packet;
@@ -26,13 +27,23 @@ public class S7COMM implements Protocol {
     @Override
     public List<Packet> processMessage(Packet requestPacket) {
 
-        byte[] request = requestPacket.getBytes();
+        List<Packet> responsePackets = new ArrayList<Packet>();
+
+        byte[] request = null;
+        if (requestPacket != null) {
+            request = requestPacket.getBytes();
+
+
+            //getRequestType(request);
+
+            responsePackets.add(new Packet(request,"")); // Response packets have to be studied yet
+            System.out.println(responsePackets);
+
 
-        for(int i=0; i<=request.length;i++) {
-            System.out.println(request[i]);
         }
 
-        return null;
+
+        return responsePackets;
     }
 
     @Override

+ 51 - 16
src/de/tudarmstadt/informatik/hostage/protocol/SNMP.java

@@ -1,9 +1,15 @@
 package de.tudarmstadt.informatik.hostage.protocol;
 
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+
 import java.io.File;
 import java.io.IOException;
 import java.util.List;
 
+import de.tudarmstadt.informatik.hostage.Hostage;
+import de.tudarmstadt.informatik.hostage.Listener;
+import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.wrapper.Packet;
 
 import org.alfresco.jlan.server.auth.spnego.OID;
@@ -62,13 +68,19 @@ public class SNMP extends BaseAgent implements Protocol {
     }
 
     @Override
-    public String toString(){
+    public String toString() {
         return "SNMP";
     }
 
 
     private String address;
 
+    private String externalIP;
+
+    SharedPreferences pref1;
+
+    private Listener sListener;
+
     public SNMP(String address) throws IOException {
 
         super(new File("conf.agent"), new File("bootCounter.agent"),
@@ -104,18 +116,18 @@ public class SNMP extends BaseAgent implements Protocol {
         vacmMIB.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c, new OctetString("cpublic"), new OctetString("v1v2group"), StorageType.nonVolatile);
 
         vacmMIB.addAccess(new OctetString("v1v2group"), new OctetString("public"),
-        SecurityModel.SECURITY_MODEL_ANY, SecurityLevel.NOAUTH_NOPRIV,
-        MutableVACM.VACM_MATCH_EXACT, new OctetString("fullReadView"),
-        new OctetString("fullWriteView"), new OctetString("fullNotifyView"), StorageType.nonVolatile);
+                SecurityModel.SECURITY_MODEL_ANY, SecurityLevel.NOAUTH_NOPRIV,
+                MutableVACM.VACM_MATCH_EXACT, new OctetString("fullReadView"),
+                new OctetString("fullWriteView"), new OctetString("fullNotifyView"), StorageType.nonVolatile);
 
         vacmMIB.addViewTreeFamily(new OctetString("fullReadView"), new org.snmp4j.smi.OID("1.3"),
-        new OctetString(), VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
+                new OctetString(), VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
     }
 
     @Override
     protected void addCommunities(SnmpCommunityMIB snmpCommunityMIB) {
 
-        Variable[] com2sec = new Variable[] { new OctetString("public"),
+        Variable[] com2sec = new Variable[]{new OctetString("public"),
 
                 new OctetString("cpublic"), // security name
 
@@ -133,31 +145,37 @@ public class SNMP extends BaseAgent implements Protocol {
 
         MOTableRow row = snmpCommunityMIB.getSnmpCommunityEntry().createRow(
 
-        new OctetString("public2public").toSubIndex(true), com2sec);
+                new OctetString("public2public").toSubIndex(true), com2sec);
 
         snmpCommunityMIB.getSnmpCommunityEntry().addRow((SnmpCommunityMIB.SnmpCommunityEntryRow) row);
     }
 
-    public void start() throws IOException {
+    public void start() {
 
-        init();
+        try {
+            init();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
         // This method reads some old config from a file and causes
         // unexpected behavior.
         // loadConfig(ImportModes.REPLACE_CREATE);
         addShutdownHook();
         getServer().addContext(new OctetString("public"));
+
+
         finishInit();
         run();
         sendColdStartNotification();
     }
 
 
-    protected void initTransportMappings() throws IOException {
+    protected void initTransportMappings() {
         transportMappings = new TransportMapping[1];
         Address addr = GenericAddress.parse(address);
 
         TransportMapping tm = TransportMappings.getInstance()
-        .createTransportMapping(addr);
+                .createTransportMapping(addr);
         transportMappings[0] = tm;
     }
 
@@ -165,19 +183,36 @@ public class SNMP extends BaseAgent implements Protocol {
     public void registerManagedObject(ManagedObject mo) {
         try {
             server.register(mo, null);
-            } catch (DuplicateRegistrationException ex) {
+        } catch (DuplicateRegistrationException ex) {
 
             throw new RuntimeException(ex);
-              }
+        }
 
     }
 
 
-    public void unregisterManagedObject(MOGroup moGroup){
-        moGroup.unregisterMOs(server,getContext(moGroup));
+    public void unregisterManagedObject(MOGroup moGroup) {
+        moGroup.unregisterMOs(server, getContext(moGroup));
     }
 
 
+    //Listner for SNMP
+    public Listener getListener() {
+        return sListener;
 
+    }
 
-}
+
+    public void initialize(Listener sListener) {
+
+        this.sListener = sListener;
+
+     //   Hostage service = sListener.getService();
+      //  pref1 = PreferenceManager.getDefaultSharedPreferences(service);
+      //  agent = new SNMP();
+      //  externalIP = connInfo.getString(service.getString(R.string.connection_info_external_ip), null);
+
+
+
+    }
+}