Markopy
Utilizing Markov Models for brute forcing attacks
Python.Markopy.BaseCLI Class Reference

Base CLI class to handle user interactions
More...

Inheritance diagram for Python.Markopy.BaseCLI:
Collaboration diagram for Python.Markopy.BaseCLI:

Public Member Functions

def __init__ (self, bool add_help=True)
 initialize base CLI More...
 
def add_arguments (self)
 
def help (self)
 
def parse (self)
 
def init_post_arguments (self)
 
def parse_arguments (self)
 
def import_model (self, str filename)
 Import a model file. More...
 
def train (self, str dataset, str seperator, str output, bool output_forced=False, bool bulk=False)
 Train a model via CLI parameters. More...
 
def export (self, str filename)
 Export model to a file. More...
 
def generate (self, str wordlist, bool bulk=False)
 Generate strings from the model. More...
 
def process (self)
 Process parameters for operation. More...
 

Static Public Member Functions

def check_import_path (str filename)
 check import path for validity More...
 
def check_corpus_path (str filename)
 check import path for validity More...
 
def check_export_path (str filename)
 check import path for validity More...
 

Public Attributes

 parser
 
 print_help
 
 model
 
 args
 

Private Member Functions

def _generate (self, str wordlist)
 wrapper for generate function. More...
 

Detailed Description

Base CLI class to handle user interactions

Definition at line 16 of file base.py.

Constructor & Destructor Documentation

◆ __init__()

def Python.Markopy.BaseCLI.__init__ (   self,
bool  add_help = True 
)

initialize base CLI

Parameters
add_helpdecide to overload the help function or not

Reimplemented in Python.Markopy.MarkovPasswordsCLI, and Python.Markopy.ModelMatrixCLI.

Definition at line 20 of file base.py.

20  def __init__(self, add_help : bool=True):
21  """!
22  @brief initialize base CLI
23  @param add_help decide to overload the help function or not
24  """
25  self.parser = argparse.ArgumentParser(description="Python wrapper for MarkovPasswords.",
26  epilog=f"""{colored("Sample runs:", "yellow")}
27  {__file__.split("/")[-1]} train untrained.mdl -d dataset.dat -s "\\t" -o trained.mdl
28  Import untrained.mdl, train it with dataset.dat which has tab delimited data, output resulting model to trained.mdl\n
29 
30  {__file__.split("/")[-1]} generate trained.mdl -n 500 -w output.txt
31  Import trained.mdl, and generate 500 lines to output.txt
32 
33  {__file__.split("/")[-1]} combine untrained.mdl -d dataset.dat -s "\\t" -n 500 -w output.txt
34  Train and immediately generate 500 lines to output.txt. Do not export trained model.
35 
36  {__file__.split("/")[-1]} combine untrained.mdl -d dataset.dat -s "\\t" -n 500 -w output.txt -o trained.mdl
37  Train and immediately generate 500 lines to output.txt. Export trained model.
38  """, add_help=add_help, formatter_class=argparse.RawTextHelpFormatter)
39  self.print_help = self.parser.print_help
40  self.model = MarkovModel()
41 

Member Function Documentation

◆ _generate()

def Python.Markopy.BaseCLI._generate (   self,
str  wordlist 
)
private

wrapper for generate function.

This can be overloaded by other models

Parameters
wordlistfilename to generate to

Reimplemented in Python.Markopy.ModelMatrixCLI, and Python.CudaMarkopy.CudaModelMatrixCLI.

Definition at line 161 of file base.py.

161  def _generate(self, wordlist : str):
162  """!
163  @brief wrapper for generate function. This can be overloaded by other models
164  @param wordlist filename to generate to
165  """
166  self.model.Generate(int(self.args.count), wordlist, int(self.args.min), int(self.args.max), int(self.args.threads))
167 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, Python.Markopy.MarkopyCLI.args, Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.generate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_arguments()

def Python.Markopy.BaseCLI.add_arguments (   self)

Reimplemented in Python.Markopy.ModelMatrixCLI, Python.Markopy.MarkopyCLI, Python.Markopy.AbstractTrainingModelCLI, Python.Markopy.AbstractGenerationModelCLI, and Python.CudaMarkopy.CudaModelMatrixCLI.

