views:

99

answers:

2

If,for example,The socket in my compiled application is designed to connect to 123.456.789.0. How do I check if its connected to 123.456.789.0? Is there a way to do this?

The idea is this:I want to prevent other people editing my program and changing the address to,for example, 127.0.0.1 and make it connect through a proxy.

Is there any function/way/trick to check the address after the socket is connected?

+4  A: 

Use the getpeername function to retrieve the address of the remote host.

If someone edits your program like you mention, they'll probably alter such a check as well though.

nos
+3  A: 

nos's comment about the insecurity of this approach is correct, but incomplete. You wouldn't even need to change the program's code to circumvent your proposed mechanism.

The easiest way around it would be to add an IP alias to one of the machine's network interfaces. Then a program can bind to that interface on the port your program connects to, and the OS's network stack will happily send connections to the attacker's local program, not your remote one.

So, now you say you want to know how to list the computer's interfaces so you can detect this sort of subversion. Your opponent counterattacks, launching your program as a sub-process of theirs after installing a Winsock hook that routes Winsock calls back through the parent process.

We then expect to find you asking how to read the executable code section of a particular DLL loaded into your process space, so you can check that the code is what you expect. Now your opponent drops the Winsock shim, switching to an NDIS layer filter, rewriting packets from your program right before they hit the NIC.

Next we find you looking for someone to tell how to list the drivers installed on a Windows system, so you can check that one of these filters isn't present. Your opponent thinks for about 6 seconds and decides to start screwing with packet routing, selecting one of at least three different attacks I can think of off the top of my head. (No, wait, four.)

I'm not a security expert. Yet, I've spent five minutes on this and already have your security beat seven different ways.

Are you doomed? Maybe, maybe not.

Instead of you coming up with fixes to the risks you can see, better to post a new question saying what it is you're trying to protect, and have the experts comment on risks and possible fixes. (Don't add it here. Your question is already answered, correctly, by nos. This is a different question.)

Security is hard. Expertise counts for far more in that discipline than in most other areas of computer science.

Warren Young
+1 for your comment,however I'm not doomed.I'm using this as an addition protection.Most important parts of my code including winsock are obfuscated with junk code,which is not easy to catch.The concept of doing that check is ,if the address is not valid,then lately,on another stage,the program will crash at random places.Doing such things instead of a stupid message box 'invalid address' will take some more time to my 'opponent'. :)
John
Wait, I'm confused then. Your question said you wanted to prevent someone from running your protocol through a proxy. I outlined seven different ways (and thought of two more after posting) of forcing your traffic through someone else's code -- a proxy -- despite your code checking that the remote peer's address is what you expect. So....WTF? Besides which, security through obscurity never works. An amateur will never win a security fight against a motivated opponent.
Warren Young