Markopy
Utilizing Markov Models for brute forcing attacks
markovPasswords.cpp
Go to the documentation of this file.
1 /** @file markovPasswords.cpp
2  * @brief Wrapper for Markov::Model to use with char represented models.
3  * @authors Ata Hakçıl, Osman Ömer Yıldıztugay
4  *
5  * This file contains the implementation for Markov::API::MarkovPasswords class.
6  *
7  * @copydoc Markov::API::MarkovPasswords
8  */
9 
10 #include "markovPasswords.h"
11 #include <string.h>
12 #include <chrono>
13 #include <thread>
14 #include <vector>
15 #include <mutex>
16 #include <string>
17 #include <signal.h>
18 #ifdef _WIN32
19 #include <Windows.h>
20 #else
21 #include <unistd.h>
22 #endif
23 
24 static volatile int keepRunning = 1;
25 
26 void intHandler(int dummy) {
27  std::cout << "Terminating.\n";
28  //Sleep(5000);
29  keepRunning = 0;
30  exit(0);
31 }
32 
33 
35 
36 
37 }
38 
39 Markov::API::MarkovPasswords::MarkovPasswords(const char* filename) {
40 
41  std::ifstream* importFile;
42 
43  this->Import(filename);
44 
45  //std::ifstream* newFile(filename);
46 
47  //importFile = newFile;
48 
49 }
50 
51 std::ifstream* Markov::API::MarkovPasswords::OpenDatasetFile(const char* filename){
52 
53  std::ifstream* datasetFile;
54 
55  std::ifstream newFile(filename);
56 
57  datasetFile = &newFile;
58 
59  this->Import(datasetFile);
60  return datasetFile;
61 }
62 
63 
64 
65 void Markov::API::MarkovPasswords::Train(const char* datasetFileName, char delimiter, int threads) {
66  signal(SIGINT, intHandler);
67  Markov::API::Concurrency::ThreadSharedListHandler listhandler(datasetFileName);
68  auto start = std::chrono::high_resolution_clock::now();
69 
70  std::vector<std::thread*> threadsV;
71  for(int i=0;i<threads;i++){
72  threadsV.push_back(new std::thread(&Markov::API::MarkovPasswords::TrainThread, this, &listhandler, delimiter));
73  }
74 
75  for(int i=0;i<threads;i++){
76  threadsV[i]->join();
77  delete threadsV[i];
78  }
79  auto finish = std::chrono::high_resolution_clock::now();
80  std::chrono::duration<double> elapsed = finish - start;
81  std::cout << "Elapsed time: " << elapsed.count() << " s\n";
82 
83 }
84 
86  char format_str[] ="%ld,%s";
87  format_str[3]=delimiter;
88  std::string line;
89  while (listhandler->next(&line) && keepRunning) {
90  long int oc;
91  if (line.size() > 100) {
92  line = line.substr(0, 100);
93  }
94  char* linebuf = new char[line.length()+5];
95 #ifdef _WIN32
96  sscanf_s(line.c_str(), "%ld,%s", &oc, linebuf, line.length()+5); //<== changed format_str to-> "%ld,%s"
97 #else
98  sscanf(line.c_str(), format_str, &oc, linebuf);
99 #endif
100  this->AdjustEdge((const char*)linebuf, oc);
101  delete linebuf;
102  }
103 }
104 
105 
106 std::ofstream* Markov::API::MarkovPasswords::Save(const char* filename) {
107  std::ofstream* exportFile;
108 
109  std::ofstream newFile(filename);
110 
111  exportFile = &newFile;
112 
113  this->Export(exportFile);
114  return exportFile;
115 }
116 
117 
118 void Markov::API::MarkovPasswords::Generate(unsigned long int n, const char* wordlistFileName, int minLen, int maxLen, int threads) {
119  char* res;
120  char print[100];
121  std::ofstream wordlist;
122  wordlist.open(wordlistFileName);
123  std::mutex mlock;
124  int iterationsPerThread = n/threads;
125  int iterationsCarryOver = n%threads;
126  std::vector<std::thread*> threadsV;
127  for(int i=0;i<threads;i++){
128  threadsV.push_back(new std::thread(&Markov::API::MarkovPasswords::GenerateThread, this, &mlock, iterationsPerThread, &wordlist, minLen, maxLen));
129  }
130 
131  for(int i=0;i<threads;i++){
132  threadsV[i]->join();
133  delete threadsV[i];
134  }
135 
136  this->GenerateThread(&mlock, iterationsCarryOver, &wordlist, minLen, maxLen);
137 
138 }
139 
140 void Markov::API::MarkovPasswords::GenerateThread(std::mutex *outputLock, unsigned long int n, std::ofstream *wordlist, int minLen, int maxLen) {
141  char* res = new char[maxLen+5];
142  if(n==0) return;
143 
144  Markov::Random::Marsaglia MarsagliaRandomEngine;
145  for (int i = 0; i < n; i++) {
146  this->RandomWalk(&MarsagliaRandomEngine, minLen, maxLen, res);
147  outputLock->lock();
148  *wordlist << res << "\n";
149  outputLock->unlock();
150  }
151 }
152 
153 void Markov::API::MarkovPasswords::Buff(const char* str, double multiplier, bool bDontAdjustSelfLoops, bool bDontAdjustExtendedLoops){
154  std::string buffstr(str);
155  std::map< char, Node< char > * > *nodes;
156  std::map< char, Edge< char > * > *edges;
157  nodes = this->Nodes();
158  int i=0;
159  for (auto const& [repr, node] : *nodes){
160  edges = node->Edges();
161  for (auto const& [targetrepr, edge] : *edges){
162  if(buffstr.find(targetrepr)!= std::string::npos){
163  if(bDontAdjustSelfLoops && repr==targetrepr) continue;
164  if(bDontAdjustExtendedLoops){
165  if(buffstr.find(repr)!= std::string::npos){
166  continue;
167  }
168  }
169  long int weight = edge->EdgeWeight();
170  weight = weight*multiplier;
171  edge->AdjustEdge(weight);
172  }
173 
174  }
175  i++;
176  }
177 
179 }