php调用shell脚本安全,从PHP调用的shell脚本问题

TLDR;

我有一个shell脚本,从命令行运行时工作正常,但如果从PHP脚本中调用(通过Web访问)则不行.

在这两种情况下,主叫用户都是www-data.

线路失败是这样的:

openssl genrsa -des3 -out certs/$PCODE.key -passout env:PASSPHRASE 2048

为什么会这样?我该怎么调试呢?

全文

我有以下脚本,它是this gist的略微修改版本,用于生成自签名SSL证书.

当我从终端运行它作为www-data时,它可以正常工作并生成密钥文件,CSR和SSL证书文件.但是当我从PHP脚本中调用脚本时,它会输出错误并且不会生成任何文件.是什么导致失败?我该怎么调试呢?

从终端:

me@machine$sudo su www-data

www-data@machine$./gencert.sh acme

www-data will generate an SSL cert for acme.dev

Command after line 32 executed oK

Passphrase expoted as I7gOnWxWd0hOk38Zu ... FbxL3K3Rzlv

Generating RSA private key, 2048 bit long modulus

..............................................+++

.................+++

e is 65537 (0x10001)

Command after line 49 executed oK

Command after line 54 executed oK

Command after line 65 executed oK

writing RSA key

Command after line 69 executed oK

Signature ok

subject=/C=IR/ST=Alborz/.../emailAddress=noreply@acme.dev

Getting Private key

Command after line 74 executed oK

产生的文件:

> certs / acme.key.org

> certs / acme.key

> certs / acme.csr

> certs / acme.crt

来自PHP:

$r = `/var/www/testbench/pm/shell/gencert.sh acme`;

echo $r;

没有生成文件,输出如下:

www-data will generate an SSL cert for acme.dev

Command after line 32 executed oK

Passphrase expoted as 1Fd1seZoe2XF ... oSmQFJdVpdwOeTo2CK5VjLxp

Error. Return value = 1 after line 49

返回1的行是这样的:

openssl genrsa -des3 -out certs / $PCODE.key -passout env:PASSPHRASE 2048

这是修改后的shell脚本:

#!/bin/bash

# Bash shell script for generating self-signed certs. Run this in a folder, as it

# generates a few files. Large portions of this script were taken from the

# following artcile:

#

# http://usrportage.de/archives/919-Batch-generating-SSL-certificates.html

# https://deliciousbrains.com/ssl-certificate-authority-for-local-https-development/

# Additional alterations by: Brad Landers

# Date: 2012-01-27

# Script accepts a single argument, the fqdn for the cert

PCODE="$1"

if [ -z "$PCODE" ]; then

echo "Usage: $(basename $0) "

exit 11

fi

THE_USER="$(whoami)"

echo "$THE_USER will generate an SSL cert for $PCODE.dev"

fail_if_error() {

[ $1 != 0 ] && {

echo -n "Error. Return value = $1 after line $LASTLINE"

unset PASSPHRASE

exit 10

}

echo "Command after line $LASTLINE executed oK"

}

# Generate a passphrase

LASTLINE="${LINENO}"

export PASSPHRASE=$(head -c 500 /dev/urandom | tr -dc a-z0-9A-Z | head -c 128; echo)

fail_if_error $?

echo -n "Passphrase expoted as "

printenv PASSPHRASE

# Certificate details; replace items in angle brackets with your own info

subj="

C=IR

ST=Alborz

O=ACME

localityName=Karaj

commonName=*.$PCODE.dev

organizationalUnitName=WebAdmin

emailAddress=noreply@$PCODE.dev

"

LASTLINE="${LINENO}"

# Generate the server private key

openssl genrsa -des3 -out certs/$PCODE.key -passout env:PASSPHRASE 2048

fail_if_error $?

LASTLINE="${LINENO}"

# Generate the CSR

openssl req \

-new \

-batch \

-subj "$(echo -n "$subj" | tr "\n" "/")" \

-key certs/$PCODE.key \

-out certs/$PCODE.csr \

-passin env:PASSPHRASE

fail_if_error $?

LASTLINE="${LINENO}"

cp certs/$PCODE.key certs/$PCODE.key.org

fail_if_error $?

LASTLINE="${LINENO}"

# Strip the password so we don't have to type it every time we restart Apache

openssl rsa -in certs/$PCODE.key.org -out certs/$PCODE.key -passin env:PASSPHRASE

fail_if_error $?

LASTLINE="${LINENO}"

# Generate the cert (good for 10 years)

openssl x509 -req -days 3650 -in certs/$PCODE.csr -signkey certs/$PCODE.key -out certs/$PCODE.crt

fail_if_error $?

解决方法:

要执行的命令具有相对路径,例如:certs / $PCODE.key.当您执行命令时(在这种情况下通过反引号操作符),路径相对于PHP进程的当前工作目录进行扩展.这很少(如果有的话)与命令shell使用的路径相同.

要调试这个,你可以使用strace扩展你的实际命令,例如:strace openssl ….这将为你提供相当大的诊断功能,接近最后,你会看到EPERM的内容.

要解决此问题,您可以在PHP中使用chdir来设置当前工作目录,也可以在脚本中使用cd,或者您的脚本可以使用绝对路径.我更喜欢后者.

标签:php,shell,command-line,sh

来源: https://codeday.me/bug/20190627/1305955.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/378541.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux 运维基础问题_Linux基础能力问题和解答

linux 运维基础问题This section contains Aptitude Questions and Answers on Linux Basics. 本节包含有关Linux基础知识的 Aptitude问答。 1) There are the following statements that are given below, which of them are correct about Linux? Linux is system software…

JS 获取浏览器信息,给出友情提示,避免部分兼容性问题

最近在做webform,浏览器兼容是个问题,这里我收集了一些获取浏览器信息的资料,可以给一些用户使用时,提示浏览器版本过低,让升级版本用. 这样会给开发的我们,省下很多用来调试兼容性的时间和精力. 本人就是这样想的 ~  检测浏览器及版本使用 JavaScript 检测关于访问者的浏览器…

两栏 三栏的css

三栏格局 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns"http://www.w3.org/1999/xhtml" xml:lang"zh" lang"zh"><head pro…

06-机器学习(Haar+Adaboost实现人脸、人眼检测)

机器学习是什么? 机器学习训练样本特征分类器&#xff0c;通过让机器学习的方式&#xff0c;来达到某种功能的过程 深度学习是什么&#xff1f; 深度学习海量的学习样本人工神经网络 机器学习需要&#xff1a;样本、特征、分类器、对训练后的数据进行预测或检验 人脸样本haar…

php xml表格形式输出,PHP XML如何输出nice格式

这里是代码&#xff1a;$doc new DomDocument(1.0);// create root node$root $doc->createElement(root);$root $doc->appendChild($root);$signed_values array(a > eee, b > sd, c > df);// process one row at a timeforeach ($signed_values as $key &…

Opencv实战【3】——图像修复与图像锐化(darling in the franxx)

目录前言图像修复图像锐化darling in the franxx图片总结前言 前天&#xff0c;在群里看见有人发了这张表情包&#xff1a; 感觉女主有点好看&#xff0c;然后问室友是啥番剧&#xff08;darling in the franxx&#xff09;&#xff0c;然后就去补番了&#xff0c;然后从晚上…

python 示例_Python date isoweekday()方法与示例

python 示例Python date.isoweekday()方法 (Python date.isoweekday() Method) date.isoweekday() method is used to manipulate objects of date class of module datetime. date.isoweekday()方法用于处理模块日期时间的日期类的对象。 It uses a date class object and r…

07-机器学习(Hog+SVM实现小狮子识别)

一、SVM支持向量机 什么是SVM支持向量机&#xff1f; SVM支持向量机本质仍是一个分类器&#xff0c;其核心为寻求一个最优超平面最终实现分类&#xff0c;实现分类问题 在寻求超平面的时候有多种方式&#xff0c;可以使用若干条直线或曲线进行分类&#xff0c;这里使用的是直线…

Net Remoting基础篇

一、Remoting基础 什么是Remoting&#xff0c;简而言之&#xff0c;我们可以将其看作是一种分布式处理方式。从微软的产品角度来看&#xff0c;可以说Remoting就是DCOM的一种升 级&#xff0c;它改善了很多功能&#xff0c;并极好的融合到.Net平台下。Microsoft .NET Remoting …

