2

I have created a simple reverse shell in python using subprocess.Popen(cmd, shell=True) for the client to run commands. However, when I use commands in the user directory, I get a shell-init error. This is what I get when I try to use ls

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

Running the ls command as sudo does not work.

It is worth mentioning that commands do work as intended in directories outside of the user directory.

Here is the code that calls the command:

output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
YulkyTulky
  • 886
  • 1
  • 6
  • 20
  • Can you present your python code? – Yi Zhao Apr 20 '20 at 02:15
  • @YiZhao updated – YulkyTulky Apr 20 '20 at 02:20
  • The code works for me. What if you call 'ls' in terminal in that directory? It is possible that you have deleted that directory? Add import os print(os.getcwd()) and check if the directory really exists. – Yi Zhao Apr 20 '20 at 02:27
  • I definitely did not delete the directory. Calling ```ls``` works fine in a regular terminal – YulkyTulky Apr 20 '20 at 02:28
  • https://stackoverflow.com/questions/29396928/shell-init-error-retrieving-current-directory-getcwd-the-usual-fixes-do-not/41704858 is this related to your problem? – Yi Zhao Apr 20 '20 at 02:32
  • I know for sure the directory exists. Interestingly, if I cd into the directory and execute ```echo test > test```, the command does execute but my shell stalls and pretends like nothing happened – YulkyTulky Apr 20 '20 at 02:40

1 Answers1

0

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 /]# 

It says if you remove the first line of the script, it works fine, too.

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

If this is true for you, too. Then it is not a problem with subprocess.

Yi Zhao
  • 346
  • 2
  • 13