PHP - WiFi Network Id and passphrase to hexadecimal key - php

To configure manually a WiFi network, I need the hexadecimal key.
In Ubuntu, I can obtain it with this comand:
wpa_passphrase network passphrase
And the result is:
network={
ssid="network"
#psk="passphrase"
psk=72feda58f99812cd6a4a075047270e361e3ae18f8cb191eb8d55ac07f928a466
}
Then.. How can I obtain the psk with PHP?
EDIT: I do this:
<?php
$fp = fopen("data.txt", "w+");
if(!$fp) die ("Errore nell'apertura del file");
exec("wpa_passphrase network passphrase",$output);
$conf = "";
for($i=0;$i<5;$i++)
$conf .= $output[$i]."";
fwrite($fp,$conf);
fclose($fp);
exec("sudo cp data.txt /etc/wpa_supplicant.conf"); //this doesn't work!
echo "ok<br>".$conf
?>

I've solved, this is the code:
<?php
$fp = fopen("data.txt", "w+");
if(!$fp) die ("Errore nell'apertura del file");
exec("wpa_passphrase network passphrase",$output);
$conf = "";
for($i=0;$i<5;$i++)
$conf .= $output[$i]."\n";
fwrite($fp,$conf);
fclose($fp);
exec("sudo cp data.txt /etc/wpa_supplicant.conf");
echo "1";
?>
But, to execute sudo command, I must add "www-data" user into sudoers file. I did this following this reply

In case anyone is still interested in this: I solved this using plain PHP by reading through the source of wpa_passphrase and comparing the result to the Wireshark PSK tool.
function wpa_passphrase($ssid, $passphrase) {
$bin = hash_pbkdf2('sha1', $passphrase, $ssid, 4096, 32, true);
return bin2hex($bin);
}
$psk = wpa_passphrase('network', 'passphrase');
Requires PHP >= 5.5

Related

Sending a hex command to Device using Php and COMPort (USB to RS232)

HI I have posted a lot of question regarding this but it is different from this one. It's like a continuation or rather other part question.
I have a card vending device (CVD), Now my laptop has no serial port so I used the USB to connect to the device.
My cable is, USB to RS232. Now I have downloaded or rather given to me by a friend an exe which send a command to device on a click of a button. But note that this is an exe only. No Source code.
I'm now creating my own program to send the data command to device on a button click as well.
Here's some facts:
1. Im using Php
2. I'm need to send a hex command of 42
3. is the command for cardout
Here's what I've got so far.
exec("mode COM15 BAUD=9600 PARITY=none data=8 stop=1 xon=off");
$fp = #fopen('COM2', "w+");
if (!$fp) {
echo "Not open";
} else {
sleep(3);
echo "Open";
$str = "01";
$str = pack("H*",$str);
fwrite($fp, $str);
fputs($fp, $str);
$str2 = "2A";
$str2 = pack("H*",$str2);
fwrite($fp, $str2);
fputs($fp,$str2);
$buff = fread($fp, 10);
echo ">> ".$buff." <<";
//fclose($fp);
}
But it is just returning Open>> <<
Try with a
print_r($buff);
It will show you the type of variable and all data it contains and maybe continue working on how to show that variable.

PHP - ssh2_exec and stream_select( ) error

When I try to use ssh2_exec to run a python script on a remote server, and try to make it running under un-blocking model. An error shows up:
stream_select(): cannot represent a stream of type SSH2 Channel as a
select()able descriptor
My code:
$ssh_connection = ssh2_connect('server ip address',22);
ssh2_auth_password($ssh_connection, 'root', 'password');
$stream = ssh2_exec($ssh_connection, 'cd /opt/; python db.py');
stream_set_blocking($stream, false);
$read = array($stream);
$write = array();
$except = array();
$return_string = stream_get_contents($stream);
//echo $return_string;
stream_select($read, $write, $except, 300);
Does anybody know why it shows me that error?
Looks like you are you dont have acces or banned on remote server. Try to run from another IP.

PHP fwrite() not writing to log file

as one of my first PHP projects I'm creating an IP logging script that logs a user's IP address. For some reason my fwrite() function doesn't seem to be writing to my logfile.
Can someone help me out?
<?php
// IP Logger Script
// By Sam Lev
// sam#levnet.us
$iplogfile = 'iplog.txt';
$ipaddress = $_SERVER['REMOTE_ADDR'];
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('m/d/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
chmod($iplogfile, 0777);
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$webpage.' '.$browser. "\r\n");
fclose($fp);
echo "IP ADDRESS: $ipaddress <br />\n";
echo "TIMESTAMP: $timestamp <br />\n";
echo "BROWSER: $browser <br />\n";
echo "Information logged to server. <br />\n";
?>
iplog.txt is still blank after running the script. Everything echos out fine.
Thanks
Shouldn't
$fp = fopen($file, 'a');
be
$fp = fopen($iplogfile, 'a');
? Because I don't see the definition of $file.
Your code checks out and it's a permissions issue.
Either manually chmod your file to 0777
or add chmod($iplogfile, 0777); after $fp = fopen($iplogfile, 'a');
chmod is a standard server command which is not exclusive to PHP.
http://en.wikipedia.org/wiki/Chmod
heres my code for logging if this helps but after adding code chmod the log files to 0777 or it wont work as adding the code doesn't make it work and will give php error hence why its not in the code btw you may have to create log files manually but easy enough this was for my site www.nzquakes.maori.nz thank you for the help
<!-- Below Code Logs ONLY User's IP Address & Time Stamp & Browser Info To http://www.example.com/logs/ip-address-mainsite.txt -->
<?php
$iplogfile = 'full path to your logs goes here eg http://www.example.com/logs/ip-address-mainsite.txt';
$ipaddress = $_SERVER['REMOTE_ADDR'];
//load the file
$file = file_get_contents($iplogfile);
//check to see if the ipaddress is already in the file
if ( ! preg_match("/$ipaddress/", $file )) {
//nope, log it!
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('d/m/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n");
fclose($fp);
}
?>

PHP: Read from socket or STDIN

I am learning socket programming in PHP and so I am trying a simple echo-chat server.
I wrote a server and it works. I can connect two netcats to it and when I write in the one netcat, I recive it at the other. Now, I want to implement what NC does in PHP
I want to use stream_select to see if I have data on STDIN or on the socket to either send the message from STDIN to the server or reading the incoming message from the server.
Unfortunately the example at the php manual doesn't give me any clue how to do that.
I tried to simply $line = fgets(STDIN) and socket_write($socket, $line) but it doesnt work. So I started to go down and just want stream_select to act up when the user typed the message.
$read = array(STDIN);
$write = NULL;
$exept = NULL;
while(1){
if(stream_select($read, $write, $exept, 0) > 0)
echo 'read';
}
Gives
PHP Warning: stream_select(): No stream arrays were passed in
/home/user/client.php on line 18
But when I var_dump($read) it tells me, that it is an array with a stream.
array(1) {
[0]=>
resource(1) of type (stream)
}
How do I get stream_select to work?
PS: In Python I can do something like
r,w,e = select.select([sys.stdin, sock.fd], [],[])
for input in r:
if input == sys.stdin:
#having input on stdin, we can read it now
if input == sock.fd
#there is input on socket, lets read it
I need the same in PHP
I found a solution. It seems to work, when I use:
$stdin = fopen('php://stdin', 'r');
$read = array($sock, $stdin);
$write = NULL;
$exept = NULL;
Instead of just STDIN. Despite php.net says, STDIN is already open and saves using
$stdin = fopen('php://stdin', 'r');
It seems not, if you want to pass it into stream_select.
Also, the socket to the server should be created with $sock = fsockopen($host); instead of using socket_create on the client side... gotta love this language and it's reasonability and clear manual...
Here a working example of a client that connects to an echo server using select.
<?php
$ip = '127.0.0.1';
$port = 1234;
$sock = fsockopen($ip, $port, $errno) or die(
"(EE) Couldn't connect to $ip:$port ".socket_strerror($errno)."\n");
if($sock)
$connected = TRUE;
$stdin = fopen('php://stdin', 'r'); //open STDIN for reading
while($connected){ //continuous loop monitoring the input streams
$read = array($sock, $stdin);
$write = NULL;
$exept = NULL;
if (stream_select($read, $write, $exept, 0) > 0){
//something happened on our monitors. let's see what it is
foreach ($read as $input => $fd){
if ($fd == $stdin){ //was it on STDIN?
$line = fgets($stdin); //then read the line and send it to socket
fwrite($sock, $line);
} else { //else was the socket itself, we got something from server
$line = fgets($sock); //lets read it
echo $line;
}
}
}
}

Starting a bash script from a php script

I'm trying to run a bash script using shell_exec but It doesnt seem to work. (Nothing seems to happen) I'm using nginx and the latest php5-cgi. Heres what the php file looks like:
<?php
$startserver = "./startserver.sh";
$startserver = shell_exec($startserver);
$getprocess = "pidof hlds_amd";
$pid = shell_exec($getprocess);
$fh = fopen('closeserver.sh', 'w');
$command = "kill -9 $pid";
fwrite($fh, $command);
fclose($fh);
$string = "at -f closeserver.sh now + 1 hour";
$closer = shell_exec($string);
?>
and this is what the bash script looks like:
#!/bin/bash
cd /home/kraffs/srcds
./hlds_run -game cstrike -autoupdate +maxplayers 12 +map de_dust2 > hlds.log 2>&1 &
Theres no errors in the phpscript and the file gets created just fine but $startserver doesnt seem to get executed and $pid is empty. Did I miss something in the php file or do I need to change permissions for an user? Thanks for your help.
replace shell_exec, with below function and try again
<?php
function runcmd($EXEC_CMD)
$handle = popen ($EXEC_CMD, 'r');
$output = "";
if ($handle) {
while(! feof ($handle)) {
$read = fgets ($handle);
$output .= $read;
}
pclose($handle);
}
return $output;
}
?>

Categories