Maven3.0.5代理nexus

Nexus简介 Nexus是Sonatype推出的强大Maven仓库管理器产品&#xff0c;要比以前TSS上介绍的Artifactory要好使用的多&#xff0c;也是一个拆箱即用的Java App&#xff0c;内嵌Jetty容器和Java Wrapper做Windows服务&#xff0c;安装简单到解压然后双击install即可。更详细的帮助…

8253译码电路设计以及初始化编程讲解

先验知识回顾&#xff1a;知识点不清晰的时候可以查询相关知识点。 https://blog.csdn.net/qq_42604176/article/details/105810973 需掌握的主要知识点 1、译码电路设计 2、初始化编程 例题1 在以 8086构成的最大方式系统中&#xff0c;有一片8254的端口地址分别为301H、3…

java安卓写文件路径,如何使用gradle作为构建系统,平台Android配置Protobuf(Java)文件的输出路径?...

我正在努力解决以下问题&#xff1a;我有2个基于maven的java项目和1个基于gradle的Android项目 . 布局如下&#xff1a;Workspace/├── MavenProj1/├── MavenProj2/├── AndroidGradleProject1/├── Protos/所有这些的包结构很常见&#xff0c;比方说 com.example.* 所…

Java System类exit()方法及示例

系统类exit()方法 (System class exit() method) exit() method is available in java.lang package. exit()方法在java.lang包中可用。 exit() method is used to exit the currently running JVM (Java Virtual Machine). exit()方法用于退出当前正在运行的JVM(Java虚拟机)。…

基于图像处理的数码印花喷墨墨滴形状规范的研究(Python+OpenCV+Mysql)

大体思路&#xff1a;由于墨滴的不同参数会对墨滴的形态产生一定的影响&#xff0c;故如果通过研究墨滴的形态则通过海量的数据就可以大概确定墨滴的各项参数指标的范围。通过OpenCV对墨滴的喷出的形状进行图像处理&#xff0c;对墨滴图像进行一系列的分析&#xff0c;通过一系…

ASP.NET 主题(Themes)FAQ

1、主题是什么 主题由一组元素组成&#xff1a;外观、级联样式表 (CSS)、图像和其他资源。主题将至少包含外观。主题是在网站或 Web 服务器上的特殊目录中定义的。主题是一组Web Control的属性设置的集合&#xff0c;提供一种简单的方法设置控件的样式属性。 主题只在Web Contr…

Head First HTML与CSS、XHTML++笔记(第四章 WEB镇之旅 第五章 认识媒体)

第四章 链接&#xff08;详解<a>元素&#xff09; 目标锚 在目标位置 <h2><a id"chai">contentTest</a></h2> 在需要链接位置 <a href"index.html#chai">See</a> 链接到自身的目标锚 <a href"#top"…

Opencv实战【4】——图片动漫化处理

博主联系方式&#xff1a; QQ:1540984562 微信&#xff1a;wxid_nz49532kbh9u22 QQ交流群&#xff1a;750313950 目录动漫化风格的特点处理手段代码实现效果总结动漫化风格的特点 &#xff08;1&#xff09;动漫中的细节相对少&#xff1b; &#xff08;2&#xff09;动漫中的边…

nextshort_Java扫描仪的nextShort()方法与示例

nextshort扫描器类的nextShort()方法 (Scanner Class nextShort() method) Syntax: 句法&#xff1a; public short nextShort();public short nextShort(int rad);nextShort() method is available in java.util package. nextShort()方法在java.util包中可用。 nextShort() …

php 生成css文件怎么打开,php生成html文件的多种步骤介绍

//定义日期函数functiongetdatetime(){$datetimegetdate();$strReturn$datetime["year"]."-";$strReturn$strReturn.$datetime["mon"]."-";$strReturn$strReturn.$datetime["mday"];return$strReturn;}//定义时间函数(文件名…

08-KNN手写数字识别

标签下载地址 文件内容备注train-images-idx3-ubyte.gz训练集图片&#xff1a;55000张训练图片&#xff0c;5000张验证图片train-labels-idx1-ubyte.gz训练集图片对应的数字标签t10k-images-idx3-ubyte.gz测试集图片&#xff1a;10000张图片t表示test&#xff0c;测试图片&…