My code is:
_cmd = "|| echo " + base64.b64encode(args.cmd) + "|base64 -d|bash"
p.update({"form_284": _cmd})
My error is:
Traceback (most recent call last):
File "openemr_rce.py", line 136, in <module>
_cmd = "|| echo " + base64.b64encode(args.cmd) + "|base64 -d|bash"
File "/usr/lib/python3.8/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
Edit:
There is no problem when you run it in python2
Your args.cmd is a string.
If it was meant to be, try base64.b64encode(args.cmd.encode("ascii")).decode("ascii").
If your command contains non-ascii characters, the bash cmd is on a system-dependent encoding, and you can use sys.getdefaultencoding() to fetch it.
As the error says, you must pass a "bytes-like object" to b64encode, not a string. To get bytes from a string, you can call encode():
base64.b64encode(args.cmd.encode('utf8'))
Related
We are trying to extract a files from AS400 / OS400 which is in Ascii mode and convert into readable format of binary mode. We have written below code
def retrlines(self, cmd, callback = None):
resp = self.sendcmd('TYPE A')
with self.transfercmd(cmd) as connectioninfo, \
connectioninfo.makefile('rt', encoding="cp500") as fp:
while 1:
line = fp.readline(self.maxline + 1)
callback(line)
return self.voidresp()
Getting an error
TypeError: a bytes-like object is required, not 'str'
Task failed with exception
Traceback (most recent call last):
File "/home/sftp_file_hook.py", line 418, in retrieve_file
conn.retrlines(f'RETR {remote_file_name}', callback)
File "/opt/ftplib.py", line 472, in retrlines
callback(line)
TypeError: a bytes-like object is required, not 'str'
Any suggestion please
I'm reading a bunch of txt.gz files but they have different encoding (at least UTF-8 and cp1252, they are old dirty files). I try to detect the encoding of fIn before reading it in text-mode but I get the error: TypeError: 'GzipFile' object is not callable
The corresponding code:
# detect encoding
with gzip.open(fIn,'rb') as file:
fInEncoding = tokenize.detect_encoding(file) #this doesn't works
print(fInEncoding)
for line in gzip.open(fIn,'rt', encoding=fInEncoding[0], errors="surrogateescape"):
if line.find("From ") == 0:
if lineNum != 0:
out.write("\n")
lineNum +=1
line = line.replace(" at ", "#")
out.write(line)
Traceback
$ ./mailmanToMBox.py list-cryptography.metzdowd.com
('Converting ', '2015-May.txt.gz', ' to mbox format')
Traceback (most recent call last):
File "./mailmanToMBox.py", line 65, in <module>
main()
File "./mailmanToMBox.py", line 27, in main
if not makeMBox(inFile,outFile):
File "./mailmanToMBox.py", line 48, in makeMBox
fInEncoding = tokenize.detect_encoding(file.readline()) #this doesn't works
File "/Users/simon/anaconda3/lib/python3.6/tokenize.py", line 423, in detect_encoding
first = read_or_stop()
File "/Users/simon/anaconda3/lib/python3.6/tokenize.py", line 381, in read_or_stop
return readline()
TypeError: 'bytes' object is not callable
EDIT
I tried to use the following code:
# detect encoding
readsource = gzip.open(fIn,'rb').__next__
fInEncoding = tokenize.detect_encoding(readsource)
print(fInEncoding)
I have no error but it always return utf-8 even when it isn't. My text editor (sublime) detect correctly the cp1252 encoding.
As the documentation of detect_encoding() says, it's input parameter has to be a callable that provides lines of input. That's why you get a TypeError: 'GzipFile' object is not callable.
import tokenize
with open(fIn, 'rb') as f:
codec = tokenize.detect_encoding(f.readline)[0]
... codec will be "utf-8" or something like that.
Here is my code.
import base64
encoded = base64.b64encode(b"data to be encoded")
print(encoded)
print(encoded.replace("b", ""))
Here is my output
b'ZGF0YSB0byBiZSBlbmNvZGVk'
Traceback (most recent call last):
File "C:\Users\user\Desktop\base64_obfuscation.py", line 8, in <module>
print(decoded.replace("b", ""))
TypeError: a bytes-like object is required, not 'str'
My overall task is to remove the single quotes and the "b" chracter from the string but I'm unsure on how to do so?
print(str(encoded).replace("b", ""))
if "\\" in item["message"]:
item["message"] = str(item["message"]).replace("\\", "\\\\").encode("utf-8")
write the program, there are many "\" in item['message'], therefore, when trying to insert this item in mysql table, it errors, I try to deal with the "\" in item["message"], so I write the program above,
when it run,it errors:
Traceback (most recent call last):
File "C:/Python/PyCharmProject/FaceBookCrawl/FBCrawl.py", line 237, in <module>
group_download.group_feed_storage(FB_access_token,group_id,group_name)
File "C:\Python\PyCharmProject\FaceBookCrawl\group_download.py", line 116, in group_feed_storage
mysql_manage().group_feed_db_manage(type,group,name,feed)
File "C:\Python\PyCharmProject\FaceBookCrawl\database_manage.py", line 95, in group_feed_db_manage
if "\\" in item["message"]:
TypeError: a bytes-like object is required, not 'str'
Process finished with exit code 1
Could you please help me for that
item['message'] is a bytes-like object, so it may contain certain sequences of bytes, but won't contain substrings. Search for a bytes-like object:
if b"\\" in item["message"]:
^
Python code breaks in python3.
Vectorize.py contains the following line
path = os.path.join('..', path[:-1])
Error output:
$ python3 vectorize_text.py
Traceback (most recent call last):
File "vectorize_text.py", line 46, in <module>
path = os.path.join('..', path[:-1])
File "/usr/lib/python3.4/posixpath.py", line 89, in join
"components") from None
TypeError: Can't mix strings and bytes in path component
On the other hand, running in python2.7 it works fine. What am I missing here ? Is the command different ? I couldn't find anything.
Your path is a bytes object, not a str string. You can then only use more bytes strings to make a different path. Use a b'..' bytes literal:
path = os.path.join(b'..', path[:-1])