VB.NET/WIN32/WMI Best way to monitor opening network connections - vb.net

I'm developing my own antimalware application. I have successfully implemented file system and process monitoring and now I'm looking for "some kind of firewall feature". I have already done some research and found iphlpapi.dll, netstat -o parsing.
Exactly, I would like to receive event with following data, when new IP (TCP/UDP/whatever) connection is opened.
Remote IP address
Protocol (TCP/UDP/...)
Port
ID of process which opened connection
I don't want to run timer which still checks netstat output, because it's not effective. I need to suspend process (I know how to suspend process) fastly, when connection to malicious IP is detected.

I suspect that besides writing a driver the only way to do this is Event Tracing for Windows (ETW). Check .NET System.Diagnostics.Eventing
About ETW https://msdn.microsoft.com/en-us/library/windows/desktop/aa363668(v=vs.85).aspx
In the ETW you can start from the following three providers:
Microsoft-Windows-NDIS-PacketCapture
Microsoft-Windows-TCPIP
Microsoft-Windows-Networking-Correlation

Related

How can I test that I've successfully connected to all *five* channels (shell, iopub, hb, stdin, control) of an IPython kernel via SSH when using 2FA?

I've set up a remote kernel running through SSH to which I connect using my Spyder IDE, and have just added 2-factor authentication (2FA) on the SSH connections using Duo .
Now when I attempt to connect, I get 4 different push notifications, and once I approve some or all of them, Spyder connects and gives me the IPython prompt; and for each attempt below I did approve all 4.
On my first attempt, it didn't display a result when testing with something like 2+2
On my second attempt, everything appeared to be working fine.
However, I am aware that there are 5 channels involved (shell, iopub, hb, stdin, control) as I can see on this Jupyter client doc page.
Is there any way I can, once connected to the remote kernel, test each of the individual 5 channels and check that they are all working properly?
And can you think of a reason why I would receive 4 push notifications rather than 5? Is it possible that one of the channels isn't used or connected to later on-demand or something like that?
UPDATE: After doing a netstat on the server side, I can see that the control channel is not connected, but the other four (shell, iopub, hb, stdin) are. Still unsure what I miss out on by not using the control channel, and whether Spyder provides the same features the control channel by other means; this page says:
Control: This channel is identical to Shell, but operates on a separate socket to avoid queueing behind execution requests. The control channel is used for shutdown and restart messages, as well as for debugging messages.
For a smoother user experience, we recommend running the control channel in a separate thread from the shell channel, so that e.g. shutdown or debug messages can be processed immediately without waiting for a long-running shell message to be finished processing (such as an expensive execute request).

Simulating loss of broker publisher connectivity in ActiveMQ

I wish to run an experiment in which the publisher loses connection with the broker and then enqueues messages in its own queue and then when it regains connectivity it sends all its queued messages to the broker. How can I I do this since if I call close connection, I can no longer send(raises an exception). A trick that I can think of is to use a network of two brokers and simulate the above by breaking the connection between the two brokers. Is there an API call that I can use to do the above?
This is very much like facebook messenger or whatsapp acting as a publisher and enqueuing our to-send messages if we are offline and sending them once we are connected.
There is plenty of solutions you could use to break the connection in order to test, here is a non-comprehensive list :
Make a script that can set/unset a firewall rule on your environement blocking the connection port
If you are working with VMs, you can suspend/resume the one running Activemq, you can even automate it with tools like vagrant (vagrant suspend, then vagrant up)
Tweak the connection manualy accessing the activemq jmx
Develop an activemq plugin able to trash connections on demand (or maybe there is one ?)
Now in order to have the behavior you wish to obtain there is two options :
1) Make sure your connection is failover so it can be reestablished, and store your message on disk before sending them with your producer.
2)Produce to a local broker embbeded in your app, and connect this one to the remote broker.

telnet catch ctrl+c server

I have written a socket-server like here. If I type ctrl+c in telnet, the server don't do anything now. I want to catch it like signal(SIGINT,SIG_IGN)
How can I do?
The telnet program catches the CTRL-C character and sends it as a single byte (\x03) down the TCP connection to the other side. It's up to the receiving program to decide what to do with that byte.
In the case of it being received by a "telnet daemon" intending to provide console-like interactivity via a pseudo-terminal, that combination generates a SIGINT to the process running under it, usually a shell.
So, to answer your question, you can either process the received \x03 character and internally generate a SIGINT or you can run your entire program as a process spawned and controlled by telnetd under a pseudo-terminal.
The telnet client (the process that recieves the SIGINT) should process it: either handle it locally (eg: terminate, or re-issue a prompt), or send it to the server as IAC something (BRK?) and/or out-of band data.
If you want to pass the interrupt to the server, Google for "telnet IAC" will probably get you started.

How to debug ActiveMQ client?

I'm a fairly new user of ActiveMQ and I'm looking for a way to get detailed debug information on the client side of a queue connection. My problem is this: I have a server that is sending a message through a queue to a client. Using the admin web page associated with the broker, I can verify the following: the queue was created, there is a consumer associated with the queue, the message has been enqueued, the message has been dispatched, the dispatched queue size is 1, the message has not been dequeued. This setup was working yesterday but mysteriously stopped working today even though I did a restart of the activemq service. The log file at /var/log/activemq.log does not contain any useful information.
At this point I'm stumped; I'm assuming that there is some sort of problem with the configuration, but it hasn't changed since yesterday. Does anybody have a suggestion about what my next step should be?
Turn on debug (or even trace) logging in the broker first of all in conf/log4j.properties.
log4j.logger.org.apache.activemq=DEBUG
restart the broker and re-run your scenario. The logging will hopefully provide you with some information.
Jconsole is also a useful tool to monitor the running broker.
Does your client use any message filters?
You can also enable remote debugging and then connect with an IDE.
To start remote debugging execute
$ ACTIVEMQ_DEBUG=true bin/activemq
and then start a remote debugger to connect to port 5005

How to find a locally available UDP port with unix Sockets API

I have an application where I have potentially many processes must send a message to one process. I figured the most expedient way to do this would be to just send a UDP datagram.
When opening a socket, I need to specify which port to listen to. As I just want to fire and forget the UDP datagram, I'd like to just have the OS pick an available port. I'd rather not have to hunt for one, especially as many instances of my process may be running. Is there a way I can tell the OS to just pick an available port (or better yet, not even open a listening socket) to fire and forget my UDP datagram?
Yes. Specify 0 as the port. The OS will pick an available port for you.
Answering the "Problem" rather than the "Question"
If all the processes are operating on the same PC, you might want to look into shared files or some other means of communications other than a networking stack.
I'd suggest you explore the options in Boost's Interprocess library
http://www.boost.org/doc/libs/1_37_0/doc/html/interprocess.html
Quick guide here:
http://www.boost.org/doc/libs/1_37_0/doc/html/interprocess/quick_guide.html
Specifically, I suggest you use a named pipe or shared memory between your processes.