Basically, i'm trying to write a script to bruteforce a protected zip file in python that tries every character combination (i.e. aa,ba,ca,da etc). But after a few tries it retrieves a strange error, and i'm not being able to find nowhere a solution for it.
Program:
import zipfile as z
class zipVictim:
def __init__(self,file):
self.found = False
self.password = ''
self.file = file
self.extracted_file_list = []
def bruteforceAttack(self,start_char: int,end_char: int,length: int,deep_loop=False,print_mode=False):
"""
Doc
"""
def _loop_chain(self,file,start_char,end_char,length):
lList = []
sPass = ''
iAttempt = 1
for iInc in range(length):
lList.append(start_char)
while lList[len(lList)-1] < end_char:
for iInc2 in range(start_char,end_char):
for iInc3 in range(len(lList)):
sPass = sPass + chr(lList[iInc3])
if iInc3 == 0:
lList[iInc3] = lList[iInc3] + 1
elif lList[iInc3 -1] > end_char:
lList[iInc3] = lList[iInc3] + 1
lList[iInc3 -1] = start_char
try:
if print_mode:
print("Attempt %s, password: %s" % (iAttempt,sPass),end='\r')
iAttempt = iAttempt + 1
oFile.extractall(pwd=sPass.encode())
self.extracted_file_list = oFile.namelist()
self.password = sPass
self.found = True
return self.found
except RuntimeError:
pass
sPass = ''
return self.found
oFile = self.file
if not deep_loop:
_loop_chain(self,oFile,start_char,end_char,length)
else:
for iInc in range(length):
_loop_chain(self,oFile,start_char,end_char,iInc+1)
if __name__ == '__main__':
file = z.ZipFile('data.zip')
s = zipVictim(file)
s.bruteforceAttack(64,125,2,print_mode=True)
Error Retrieved:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\zipfile.py", line 925, in _read1
data = self._decompressor.decompress(data, n)
zlib.error: Error -3 while decompressing data: invalid block type
Does anybody know what trigger this error and how to solve it?
Related
I have a strange error that I can't get my head around. I am trying to test whether a method is called within side another method. I have used the debugger on PyCharm and it seems to be, however, the test fails because the second function is called raises an exception.
test.py
def test_reply_to_toot(self, directory):
with patch("mastodon.Mastodon") as mastodon_mock:
mastodon_mock.return_value = Mock()
mastodon_mock.status_post.assert_called_with(bot.reply_to_toot("1", account_name="#fake", message="test"))
bot.py (method being tested)
# Set up Mastodon
mastodon = Mastodon(
access_token=os.getenv("ACCESS_TOKEN"),
api_base_url=settings.BASE_ADDRESS
)
def reply_to_toot(post_id, account_name, message=None, status_notifications=None):
media_ids = []
for fn in os.listdir(str(settings.INPUT_FOLDER)):
if fn.endswith(('.jpeg', '.png')):
print(Path(fn))
image_dict = mastodon.media_post(str(settings.INPUT_FOLDER / fn))
media_ids.append(image_dict["id"])
if message is not None:
parts = []
total_len = str(len(message) // settings.MAX_MESSAGE_LENGTH + 1)
count = 1
split_lines = message.splitlines(True)
while split_lines:
message_part = "#" + account_name + " {}/".format(count) + total_len + "\n\n"
while split_lines != [] and len(message_part) + len(split_lines[0]) < settings.MAX_MESSAGE_LENGTH:
message_part += split_lines[0]
split_lines = split_lines[1:]
parts.append(message_part)
count += 1
for part in parts:
print(part)
post_id = mastodon.status_post(status=part, media_ids=media_ids, in_reply_to_id=post_id)
else:
while media_ids:
mastodon.status_post(status=message, media_ids=media_ids[0:4], in_reply_to_id=post_id)
media_ids = media_ids[4:]
Exception raised:
Error
Traceback (most recent call last):
File "C:\Python35\lib\unittest\case.py", line 58, in testPartExecutor
yield
File "C:\Python35\lib\unittest\case.py", line 600, in run
testMethod()
File "C:\Python35\lib\unittest\mock.py", line 1157, in patched
return func(*args, **keywargs)
File "C:\Users\Hugh\PycharmProjects\summer-project\test\test.py", line 70, in test_reply_to_toot
mastodon_mock.status_post.assert_called_with(bot.reply_to_toot("1", account_name="#fake", message="test"))
File "C:\Users\Hugh\PycharmProjects\summer-project\src\bot.py", line 65, in reply_to_toot
post_id = mastodon.status_post(status=part, media_ids=media_ids, in_reply_to_id=post_id)
File "<decorator-gen-60>", line 2, in status_post
File "C:\Users\Hugh\PycharmProjects\summer-project\venv\lib\site-packages\mastodon\Mastodon.py", line 102, in wrapper
return function(self, *args, **kwargs)
File "C:\Users\Hugh\PycharmProjects\summer-project\venv\lib\site-packages\mastodon\Mastodon.py", line 1776, in status_post
return self.__api_request('POST', '/api/v1/statuses', params, headers = headers, use_json = use_json)
File "C:\Users\Hugh\PycharmProjects\summer-project\venv\lib\site-packages\mastodon\Mastodon.py", line 3429, in __api_request
error_msg)
mastodon.Mastodon.MastodonNotFoundError: ('Mastodon API returned error', 404, 'Not Found', 'The status you are trying to reply to does not appear to exist.')
Assertion failed
Assertion failed
Ran 1 test in 0.146s
FAILED (errors=1)
Process finished with exit code 1
Assertion failed
Assertion failed
Assertion failed
Assertion failed
Solution
Mock Mastodon within the context of bot module
Example
test_bot.py
import unittest
import bot
from unittest.mock import patch
class TestBot(unittest.TestCase):
#patch("bot.Mastodon")
def test_bot(self, mastodon_mock):
b = bot.Bot()
breakpoint()
mastodon_mock.return_value = Mock()
mastodon_mock.status_post.assert_called_with(
b.reply_to_toot("1", account_name="#fake", message="test")
)
bot.py
import os
from mastodon import Mastodon
class Bot(object):
# Set up Mastodon
def __init__(self, access_token="", base_address=""):
self.mastadon = Mastodon(
access_token,
base_address
)
def reply_to_toot(post_id, account_name, message=None, status_notifications=None):
media_ids = []
for fn in os.listdir(str(settings.INPUT_FOLDER)):
if fn.endswith(('.jpeg', '.png')):
print(Path(fn))
image_dict = self.mastodon.media_post(str(settings.INPUT_FOLDER / fn))
media_ids.append(image_dict["id"])
if message is not None:
parts = []
total_len = str(len(message) // settings.MAX_MESSAGE_LENGTH + 1)
count = 1
split_lines = message.splitlines(True)
while split_lines:
message_part = "#" + account_name + " {}/".format(count) + total_len + "\n\n"
while split_lines != [] and len(message_part) + len(split_lines[0]) < settings.MAX_MESSAGE_LENGTH:
message_part += split_lines[0]
split_lines = split_lines[1:]
parts.append(message_part)
count += 1
for part in parts:
print(part)
post_id = self.mastodon.status_post(status=part, media_ids=media_ids, in_reply_to_id=post_id)
else:
while media_ids:
self.mastodon.status_post(status=message, media_ids=media_ids[0:4], in_reply_to_id=post_id)
media_ids = media_ids[4:]
References
Partial Mocking: https://docs.python.org/3/library/unittest.mock-examples.html#partial-mocking
This is my code:
class Parser(object):
def __init__(self, inputFile): # initalizer / constructor
#open input file and gets ready to parse it
f = open(inputFile, "r")
self.commands = list(f)
f.close()
print(self.commands)
self.currentCommand = 0
self.index = 0
def hasMoreCommands(self):
#are there any more commands in the input
#returns boolean
if (self.commands[self.currentCommand][self.index] == "\\") and (self.commands[self.currentCommand][self.index+1] == "n"): # checks for "/n", alluding that the command has ended and we can advance to the next command
return True
else:
return False
def advance(self):
#reads next command and makes it current command
#called only if hasMoreCommands is true
if self.hasMoreCommands():
self.currentCommand += 1
def commandType(self):
#returns type of current command A_COMMAND, C_COMMAND, L_COMMAND
#C A or L(psuedo command for (XxX))
#dest=comp; jmp, #, ()
self.type = self.commands[self.currentCommand][0]
if self.type == "#":
return "A_COMMAND"
elif self.type == "(":
return "L_COMMAND"
else:
return "C_COMMAND"
def dest(self):
#returns dest mnemoic of current C instruction - 8 Poss
#called when command type is C
#return string
if (self.commandType() == "C_COMMAND") and ("=" in self.commands[self.currentCommand]):
return self.commands[self.currentCommand][0:(self.commands[self.currentCommand].index("="))]
def main(inputFile):
d = Parser(inputFile)
d.commandType = "C_COMMAND"
d.commands = ["D=A+2\\n", "AMD=A+5\\n"]
d.currentCommand = 0
print(d.dest())
main("/Users/user1/Desktop/filelocation/projects/06/add/add.asm")
The file in question:
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/06/add/Add.asm
// Computes R0 = 2 + 3
#2
D=A
#3
D=D+A
#0
M=D
Error returned:
['// This file is part of www.nand2tetris.org\n', '// and the book "The Elements of Computing Systems"\n', '// by Nisan and Schocken, MIT Press.\n', '// File name: projects/06/add/Add.asm\n', '\n', '// Computes R0 = 2 + 3\n', '\n', '#2\n', 'D=A\n', '#3\n', 'D=D+A\n', '#0\n', 'M=D\n']
Traceback (most recent call last):
File "/Users/user1/Desktop/Python/filelocation/assembler.py", line 104, in <module>
main("/Users/user1Desktop/filelocation/projects/06/add/add.asm")
File "/Users/user1/Desktop/Python/filelocation/assembler.py", line 99, in main
print(d.dest())
File "/Users/user1/Desktop/Python/filelocation/assembler.py", line 50, in dest
if (self.commandType() == "C_COMMAND") and ("=" in self.commands[self.currentCommand]):
TypeError: 'str' object is not callable
[Finished in 0.1s with exit code 1]
I was attempting to test dest.
This is a part of the Nand 2 Tetris / Elements of Computing Systems curriculum at Chapter 6.
In your main, you are replacing the method def commandType(self) with d.commandType = "C_COMMAND", which is a str and therefore cannot be called like a method.
For your class....
class Parser(object):
def __init__(self, inputFile): # initalizer / constructor
#open input file and gets ready to parse it
f = open(inputFile, "r")
self.commands = list(f)
f.close()
You already set self.commands from the file
And note: index and currentCommand appear to serve the exact same purpose
Your function uses that list.
def commandType(self):
#returns type of current command A_COMMAND, C_COMMAND, L_COMMAND
#C A or L(psuedo command for (XxX))
#dest=comp; jmp, #, ()
self.type = self.commands[self.currentCommand][0]
Therefore, you do not need these lines
def main(inputFile):
d = Parser(inputFile)
# d.commandType = "C_COMMAND"
# d.commands = ["D=A+2\\n", "AMD=A+5\\n"]
# d.currentCommand = 0
So you only need this main assuming the input file is correct.
def main(inputFile):
d = Parser(inputFile)
print(d.dest())
Your error is that d.commandType = "C_COMMAND" cannot be "called" by "C_COMMAND()", (i.e. d.commandType())
When I run the line:
def book_processing(pair, pool_length):
p = Pool(len(pool_length)*3)
temp_parameters = partial(book_call_mprocess, pair)
p.map_async(temp_parameters, pool_length).get(999999)
p.close()
p.join()
return exchange_books
I get the following error:
Traceback (most recent call last):
File "test_code.py", line 214, in <module>
current_books = book_call.book_processing(cp, book_list)
File "/home/user/Desktop/book_call.py", line 155, in book_processing
p.map_async(temp_parameters, pool_length).get(999999)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get
raise self._value
zipfile.BadZipfile: Truncated file header
I feel as though there is some resource that is being used that didn't close during the last loop, but I am not sure how to close it (still learning about multiprocessing library). This error only occurs when my code repeats this section relatively quickly (within the same minute). This does not happen often, but is clear when it does.
Edit (adding the book_call code):
def book_call_mprocess(currency_pair, ex_list):
polo_error = 0
live_error = 0
kraken_error = 0
gdax_error = 0
ex_list = set([ex_list])
ex_Polo = 'Polo'
ex_Live = 'Live'
ex_GDAX = 'GDAX'
ex_Kraken = 'Kraken'
cp_polo = 'BTC_ETH'
cp_kraken = 'XETHXXBT'
cp_live = 'ETH/BTC'
cp_GDAX = 'ETH-BTC'
# Instances
polo_instance = poloapi.poloniex(polo_key, polo_secret)
fookraken = krakenapi.API(kraken_key, kraken_secret)
publicClient = GDAX.PublicClient()
flag = False
while not flag:
flag = False
err = False
# Polo Book
try:
if ex_Polo in ex_list:
polo_books = polo_instance.returnOrderBook(cp_polo)
exchange_books['Polo'] = polo_books
except:
err = True
polo_error = 1
# Livecoin
try:
if ex_Live in ex_list:
method = "/exchange/order_book"
live_books = OrderedDict([('currencyPair', cp_live)])
encoded_data = urllib.urlencode(live_books)
sign = hmac.new(live_secret, msg=encoded_data, digestmod=hashlib.sha256).hexdigest().upper()
headers = {"Api-key": live_key, "Sign": sign}
conn = httplib.HTTPSConnection(server)
conn.request("GET", method + '?' + encoded_data, '', headers)
response = conn.getresponse()
live_books = json.load(response)
conn.close()
exchange_books['Live'] = live_books
except:
err = True
live_error = 1
# Kraken
try:
if ex_Kraken in ex_list:
kraken_books = fookraken.query_public('Depth', {'pair': cp_kraken})
exchange_books['Kraken'] = kraken_books
except:
err = True
kraken_error = 1
# GDAX books
try:
if ex_GDAX in ex_list:
gdax_books = publicClient.getProductOrderBook(level=2, product=cp_GDAX)
exchange_books['GDAX'] = gdax_books
except:
err = True
gdax_error = 1
flag = True
if err:
flag = False
err = False
error_list = ['Polo', polo_error, 'Live', live_error, 'Kraken', kraken_error, 'GDAX', gdax_error]
print_to_excel('excel/error_handler.xlsx', 'Book Call Errors', error_list)
print "Holding..."
time.sleep(30)
return exchange_books
def print_to_excel(workbook, worksheet, data_list):
ts = str(datetime.datetime.now()).split('.')[0]
data_list = [ts] + data_list
wb = load_workbook(workbook)
if worksheet == 'active':
ws = wb.active
else:
ws = wb[worksheet]
ws.append(data_list)
wb.save(workbook)
The problem lies in the function print_to_excel
And more specifically in here:
wb = load_workbook(workbook)
If two processes are running this function at the same time, you'll run into the following race condition:
Process 1 wants to open error_handler.xlsx, since it doesn't exist it creates an empty file
Process 2 wants to open error_handler.xlsx, it does exist, so it tries to read it, but it is still empty. Since the xlsx format is just a zip file consisting of a bunch of XML files, the process expects a valid ZIP header which it doesn't find and it omits zipfile.BadZipfile: Truncated file header
What looks strange though is your error message as in the call stack I would have expected to see print_to_excel and load_workbook.
Anyway, Since you confirmed that the problem really is in the XLSX handling you can either
generate a new filename via tempfile for every process
use locking to ensure that only one process runs print_to_excel at a time
I am doing a location search in Google App Engine and I want my search to be sorted based on proximity. I am getting the following error on the deployed version (production):
Search error:
Traceback (most recent call last):
File "/base/data/home/apps/s~sound-helper-87921/1.385231928987755902/application/search_handler.py", line 68, in doProductSearch
search_results = docs.Product.getIndex().search(search_query)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 105, in positional_wrapper
return wrapped(*args, **kwds)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/search/search.py", line 3676, in search
return self.search_async(query, deadline=deadline, **kwargs).get_result()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/search/search.py", line 262, in get_result
return self._get_result_hook()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/search/search.py", line 3690, in hook
_CheckStatus(response.status())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/search/search.py", line 517, in _CheckStatus
raise _ERROR_MAP[status.code()](status.error_detail())
InvalidRequest: Failed to parse search request "distance(location, geopoint(30.008164999999998,-95.52959159999999)) < 2000"; Default text value is not appropriate for sort expression 'distance(location, geopoint(30.008165,-95.529592))'
The following is my code, which is pretty much copied from Google's tutorial:
def _buildQueryString(self, params):
userstr = string = params.get('querystr')
userprice = params.get('price')
userdist = params.get('less_than_distance')
loc = params.get('cur_location')
lat = loc.split(',')[0].split()[0]
lng = loc.split(',')[1].split()[0]
if userstr:
string = userstr
if userprice:
string = string + ' price < %s' % userprice
if userdist:
if not os.environ.get('SERVER_SOFTWARE','').startswith('Development'):
string = string + ' distance(%s, geopoint(%s,%s)) < %s' % (
docs.Product.LOCATION,lat,lng,userdist)
return string
def _buildQuery(self, params):
"""Build and return a search query object."""
user_query = self._buildQueryString(params)
doc_limit = self._getDocLimit()
try:
offsetval = int(params.get('offset', 0))
except ValueError:
offsetval = 0
loc = params.get('cur_location')
lat = loc.split(',')[0].split()[0]
lng = loc.split(',')[1].split()[0]
expr = 'distance(%s, geopoint(%f,%f))' % (docs.Product.LOCATION,float(lat),float(lng))
computed_expr_distance = search.FieldExpression(name='actual_distance',
expression=expr)
computed_expr_score = search.FieldExpression(name='actual_score',
expression='score')
returned_fields = [docs.Product.PID]
expr_list = []
expr_list.append(search.SortExpression(
expression=expr,
direction=search.SortExpression.ASCENDING,
default_value='2001'))
sortopts = search.SortOptions(expressions=expr_list, limit = doc_limit)
search_query = search.Query(
query_string=user_query.strip(),
options=search.QueryOptions(
limit=doc_limit,
offset=offsetval,
sort_options=sortopts,
returned_expressions=[computed_expr_distance],
returned_fields=returned_fields
)
)
return search_query
def doProductSearch(self, params):
"""Perform a product search and display the results."""
try:
search_query = self._buildQuery(params)
search_results = docs.Product.getIndex().search(search_query)
returned_count = len(search_results.results)
except search.Error:
logging.exception("Search error:")
msg = 'There was a search error (see logs).'
url = '/'
print('%s' % msg)
return [],[]
psearch_response = []
distances = []
# For each document returned from the search
for doc in search_results:
pdoc = docs.Product(doc)
for expr in doc.expressions:
if expr.name == 'actual_distance':
distances.append(expr.value)
pid = pdoc.getPID()
psearch_response.append(long(pid))
logging.debug('Distances: ' +str(distances))
return psearch_response, distances
Why is the Search API not recognizing my search query?
The problem was in my default_value. I modified the SortExpression to have an integer default_value instead of a string:
expr_list.append(search.SortExpression(
expression=expr,
direction=search.SortExpression.ASCENDING,
default_value=500000))
I am learning to write Python Scripts for work and I have run into some problems. The script is supposed to read a file, and print the permissions to an email. My problem is I am getting an error when it tries to call the permission() method, and I don't know how to fix it.
Python Code
import smtplib
import os
import stat
result = ""
def permission(file):
s = os.stat(file)
mode = s.st_mode
if(stat.S_IRUSR & mode):
ownerRead = 1
result += ownerRead
else:
ownerRead = 0
result += ownerRead
if(stat.S_IWUSR & mode):
ownerWrite = 1
result += ownerWrite
else:
ownerWrite = 0
result += ownerWrite
if(stat.S_IXUSR & mode):
ownerExecute = 1
result += ownerExecute
else:
ownerExecute = 0
result += ownerExecute
if(stat.S_IRGRP & mode):
groupRead = 1
result += groupRead
else:
groupRead = 0
result += groupRead
if(stat.S_IWGRP & mode):
groupWrite = 1
result += groupWrite
else:
groupWrite = 0
result += groupWrite
if(stat.S_IXGRP & mode):
groupExecute = 1
result += groupExecute
else:
groupExecute = 0
result += groupExecute
if(stat.S_IROTH & mode):
otherRead = 1
result += otherRead
else:
otherRead = 0
result += otherRead
if(stat.S_IWOTH & mode):
otherWrite = 1
result += otherWrite
else:
otherWrite = 0
result += otherWrite
if(stat.S_IXOTH & mode):
otherExecute = 1
result += otherExecute
else:
otherExecute = 0
result += otherExecute
return result
to = 'email#yahoo.com'
gmail_user = 'email#gmail.com'
gmail_pwd = 'pwd'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:permissions \n'
print header
values = permission(file)
print values
msg = header + values
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()
Error Output
Traceback (most recent call last):
File "lastpart.py", line 83, in <module>
values = permission(file)
File "lastpart.py", line 15, in permission
s = os.stat(file)
TypeError: coercing to Unicode: need string or buffer, type found
You fix it by passing the actual filename to the function, not the file built-in type.
>>> file
<type 'file'>
Name your variable something other than file, and actually pass a filename to it. You never actually define the thing you're passing to your call of the function, and thus the only reason it's not crashing with an undefined variable error is because Python happens to already define something with that name, built-in.