Advertisement
RoundSparrow

Logitech camera - reverse engineering - smack talk 0

Jul 11th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.32 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import org.jivesoftware.smack.Chat;
  7. import org.jivesoftware.smack.ConnectionConfiguration;
  8. import org.jivesoftware.smack.ConnectionListener;
  9. import org.jivesoftware.smack.MessageListener;
  10. import org.jivesoftware.smack.PacketListener;
  11. import org.jivesoftware.smack.Roster;
  12. import org.jivesoftware.smack.RosterEntry;
  13. import org.jivesoftware.smack.SASLAuthentication;
  14. import org.jivesoftware.smack.XMPPConnection;
  15. import org.jivesoftware.smack.XMPPException;
  16. import org.jivesoftware.smack.filter.PacketTypeFilter;
  17. import org.jivesoftware.smack.packet.Message;
  18. import org.jivesoftware.smack.packet.Packet;
  19. import org.jivesoftware.smack.packet.PacketExtension;
  20. import org.jivesoftware.smack.packet.Presence;
  21. import org.jivesoftware.smack.provider.ProviderManager;
  22. import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
  23. import org.jivesoftware.smack.packet.IQ;
  24. import org.jivesoftware.smackx.FormField;
  25. import org.jivesoftware.smackx.pubsub.AccessModel;
  26. import org.jivesoftware.smackx.pubsub.ConfigureForm;
  27. import org.jivesoftware.smackx.pubsub.FormType;
  28. import org.jivesoftware.smackx.pubsub.LeafNode;
  29. import org.jivesoftware.smackx.pubsub.Node;
  30. import org.jivesoftware.smackx.pubsub.PubSubManager;
  31. import org.jivesoftware.smackx.pubsub.PublishModel;
  32. import org.jivesoftware.smackx.pubsub.SubscribeOptionFields;
  33.  
  34.  
  35. /*
  36.  * WARNING: This is a sample of non-working code.
  37.  * Questions and Problems:
  38.  *     1) I am uncertain as to exactly how and where the client (this App) JID should be established.
  39.  *     2) I am uncertain as to exactly when to use server JID or client JID, and if server JID is correct.
  40.  *     3) I am uncertain as to the class structure of PubSub and how to generate the desired captured XML/XMPP content desired to talk to the Logitech Alert cameras.
  41.  *     4) related to #3, createNode is likely being used entirely with incorrect understanding in this code sample - does the "urn:" go here at all?
  42.  *    
  43.  *     This is a class intended to talk to one camera, multiple instances would be used for more than one camera.
  44.  *       It has been hacked into a reverse-engineer console application with many prints and exception blocks.
  45.  */
  46. public class Main implements MessageListener {
  47.  
  48.     public XMPPConnection connection;
  49.     public String targetHostname = "jabber.org";
  50.     public String targetUsername = "admin";
  51.     public String whoknows_Hostname = "server@127.0.0.1/NvrCore";
  52.     public String myClientJID = "";
  53.     public String targetCameraJID = "";
  54.    
  55.      
  56.     public void loginCamera(String userName, String password) throws XMPPException {
  57.         // LearningQ: third ConnectionConfiguration parameter is proxy name? required?
  58.         ConnectionConfiguration conconfig = new ConnectionConfiguration(targetHostname, 5222, targetHostname);
  59.         // set authentication just to have the topic open for debate. Not sure if this line is required with defaults in smack.
  60.         conconfig.setSASLAuthenticationEnabled(true);
  61.         // Enable debugging so we can gather all the connection information we can.
  62.         conconfig.setDebuggerEnabled(true);
  63.         // Set presence to semi-automatic so we can pull trigger as we desire.
  64.          conconfig.setSendPresence(false);
  65.                
  66.         connection = new XMPPConnection(conconfig);
  67.  
  68.         try
  69.         {
  70.             // Do the actual connection step.
  71.             connection.connect();
  72.         }
  73.         catch (Exception e1)
  74.         {
  75.             System.out.println("PLACE0000 Exception on connect");
  76.             e1.printStackTrace();
  77.         }
  78.        
  79.         if (connection.isConnected())
  80.         {
  81.             // Let's do a short sleep just to let some XMPP flow before we invoke more activity.
  82.             try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
  83.             System.out.println("PLACE0001 after post-connect sleep. Camera login next.");
  84.  
  85.             try
  86.             {
  87.                 // Note that smack seems to have some differences in username as to the need for the user@ part.
  88.                 // For the Logitech cameras, the user@ seems to work if included, exception of left off.
  89.                 connection.login(targetUsername + "@" + targetHostname, password, "XMPPCamera_smack");
  90.             } catch (XMPPException e1) {
  91.                 System.out.println("PLACE0002 Exception on login");
  92.                 e1.printStackTrace();
  93.             }
  94.             catch (Exception e2)
  95.             {
  96.                 System.out.println("PLACE0003 Exception on login");
  97.                 e2.printStackTrace();
  98.             }
  99.        
  100.             System.out.println("PLACE0010 isAuthenticated: " + connection.isAuthenticated());
  101.        
  102.             if (connection.isAuthenticated())
  103.             {
  104.                 // Let's do a short sleep just to let some XMPP flow before we invoke more activity.
  105.                 try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
  106.                 System.out.println("PLACE0011 after authenticate sleep, going to set presence to available");
  107.                 setMyPresenceAvailable();
  108.             };
  109.         }
  110.     }
  111.    
  112.     public void setMyPresenceAvailable()
  113.     {
  114.         Presence myPresence = new Presence(Presence.Type.available);
  115.         myPresence.setStatus("Available");
  116.         connection.sendPacket(myPresence);
  117.     }
  118.  
  119.     public void disconnect() {
  120.         connection.disconnect();
  121.     }
  122.  
  123.    
  124.     private PubSubManager psManager1;
  125.     private LeafNode node1;
  126.     private ConnectionListener connectionListener;
  127.     private static Main myInstance;
  128.  
  129.     /*
  130.      * Try to subscribe to settings, as the captured XMPP is clearly using PubSub ;)
  131.      * This needs work, entirely not working code.
  132.      * Written by someone lost in trying to use smack classes to reverse-engineer captured XMPP conversations.
  133.      */
  134.     public void testPubSub1()
  135.     {
  136.         try {
  137.             // LearningQ: This is one of the problem areas.
  138.             // Should we be targeting the pubsub to name? 2nd parameter be whoknows_Hostname ?
  139.             psManager1 = new PubSubManager(connection, targetCameraJID);
  140.         } catch (Exception e1) {
  141.             System.out.println("PLACE0300 Exception in testPubSub1");
  142.             e1.printStackTrace();
  143.         }
  144.        
  145.         try {
  146.             // LearningQ: should we be using getNode and catching the exception as normal behavior - then createNode?
  147.             node1 = (LeafNode) psManager1.createNode("urn:logitech-com:logitech-alert:remote-event:device:alert:settings");
  148.         } catch (Exception e1) {
  149.             System.out.println("PLACE0310 Exception in testPubSub1");
  150.             e1.printStackTrace();
  151.         }
  152.        
  153.         try {
  154.             ConfigureForm form1 = new ConfigureForm(FormType.submit);
  155.             // Logitech Camera, let's try to turn OFF the email alerts
  156.             FormField field1 = new FormField("ALERT EMAILALERTSENABLED");
  157.             field1.setType(FormField.TYPE_HIDDEN);
  158.             field1.addValue("0");
  159.             form1.addField(field1);
  160.            
  161.             node1.sendConfigurationForm(form1);
  162.         } catch (Exception e1) {
  163.             System.out.println("PLACE0320 Exception in testPubSub1");
  164.             e1.printStackTrace();
  165.         }
  166.     }
  167.  
  168.     public void test1(String inTargetHostname) throws XMPPException, IOException {
  169.         targetHostname = inTargetHostname;
  170.         targetUsername = "admin";
  171.        
  172.         // turn on the enhanced debugger
  173.         XMPPConnection.DEBUG_ENABLED = true;
  174.  
  175.         loginCamera(targetUsername, "Logitech");
  176.        
  177.         // Let's do a short sleep just to let some XMPP flow before we invoke more activity.
  178.         try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); }
  179.         System.out.println("PLACE0200 after post-loginCamera sleep.");
  180.        
  181.         if (connection.isConnected())
  182.         {
  183.             if (connection.isAuthenticated())
  184.             {
  185.                 // This is probably entirely wrong and shows how lost in using smack that I am.
  186.                 targetCameraJID = targetUsername + "@" + targetHostname;
  187.  
  188.                 System.out.println("PLACE0201 Calling testPubSub1...");
  189.                 testPubSub1();
  190.                 System.out.println("PLACE0202 after testPubSub1...");
  191.             }
  192.         }
  193.  
  194.         // Let's do a longer sleep to let some XMPP flow before we quit.
  195.         try { Thread.sleep(8500); } catch (InterruptedException e) { e.printStackTrace(); }
  196.         System.out.println("PLACE0290 after post-testPubSub1 attemp sleep.");
  197.          
  198.         System.out.println("PLACE9990 app going to end connection and exit...");
  199.         disconnect();      
  200.     }
  201.    
  202.     public static void main(String args[]) throws XMPPException, IOException {
  203.         // Put your test target Logitech Alert camera IP Address here
  204.         String inTargetHostname = "192.168.4.116";
  205.  
  206.         myInstance = new Main();
  207.         myInstance.test1(inTargetHostname);
  208.         System.exit(0);
  209.     }
  210.    
  211.     @Override
  212.     public void processMessage(Chat inChat, Message inMessage) {
  213.         // TODO Auto-generated method stub
  214.         System.out.println("PLACE0400 incoming chat message " + inChat.toString() + " ::: " + inMessage.toString());
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement