How to interpret wireshark messages and send them with c# - c#

I have a Barco projector that I would like to remote control myself.
Barco has his own tool to do this, but is fully written in java.
When I catch the data being sent to swith to HDMI source with wireshark I get the following:
0000 00 0d 0a 01 2a bc d8 d3 85 95 91 57 08 00 45 00 ....*......W..E.
0010 00 31 51 72 40 00 80 06 00 00 0a 00 00 0d 0a 00 .1Qr#...........
0020 00 3e ef e1 04 01 c2 67 14 b8 9e da c6 b2 50 18 .>.....g......P.
0030 01 00 14 6e 00 00 3a 49 48 44 4d 20 31 20 0d ...n..:IHDM 1 .
0000 00 0d 0a 01 2a bc d8 d3 85 95 91 57 08 00 45 00 ....*......W..E.
0010 00 28 51 73 40 00 80 06 00 00 0a 00 00 0d 0a 00 .(Qs#...........
0020 00 3e ef e1 04 01 c2 67 14 c1 9e da c6 c4 50 10 .>.....g......P.
0030 01 00 14 65 00 00 ...e..
And to switch back to DVI I get:
0000 00 0d 0a 01 2a bc d8 d3 85 95 91 57 08 00 45 00 ....*......W..E.
0010 00 31 53 1e 40 00 80 06 00 00 0a 00 00 0d 0a 00 .1S.#...........
0020 00 3e ef e1 04 01 c2 67 14 c1 9e da c6 c4 50 18 .>.....g......P.
0030 01 00 14 6e 00 00 3a 49 44 56 49 20 31 20 0d ...n..:IDVI 1 .
0000 00 0d 0a 01 2a bc d8 d3 85 95 91 57 08 00 45 00 ....*......W..E.
0010 00 28 53 20 40 00 80 06 00 00 0a 00 00 0d 0a 00 .(S #...........
0020 00 3e ef e1 04 01 c2 67 14 ca 9e da c6 d6 50 10 .>.....g......P.
0030 00 ff 14 65 00 00 ...e..
I've tried some things out in c#, but with little success:
IPAddress beamerIP = new IPAddress(IpToBin("!!Beamer IP!!"));
IPEndPoint ip = new IPEndPoint(beamerIP, 1025);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ip);
Console.WriteLine("Socket connected to " + socket.RemoteEndPoint.ToString());
byte[] msg = Encoding.ASCII.GetBytes(":IDVI 1 .");
byte[] dvi = new byte[] { 0x3a, 0x49, 0x44, 0x56, 0x49, 0x20, 0x31, 0x20, 0x0d };
byte[] hdmi = new byte[] { 0x3a, 0x49, 0x48, 0x44, 0x4d, 0x20, 0x31, 0x20, 0x0d };
int bytesSent = socket.Send(dvi);
Console.WriteLine("Sent {0} bytes.", bytesSent);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
I've also tried sending with PCAP.NET like in this thread, but with little success.
Am I doing something wrong or is there another approach to this?

Turns out I was almost there. This is the code that worked for me:
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Establish the remote endpoint for the socket.
IPAddress beamerIP = new IPAddress(IpToBin("!!Beamer IP!!"));
IPEndPoint ip = new IPEndPoint(beamerIP, 1025);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
socket.Connect(ip);
// Send the data through the socket.
int bytesSent = socket.Send(message); //message => DVI = Encoding.ASCII.GetBytes(":IDVI 1 \r") || HDMI = Encoding.ASCII.GetBytes(":IHDM 1 \r")
// Receive the response from the remote device.
int bytesRec = socket.Receive(bytes);
// Release the socket.
socket.Shutdown(SocketShutdown.Both);
socket.Close();

Related

C# bouncycastle AsymmetricCipherKeyPair to byte array ECDSA

I generate key pair like this.
ECKeyPairGenerator gen = new ECKeyPairGenerator("ECDSA");
SecureRandom secureRandom = new SecureRandom();
Org.BouncyCastle.Asn1.X9.X9ECParameters ecp = Org.BouncyCastle.Asn1.Nist.NistNamedCurves.GetByName("P-256");
ECDomainParameters ecSpec = new ECDomainParameters(ecp.Curve, ecp.G, ecp.N, ecp.H, ecp.GetSeed());
ECKeyGenerationParameters ecgp = new ECKeyGenerationParameters(ecSpec, secureRandom);
gen.Init(ecgp);
AsymmetricCipherKeyPair eckp = gen.GenerateKeyPair();
and I want to convert AsymmetricCipherKeyPair to byte array.
so I add code.
ECPublicKeyParameters ecPub = (ECPublicKeyParameters)eckp.Public;
ECPrivateKeyParameters ecPri = (ECPrivateKeyParameters)eckp.Private;
But I know there were two ways for convert AsymmetricCipherKeyPair to byte array.
first,
byte[] pubs = ecPub.Q.GetEncoded();
Second,
byte[] pubX = ecPub.Q.XCoord.ToBigInteger().ToByteArray();
byte[] pubY = ecPub.Q.YCoord.ToBigInteger().ToByteArray();
The results of both methods are slightly different.
first way, pubs[0] is always 0x04, and it make array length to 65 bytes.
like this
04 F0 9E 70 EB ED 52 4B 56 E8 64 9C 9A D9 1C 97 6F F1 92 86 BA 87 FC F5 AB E4 CC 72 C6 EA 77 FA 0D 30 4C 39 0F 38 BE E3 C7 3E 8B 4D 2F 05 C3 55 3F 78 DB 8E DD 77 DF 24 D4 3B 56 88 33 D7 CB 0B 9E
seconde way, pubX[0] is sometimes 0x00, and it make array length to 65 bytes.
like this
pubX = 00 F0 9E 70 EB ED 52 4B 56 E8 64 9C 9A D9 1C 97 6F F1 92 86 BA 87 FC F5 AB E4 CC 72 C6 EA 77 FA 0D
pubY = 30 4C 39 0F 38 BE E3 C7 3E 8B 4D 2F 05 C3 55 3F 78 DB 8E DD 77 DF 24 D4 3B 56 88 33 D7 CB 0B 9E
Except for the value of index 0, the rest are the same.
Why does this difference occur?
What value do I actually use?
p.s.
I convert private key like this
byte[] pri = ecPri.D.ToByteArray();
Is this the right way?
And private key also has 0x00 on index 0. Why?

BinaryReader EndOfStreamException when BaseStream.Position < BaseStream.Length

While using a BinaryReader, how is it possible that I get an EndOfStreamException on ReadUInt16() when BaseStream.Position is equal to 20 and BaseStream.Length is equal to 174? I can't find anything in the docs to indicate how this can occur.
EndOfStreamException: Failed to read past end of stream.
System.IO.BinaryReader.FillBuffer (Int32 numBytes) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:119)
System.IO.BinaryReader.ReadUInt16 () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:513)
Chunk.SaveChunk () (at Assets/Scripts/Chunk.cs:293)
Here is the exact data I believe to be in the stream. This data was retrieved from the program Hex Editor Neo while Visual Studio was halted at a breakpoint on the line of the exception.
01 00 00 00 00 00 00 00 01 00 a0 00 00 00 00 00
ff ff 00 00 13 00 00 00 00 10 0c 10 02 00 00 0c
00 0b 01 03 01 00 00 0c 03 04 02 0d 02 00 00 0e
03 01 01 0d 01 00 01 0e 03 03 01 09 01 00 01 0e
0c 0f 02 04 02 00 04 0c 03 06 01 01 01 00 04 0c
04 0c 02 0c 02 00 04 0e 04 0a 01 05 01 00 04 0e
09 07 02 03 02 00 0a 0c 03 06 02 01 02 00 0a 0e
03 06 01 01 01 00 0b 0c 00 05 02 03 02 00 0b 0e
00 05 01 03 01 00 0b 0e 09 01 01 01 01 00 0b 0e
0a 05 02 02 02 00 0c 0e 09 04 02 01 02 00 0e 0e
04 02 01 04 01 00 0e 0e 08 02 02 01 02 00
Edit: Code from opening the stream up until the exception.
public void SaveChunk()
{
lock (chunkDataLock)
{
FileStream readStream = File.Open("GameData/chunkdata.bin", FileMode.Open, FileAccess.Read, FileShare.Write);
FileStream writeStream = File.Open("GameData/chunkdata.bin", FileMode.Open, FileAccess.Write, FileShare.Read);
using (BinaryReader reader = new BinaryReader(readStream))
using (BinaryWriter writer = new BinaryWriter(writeStream))
{
//reader.BaseStream.Length == 174 here
if (reader.BaseStream.Length == 0)
{
writer.Write((ushort)0);
writer.Flush();
writer.BaseStream.Position = 0;
reader.BaseStream.Position = 0;
}
// 0, 0, 0
int[] regionPos = GetRegionFromChunkPosition(chunkPosX, chunkPosY, chunkPosZ);
// numRegions = 1
ushort numRegions = reader.ReadUInt16();
writer.BaseStream.Position += 2;
for (int i = 0; i < numRegions; i++)
{
// 0, 0, 0
short regXPos = reader.ReadInt16(), regYPos = reader.ReadInt16(), regZPos = reader.ReadInt16();
// numChunksPosition = 8
long numChunksPosition = reader.BaseStream.Position;
// numChunks = 1
ushort numChunks = reader.ReadUInt16();
// regionLengthPosition = 10
long regionLengthPosition = reader.BaseStream.Position;
// regionLength = 160
uint regionLength = reader.ReadUInt32();
uint newRegionLength = 0;
writer.BaseStream.Position += 12;
// At this point reader and writer both have BaseStream.Position of 14
// True
if (regXPos == regionPos[0] && regYPos == regionPos[1] && regZPos == regionPos[2])
{
bool chunkFound = false;
for (ushort j = 0; j < numChunks; j++)
{
// 0, -1, 0
short xPos = reader.ReadInt16(), yPos = reader.ReadInt16(), zPos = reader.ReadInt16();
writer.BaseStream.Position += 6;
// Exception occurs on this line.
// at this point, reader.BaseStream.Position = 20
// and reader.BaseStream.Length = 174
ushort numVoxelGroups = reader.ReadUInt16();
...
}
...
}
...
}
...
}
}
}

C# Convert a byte string into a byte array

I have a situation where I need to reverse engineer a TCP request. Using wireshark I copy the request (Binary serialized byte array) as a string into VS. I need to deserialize the string to see what information is being passed.
This is what my string looks like:
0000 d4 81 d7 dd 44 37 00 01 45 03 ec ef 08 00 45 00 ....D7..E.....E.
0010 00 5f 91 b4 40 00 40 06 c2 a6 0a d4 ee 47 0a d4 ._..#.#......G..
0020 e2 4e 04 01 e4 9d 75 98 96 de cb 2a 29 25 50 18 .N....u....*)%P.
0030 27 10 fc f1 00 00 43 50 43 52 00 01 19 00 00 00 '.....CPCR......
0040 37 00 00 00 1e 00 00 00 05 01 05 01 3c 00 00 32 7...........<..2
0050 00 02 ff ff 01 00 4d 54 20 52 61 63 6b 00 00 00 ......MT Rack...
0060 01 04 03 02 41 05 01 00 00 00 00 00 00 ....A........
How do I go about de-serializing it to see the contents?
I pasted your string into a file and then ran code below :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
namespace ConsoleApplication7
{
class Program
{
const string FILENAME = #"c:\temp\test.txt";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
string input = "";
List<byte> data = new List<byte>();
while((input = reader.ReadLine()) != null)
{
string byteStr = input.Substring(6, 3 * 16).Trim();
data.AddRange(byteStr.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(x => byte.Parse(x, NumberStyles.HexNumber)));
}
}
}
}

