I want to send something to my connected clients over another php script. But when I use the function "send", clientlist is null.
Can you help me about this please?
Socket.php:
$clientlist = array();
function run() {
global $clientlist;
set_time_limit(0);
$address = '127.0.0.1';
$port = 80;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, 0, $port) or die('Could not bind to address');
socket_listen($sock);
printf("Listening...\r\n");
while (true) {
$client = socket_accept($sock);
$input = socket_read($client, 1024000);
$clientlist[] = $client;
}
}
function send($msg) {
global $clientlist;
printf("Count: " . count($clientlist) . "\r\n");
socket_write($clientlist[0], "Hey");
}
Msg.php:
include("socket.php");
send($_GET['msg']);
Related
I'm trying to connect to sftp with rsa private key auth, through proxy.
use phpseclib\Net\SFTP;
use phpseclib\Crypt\RSA;
$proxyHost = 'proxy-host';
$proxyPort = 1080;
$fsock = fsockopen($proxyHost, $proxyPort);
$host = 'sftp.hostname.com';
$login = 'sftp_login';
$privatekey = file_get_contents('sftp_private.ppk');
$password = new RSA();
$password->loadKey($privatekey);
$request = "CONNECT $host:22 HTTP/1.0\r\nContent-Length: 0\r\n\r\n";
if(fputs($fsock, $request) != strlen($request)) {
exit("premature termination");
}
while ($line = fgets($fsock, 1024)) {
if ($line == "\r\n") {
break;
}
//echo $line;
}
$sftp = new SFTP($fsock);
if (!$sftp->login($login, $password)) {
exit('Login Failed');
}
I get the "Login Failed" exit.
Thanks
Quoting
https://github.com/phpseclib/phpseclib/issues/1460 :
Port 1080 is typically used for SOCKS5 proxies - not HTTP proxies.
Assuming you're using a SOCKS5 proxy then something like this should
work:
// SSH connection info
$port = 22;
$address = 'localhost';
// SOCKS5 connection info
$fsock = fsockopen('127.0.0.1', 1080, $errno, $errstr, 1);
if (!$fsock) {
throw new \Exception($errstr);
}
$port = pack('n', $port);
$address = chr(strlen($address)) . $address;
$request = "\5\1\0";
if (fwrite($fsock, $request) != strlen($request)) {
throw new \Exception('Premature termination');
}
$response = fread($fsock, 2);
if ($response != "\5\0") {
throw new \Exception('Unsupported protocol or unsupported method');
}
$request = "\5\1\0\3$address$port";
if (fwrite($fsock, $request) != strlen($request)) {
throw new \Exception('Premature termination');
}
$response = fread($fsock, strlen($address) + 6);
if (substr($response, 0, 2) != "\5\0") {
echo bin2hex($response) . "\n";
throw new \Exception("Unsupported protocol or connection refused");
}
$ssh = new SSH2($fsock);
$ssh->login('username', 'password');
echo $ssh->exec('ls -latr');
That said, SOCKS5 has a lot more bells and whistles then this sample
code currently accommodates. Really, what'd be good is a full on
SOCKS5 class.
Hope it will help someone else.
When the WebSocket handshake is done I receive a PHPSESSID different from the one in the browser, why is this?
Client code for connecting:
websocket = new WebSocket("ws://192.168.0.109:9000/php_servers/socketserver1/socketserver.php");
Server code for reading header:
<?php
include "../serverfunctions.php";
include "eventfunctions.php";
$host = '192.168.0.109';
$port = '9000';
$null = NULL;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, 0, $port);
socket_listen($socket);
$clients = array($socket);
while (true)
{
$changed = $clients;
socket_select($changed, $null, $null, 0, 10);
if (in_array($socket, $changed))
{
$socket_new = socket_accept($socket);
$clients[] = $socket_new;
$header = socket_read($socket_new, 1024);
I am writing PHP socket server, displayed here is error causing portion. I find out socket_recv() is causing problem, it only lets one computer connect. However, if I comment out socket_recv then its working fine. But I have to receive data also in socket server. Help me find out solution. Please also point out any wrong with code. JQuery part is working fine, hence didn't print it here.
<?php
set_time_limit(0);
$host = '172.28.4.5';
$port = 10000;
$null = NULL;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, $host, $port);
socket_listen($socket);
$clients = array($socket);
while(true) {
$new_socket = socket_accept($socket);//Accepting new connection/socket/client if any
$clients[] = $new_socket;//Adding the new client/socket/connection to client array
$header = socket_read($new_socket, 1024);
perform_handshaking($header, $new_socket, $host, $port);
/* If I want to notify if new connection is established**/
socket_getpeername($new_socket, $ip);
$message = "Welcome to WebSocket $ip";
$array = array(
'message' => $message
);
$message = mask(json_encode($array));
write_to_socket($message);
$found = array_search($socket, $clients);
unset($clients["$found"]);
//Going through each client
foreach($clients as $client) {
//Getting messages with loop how many packages for each client has
while(socket_recv($client, $buf, 1024, 0) >= 1) {
$array = json_decode(unmask($buf));
print_r($array);
}
}
}
}
?>
it seems that
the code is wrong
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, $host, $port);
socket_listen($socket);
//$clients = array($socket); <---- here
while(true) {
it seems that you add the listened socket to array , you should not receive and data from the listened socket.
I am working on an UDP spoofer written in PHP and I wanted to know how can I forge those packets. Any help is appreciated. Thank you!
The following function will create a socket in PHP with a port number. Depending on what you are spoofing, you will need to modify the 'message'. In the example below is in clear text but often applications transmit this in the form of binary information which will need to be reserve engineered to spoof it.
function sendUDP($host, $msg, $timeout = 1) {
/* ICMP ping packet with a pre-calculated checksum */
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
socket_connect($socket, $host, 8000); //Set up to send to port 8000, change to any port
socket_send($socket, $msg, strLen($msg), 0);
$buf = "";
$from = "";
$port = 0;
#socket_recvfrom($socket , $buf , 24 , 0 , $from , $port );
socket_close($socket);
return $buf;
}
just read those code:
server.php
<?php
//error_reporting( E_ALL );
set_time_limit( 0 );
ob_implicit_flush();
$socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
if ( $socket === false ) {
echo "socket_create() failed:reason:" . socket_strerror( socket_last_error() ) . "\n";
}
$ok = socket_bind( $socket, '202.85.218.133', 11109 );
if ( $ok === false ) {
echo "socket_bind() failed:reason:" . socket_strerror( socket_last_error( $socket ) );
}
while ( true ) {
$from = "";
$port = 0;
socket_recvfrom( $socket, $buf,1024, 0, $from, $port );
echo $buf;
usleep( 1000 );
}
?>
client.php
<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$msg = 'hello';
$len = strlen($msg);
socket_sendto($sock, $msg, $len, 0, '202.85.218.133', 11109);
socket_close($sock);
?>
I have the following :
$host = "192.168.0.117";
$port = 777;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not createsocket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
The variable result gives me an error:
unable to listen on socket [0]: An invalid argument was supplied
You need to bind a socket to (address, port) pair first and only then call listen on it.
$host = '192.168.0.117';
$port = '777';
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($socket, $adddress, $port) or die("Could not connect");
socket_listen($socket);
$client_connection = socket_accept($socket);
$input = socket_read($client_connection, 1024);
$output = ereg_replace('[\t\n\r]','', $input).chr(0);
//display output
socket_write($client_connection, $output);
socket_close($client_connection);
socket_close($socket);