Simulating TCP data from another device

This almost feels too simple to even mention it. But I guess it doesn’t hurt putting it our there. Someone might hit it in a desperate Google hunt.
So this is the deal. You have a device on your network that controls another device by means of sending out simple TCP payloads.
Now you want to perform the same tasks with your program to get rid of the device. This is how you do it.

First, you wanna fire up your old friend Wireshark. Now set up the device you want to emulate to send the data to the IP of your machine. Now you can start a Live Capture in Wireshark (Ctrl+E). Now you command your device to send the message by whatever means needed (like pressing the correct button on the device). Now stop the Live Capture in Wireshark (Ctrl+E) and add a filter to get the packet you need.

Let’s assume the devices IP address is 10.0.0.1 and your IP address is 10.0.0.2. Now the correct filter would be

ip.src == 10.0.0.1 && ip.dst == 10.0.0.2

Now, hopefully you’ll see a single packet. In the center panel of Wireshark there should be a Data segment. This is what we want. Right-click it and select Copy -> Bytes (Hex Stream). You’ll get something like:

4e656574732c4b2c0100

(In case you’re interested, this is the payload when you press the first button on a Neets Control EU Standard device.)

Now let’s put this into code. The below example already has 2 payloads that would signal the target device to be turned on or off. For this 2 packets have been captured and can now be selected via the command line. The implementation is straight-forward.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace Neets_Control {
  class Program {

    private static byte[] WORKLOAD_ON   = new byte[] { 0x4e, 0x65, 0x65, 0x74, 0x73, 0x2c, 0x4b, 0x2c, 0x01, 0x00 };
    private static byte[] WORKLOAD_OFF  = new byte[] { 0x4e, 0x65, 0x65, 0x74, 0x73, 0x2c, 0x4b, 0x2c, 0x02, 0x00 };

    private static string TARGET_HOST   = "10.11.110.11";
    private static int    TARGET_PORT   = 5009;

    static void Main( string[] args ) {
      
      TcpClient sender = new TcpClient( TARGET_HOST, TARGET_PORT );
      NetworkStream stream = sender.GetStream();
      if( args[ 0 ] == "on" ) {
        Console.WriteLine( "Switching on" );
        stream.Write( WORKLOAD_ON, 0, WORKLOAD_ON.Length );

      } else if( args[ 0 ] == "off" ) {
        Console.WriteLine( "Switching off" );
        stream.Write( WORKLOAD_OFF, 0, WORKLOAD_OFF.Length );

      } else {
        Console.WriteLine( "Missing parameter (on/off)." );
      }

      stream.Close();
      sender.Close();

    }
  }
}

So, yeah, I can now just refer back to my introduction. Possibly this wasn’t event worth mentioning ;)

Leave a Reply

You must be logged in to post a comment.