Skip to content

Commit 0fa0717

Browse files
authored
Merge pull request #782 from zeroSteiner/fix/met/php-socket-channel-tests
PHP Send the channel address info back to Metasploit
2 parents 186c4bd + ce42122 commit 0fa0717

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

php/meterpreter/ext_server_stdapi.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,10 @@ function add_stat_buf($path) {
443443
$st_buf .= pack_p($st['atime']);
444444
$st_buf .= pack_p($st['mtime']);
445445
$st_buf .= pack_p($st['ctime']);
446-
446+
447447
$st_buf .= pack("V", $st['blksize']);
448448
$st_buf .= pack("V", $st['blocks']);
449-
449+
450450
return create_tlv(TLV_TYPE_STAT_BUF, $st_buf);
451451
}
452452
return false;
@@ -1331,15 +1331,15 @@ function stdapi_net_config_get_arp_table($req, &$pkt) {
13311331
if ($content === false) {
13321332
return ERROR_FAILURE;
13331333
}
1334-
$lines = explode(PHP_EOL, $content);
1334+
$lines = explode(PHP_EOL, $content);
13351335
array_shift($lines); // first line is the header of the array
1336-
foreach($lines as $line) {
1337-
if ($line == '') continue;
1338-
$v = preg_split('/\s+/', $line);
1339-
$ip = $v[0];
1340-
$mac = $v[3];
1341-
$iface = $v[5];
1342-
my_print("arp line: $ip $mac $iface");
1336+
foreach($lines as $line) {
1337+
if ($line == '') continue;
1338+
$v = preg_split('/\s+/', $line);
1339+
$ip = $v[0];
1340+
$mac = $v[3];
1341+
$iface = $v[5];
1342+
my_print("arp line: $ip $mac $iface");
13431343
$arp_tlv = tlv_pack(create_tlv(TLV_TYPE_IP, inet_pton($ip)));
13441344
$arp_tlv .= tlv_pack(create_tlv(TLV_TYPE_MAC_ADDRESS, pack("H*", str_replace(':', '', $mac))));
13451345
$arp_tlv .= tlv_pack(create_tlv(TLV_TYPE_MAC_NAME, $iface));
@@ -1432,6 +1432,34 @@ function channel_create_stdapi_fs_file($req, &$pkt) {
14321432
}
14331433
}
14341434

1435+
if (!function_exists('packet_add_tlv_local_addrinfo')) {
1436+
function packet_add_tlv_local_addrinfo(&$pkt, $sock) {
1437+
switch (get_resource_type($sock)) {
1438+
case 'Socket':
1439+
$local_host = '';
1440+
$local_port = 0;
1441+
socket_getsockname($sock, $local_host, $local_port);
1442+
packet_add_tlv($pkt, create_tlv(TLV_TYPE_LOCAL_HOST, $local_host));
1443+
packet_add_tlv($pkt, create_tlv(TLV_TYPE_LOCAL_PORT, $local_port));
1444+
case 'stream':
1445+
$local_name = stream_socket_get_name($sock, false);
1446+
if (preg_match('/^\[([^\]]+)\]:(\d+)$/', $local_name, $matches)) {
1447+
// IPv6 with brackets
1448+
packet_add_tlv($pkt, create_tlv(TLV_TYPE_LOCAL_HOST, $matches[1]));
1449+
packet_add_tlv($pkt, create_tlv(TLV_TYPE_LOCAL_PORT, (int)$matches[2]));
1450+
} elseif (preg_match('/^([^:]+):(\d+)$/', $local_name, $matches)) {
1451+
// IPv4
1452+
packet_add_tlv($pkt, create_tlv(TLV_TYPE_LOCAL_HOST, $matches[1]));
1453+
packet_add_tlv($pkt, create_tlv(TLV_TYPE_LOCAL_PORT, (int)$matches[2]));
1454+
} else {
1455+
return false;
1456+
}
1457+
default:
1458+
return false;
1459+
}
1460+
return true;
1461+
}
1462+
}
14351463

14361464
if (!function_exists('channel_create_stdapi_net_tcp_client')) {
14371465
function channel_create_stdapi_net_tcp_client($req, &$pkt) {
@@ -1465,6 +1493,7 @@ function channel_create_stdapi_net_tcp_client($req, &$pkt) {
14651493

14661494
$id = register_channel($sock);
14671495
packet_add_tlv($pkt, create_tlv(TLV_TYPE_CHANNEL_ID, $id));
1496+
packet_add_tlv_local_addrinfo($pkt, $sock);
14681497
add_reader($sock);
14691498
return ERROR_SUCCESS;
14701499
}
@@ -1496,6 +1525,7 @@ function channel_create_stdapi_net_udp_client($req, &$pkt) {
14961525

14971526
$id = register_channel($sock);
14981527
packet_add_tlv($pkt, create_tlv(TLV_TYPE_CHANNEL_ID, $id));
1528+
packet_add_tlv_local_addrinfo($pkt, $sock);
14991529
add_reader($sock);
15001530
return ERROR_SUCCESS;
15011531
}

0 commit comments

Comments
 (0)