PHPSeclib Proxy send username and password as arguments - php

Im using PHPSECLIB to send a file and a XML to a SFTP server.
In this case the server im trying to reach is outside my work network.
To connect to the internet outside we have a proxy to do that.
What i need to do is configure the proxy of this connection to the one i need.
EDIT --
I have the following code, how can i pass the username and password of my proxy ?
$proxyHost = '******'
$fsock = fsockopen($proxyHost, $proxyPort);
$address = '*****';
$port = '*****';
$request = "CONNECT $address:$port HTTP/1.0\r\nContent-Length: 0\r\n\r\n";
if(fputs($fsock, $request) != strlen($request)) {
exit("premature termination");
}
$response = fgets($fsock);
$sftp = new SFTP($fsock);
.......

Quoting https://github.com/phpseclib/phpseclib/issues/1339#issuecomment-462224179:
With authorization:
$fsock = fsockopen('127.0.0.1', 80, $errno, $errstr, 1);
if (!$fsock) {
echo $errstr; exit;
}
fputs($fsock, "CONNECT website.com:22 HTTP/1.0\r\n");
fputs($fsock, "Proxy-Authorization: Basic " . base64_encode('user:pass') . "\r\n");
fputs($fsock, "\r\n");
while ($line = fgets($fsock, 1024)) {
if ($line == "\r\n") {
break;
}
//echo $line;
}
$ssh = new Net_SSH2($fsock);
$ssh->login('user', 'pass');
echo $ssh->exec('ls -latr');
If that doesn't work then run the script and tell me what the headers you get back are. Digest authentication is more of a PITA then Basic but it's not impossible.
More info on how authorization works with HTTP proxies:
https://www.rfc-editor.org/rfc/rfc7235#section-4.3

Related

How do I implement proxy authentication with fsockopen in PHP?

I want to use a proxy with authentication to my below code to communicate and get data from whois servers.
My code is as follows:
$whoisserver = "whois.verisign-grs.com";
$port = 43;
$timeout = 10;
$fp = #fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die("Socket Error " . $errno . " - " . $errstr);
fputs($fp, $domain . "\r\n");
$out = "";
while(!feof($fp)){
$out .= fgets($fp);
}
fclose($fp);
In the above I want to use a username and password to authenticate my proxy.
What do I change in the code above to achieve my desired outcome?

PHP - Connect to sftp from private key through socks5 proxy

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.

Register user in ejabberd with xmpphp

I have connected ejabberd xmpp server with xmpphp library. Its working fine. I need to create a user in ejabberd xmpp server using xmpphp library.
So i have added following two functions in XMPPHP/XMPP.php file :
public function register($username, $password = null){
if (!isset($password)) $password = $this->genRandomString(15);
$id = 'reg_' . $this->getID();
$xml = "<iq type='set' id='$id'>
<query xmlns='jabber:iq:register'>
<username>" . $username . "</username>
<password>" . $password . "</password>
<email></email>
<name></name>
</query>
</iq>";
$this->addIdHandler($id, 'register_new_user_handler');
$this->send($xml);
}
protected function register_new_user_handler($xml){
switch ($xml->attrs['type']) {
case 'error':
$this->event('new_user_registered', 'error');
break;
case 'result':
$query = $xml->sub('query');
$username='';
$password='';
if(!is_array($query->subs)) {
foreach ($query->sub as $key => $value) {
switch ($value->name) {
case 'username':
$username = $value->data;
break;
case 'password':
$password = $value->data;
break;
}
}
}
$this->event('new_user_registered', array('jid' => $username . "#{$this->server}", 'password' => $password));
default:
$this->event('new_user_registered', 'default');
}
}
and m calling the above functions in sendmessage_example.php as follows :
<?php
// activate full error reporting
//error_reporting(E_ALL & E_STRICT);
include 'XMPPHP/XMPP.php';
$conn = new XMPPHP_XMPP('serverhost', 5222, 'admin#localhost', 'password', 'xmpphp', 'localhost', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);
try {
$conn->connect();
$conn->processUntil('session_start');
$conn->presence();
$conn->register('uname', 'pass');
$conn->message('vaiju#localhost', 'This is a test message!');
$conn->disconnect();
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}
I have manually added a user called vaiju. I'm able to connect it and m getting a message in my pidgin client. But user registration is not working properly.
You could do like this. tested and working with me.
define('JABBER_REST_HOST','localhost:5285');
define('JABBER_REST_URL','http://localhost:5285/rest');
$request = "register ketan13 localhost ketan13";
$jabberResponse = sendRESTRequest(JABBER_REST_URL, $request);
echo '<pre>'; print_r($jabberResponse);
function sendRESTRequest ($url, $request) {
// Create a stream context so that we can POST the REST request to $url
$context = stream_context_create (array ('http' => array ('method' => 'POST'
,'header' => "Host: ".JABBER_REST_HOST."\nContent-Type: text/html; charset=utf-8\nContent-Length: ".strlen($request)
,'content' => $request)));
// Use file_get_contents for PHP 5+ otherwise use fopen, fread, fclose
if (version_compare(PHP_VERSION, '5.0.0', '>=')) {
$result = file_get_contents($url, false, $context);
} else {
// This is the PHP4 workaround which is slightly less elegant
// Suppress fopen warnings, otherwise they interfere with the page headers
$fp = #fopen($url, 'r', false, $context);
$result = '';
// Only proceed if we have a file handle, otherwise we enter an infinite loop
if ($fp) {
while(!feof($fp)) {
$result .= fread($fp, 4096);
}
fclose($fp);
}
}
return $result;
}

