C# & Pcap.Net - Forwarding received packets - c#

First I'm using Visual Studio 2012 with C# and the Pcap.Net Library.
I try to to forward packets which I captured before.
What I try to do:
Spoof ARP-Table of my phone.
Redirect the traffic which normally goes to the gateway to my computer.
Log the packets.
Forward them to the gateway.
What I did:
Spoofing ARP-Table -> works fine.
Redirect traffic to my PC -> works fine (logically).
Log the packets to a dumpfile (.pcap) as shown in the tutorial on this site -> works fine (I can open it and read it with wireshark and it looks good).
Forward the packets to the gateway. -> does not work.
I would like to forward them fluently. So what I did was use the "sendBuffer()" function as shown in the tutorial. So I just read in the .pcap file where all the packet information is saved and try to resend it with this "sendBuffer()" function. And of course I use the same adapter to do it.
When I capture the traffic with wireshark I can see that my packets don't even get sent.
(I'm also not sure if it works at the same time while I capture the data to the file. Because the code which should forward them need to read the packets from the file. Isn't there another way?)
My code to forward the packets from the .pcap file (the IDE doesn't give me any error):
It's approximately my code, I don't have it available cause I'm not at home. But should be right.
IList<LivePacketDevice> devices = LivePacketDevice.AllLocalMachine;
PacketDevice selectedOutputDevice = devices[0];
long capLength = new FileInfo(#"E:\CSharp\Pcap\dumpFile.pcap").Length;
bool isSync = true;
OfflinePacketDevice selectedInputDevice = new OfflinePacketDevice(#"E:\CSharp\Pcap\dumpFile.pcap");
using (PacketCommunicator inputCommunicator = selectedInputDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
{
using (PacketCommunicator outputCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000))
{
if (inputCommunicator.DataLink != outputCommunicator.DataLink)
{
tB_Log.Text = tB_Log.Text + Environement.NewLine + "ERROR: Different Datalinks!";
}
using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)capLength))
{
Packet packet;
while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok)
{
sendBuffer.Enqueue(packet);
}
outputCommunicator.Transmit(sendBuffer, isSync);
}
}
}
Thank you very much for helping!

Related

C# GUI to Arduino: Serial Data Not Working

I'm trying to send an integer through the usb connection to my arduino. When using the sketch monitor I can verify that the code works; However, when using the C# .NET GUI, I'm unable to get anything working.
I do know that the data is sending via the LEDs lighting up on the arduino.
I'm typing an RPM into a text box, converting it to 4 bytes (integer) and writing it. Here is the GUI code:
RPMMove = Convert.ToInt32(tbRPM.Text);
byte[] bRPM = BitConverter.GetBytes(RPMMove);
port.Write(bRPM, 0, 4);
On the arduino:
void setup()
{
Serial.begin(9600);
Serial.setTimeout(100);
}
void loop()
{
while (Serial.available() > 0)
{
i = Serial.parseInt();
stepper.setRPM(i); //move motor of other library
//Serial.println(i); //I get the correct integer via the arduino monitor here.
}
}
parseInt and the fact that it works when you type the number into the console both indicate that the device wants the string sent as text. So do not use BitConverter, instead:
byte[] bRPM = Encoding.Ascii.GetBytes($"{RPMMove}\r\n");
You may need to adjust the line ending, naturally using the same one configured in your terminal emulator ("console") should work.

Does anyone know where I can find a Universal Windows Private Network (Client & Server) code Example?

I am brand new to Universal Windows Apps (Win 10). I am trying to port a console application over to UWP that acts as a remote testing and administrative console for a custom Windows Service application. I can not seem to find any solid example code to demonstrate where to place a socket listener in the MainPage.xaml.cs file (or wherever it's supposed to go). I have been successful with porting the MSDN example into a method that serializes a PCL model object with Json and sends it to the server. I just can not seem to handle the listener correctly. I don't think that I am using it in the right place, especially when it comes to the async usage. I am having protocol\port usage errors because it's basically saying that it is already open (I just tossed it in the test method). I would like to deserialize the Json response that is received and use it to populate a List. Here is an example of what is working for me for sending.
private async void Pulse(string target)
{
if (target == null || target == string.Empty)
{
greetingOutput.Text = "No Ip specified";
return;
}
else
{
try
{
Windows.Networking.Sockets.StreamSocket socket = new Windows.Networking.Sockets.StreamSocket();
Windows.Networking.HostName serverHost = new Windows.Networking.HostName(target);
await socket.ConnectAsync(serverHost, serverPort);
Stream streamOut = socket.OutputStream.AsStreamForWrite();
StreamWriter writer = new StreamWriter(streamOut);
HeartBeatPing heartBeatPing = new HeartBeatPing(GetLocalIp(), target);
string msg = JsonConvert.SerializeObject(heartBeatPing);
await writer.WriteLineAsync(msg);
await writer.FlushAsync();
Stream streamIn = socket.InputStream.AsStreamForRead();
StreamReader reader = new StreamReader(streamIn);
string response = await reader.ReadLineAsync();
}
catch (Exception xCeption)
{
greetingOutput.Text += "\n" + xCeption.ToString();
}
}
}
Some of you might notice from the greetingsOutput.text that I have started with the "C# Hello World" example from Microsoft's training site.
I would also like to add that I am not going to be using any HTTP for this because there is going to be some custom encryption and other "things" happening with the Json objects that will require separate ports.
I'm not far enough into my Universal Windows Apps with XAML and C# (Unleashed) books to have even a clue as to what I am doing. I am however well seasoned C# programmer in other platforms such as MVC, Windows Service, Console, and others. I have a solid understand of enterprise class patterns and practices based on my knowledge of "The Gang of Four".
Any help would be greatly appreciated. Thank you.
(https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DatagramSocket)
Here is a sample. There are CPP, js, and cs code in this sample, I've only tested the cs code. Wish this can help you.

Can't get cached items back from memcached?

I just setup memcached on a ubuntu system i have on my network. Didn't change any options so everything is default. I think tried to connect to the server using Enyim. It doesn't fail but when i try to retrieve the items they are always null. I'm not much of a low level won't but i've been able to discern things from wireshark before so i decided to give it a try. I'haven't been able to discern anything but i noticed the first .Store() command i sent actually sent network packets to the correct address. Every .Store() command did absolutely nothing.
Here is my app.config:
I've tried both "Binary" & "Text" Protocols and they did the same thing.
Here is my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
namespace MemCacheTest
{
internal class Program
{
private static void Main(string[] args)
{
//var config = new MemcachedClientConfiguration();
//config.Servers.Add(new IPEndPoint(new IPAddress(new byte[] {10, 0, 0, 1}), 11211));
//config.Protocol = MemcachedProtocol.Binary;
var mc = new MemcachedClient();
for (var i = 0; i < 100; i++)
mc.Store(StoreMode.Set, "Hello", "World");
mc.Store(StoreMode.Set, "MyKey", "Hello World");
Console.WriteLine(mc.Get("MyKey"));
Console.WriteLine("It should have failed!!!");
Console.ReadLine();
}
}
}
Does anyone know whats going on or how i could determine what is wrong? I thought it was strange that i wasn't getting any exceptions so i set an invalid ip address in the config file. Same results.
The short answer: If your service is running check your port, it should be blocked somehow. (did you add an exception for port 11211 in Ubuntu firewall(iptables)?)
telnet 10.0.0.1 11211 If you have a telnet client on your server this will show the port is inaccessible.
Enyim doesn't throw you an error even the port is inaccessible. yes, it's strange.
To add to dasun answer. When you install memcached by default its configured to only listen on the "lo/localhost" interface. Its the only security memcache really has. Even if you try to telnet locally and do specify the lo interface for example:
telnet 10.0.0.1 11211
it will fail. To fix you have to go into the memcached.conf file and comment out
# -l 127.0.0.1
and then restart the service

Using Sockets to transfer Data with MonoDroid

I'm trying to send some information to a server with Android using Monodroid.
The code is as follows:
public void sendSomething()
{
sock = new TcpClient();
sock.Connect(Dns.GetHostAddresses("a.domain.com"), 7777);
String d;
d = "somedata";
StreamWriter w = new StreamWriter(sock.GetStream());
// StreamReader r = new StreamReader(sock.GetStream());
w.WriteLine(d);
w.Flush();
sock.Close();
}
It works fine if I run the exact same routine in a winforms application, but when linked to a button click in monodroid (running on the android virtual device - I'm using the evaluation version) the server will see the connection but no data is received.
Does anybody have any idea why this could be?
(edited to ammend code)
It could be a server issue. E.g. let's assume that:
a) your winform app running on Windows / MS.NET (and not on Mono / Linux or OSX);
b) your server is Windows-based too and does a ReadLine to read sockets
Then the NewLine between the write (Unix \n) and the read (Windows \r\n\) could explain why the server does not report what's being read.
Can you show us how you're reading the data on the server ? (edit your question)

Send a XMPP message to an OpenFire room from the command line

I'm having problems trying to send an XMPP message to a 'Room' in our OpenFire instance. The end result is for our CruiseControl.NET build server to be able to send success/failure messages to the appropriate 'Rooms' as an additional means of notification.
I'm using the Matrix XMPP library to create a Console Application in C# using VS2010. The idea was to create a simple .exe that I can wire up to CCNet and pass a few arguments into as required.
The code below is basically the sample code from the Matrix site/documentation which I have updated to point to a room.
static void Main(string[] args)
{
var xmppClient = new XmppClient
{
XmppDomain = "SERVER",
Username = "davidc",
Password = "*********"
};
xmppClient.OnRosterEnd += delegate
{
xmppClient.Send(new Message
{
To = "roomname#conference.SERVER",
From = "davidc#SERVER",
Type = MessageType.groupchat,
Body = "Just Testing the XMPP SDK"
});
};
xmppClient.Open();
Console.WriteLine("Press return key to exit the application");
Console.ReadLine();
xmppClient.Close();
}
I can send to an individual user (changing the To and Type accordingly) without any problems but changing the code to point to a room ends in silence! Is there some additional 'handshaking' that needs to be done to address a room?
I don't really have to use C# for the solution as long as it will run on a Windows Server.
You'll want to read XEP-0045, "Multi-User Chat". You need to enter the room before sending a message to it. For a quick fix, see section 7.1.1, which shows how to join a room using a simplified (older) protocol:
<presence
to='darkcave#chat.shakespeare.lit/thirdwitch'/>
For the newer protocol, include an extra x tag from section 7.1.2:
<presence
to='darkcave#chat.shakespeare.lit/thirdwitch'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence>
I don't know your library, but you'll want code something like:
xmppClient.Send(new Presence
{
To = "roomname#conference.SERVER/mynick",
});

Categories

Resources