SNMP.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import android.content.SharedPreferences;
  3. import org.snmp4j.TransportMapping;
  4. import org.snmp4j.agent.BaseAgent;
  5. import org.snmp4j.agent.CommandProcessor;
  6. import org.snmp4j.agent.DuplicateRegistrationException;
  7. import org.snmp4j.agent.MOGroup;
  8. import org.snmp4j.agent.ManagedObject;
  9. import org.snmp4j.agent.mo.MOAccessImpl;
  10. import org.snmp4j.agent.mo.MOTableRow;
  11. import org.snmp4j.agent.mo.snmp.RowStatus;
  12. import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
  13. import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
  14. import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
  15. import org.snmp4j.agent.mo.snmp.StorageType;
  16. import org.snmp4j.agent.mo.snmp.VacmMIB;
  17. import org.snmp4j.agent.security.MutableVACM;
  18. import org.snmp4j.mp.MPv3;
  19. import org.snmp4j.security.SecurityLevel;
  20. import org.snmp4j.security.SecurityModel;
  21. import org.snmp4j.security.USM;
  22. import org.snmp4j.smi.Address;
  23. import org.snmp4j.smi.Gauge32;
  24. import org.snmp4j.smi.GenericAddress;
  25. import org.snmp4j.smi.Integer32;
  26. import org.snmp4j.smi.OID;
  27. import org.snmp4j.smi.OctetString;
  28. import org.snmp4j.smi.SMIConstants;
  29. import org.snmp4j.smi.Variable;
  30. import org.snmp4j.transport.TransportMappings;
  31. import java.io.File;
  32. import java.io.IOException;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import de.tudarmstadt.informatik.hostage.Listener;
  36. import de.tudarmstadt.informatik.hostage.protocol.SNMPUtils.MOTableBuilder;
  37. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  38. /**
  39. * Created by root on 06.07.15.
  40. */
  41. //public class SNMP extends BaseAgent implements Protocol {
  42. public class SNMP implements Protocol {
  43. @Override
  44. public int getPort() {
  45. return 161;
  46. }
  47. @Override
  48. public boolean isClosed() {
  49. return false;
  50. }
  51. @Override
  52. public boolean isSecure() {
  53. return false;
  54. }
  55. @Override
  56. public List<Packet> processMessage(Packet requestPacket) {
  57. List<Packet> responsePackets = new ArrayList<Packet>();
  58. responsePackets.add(requestPacket);
  59. try {
  60. //setUp();
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. return responsePackets;
  65. }
  66. @Override
  67. public TALK_FIRST whoTalksFirst() {
  68. return TALK_FIRST.CLIENT;
  69. }
  70. @Override
  71. public String toString() {
  72. return "SNMP";
  73. }
  74. private String address;
  75. private String externalIP;
  76. SharedPreferences pref1;
  77. private Listener sListener;
  78. /* public SNMP(String address) throws IOException {
  79. super(new File("conf.agent"), new File("bootCounter.agent"),
  80. new CommandProcessor(
  81. new OctetString(MPv3.createLocalEngineID())));
  82. this.address = address;
  83. }*/
  84. /* @Override
  85. protected void addCommunities(SnmpCommunityMIB snmpCommunityMIB) {
  86. Variable[] com2sec = new Variable[]{
  87. new OctetString("public"),
  88. new OctetString("cpublic"), // security name
  89. getAgent().getContextEngineID(), // local engine ID
  90. new OctetString("public"), // default context name
  91. new OctetString(), // transport tag
  92. new Integer32(StorageType.nonVolatile), // storage type
  93. new Integer32(RowStatus.active) // row status
  94. };
  95. MOTableRow row = snmpCommunityMIB.getSnmpCommunityEntry().createRow(
  96. new OctetString("public2public").toSubIndex(true), com2sec);
  97. snmpCommunityMIB.getSnmpCommunityEntry().addRow((SnmpCommunityMIB.SnmpCommunityEntryRow) row);
  98. }
  99. @Override
  100. protected void addNotificationTargets(SnmpTargetMIB snmpTargetMIB, SnmpNotificationMIB snmpNotificationMIB) {
  101. }*/
  102. /*@Override
  103. protected void addUsmUser(USM usm) {
  104. }
  105. @Override
  106. protected void addViews(VacmMIB vacmMIB) {
  107. vacmMIB.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c, new OctetString("cpublic"), new OctetString("v1v2group"), StorageType.nonVolatile);
  108. vacmMIB.addAccess(new OctetString("v1v2group"), new OctetString("public"),
  109. SecurityModel.SECURITY_MODEL_ANY, SecurityLevel.NOAUTH_NOPRIV,
  110. MutableVACM.VACM_MATCH_EXACT, new OctetString("fullReadView"),
  111. new OctetString("fullWriteView"), new OctetString("fullNotifyView"), StorageType.nonVolatile);
  112. vacmMIB.addViewTreeFamily(new OctetString("fullReadView"), new org.snmp4j.smi.OID("1.3"),
  113. new OctetString(), VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
  114. }
  115. @Override
  116. protected void registerManagedObjects() {
  117. }
  118. @Override
  119. protected void unregisterManagedObjects() {
  120. }
  121. public SNMP() {
  122. super("");
  123. }
  124. public void start() throws IOException{
  125. init();
  126. try {
  127. setUp();
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. }
  131. // This method reads some old config from a file and causes
  132. // unexpected behavior.
  133. // loadConfig(ImportModes.REPLACE_CREATE);
  134. addShutdownHook();
  135. getServer().addContext(new OctetString("public"));
  136. finishInit();
  137. run();
  138. sendColdStartNotification();
  139. }
  140. */
  141. /* protected void initTransportMappings() throws IOException {
  142. transportMappings = new TransportMapping[1];
  143. Address addr = GenericAddress.parse(address);
  144. TransportMapping tm = TransportMappings.getInstance()
  145. .createTransportMapping(addr);
  146. transportMappings[0] = tm;
  147. }
  148. public void registerManagedObject(ManagedObject mo) {
  149. try {
  150. server.register(mo, null);
  151. } catch (DuplicateRegistrationException ex) {
  152. throw new RuntimeException(ex);
  153. }
  154. }*/
  155. /* public void unregisterManagedObject(MOGroup moGroup) {
  156. moGroup.unregisterMOs(server, getContext(moGroup));
  157. }
  158. // standard in RFC-1213
  159. static final OID interfacesTable = new OID(".1.3.6.1.2.1.2.2.1");
  160. public static void setUp() throws Exception {
  161. SNMP agent = new SNMP("0.0.0.0/161");
  162. agent.start();
  163. MOTableBuilder builder = new MOTableBuilder(interfacesTable)
  164. .addColumnType(SMIConstants.SYNTAX_INTEGER, MOAccessImpl.ACCESS_READ_ONLY)
  165. .addColumnType(SMIConstants.SYNTAX_OCTET_STRING,MOAccessImpl.ACCESS_READ_ONLY)
  166. .addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)
  167. .addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)
  168. .addColumnType(SMIConstants.SYNTAX_GAUGE32,MOAccessImpl.ACCESS_READ_ONLY)
  169. .addColumnType(SMIConstants.SYNTAX_OCTET_STRING,MOAccessImpl.ACCESS_READ_ONLY)
  170. .addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)
  171. .addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)
  172. .addRowValue(new Integer32(1))
  173. .addRowValue(new OctetString("loopback"))
  174. .addRowValue(new Integer32(24))
  175. .addRowValue(new Integer32(1500))
  176. .addRowValue(new Gauge32(10000000))
  177. .addRowValue(new OctetString("00:00:00:00:01"))
  178. .addRowValue(new Integer32(1500))
  179. .addRowValue(new Integer32(1500))
  180. //next row
  181. .addRowValue(new Integer32(2))
  182. .addRowValue(new OctetString("eth0"))
  183. .addRowValue(new Integer32(24))
  184. .addRowValue(new Integer32(1500))
  185. .addRowValue(new Gauge32(10000000))
  186. .addRowValue(new OctetString("00:00:00:00:02"))
  187. .addRowValue(new Integer32(1500))
  188. .addRowValue(new Integer32(1500));
  189. agent.registerManagedObject(builder.build());
  190. // Setup the client to use our newly started agent
  191. //client = new SimpleSnmpClient("udp:127.0.0.1/2001");
  192. }
  193. */
  194. }