PHP Websocket client + ssl

There is a PHP Websocket client: https://github.com/symbiose/php-websocket-client repository. And it works fine for not-secured connection.
What I need is to connect to a websocket server from php code through secure connection. To implement this task I took the code above and modified it a bit. The connect() method of WebSocketClient now looks like this in my code:
public function connect()
{
$root = $this;
if ($this->getPort() == 443) {
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);
$client = stream_socket_client("tls://{$this->getHost()}:{$this->getPort()}", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
} else {
$client = stream_socket_client("tcp://{$this->getHost()}:{$this->getPort()}", $errno, $errstr);
}
if (!$client) {
throw new RuntimeException('Cannot connect to socket ([#'.$errno.'] '.$errstr.')');
}
$this->setSocket(new Connection($client, $this->getLoop()));
$this->getSocket()->on('data', function ($data) use ($root) {
$data = $root->parseIncomingRaw($data);
$root->parseData($data);
});
$this->getSocket()->write($this->createHeader());
return $this;
}
As a result I almost managed to connect to a server. Websocket server saw my connection, replied that everything is ok and wrote it to the log.
But unfortunately, the library doesn't understand, that it has successfully connected to the server.
This "if":
if (base64_encode(pack('H*', sha1($this->key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'))) === $response['Sec-Websocket-Accept']) {
$this->connected = true;
}
never gets executed. And so I can't establish the connection.
I can also confirm, that if we use 80 port - everything works like a charm.
So if you have any ideas on why this can happen, i"d really really appreciate them. Cause I've already ran out of ideas.
Best regards,
Kostya

Apple Push Notification Service connection, through proxy, timed out with stream_socket_client

I'm trying to connect APNS, but i need to pass through a proxy, here the connection test code:
if (!extension_loaded('openssl')) {
exit("need openssl");
}
$http = array();
$http['http']['proxy'] = 'tcp://proxy.net:8080';
$http['http']['request_fulluri'] = true;
$ssl = array();
$ssl['ssl']['local_cert'] = 'ck.pem';
$ssl['ssl']['passphrase'] = 'passphrase';
$opts = array_merge($http,$ssl);
$context = stream_context_create($opts);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $context);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
The problem is that I have always a timeout error, like if i make a direct connection to apple gateway, like if options in stream_context_creat were ognored.
OpenSSL and Socket support are enabled, and port 2195 is open.
Any ideas?
Edit 1:
Trying connect to proxy only it works
$fp = stream_socket_client('tcp://proxy-dmz.pgol.net:8080', $err, $errstr, 60, STREAM_CLIENT_CONNECT);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected!' . PHP_EOL;
Edit 2:
And a script like this (found in some forum) seems to work... i'm stuck
$ip = "tcp://proxy.net"; // proxy IP
$port = "8080"; // proxy port
$url = "http://search.yahoo.com/search?p=test";
$request = "GET $url HTTP/1.0\r\nHost:www.yahoo.com:80\r\n\r\n";
$fp = fsockopen($ip,$port); // connect to proxy
fputs($fp, $request);
$data="";
while (!feof($fp)) $data.=fgets($fp,64000);
fclose($fp);
print $data;

Categories