Definition at line 43 of file base.py.

43  def add_arguments(self):
44  "! @brief Add command line arguements to the parser"
45  self.parser.add_argument("mode", help="Process mode. Either 'Train', 'Generate', or 'Combine'.")
46  self.parser.add_argument("-t", "--threads",default=10, help="Number of lines to generate. Ignored in training mode.")
47  self.parser.add_argument("-v", "--verbosity",action="count", help="Output verbosity.")
48  self.parser.add_argument("-b", "--bulk",action="store_true", help="Bulk generate or bulk train every corpus/model in the folder.")
49 

References Python.Markopy.BaseCLI.parser.

Referenced by Python.Markopy.BaseCLI.parse(), and Python.Markopy.MarkopyCLI.parse().

Here is the caller graph for this function:

◆ check_corpus_path()

def Python.Markopy.BaseCLI.check_corpus_path ( str  filename)
static

check import path for validity

Parameters
filenamefilename to check

Definition at line 181 of file base.py.

181  def check_corpus_path(filename : str):
182  """!
183  @brief check import path for validity
184  @param filename filename to check
185  """
186 
187  if(not os.path.isfile(filename)):
188  return False
189  return True
190 

Referenced by Python.Markopy.BaseCLI.train().

Here is the caller graph for this function:

◆ check_export_path()

def Python.Markopy.BaseCLI.check_export_path ( str  filename)
static

check import path for validity

Parameters
filenamefilename to check

Definition at line 192 of file base.py.

192  def check_export_path(filename : str):
193  """!
194  @brief check import path for validity
195  @param filename filename to check
196  """
197 
198  if(filename and os.path.isfile(filename)):
199  return True
200  return True
201 

Referenced by Python.Markopy.BaseCLI.train().

Here is the caller graph for this function:

◆ check_import_path()

def Python.Markopy.BaseCLI.check_import_path ( str  filename)
static

check import path for validity

Parameters
filenamefilename to check

Definition at line 169 of file base.py.

169  def check_import_path(filename : str):
170  """!
171  @brief check import path for validity
172  @param filename filename to check
173  """
174 
175  if(not os.path.isfile(filename)):
176  return False
177  else:
178  return True
179 

Referenced by Python.Markopy.BaseCLI.import_model().

Here is the caller graph for this function:

◆ export()

def Python.Markopy.BaseCLI.export (   self,
str  filename 
)

Export model to a file.

Parameters
filenamefilename to export to

Definition at line 138 of file base.py.

138  def export(self, filename : str):
139  """!
140  @brief Export model to a file
141  @param filename filename to export to
142  """
143  self.model.Export(filename)
144 

References Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.train().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generate()

def Python.Markopy.BaseCLI.generate (   self,
str  wordlist,
bool  bulk = False 
)

Generate strings from the model.

Parameters
modelmodel instance
wordlistwordlist filename
bulkmarks bulk operation with directories

Definition at line 145 of file base.py.

145  def generate(self, wordlist : str, bulk : bool=False):
146  """!
147  @brief Generate strings from the model
148  @param model: model instance
149  @param wordlist wordlist filename
150  @param bulk marks bulk operation with directories
151  """
152  if not (wordlist or self.args.count):
153  logging.pprint("Generation mode requires -w/--wordlist and -n/--count parameters. Exiting.")
154  return False
155 
156  if(bulk and os.path.isfile(wordlist)):
157  logging.pprint(f"{wordlist} exists and will be overwritten.", 1)
158  self._generate(wordlist)
159 

References Python.CudaMarkopy.CudaModelMatrixCLI._generate(), Python.Markopy.BaseCLI._generate(), Python.Markopy.ModelMatrixCLI._generate(), Python.Markopy.MarkovPasswordsCLI._generate(), Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, and Python.Markopy.MarkopyCLI.args.

Referenced by Python.Markopy.BaseCLI.process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ help()

def Python.Markopy.BaseCLI.help (   self)

Reimplemented in Python.Markopy.MarkopyCLI, and Python.CudaMarkopy.CudaMarkopyCLI.

Definition at line 51 of file base.py.

