PHP server raw packet listening - php

I need to make a server on php that would write me raw packets (full, not some data parts) send to it... is it possible? Setting out listen port and ip would assume TCP/IP, and won't receive full raw packet... any ideas on how to do it right?
What I basically need is to display full TCP/IP packet with all headers. ALL!

Instead of opening a TCP socket, you can open a raw socket.
http://php.net/socket-create
Instead of SOCK_STREAM use SOCK_RAW.
If you are into linux and you are trying WIFI hacks, install aircrack-ng, setup airmon-ng and listen through this interface (non-blocking benefits) using something like ngrep mon0.
ngrep tho can be used to watch any interface, e.g. eth0 (see its manpage).

Related

Nodemcu Lua ESP8266 not sending TCP sockets using IP address

I have recently bought a NodeMcu Lua (Lolin) and I am making some tests with it.
What I have tried to do is send a TCP packet to a php script I have on my server, If I try to send the packet using port and hostname it works, but if I try with IP it doesn't, is this a bug or what?
This is just a test because what I actually need to do is use this board to send TCP packets to a php script I have on my pc (so in the same network) running with XAMPP.
Why the board doesn't send any packet using IP but does typing the hostname?
Also what addresses I need to use to send packets from the board to my computer (both in the php script that acts as a server and the board which acts as a client)?
This is the LUA code I am using for the board:
wifi.setmode(wifi.STATION)
wifi.sta.config("Alice-49289348", "mypassword")
wifi.sta.connect()
Disp = "D1"
Port = 9863
HostIP = "81.139.206.12"
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, c) print(c) end )
conn:on("connection", function(conn, c)
print("Connected")
conn:send(Disp)
end )
conn:on("sent", function(conn, c)
print("Sent: " .. Disp)
conn:close()
end )
conn:connect(Port,HostIP)
print("Done")
I have changed some values like IP address, wifi, password etc for privacy.
It seems that you can obtain feedback using "reconnection", "disconnection" events.
https://nodemcu.readthedocs.io/en/master/en/modules/net/#netsocketon
Also, this function may help to verify the correctness of ip.
https://nodemcu.readthedocs.io/en/master/en/modules/net/#netsocketdns

PHP Sockets on Linux - Free tcp port immediately after/when php exits via command?

I have a nginx server running a php script 24/7 that receives messages via sockets and stores them using mysql.
Sometimes I have to stop it running for a reason or another and I need a way to have it restart as soon as possible in order to avoid losing messages.
I use this script (.sh) to stop and run the process again:
pkill -f socketserver.php
#<Line that frees the tcp port>
/usr/bin/php /var/www/webroot/ROOT/socketserver.php #uses tcp port: 20491
If I don't use the command that frees the tcp port, I have (and will have) to wait something between 2 and 5 minutes until the OS perceives there is no process using that tcp port and frees/releases it.
Should I use: fuser, tcpkill, what?
I prefer things that are already installed on the ubuntu server 14.04
Thank you.
You cannot forcibly "free" a TCP port. To remove the obligatory wait time, the socket server should set the SO_REUSEADDR socket option when it creates the listening socket.
This way you'll still lose messages if the server is busy when you restart it. A better solution is doing what other people do: have a "master" process create and bind the listening socket, and delegate accepting connections and working on them to a child process. Most reasons you have for restarting, like reloading config, can be done by restarting the child only, while the listening socket stays open for connections.
Building on #Joni's answer, you'll want to use the socket_set_option() function before binding your socket (http://php.net/manual/en/function.socket-set-option.php - and yes, this code is shamelessly copy/pasted from that page):
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}
Once you use this, subsequent restarts of the script should work fine, without the need to manually free the socket.

socket_bind() unable to bind address

I'm trying to create a Web Socket Server for a small project I am working on.
I have set up the server (located at home) and have port forwarded 80 and 22. I read this tutorial: here
And whenever I ssh into my server to run "startDarmon.php" I get the following error:
PHP Warning: socket_bind(): unable to bind address [98]: Address already in use in
/var/www/server/socket.class.php on line 48
2013-02-23 14:15:38 System: Socket bound to localhost:8000.
2013-02-23 14:15:38 System: Start listening on Socket
This is what i think is preventing my client from connecting to the server. So in the startDarmon.php file I have:
$WebSocket = new socketWebSocket('MY_IP_NOT_LAN_IP',8000);
And inside my client file, I have:
var host = "ws://MY_IP_NOT_LAN:8000/server/startDaemon.php";
Does anyone have any suggestions to why this is not allowing me to establish a connection?
I'm guessing the issue is on the Linux server.
Run the netstat -a -p -n command under root (e.g. with sudo) to understand which process is using that port. Then perhaps do a setsockopt(2) with SO_REUSEADDR
(see socket(7) for more).
The TCP protocol has some specified delays in minutes (eg keepalive, etc etc...). See e.g. tcp(7)
I do suggest reading a good book on Linux system programming like Advanced Linux Programming and perhaps some material on network programming.
You can reuse an address using the setting below:
if ( ! socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1))
{
echo socket_strerror(socket_last_error($sock));
exit;
}
Another application is already using the port that you are trying to use.You can run
lsof -i:port_no
to make the port free.

Cannot connect to mysql with 127.0.0.1

With the following code I can connect to mysql:
mysql_connect("localhost","username","");
But if I change localhost to 127.0.0.1 I get the following error:
Can't connect to MySQL server on '127.0.0.1' (13)
Why doesn't it work with 127.0.0.1?
localhost is special cased and uses UNIX sockets instead of TCP/IP. 127.0.0.1 doesn't get that special handling.
See the documentation:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.
If it doesn't work when you use TCP/IP then the database probably isn't listening on the network. This is generally a good thing as it enhances security (not that listening on 127.0.0.1 exposes any problems, but listening on all interfaces gives more opportunity for attacks).
If you really want to allow connections via the network, then see skip-networking.
have you got an entry in your hosts file mapping 127.0.0.7 to localhost?
Do you have more than 1 mysql servers installed/running on your system? If so, please specify the port number of the mysql server you are trying to access like 127.0.0.1:3306,127.0.0.1:8889 etc.
If you do not know whether there are any other mysql server instances running on your system also, please specify the port.
You will be able to access it when you add the privileges for 'root'#'127.0.0.1' in the "USER_PRIVILEGES" table in the "information_schema" database
You might also try disabling SELINUX

Access raw Apache webserver request

I intend to design a web gps tracking application. the gps transmits data using TCP (no HTTP headers) on port 7070 (which I intented to change to 80). I know the protocol for communication between the GPS tracker and client, however i am stuck as i cannot intercept the datapacket on webserver.
Since application is in development stage and me being a hobbyist, I cannot afford a dedicated web host server and thus get access to php-cli interface for socket programming.
is there any way i can circumvent the need for php-cli and intercept the raw tcp packet.
Thanks
Simply have a dedicated PHP script listening on port 7070, which you can accomplish with fsockopen(). You don't want to have your GPS sending directly to port 80 when Apache's already listening on port 80. Apache'll see a non-HTTP set of data come in and ignore the request completely.
$handle = fsockopen('localhost', 7070, $errno, $errstr);
if (!$handle) {
die("Couldn't bind to socket (err $errno): $errstr");
}
while($data = fgets($handle)) {
... process gps data ...
}
would be the very simplest basic form of this.

Categories