Markopy
Utilizing Markov Models for brute forcing attacks
argparse.h
Go to the documentation of this file.
1 /** @file argparse.h
2  * @brief Arguement handler class for native CPP cli
3  * @authors Celal Sahir Çetiner
4  *
5  * @copydoc Markov::API::CLI::Argparse:
6  */
7 
8 #include<string>
9 #include<iostream>
10 
11 #define BOOST_ALL_STATIC_LIB 1
12 #define BOOST_PROGRAM_OPTIONS_STATIC_LIB 1
13 
14 #include <boost/program_options.hpp>
15 
16 /** @brief Structure to hold parsed cli arguements.
17 */
18 namespace opt = boost::program_options;
19 
20 /**
21  @brief Namespace for the CLI objects
22 */
23 namespace Markov::API::CLI{
24 
25  /** @brief Structure to hold parsed cli arguements. */
26  typedef struct _programOptions {
27  /**
28  @brief Import flag to validate import
29  */
30  bool bImport;
31 
32  /**
33  @brief Export flag to validate export
34  */
35  bool bExport;
36 
37  /**
38  @brief Failure flag to validate succesfull running
39  */
40  bool bFailure;
41 
42  /**
43  @brief Seperator character to use with training data. (character between occurence and value)"
44  */
45  char seperator;
46 
47  /**
48  @brief Import name of our model
49  */
50  std::string importname;
51 
52  /**
53  @brief Import name of our given wordlist
54  */
55  std::string exportname;
56 
57  /**
58  @brief Import name of our given wordlist
59  */
60  std::string wordlistname;
61 
62  /**
63  @brief Output name of our generated password list
64  */
65  std::string outputfilename;
66 
67  /**
68  @brief The name of the given dataset
69  */
70  std::string datasetname;
71 
72  /**
73  @brief Number of passwords to be generated
74  */
75  int generateN;
76 
77  } ProgramOptions;
78 
79 
80  /** @brief Parse command line arguements
81  */
82  class Argparse {
83  public:
84 
86 
87  /** @brief Parse command line arguements.
88  *
89  * Parses command line arguements to populate ProgramOptions structure.
90  *
91  * @param argc Number of command line arguements
92  * @param argv Array of command line parameters
93  */
94  Argparse(int argc, char** argv) {
95 
96  /*bool bImp;
97  bool bExp;
98  bool bFail;
99  char sprt;
100  std::string imports;
101  std::string exports;
102  std::string outputs;
103  std::string datasets;
104  int generateN;
105  */
106  opt::options_description desc("Options");
107 
108 
109  desc.add_options()
110  ("generate", "Generate strings with given parameters")
111  ("train", "Train model with given parameters")
112  ("combine", "Combine")
113  ("import", opt::value<std::string>(), "Import model file")
114  ("output", opt::value<std::string>(), "Output model file. This model will be exported when done. Will be ignored for generation mode")
115  ("dataset", opt::value<std::string>(), "Dataset file to read input from training. Will be ignored for generation mode")
116  ("seperator", opt::value<char>(), "Seperator character to use with training data. (character between occurence and value)")
117  ("wordlist", opt::value<std::string>(), "Wordlist file path to export generation results to. Will be ignored for training mode")
118  ("count", opt::value<int>(), "Number of lines to generate. Ignored in training mode")
119  ("verbosity", "Output verbosity")
120  ("help", "Option definitions");
121 
122  opt::variables_map vm;
123 
124  opt::store(opt::parse_command_line(argc, argv, desc), vm);
125 
126  opt::notify(vm);
127 
128  //std::cout << desc << std::endl;
129  if (vm.count("help")) {
130  std::cout << desc << std::endl;
131  }
132 
133  if (vm.count("output") == 0) this->po.outputfilename = "NULL";
134  else if (vm.count("output") == 1) {
135  this->po.outputfilename = vm["output"].as<std::string>();
136  this->po.bExport = true;
137  }
138  else {
139  this->po.bFailure = true;
140  std::cout << "UNIDENTIFIED INPUT" << std::endl;
141  std::cout << desc << std::endl;
142  }
143 
144 
145  if (vm.count("dataset") == 0) this->po.datasetname = "NULL";
146  else if (vm.count("dataset") == 1) {
147  this->po.datasetname = vm["dataset"].as<std::string>();
148  }
149  else {
150  this->po.bFailure = true;
151  std::cout << "UNIDENTIFIED INPUT" << std::endl;
152  std::cout << desc << std::endl;
153  }
154 
155 
156  if (vm.count("wordlist") == 0) this->po.wordlistname = "NULL";
157  else if (vm.count("wordlist") == 1) {
158  this->po.wordlistname = vm["wordlist"].as<std::string>();
159  }
160  else {
161  this->po.bFailure = true;
162  std::cout << "UNIDENTIFIED INPUT" << std::endl;
163  std::cout << desc << std::endl;
164  }
165 
166  if (vm.count("import") == 0) this->po.importname = "NULL";
167  else if (vm.count("import") == 1) {
168  this->po.importname = vm["import"].as<std::string>();
169  this->po.bImport = true;
170  }
171  else {
172  this->po.bFailure = true;
173  std::cout << "UNIDENTIFIED INPUT" << std::endl;
174  std::cout << desc << std::endl;
175  }
176 
177 
178  if (vm.count("count") == 0) this->po.generateN = 0;
179  else if (vm.count("count") == 1) {
180  this->po.generateN = vm["count"].as<int>();
181  }
182  else {
183  this->po.bFailure = true;
184  std::cout << "UNIDENTIFIED INPUT" << std::endl;
185  std::cout << desc << std::endl;
186  }
187 
188  /*std::cout << vm["output"].as<std::string>() << std::endl;
189  std::cout << vm["dataset"].as<std::string>() << std::endl;
190  std::cout << vm["wordlist"].as<std::string>() << std::endl;
191  std::cout << vm["output"].as<std::string>() << std::endl;
192  std::cout << vm["count"].as<int>() << std::endl;*/
193 
194 
195  //else if (vm.count("train")) std::cout << "train oldu" << std::endl;
196  }
197 
198  /** @brief Getter for command line options
199  *
200  * Getter for ProgramOptions populated by the arguement parser
201  * @returns ProgramOptions structure.
202  */
203  Markov::API::CLI::ProgramOptions getProgramOptions(void) {
204  return this->po;
205  }
206 
207  /** @brief Initialize program options structure.
208  *
209  * @param i boolean, true if import operation is flagged
210  * @param e boolean, true if export operation is flagged
211  * @param bf boolean, true if there is something wrong with the command line parameters
212  * @param s seperator character for the import function
213  * @param iName import filename
214  * @param exName export filename
215  * @param oName output filename
216  * @param dName corpus filename
217  * @param n number of passwords to be generated
218  *
219  */
220  void setProgramOptions(bool i, bool e, bool bf, char s, std::string iName, std::string exName, std::string oName, std::string dName, int n) {
221  this->po.bImport = i;
222  this->po.bExport = e;
223  this->po.seperator = s;
224  this->po.bFailure = bf;
225  this->po.generateN = n;
226  this->po.importname = iName;
227  this->po.exportname = exName;
228  this->po.outputfilename = oName;
229  this->po.datasetname = dName;
230 
231  /*strcpy_s(this->po.importname,256,iName);
232  strcpy_s(this->po.exportname,256,exName);
233  strcpy_s(this->po.outputfilename,256,oName);
234  strcpy_s(this->po.datasetname,256,dName);*/
235 
236  }
237 
238  /** @brief parse cli commands and return
239  * @param argc - Program arguement count
240  * @param argv - Program arguement values array
241  * @return ProgramOptions structure.
242  */
243  static Markov::API::CLI::ProgramOptions* parse(int argc, char** argv);
244 
245 
246  /** @brief Print help string.
247  */
248  static void help();
249 
250  private:
251  /**
252  @brief ProgramOptions structure object
253  */
254 
255  Markov::API::CLI::ProgramOptions po;
256  };
257 
258 };