Markopy
Utilizing Markov Models for brute forcing attacks
Markov::Model< NodeStorageType > Class Template Reference

class for the final Markov Model, constructed from nodes and edges. More...

#include <model.h>

Collaboration diagram for Markov::Model< NodeStorageType >:

Public Member Functions

 Model ()
 Initialize a model with only start and end nodes. More...
 
NodeStorageTypeRandomWalk (Markov::Random::RandomEngine *randomEngine, int minSetting, int maxSetting, NodeStorageType *buffer)
 Do a random walk on this model. More...
 
void AdjustEdge (const NodeStorageType *payload, long int occurrence)
 Adjust the model with a single string. More...
 
bool Import (std::ifstream *)
 Import a file to construct the model. More...
 
bool Import (const char *filename)
 Open a file to import with filename, and call bool Model::Import with std::ifstream. More...
 
bool Export (std::ofstream *)
 Export a file of the model. More...
 
bool Export (const char *filename)
 Open a file to export with filename, and call bool Model::Export with std::ofstream. More...
 
Node< NodeStorageType > * StarterNode ()
 Return starter Node. More...
 
std::vector< Edge< NodeStorageType > * > * Edges ()
 Return a vector of all the edges in the model. More...
 
std::map< NodeStorageType, Node< NodeStorageType > * > * Nodes ()
 Return starter Node. More...
 
void OptimizeEdgeOrder ()
 Sort edges of all nodes in the model ordered by edge weights. More...
 

Private Attributes

std::map< NodeStorageType, Node< NodeStorageType > * > nodes
 Map LeftNode is the Nodes NodeValue Map RightNode is the node pointer. More...
 
Node< NodeStorageType > * starterNode
 Starter Node of this model. More...
 
std::vector< Edge< NodeStorageType > * > edges
 A list of all edges in this model. More...
 

Detailed Description

template<typename NodeStorageType>
class Markov::Model< NodeStorageType >

class for the final Markov Model, constructed from nodes and edges.

Each atomic piece of the generation result is stored in a node, while edges contain the relation weights. Extending: To extend the class, implement the template and inherit from it, as "class MyModel : public Markov::Model<char>". For a complete demonstration of how to extend the class, see MarkovPasswords.

Whole model can be defined as a list of the edges, as dangling nodes are pointless. This approach is used for the import/export operations. For more information on importing/exporting model, check out the github readme and wiki page.

Definition at line 45 of file model.h.

Constructor & Destructor Documentation

◆ Model()

template<typename NodeStorageType >
Markov::Model< NodeStorageType >::Model

Initialize a model with only start and end nodes.

Initialize an empty model with only a starterNode Starter node is a special kind of node that has constant 0x00 value, and will be used to initiate the generation execution from.

Definition at line 210 of file model.h.

210  {
212  this->nodes.insert({ 0, this->starterNode });
213 }
std::map< NodeStorageType, Node< NodeStorageType > * > nodes
Map LeftNode is the Nodes NodeValue Map RightNode is the node pointer.
Definition: model.h:193
Node< NodeStorageType > * starterNode
Starter Node of this model.
Definition: model.h:198

Member Function Documentation

◆ AdjustEdge()

template<typename NodeStorageType >
void Markov::Model< NodeStorageType >::AdjustEdge ( const NodeStorageType payload,
long int  occurrence 
)

Adjust the model with a single string.

Start from the starter node, and for each character, AdjustEdge the edge EdgeWeight from current node to the next, until NULL character is reached.

Then, update the edge EdgeWeight from current node, to the terminator node.

This function is used for training purposes, as it can be used for adjusting the model with each line of the corpus file.

Example Use: Create an empty model and train it with string: "testdata"

char test[] = "testdata";
model.AdjustEdge(test, 15);
void AdjustEdge(const NodeStorageType *payload, long int occurrence)
Adjust the model with a single string.
Definition: model.h:337
Parameters
string- String that is passed from the training, and will be used to AdjustEdge the model with
occurrence- Occurrence of this string.

Definition at line 337 of file model.h.

