1

Possible Duplicate:
How to get a specific socket and close it

I want to know a way to close an existing socket connection from a different process (In Windows). I don't have handle to the socket, I only know the port number. I think I may need to write kernel level code to do this. Any references in C#, or C++?

Community
  • 1
  • 1
ActiveLeo
  • 11
  • 2
  • may also be helpful: http://stackoverflow.com/questions/1337392/how-can-i-enumerate-sockets-on-a-given-windows-process – IAbstract Aug 29 '12 at 18:43

1 Answers1

2

There are many ways to do that.

One of them is to inject a dll into the target process which will wait, for a packet or an other signal, to be sent by your main process and then close the socket.

enter image description here

Or you could just send a packet to the already open socket that will trigger an exception and therefore the deletion of the socket but I doubt that's going to be any easier than injecting a DLL.

Or maybe you could send a FIN signal to the open socket.

Kev
  • 118,037
  • 53
  • 300
  • 385
Christian
  • 1,492
  • 3
  • 16
  • 19
  • Thanks Christian. Do you have a code sample for that? Please note that I cannot modify the source code of the target process. You mean the injection will occur on run time, right? – ActiveLeo Aug 29 '12 at 18:55
  • 1
    @ActiveLeo I could provide with the code for the injection. But you'll, most probably, have to reverse engineer the target process to find the address of the socket. This post might help if you'll attempt to send packets to an already open socket http://stackoverflow.com/questions/11876083/using-a-third-party-application-to-send-data-to-an-already-open-tcp-connection – Christian Aug 29 '12 at 18:58
  • I know the port number, but what is the socket address? – ActiveLeo Aug 29 '12 at 19:01
  • @ActiveLeo Download WPE Pro and choose the target process. If it tries to send anything, the packets along with the socket number will be captured. Or you could try **netstat -socket**. – Christian Aug 29 '12 at 19:02
  • @ActiveLeo Remember. The socket isn't always the same! – Christian Aug 29 '12 at 19:06