Reputation: 25
I am attempting to make a basic port scan for penntesting, and am wanting the data to be stored in their own array, rather than each individual port being its own array.
This is my code (I see the issue, but am looking for ways to solve it)
$host = $_GET["host"];
$ports = array(80, 443);
foreach ($ports as $port)
{
$connection = @fsockopen($host, $port, $errno, $errstr, 2);
if (is_resource($connection)) {
$out=array("success:true");
$out2=array("ports:$port");
echo json_encode($out);
echo "\n";
echo json_encode($out2);
}else{
$out=array("success:false");
echo json_encode($out);
}
fclose($connection);
}
The issue (I think) is the echo command is contained within the ForEach string, and therefore is being ran on its own per port. I am confused how else I could do it. The result I currently get is:
["success:true"] ["ports:80"]["success:true"] ["ports:443"]
And the result I would like to get is
["success:true"] ["ports:80, 443"]
Any tips on what I could do differently, or how I could reword this question would be much appreciated!
Edit: Would actually prefer to get an output like this:
{"success": true, "ports": 80, 443}
If anyone knows how I could achieve this, I would be very thankful for your help!
Edit 2: Also, if a failure were to occur (No ports replied) I would like it to not list the ports which failed, but rather just output something like
{"success": false}
Upvotes: 2
Views: 69
Reputation: 270609
Creating a JSON structure which allows you to see the success or failure of the connection together with an array of successful ports will be a more machine readable. The loop should gather all the information you need, then after the loop assemble it into a structured array and output it as JSON. I would recommend striving to send output as infrequently as possible - instead of many echo
in a loop, build an array or string then echo
once at the end for output.
// Start with an empty array to collect successful ports
$ports_success = array();
foreach ($ports as $port)
{
$connection = @fsockopen($host, $port, $errno, $errstr, 2);
// On success
if (is_resource($connection)) {
// Add the port to the array of successes
$ports_success[] = $port;
}
}
// Now outside your loop, test if count() of succeeded ports
// is more than zero, meaning you had some succeed
if (count($ports_success) > 0) {
$success = true;
}
else {
$success = false;
}
// Combine your success flag and ports list into json
$output = array(
'success' => $success,
'ports' => $ports_success
);
echo json_encode($output);
The above would include an empty ports: []
even on failure. You can exclude that with different logic after the loop.
// Start with empty output
$output = array();
if (count($ports_success) > 0) {
// Only put in the ports if there are some
$output = array(
'success' => true,
'ports' => $ports_success
);
}
else {
$output = array(
'success' => false
);
}
echo json_encode($output);
A final note since you are new to PHP - some of the if/else
logic can be more succinct if you rely on a boolean evaluation from count() > 0
. This is how I would probably express it after your loop.
$output = array(
// true if > 0, false otherwise
'success' => (count($ports_success) > 0),
'ports' => $ports_success
);
echo json_encode($output);
Upvotes: 2