337  {
338  NodeStorageType p = payload[0];
341  int i = 0;
342 
343  if (p == 0) return;
344  while (p != 0) {
345  e = curnode->FindEdge(p);
346  if (e == NULL) return;
347  e->AdjustEdge(occurrence);
348  curnode = e->RightNode();
349  p = payload[++i];
350  }
351 
352  e = curnode->FindEdge('\xff');
353  e->AdjustEdge(occurrence);
354  return;
355 }
Edge class used to link nodes in the model together.
Definition: edge.h:23
Node< NodeStorageType > * RightNode()
return edge's RightNode
Definition: edge.h:170
void AdjustEdge(long int offset)
Adjust the edge EdgeWeight with offset. Adds the offset parameter to the edge EdgeWeight.
Definition: edge.h:137
Edge< storageType > * FindEdge(storageType repr)
Find an edge with its character representation.
Definition: node.h:260

Referenced by Markov::API::MarkovPasswords::TrainThread().

Here is the caller graph for this function:

◆ Edges()

template<typename NodeStorageType >
std::vector<Edge<NodeStorageType>*>* Markov::Model< NodeStorageType >::Edges ( )
inline

Return a vector of all the edges in the model.

Returns
vector of edges

Definition at line 176 of file model.h.

176 { return &edges;}
std::vector< Edge< NodeStorageType > * > edges
A list of all edges in this model.
Definition: model.h:204

◆ Export() [1/2]

template<typename NodeStorageType >
bool Markov::Model< NodeStorageType >::Export ( const char *  filename)

Open a file to export with filename, and call bool Model::Export with std::ofstream.

Returns
True if successful, False for incomplete models or corrupt file formats

Example Use: Export file to filename

model.Export("test.mdl");
bool Export(std::ofstream *)
Export a file of the model.
Definition: model.h:288

Definition at line 300 of file model.h.

300  {
301  std::ofstream exportfile;
302  exportfile.open(filename);
303  return this->Export(&exportfile);
304 }

Referenced by Markov::Markopy::CUDA::BOOST_PYTHON_MODULE(), Markov::Markopy::BOOST_PYTHON_MODULE(), and main().

Here is the caller graph for this function:

◆ Export() [2/2]

template<typename NodeStorageType >
bool Markov::Model< NodeStorageType >::Export ( std::ofstream *  f)

Export a file of the model.

File contains a list of edges. Format is: Left_repr;EdgeWeight;right_repr. For more information on the format, check out the project wiki or github readme.

Iterate over this vertices, and their edges, and write them to file.

Returns
True if successful, False for incomplete models.

Example Use: Export file to ofstream

std::ofstream file("test.mdl");
model.Export(&file);

Definition at line 288 of file model.h.

288  {
290  for (std::vector<int>::size_type i = 0; i != this->edges.size(); i++) {
291  e = this->edges[i];
292  //std::cout << e->LeftNode()->NodeValue() << "," << e->EdgeWeight() << "," << e->RightNode()->NodeValue() << "\n";
293  *f << e->LeftNode()->NodeValue() << "," << e->EdgeWeight() << "," << e->RightNode()->NodeValue() << "\n";
294  }
295 
296  return true;
297 }
uint64_t EdgeWeight()
return edge's EdgeWeight.
Definition: edge.h:160
Node< NodeStorageType > * LeftNode()
return edge's LeftNode
Definition: edge.h:165
unsigned char NodeValue()
Return character representation of this node.
Definition: node.h:215
f
output file handle
Definition: model_2gram.py:16

Referenced by Markov::API::MarkovPasswords::Save().

Here is the caller graph for this function:

◆ Import() [1/2]

template<typename NodeStorageType >
bool Markov::Model< NodeStorageType >::Import ( const char *  filename)

Open a file to import with filename, and call bool Model::Import with std::ifstream.

Returns
True if successful, False for incomplete models or corrupt file formats

Example Use: Import a file with filename

model.Import("test.mdl");
bool Import(std::ifstream *)
Import a file to construct the model.
Definition: model.h:216

Definition at line 280 of file model.h.

280  {
281  std::ifstream importfile;
282  importfile.open(filename);
283  return this->Import(&importfile);
284 
285 }

Referenced by Markov::Markopy::BOOST_PYTHON_MODULE(), Markov::API::ModelMatrix::Import(), and Markov::API::MarkovPasswords::MarkovPasswords().

Here is the caller graph for this function:

◆ Import() [2/2]

template<typename NodeStorageType >
bool Markov::Model< NodeStorageType >::Import ( std::ifstream *  f)

Import a file to construct the model.

File contains a list of edges. For more info on the file format, check out the wiki and github readme pages. Format is: Left_repr;EdgeWeight;right_repr

Iterate over this list, and construct nodes and edges accordingly.

Returns
True if successful, False for incomplete models or corrupt file formats

Example Use: Import a file from ifstream

std::ifstream file("test.mdl");
model.Import(&file);

Definition at line 216 of file model.h.

216  {
217  std::string cell;
218 
219  char src;
220  char target;
221  long int oc;
222 
223  while (std::getline(*f, cell)) {
224  //std::cout << "cell: " << cell << std::endl;
225  src = cell[0];
226  target = cell[cell.length() - 1];
227  char* j;
228  oc = std::strtol(cell.substr(2, cell.length() - 2).c_str(),&j,10);
229  //std::cout << oc << "\n";
233  if (this->nodes.find(src) == this->nodes.end()) {
234  srcN = new Markov::Node<NodeStorageType>(src);
235  this->nodes.insert(std::pair<char, Markov::Node<NodeStorageType>*>(src, srcN));
236  //std::cout << "Creating new node at start.\n";
237  }
238  else {
239  srcN = this->nodes.find(src)->second;
240  }
241 
242  if (this->nodes.find(target) == this->nodes.end()) {
243  targetN = new Markov::Node<NodeStorageType>(target);
244  this->nodes.insert(std::pair<char, Markov::Node<NodeStorageType>*>(target, targetN));
245  //std::cout << "Creating new node at end.\n";
246  }
247  else {
248  targetN = this->nodes.find(target)->second;
249  }
250  e = srcN->Link(targetN);
251  e->AdjustEdge(oc);
252  this->edges.push_back(e);
253 
254  //std::cout << int(srcN->NodeValue()) << " --" << e->EdgeWeight() << "--> " << int(targetN->NodeValue()) << "\n";
255 
256 
257  }
258 
259  this->OptimizeEdgeOrder();
260 
261  return true;
262 }
void OptimizeEdgeOrder()
Sort edges of all nodes in the model ordered by edge weights.
Definition: model.h:265
Edge< storageType > * Link(Node< storageType > *)
Link this node with another, with this node as its source.
Definition: node.h:220

Referenced by Markov::API::MarkovPasswords::OpenDatasetFile().

Here is the caller graph for this function:

◆ Nodes()

template<typename NodeStorageType >
std::map<NodeStorageType, Node<NodeStorageType>*>* Markov::Model< NodeStorageType >::Nodes ( )
inline

Return starter Node.

Returns
starter node with 00 NodeValue

Definition at line 181 of file model.h.

181 { return &nodes;}

Referenced by Markov::API::MarkovPasswords::Buff(), and Markov::API::ModelMatrix::ConstructMatrix().

Here is the caller graph for this function:

◆ OptimizeEdgeOrder()

template<typename NodeStorageType >
void Markov::Model< NodeStorageType >::OptimizeEdgeOrder

Sort edges of all nodes in the model ordered by edge weights.

Definition at line 265 of file model.h.

265  {
266  for (std::pair<unsigned char, Markov::Node<NodeStorageType>*> const& x : this->nodes) {
267  //std::cout << "Total edges in EdgesV: " << x.second->edgesV.size() << "\n";
268  std::sort (x.second->edgesV.begin(), x.second->edgesV.end(), [](Edge<NodeStorageType> *lhs, Edge<NodeStorageType> *rhs)->bool{
269  return lhs->EdgeWeight() > rhs->EdgeWeight();
270  });
271  //for(int i=0;i<x.second->edgesV.size();i++)
272  // std::cout << x.second->edgesV[i]->EdgeWeight() << ", ";
273  //std::cout << "\n";
274  }
275  //std::cout << "Total number of nodes: " << this->nodes.size() << std::endl;
276  //std::cout << "Total number of edges: " << this->edges.size() << std::endl;
277 }

Referenced by Markov::API::MarkovPasswords::Buff().

Here is the caller graph for this function:

◆ RandomWalk()

template<typename NodeStorageType >
NodeStorageType * Markov::Model< NodeStorageType >::RandomWalk ( Markov::Random::RandomEngine randomEngine,
int  minSetting,
int  maxSetting,
NodeStorageType buffer 
)

Do a random walk on this model.

Start from the starter node, on each node, invoke RandomNext using the random engine on current node, until terminator node is reached. If terminator node is reached before minimum length criateria is reached, ignore the last selection and re-invoke randomNext

If maximum length criteria is reached but final node is not, cut off the generation and proceed to the final node. This function takes Markov::Random::RandomEngine as a parameter to generate pseudo random numbers from

This library is shipped with two random engines, Marsaglia and Mersenne. While mersenne output is higher in entropy, most use cases don't really need super high entropy output, so Markov::Random::Marsaglia is preferable for better performance.

This function WILL NOT reallocate buffer. Make sure no out of bound writes are happening via maximum length criteria.

Example Use: Generate 10 lines, with 5 to 10 characters, and print the output. Use Marsaglia

Model.import("model.mdl");
char* res = new char[11];
Markov::Random::Marsaglia MarsagliaRandomEngine;
for (int i = 0; i < 10; i++) {
this->RandomWalk(&MarsagliaRandomEngine, 5, 10, res);
std::cout << res << "\n";
}
NodeStorageType * RandomWalk(Markov::Random::RandomEngine *randomEngine, int minSetting, int maxSetting, NodeStorageType *buffer)
Do a random walk on this model.
Definition: model.h:307
Model()
Initialize a model with only start and end nodes.
Definition: model.h:210
Implementation of Marsaglia Random Engine.
Definition: random.h:125
Parameters
randomEngineRandom Engine to use for the random walks. For examples, see Markov::Random::Mersenne and Markov::Random::Marsaglia
minSettingMinimum number of characters to generate
maxSettingMaximum number of character to generate
bufferbuffer to write the result to
Returns
Null terminated string that was generated.

Definition at line 307 of file model.h.

307  {
309  int len = 0;
311  while (true) {
312  temp_node = n->RandomNext(randomEngine);
313  if (len >= maxSetting) {
314  break;
315  }
316  else if ((temp_node == NULL) && (len < minSetting)) {
317  continue;
318  }
319 
320  else if (temp_node == NULL){
321  break;
322  }
323 
324  n = temp_node;
325 
326  buffer[len++] = n->NodeValue();
327  }
328 
329  //null terminate the string
330  buffer[len] = 0x00;
331 
332  //do something with the generated string
333  return buffer; //for now
334 }
Node< storageType > * RandomNext(Markov::Random::RandomEngine *randomEngine)
Chose a random node from the list of edges, with regards to its EdgeWeight, and TraverseNode to that.
Definition: node.h:234

Referenced by Markov::API::MarkovPasswords::GenerateThread().

Here is the caller graph for this function:

◆ StarterNode()

template<typename NodeStorageType >
Node<NodeStorageType>* Markov::Model< NodeStorageType >::StarterNode ( )
inline

Return starter Node.

Returns
starter node with 00 NodeValue

Definition at line 171 of file model.h.

171 { return starterNode;}

Referenced by Markov::API::ModelMatrix::ConstructMatrix().

Here is the caller graph for this function:

Member Data Documentation

◆ edges

template<typename NodeStorageType >
std::vector<Edge<NodeStorageType>*> Markov::Model< NodeStorageType >::edges
private

A list of all edges in this model.

Definition at line 204 of file model.h.

Referenced by Markov::Model< char >::Edges().

◆ nodes

template<typename NodeStorageType >
std::map<NodeStorageType, Node<NodeStorageType>*> Markov::Model< NodeStorageType >::nodes
private

Map LeftNode is the Nodes NodeValue Map RightNode is the node pointer.

Definition at line 193 of file model.h.

Referenced by Markov::Model< char >::Nodes().

◆ starterNode

template<typename NodeStorageType >
Node<NodeStorageType>* Markov::Model< NodeStorageType >::starterNode
private

Starter Node of this model.

Definition at line 198 of file model.h.

Referenced by Markov::Model< char >::StarterNode().


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