【问题标题】:subprocess.Popen returns "shell-init: error retrieving current directory"subprocess.Popen 返回“shell-init:检索当前目录时出错”
【发布时间】:2020-08-02 11:02:30
【问题描述】:

我使用subprocess.Popen(cmd, shell=True) 在python 中创建了一个简单的反向shell,供客户端运行命令。但是,当我在用户目录中使用命令时,我得到一个 shell-init 错误。这就是我尝试使用ls时得到的结果@

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
ls: .: Operation not permitted

以 sudo 身份运行 ls 命令不起作用。

值得一提的是,命令在用户目录之外的目录中确实可以按预期工作。

下面是调用命令的代码:

output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

【问题讨论】:

  • 你能介绍一下你的python代码吗?
  • @YiZhao 更新
  • 代码对我有用。如果您在该目录的终端中调用“ls”怎么办?您可能已删除该目录?添加 import os print(os.getcwd()) 并检查该目录是否真的存在。
  • 我绝对没有删除目录。在常规终端中调用 ls 可以正常工作
  • stackoverflow.com/questions/29396928/…这和你的问题有关吗?

标签: python python-3.x shell subprocess reverse-shell


【解决方案1】:

From a Chinese blog

[root@smart hello]# cat /root/Desktop/hello.sh 
#!/bin/bash
echo "hello"
[root@smart hello]# /root/Desktop/hello.sh
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
hello
[root@smart hello]# cd /
[root@smart /]# /root/Desktop/hello.sh
hello
[root@smart /]# 

它说如果你删除脚本的第一行,它也可以正常工作。

[root@smart hello]# cat /root/Desktop/hello.sh 
echo "hello"
[root@smart hello]# /root/Desktop/hello.sh
hello

如果你也是这样的话。那么子进程就不是问题了。

【讨论】:

    猜你喜欢
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多