51  def help(self):
52  "! @brief Handle help strings. Defaults to argparse's help"
53  self.print_help()
54 

References Python.Markopy.BaseCLI.print_help.

Referenced by Python.Markopy.MarkopyCLI.add_arguments().

Here is the caller graph for this function:

◆ import_model()

def Python.Markopy.BaseCLI.import_model (   self,
str  filename 
)

Import a model file.

Parameters
filenamefilename to import

Definition at line 77 of file base.py.

77  def import_model(self, filename : str):
78  """!
79  @brief Import a model file
80  @param filename filename to import
81  """
82  logging.pprint("Importing model file.", 1)
83 
84  if not self.check_import_path(filename):
85  logging.pprint(f"Model file at {filename} not found. Check the file path, or working directory")
86  return False
87 
88  self.model.Import(filename)
89  logging.pprint("Model imported successfully.", 2)
90  return True
91 
92 
93 

References Python.Markopy.BaseCLI.check_import_path(), Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_post_arguments()

def Python.Markopy.BaseCLI.init_post_arguments (   self)

Reimplemented in Python.Markopy.ModelMatrixCLI, Python.Markopy.MarkopyCLI, Python.CudaMarkopy.CudaModelMatrixCLI, and Python.Markopy.MarkopyCLI.

Definition at line 62 of file base.py.

62  def init_post_arguments(self):
63  "! @brief set up stuff that is collected from command line arguements"
64  logging.VERBOSITY = 0
65  try:
66  if self.args.verbosity:
67  logging.VERBOSITY = self.args.verbosity
68  logging.pprint(f"Verbosity set to {self.args.verbosity}.", 2)
69  except:
70  pass
71 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, and Python.Markopy.MarkopyCLI.args.

Referenced by Python.Markopy.BaseCLI.parse(), and Python.Markopy.MarkopyCLI.parse().

Here is the caller graph for this function:

◆ parse()

def Python.Markopy.BaseCLI.parse (   self)

◆ parse_arguments()

def Python.Markopy.BaseCLI.parse_arguments (   self)

Definition at line 73 of file base.py.

73  def parse_arguments(self):
74  "! @brief trigger parser"
75  self.args = self.parser.parse_known_args()[0]
76 

Referenced by Python.Markopy.BaseCLI.parse(), and Python.Markopy.MarkopyCLI.parse().

Here is the caller graph for this function:

◆ process()

def Python.Markopy.BaseCLI.process (   self)

Process parameters for operation.

Reimplemented in Python.Markopy.MarkopyCLI.

Definition at line 202 of file base.py.

202  def process(self):
203  """!
204  @brief Process parameters for operation
205  """
206  if(self.args.bulk):
207  logging.pprint(f"Bulk mode operation chosen.", 4)
208  if (self.args.mode.lower() == "train"):
209  if (os.path.isdir(self.args.output) and not os.path.isfile(self.args.output)) and (os.path.isdir(self.args.dataset) and not os.path.isfile(self.args.dataset)):
210  corpus_list = os.listdir(self.args.dataset)
211  for corpus in corpus_list:
212  self.import_model(self.args.input)
213  logging.pprint(f"Training {self.args.input} with {corpus}", 2)
214  output_file_name = corpus
215  model_extension = ""
216  if "." in self.args.input:
217  model_extension = self.args.input.split(".")[-1]
218  self.train(f"{self.args.dataset}/{corpus}", self.args.seperator, f"{self.args.output}/{corpus}.{model_extension}", output_forced=True, bulk=True)
219  else:
220  logging.pprint("In bulk training, output and dataset should be a directory.")
221  exit(1)
222 
223  elif (self.args.mode.lower() == "generate"):
224  if (os.path.isdir(self.args.wordlist) and not os.path.isfile(self.args.wordlist)) and (os.path.isdir(self.args.input) and not os.path.isfile(self.args.input)):
225  model_list = os.listdir(self.args.input)
226  print(model_list)
227  for input in model_list:
228  logging.pprint(f"Generating from {self.args.input}/{input} to {self.args.wordlist}/{input}.txt", 2)
229  self.import_model(f"{self.args.input}/{input}")
230  model_base = input
231  if "." in self.args.input:
232  model_base = input.split(".")[1]
233  self.generate(f"{self.args.wordlist}/{model_base}.txt", bulk=True)
234  else:
235  logging.pprint("In bulk generation, input and wordlist should be directory.")
236 
237  else:
238  self.import_model(self.args.input)
239  if (self.args.mode.lower() == "generate"):
240  self.generate(self.args.wordlist)
241 
242 
243  elif (self.args.mode.lower() == "train"):
244  self.train(self.args.dataset, self.args.seperator, self.args.output, output_forced=True)
245 
246 
247  elif(self.args.mode.lower() == "combine"):
248  self.train(self.args.dataset, self.args.seperator, self.args.output)
249  self.generate(self.args.wordlist)
250 
251 
252  else:
253  logging.pprint("Invalid mode arguement given.")
254  logging.pprint("Accepted modes: 'Generate', 'Train', 'Combine'")
255  exit(5)
256 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, Python.Markopy.MarkopyCLI.args, Python.Markopy.BaseCLI.generate(), Python.Markopy.BaseCLI.import_model(), Markov::GUI::Generate.train(), Markov::GUI::Train.train(), and Python.Markopy.BaseCLI.train().