How to Ignore a property from being serialized using BinaryFormatter?

[Serializable]
class DOThis
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Value
{
get
{
if (_name == "Hi")
return "Hey Hi";
else
return "Sorry I dont know you";
}
}
}
I have the above class to be serialized using BinaryFormatter. Below is the serialization code,
DOThis obj = new DOThis();
obj.Name = "Ho";
BinaryFormatter bfm = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bfm.Serialize(ms, obj);
Here how to ignore the property 'Value' from being serialized and also in deserialize, as I can always retrieve 'Value' property using 'Name' property?
You don't have to make any changes to your code: BinaryFormatter only serializes fields, not properties, so it won't serialize Value.
Here's a hex dump of the resulting MemoryStream which shows that only "_name" and "Ho" are serialized:
00 01 00 00 00 FF FF FF FF 01 00 00 00 00 00 00 .....ÿÿÿÿ.......
00 0C 02 00 00 00 3B 44 65 6D 6F 2C 20 56 65 72 ......;Demo, Ver
73 69 6F 6E 3D 31 2E 30 2E 30 2E 30 2C 20 43 75 sion=1.0.0.0, Cu
6C 74 75 72 65 3D 6E 65 75 74 72 61 6C 2C 20 50 lture=neutral, P
75 62 6C 69 63 4B 65 79 54 6F 6B 65 6E 3D 6E 75 ublicKeyToken=nu
6C 6C 05 01 00 00 00 0B 44 65 6D 6F 2E 44 4F 54 ll......Demo.DOT
68 69 73 01 00 00 00 05 5F 6E 61 6D 65 01 02 00 his....._name...
00 00 06 03 00 00 00 02 48 6F 0B ........Ho.
Look at the NonSerializedAttribute.
[Serializable]
class DOThis
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
[NonSerialized()]
public string Value
{
get
{
if (_name == "Hi")
return "Hey Hi";
else
return "Sorry I dont know you";
}
}
}

non string binary code to plain text 16 bit

Hallo,
how to convert same binary data to plain text. I think there a 16 Bytes rows.
Have to be
000006F0 DB 4D D9 94 B7 F0 F9 C9 70 F1 D3 7C E3 EC 65 93 .M......p..|..e.
00000700 18 66 FD 0E C1 B9 78 BE 83 14 B0 E0 76 27 3C 69 .f....x.....v'<i
00000710 5F 18 19 FF 5C AC 15 24 84 CF BC F9 F1 04 56 06 _...\..$......V.
00000720 4A 45 07 6D 8B 9F 96 51 8C E7 FE 98 B7 32 87 F6 JE.m...Q.....2..
00000730 94 0B 3F 09 BB 15 E5 9F D3 B2 4D 40 03 DE 23 B2 ..?.......M#..#.
00000740 84 6C 39 37 15 C6 4D 0E 02 57 0B B2 AC 69 A8 7C .l97..M..W...i.|
00000750 A4 71 D8 DB CF 52 28 10 6C 3C 3E A2 59 B0 CD CF .q...R(.l<>.Y...
00000760 34 6B D9 9D 7E 5A D3 49 32 E5 91 97 2C AC 40 F2 4k..~Z.I2...,.#.
00000770 8C 15 25 92 07 DE A7 B2 72 22 84 6B CD 33 56 D5 ..%.....r".k.3V.
00000780 72 16 78 5F AD DB FC 12 AE 7D BB 80 AA AE DE 8A r.x_.....}......
Is right now
I tried Encoding.ASCII.GetString. My Text length is not always the same like above and there a some special character in my version why ?
0200 43 93 87 31 D1 13 50 C2 73 9A 74 12 72 65 1C 23 C??1?P?s?tre#
0220 1D D3 35 6D A9 24 2C EC 70 CC 73 1A 03 14 4D D1 ?5m?$,?p?sM?
0240 13 42 69 2A 2C 45 07 DF A2 D4 72 CB 17 CB 4E A9 Bi*,E???r??N?
0260 F1 1B 53 58 53 1B BF 6C 80 39 B4 66 DB 27 C9 6C ?SXS?l?9?f?'?l
0280 F3 18 BF 44 A0 2C 4F 84 BA 65 E8 A7 EB 32 B0 30 ??D?,O??e???2?0
02a0 B9 19 39 13 70 B8 A2 10 18 FD 26 4D 23 9B 44 7C ?9p???&M#?D|
02c0 90 8F F9 B4 16 D6 63 C2 22 0D 7A FD 3E 6A C1 55 ?????c?"z?>j?U
02e0 E8 BA A6 B2 55 D1 2E 95 D1 83 22 C0 CB 64 00 AA ????U?.???"??d?
0300 E3 21 49 A0 E2 B2 DC 0E 36 C2 04 4B 97 C7 58 35 ?!I????6?K??X5
A other thing is the Textbox in WPF. There are no returns (\n) in my text. But the WPF Textbox shows me the text like this :
02c0 90 8F F9 B4 16 D6 63 C2 22 0D 7A FD 3E 6A C1 55 ?????c?"z?>j?U
02e0 E8 BA A6 B2 55 D1 2E 95 D1 83 22 C0 CB 64 00 AA ????U?.
???"??d?
0300 E3 21 49 A0 E2 B2 DC 0E 36 C2 04 4B 97 C7 58 35 ?!I????6?K??X5
ASCII only covers a subset of the possible values you can contain in a byte, so anything outside of the ASCII range will appear as any variety of junk, depending on the default character set of your machine.
You simply need to replace the byte values below 0x20 and above 0x7F with some visible character. (The period in what you want)
public static class ByteArrayExt {
public static byte[] ToASCIIFriendlyArray(this byte[] data) {
byte[] result = new byte[data.Length];
for (int i=0;i<data.Length;i++)
result[i] = b >= 0x20 || b < 0x79 ? b : '.';
return result;
}
}
Encoding.ASCII.GetString(data.ToASCIIFriendlyArray());

Categories