Request service name via TCP socket - php

I'm making a simple port scanner in PHP, and am trying to figure out how I'd request the name of the service I'm connecting to, similar to how Nmap is able to discover services.
I'm using fsockopen() to open the socket, so I'd use fwrite($socket, "WHAT SERVICE AM I COMMUNICATING WITH?") to ask the question, then listen using fgets($socket).
How do I ask the service what it is?

It's hard coded into the program what the different ports are for. The remote applications don't report them. There are tons of lists on the internet that provide this information.
Same thing for looking at standard services and responses from similar services on different OSs. That's how nmap guesses what OS it's talking to.
Here is a decent starting point.
Here is the source file with the port mappings for nmap.
The scary part is the code that tells which versions and such is running. That's here and I'm glad I don't have to write this :\
Last one, pretty sure this is the file that says how to guess which OS a remote computer is: here

Related

Why doesn't stream_socket_server block the port from additional sockets on Windows?

I have a reactphp script opening multiple ports for listening. Code comes down to trying to open a socket on port x, if occupied choose port+1.
I've found that I can open multiple sockets for the same port without error message which makes the above method of finding a "free" port invalid:
var_dump($s1 = stream_socket_server("tcp://127.0.0.1:7777", $errno, $errstr));
var_dump($s2 = stream_socket_server("tcp://127.0.0.1:7777", $errno, $errstr));
Both calls return a resource with different id. Why does this happen and is it possible that a port already has an open socket from the same process (without keeping book on the sockets)?
PS.: Opening two sockets from different processes fails as expected.
Related questions: Multiple UDP Sockets to listen for specific source on the same port
Update
See https://3v4l.org/6eWY1, it seems the decribed behaviour applies to Windows versions of PHP only.
Have a look at this technique to test if a port is open.
I get the same results with your code and this technique works for me to identify if a port was already open by the same process.
That could be an option if you don't mind the overhead.
FYI I do not know PHP and I primarily use Linux, so your mileage may vary. It seems though that I might help you with some tcp knowledge. If you already know this, forgive me and ignore my answer ;)
So I don't know how you connected to your server socket and how your server handled the connection, but if it is programmed correctly your server will not occupy the port, hence blocking future connections. You can of course do that if you want to.
Normally when you create a server, you want to have 1 known port, so that multiple clients can connect to it (like port 80 for http). The server uses 'listen' to listen for connections, followed by an 'accept' and finally a 'close'. The accept makes sure that you can get multiple connections via your server port.
Btw:
you can find a free port by opening a socket on port 0.
you can handle multiple sockets via 'select'
a nice book to read up on sockets is Working with Tcp sockets by Jesse Storimer (FYI I don't have stocks, only the book ;). But there are many intros to socket programming if your google fu is with you.

"No connection could be made because the target machine actively refused it." from external client

I have a small home server (Ubuntu+XAMPP) and 2 PHP scripts: server.php and client.php, which both communicate to each other via sockets.
When I run server.php / client.php on the same machine (localhost), it works fine. Also, when I run server.php on the server, and client.php on the same server but from other local PC (i.e. local_server_ip/client.php), all works fine as well.
However, when I run server.php on the server and client.php on the other PC on the same network (replacing localhost with local_server_ip_addr in the client.php script), it fails with the actively refused connection error.
All necessary ports are forwarded in the router. I guess it is kind of security block on XAMPP/Linux and can be eliminated by some configuration file. I replaced Deny from all in the New XAMPP security concept with Allow from all in httpd-xamp.conf file, but it still fails.
Any help would be much appreciated.
(PS: server/client scripts examples taken from http://i-novice.net/sokety-v-php/ )
UPD: Have modified port 8080 (the one is dedicated for sockets in my system) to XXXXX. All works fine!
In the event that this happens dependably, it actually implies that the machine exists however that it has no administrations listening on the predetermined port, or there is a firewall ceasing you.
In the event that it happens periodically - you utilized "now and then" - and retrying succeeds, it is likely in light of the fact that the server has a full 'overabundance'.
When you are holding up to be acknowledged on a listening attachment, you are put in a build-up. This overabundance is limited and short - estimations of 1, 2 or 3 are not bizarre - thus the OS may be not able to line your solicitation for the "acknowledge" to devour.
The build-up is a parameter on the listen capacity - all dialects and stages have essentially the same API in such manner, even the C# one. This parameter is frequently configurable in the event that you control the server, and is likely read from a few settings document or the registry. Research how to arrange your server.
In the event that you composed the server, you may have substantial preparing in the acknowledge of your attachment, and this can be better moved to a different specialist string so your acknowledge is constantly prepared to get associations. There are different structural engineering decisions you can investigate that alleviate lining up customers and handling them successively.
Notwithstanding whether you can build the server build-up, you do need retry rationale in your customer code to adapt to this issue - as even with a long build-up the server may be getting bunches of different demands on that port around then.
There is an uncommon probability where a NAT switch would give this blunder if its ports for mappings be depleted. I think we can toss this probability as a lot of a long shot however, since the switch has 64K concurrent associations with the same destination location/port before weariness.

How to implement Websocket (RFC 6455) in my server?

I have searched it in Google and here in stackoverflow there are some questions about it.
The problem is that many of them are old, so I guess that they use older and deprecated protocols instead of RFC 6455. I don't know if it is because of that or if I am doing something wrong, but when I try it it doesn't work.
So...
If I understand well, I can implement Websockets with aproppiate client (Javascript) and server (PHP) code, without needing to configure my Apache server. I am right?
Why the hell all examples I have found require me to go to command line and do
php -q C:\path\to\file\Websocket\Server.php
In the implementations I have found, there are a default server and port. Must I change them to the server (localhost) and port which I use for normal webpages? Or should I tell my server to listen to another port and use it for websocket communication?
Where can I find a good PHP implementation of Websockets (RFC 6455)?
Yes, that is correct.
Because the Websocket server doesn't use your web daemon to serve data. In this case, you're writing a server daemon completely in PHP.
You need to choose a port, since you're not using the web daemon to serve your websockets. Any port that's not in use will do.
Try Ratchet (main site here). We've been using the dev-master branch for a couple months in production now and we haven't had any problems. It's the only solution we found that out of the box will work with both Firefox clients and iPhones.

HTML5 - WebSocket in shared hosting

I used to have a small chat app(which was almost working), that uses PHP, jQuery and MySQL. The volume of users is very small (only my friends uses it). I used long polling method for this.
And now, I am thinking about using HTML5 Websockets for this, because it is a lot more efficient. And also most of my friends are using Google Chrome(which already supports HTML5). I have gone through some tutorials that talks about HTML5 websockets. And I have downloaded the phpWebSocket from github. I have gone through the code. But the readme file says that the PHP page that listens to incoming connections should be run using "PHP -q" from commandline. So, I have searched what this "q" flag would do. And I found that it runs the page in quiet mode. So, when I run this in quiet mode what is happened ? It would run endlessly ? Will this running process affect the system resources ?
This PHP page should run the entire time. Then only the connections could be accepted. Isn't it ?
I am having a shared hosting package with HostGator. And they allow cron jobs too. And my present chat app(that uses long polling method) inserts all the messages to database. When the user polls, it would search for any new messages from the database and then output them (if any).
So, I am bit stuck here. :(
It should be run from the command line because as you suspected, it is intended to run endlessly. It binds to a socket on the server and listens for incoming connections. It can't be reliably run from the browser.
The "-q" option tells it not to output any browser headers such as X-Powered-By: PHP or Content-Type: text/html
It will consume as much memory as PHP requires as long as its running. Your memory footprint on startup with no clients will vary between configurations. The more connected clients, the more cpu, memory and socket descriptors you will use. It uses select so it is efficient socket handling.
Also, since you're on shared hosting, you probably won't be able to use it because your user will most likely not have the ability to bind to a port and listen for connections.
As you can see in the demo, the URL to connect the WebSocket to is ws://localhost:12345/websocket/server.php. Unless you have a webserver capable of using WebSockets, you will have to run something like phpWebSocket that acts as a server and listens on a port other than 80.
Hope that helps.
The shared hosting package for HostGator does not allow clients to bind to local ports for incoming. This might be part of the problem.
http://support.hostgator.com/articles/pre-sales-policies/socket-connections

VPN Connection through PHP or some other server-side scripting?

Ok, let me first start off by saying that I've only ever dealt with VPN access through windows by setting up a connection through the control panel. It's pretty simple since everything is pretty much a point-and-click setup.
I'm now working on a project where I need to access a computer cloud on a private network (there is no public IP directly to the cloud so it can only be accessed when I'm on the network). My project involves a website that needs to access that cloud "somehow". Because of my lack of experience/knowledge with VPN's through the command line and how to programmatically connect to a VPN, I've hit a mild obstacle that I'm hoping someone here can help me with.
What kind of server side scripting would I do to get a VPN connection up and running? The website is being hosted on a linux machine. Is their a "default" VPN utility under linux that I can call through PHP to establish a connection? If not, I would really appreciate any and all suggestions on how to circumvent this little problem of mine. FYI the VPN uses PPTP.
Looks like there is a little bit to getting it set up from a shell rather than the GUI, but here are some references that will hopefully help you out.
If you set up the connection and connect it, when your PHP script attempts to communicate with an IP address on the remote side of the connection, it will go through the PPTP connection. Having the connection always open is probably better than having to your PHP script connect every time it needs to do something.
http://ubuntuforums.org/showthread.php?t=1443735 - An easy PPTP client setup
http://pptpclient.sourceforge.net/ - Client you can install and configure
http://www.cyberciti.biz/tips/howto-configure-ubuntu-fedora-linux-pptp-client.html - Walk through of setting up PPTP using the linux PPTP network manager
Hopefully those will help you out a bit. The first one looks like it may be worth trying first.

Categories