Here is the call graph for this function:

◆ train()

def Python.Markopy.BaseCLI.train (   self,
str  dataset,
str  seperator,
str  output,
bool  output_forced = False,
bool  bulk = False 
)

Train a model via CLI parameters.

Parameters
modelModel instance
datasetfilename for the dataset
seperatorseperator used with the dataset
outputoutput filename
output_forcedforce overwrite
bulkmarks bulk operation with directories

Definition at line 94 of file base.py.

94  def train(self, dataset : str, seperator : str, output : str, output_forced : bool=False, bulk : bool=False):
95  """!
96  @brief Train a model via CLI parameters
97  @param model Model instance
98  @param dataset filename for the dataset
99  @param seperator seperator used with the dataset
100  @param output output filename
101  @param output_forced force overwrite
102  @param bulk marks bulk operation with directories
103  """
104  logging.pprint("Training.")
105 
106  if not (dataset and seperator and (output or not output_forced)):
107  logging.pprint(f"Training mode requires -d/--dataset{', -o/--output' if output_forced else''} and -s/--seperator parameters. Exiting.")
108  return False
109 
110  if not bulk and not self.check_corpus_path(dataset):
111  logging.pprint(f"{dataset} doesn't exists. Check the file path, or working directory")
112  return False
113 
114  if not self.check_export_path(output):
115  logging.pprint(f"Cannot create output at {output}")
116  return False
117 
118  if(seperator == '\\t'):
119  logging.pprint("Escaping seperator.", 3)
120  seperator = '\t'
121 
122  if(len(seperator)!=1):
123  logging.pprint(f'Delimiter must be a single character, and "{seperator}" is not accepted.')
124  exit(4)
125 
126  logging.pprint(f'Starting training.', 3)
127  self.model.Train(dataset,seperator, int(self.args.threads))
128  logging.pprint(f'Training completed.', 2)
129 
130  if(output):
131  logging.pprint(f'Exporting model to {output}', 2)
132  self.export(output)
133  else:
134  logging.pprint(f'Model will not be exported.', 1)
135 
136  return True
137 

References Python.CudaMarkopy.CudaMarkopyCLI.args, Python.Markopy.BaseCLI.args, Python.Markopy.MarkopyCLI.args, Python.Markopy.BaseCLI.check_corpus_path(), Python.Markopy.BaseCLI.check_export_path(), Python.Markopy.BaseCLI.export(), Python.CudaMarkopy.CudaModelMatrixCLI.model, Python.Markopy.BaseCLI.model, Python.Markopy.ModelMatrixCLI.model, Python.Markopy.MarkovPasswordsCLI.model, and Markov::GUI::MarkovPasswordsGUI.model().

Referenced by Python.Markopy.BaseCLI.process().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ args

◆ model

◆ parser

◆ print_help

Python.Markopy.BaseCLI.print_help

Definition at line 39 of file base.py.

Referenced by Python.Markopy.BaseCLI.help(), and Python.Markopy.MarkopyCLI.help().


The documentation for this class was generated from the following file: