我正在为Hack the box,Try Hack Me等上的服务器构建一个枚举工具。当我尝试自动化端口扫描时,我遇到了子进程和将输出写入文件的问题。
import os
import sys
import traceback
import subprocess as sub
import re
ip_addr = ''
nickName = ''
Dir = ''
def getIP():
global ip_addr
ip_addr = str(input('[+] Please enter the IP address you would like to enumerate: \n'))
if not re.match("^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$", ip_addr):
print('[-] That is not the correct format for an IP address. \n [-] Please try again.')
getIP()
def mk_nickname():
global nickName
nickName = str(input('[+] Please give this IP a nickname. \n [+] This will be used to create a directory to keep you notes organized. \n [+] This will be found in your documents folder within your home directory.\n'))
if nickName == '':
mk_nickname()
return
#add if file already exsists clause (exsit_ok may have done the trick)
def mkdir():
global Dir
Dir = f"{os.getenv('HOME')}/Documents/" + nickName
os.makedirs(Dir, mode=0o700, exist_ok=True)
def PortScan():
YN = str(input('[+] Would you like to run a port scan? '))
portDir = Dir +'/portscan.txt'
print(portDir)
if YN == 'y' or YN == 'yes':
print('[+] Starting portscan.\n [+] The results can be found here: ' + portDir )
cmd = "rustscan", "-a", ip_addr, "--", "-sV", "-sC", "-A"
print(cmd)
sub.Popen([cmd], stdout=sub.PIPE, stderr=sub.PIPE, text=True)
with open(portDir, w) as f:
file.write(result.stdout)
elif YN == 'n' or YN == 'no':
return
else:
print('[-] Invalid input!\n[-] Please try again.')
print('[+] Lets start enumerating!!!')
getIP()
mk_nickname()
mkdir()
PortScan()我尝试了许多不同的方法,但似乎不能让它工作。
下面是接收到的错误:
[+] Lets start enumerating!!!
[+] Please enter the IP address you would like to enumerate:
10.10.10.75
[+] Please give this IP a nickname.
[+] This will be used to create a directory to keep you notes organized.
[+] This will be found in your documents folder within your home directory.
nibbles
[+] Would you like to run a port scan? yes
/home/kali/Documents/nibbles/portscan.txt
[+] Starting portscan.
[+] The results can be found here: /home/kali/Documents/nibbles/portscan.txt
('rustscan', '-a', '10.10.10.75', '--', '-sV', '-sC', '-A')
Traceback (most recent call last):
File "/home/kali/Desktop/OSCPENUM.py", line 57, in <module>
PortScan()
File "/home/kali/Desktop/OSCPENUM.py", line 41, in PortScan
sub.Popen([cmd], stdout=sub.PIPE, stderr=sub.PIPE, text=True)
File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.9/subprocess.py", line 1698, in _execute_child
and os.path.dirname(executable)
File "/usr/lib/python3.9/posixpath.py", line 152, in dirname
p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not tuple我可以把这归因于没有完全理解子进程,但在查看了文档和许多不同的fourms/栈溢出帖子后,我仍然无法找到解决方案。这就是为什么我求助于Stack Overflow的领主们!:)
发布于 2021-09-11 18:11:44
将Popen行替换为sub.Popen(cmd,stdout=sub.PIPE,stderr=sub.PIPE,text=True)。(注意,我去掉了方括号)- Flimm 1
这解决了这个问题。现在开始调试程序的其余部分。这很容易,因为这个问题已经过去了。
根据我对这个问题的理解:子流程文档指定使用括号[]作为"arg“的值。当您通过subprocess.Popen()而不是直接命令传递变量时,这不适用。
def PortScan():
YN = str(input('[+] Would you like to run a port scan?\n'))
portDir = Dir +'/portscan.txt'
print(portDir)
if YN == 'y' or YN == 'yes':
print('[+] Starting portscan.\n [+] The results can be found here: ' + portDir )
cmd = "rustscan", "-a", ip_addr, "--", "-sV", "-sC", "-A"
print(cmd)
f = open(portDir, "w")
sub.Popen(cmd, stdout=f, text=True)非常感谢Flimm的快速响应!
https://stackoverflow.com/questions/69144976
复制相似问题