Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

main.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // snf2check.cpp
  2. //
  3. // Copyright 2002-2004 MicroNeil Research Corporation.
  4. //
  5. // 20040502 _M - Completed testing. Digest checking rocks!
  6. // 20040419 _M - Beginning modification to check an embedded digest of the entire
  7. // rulebase file. The digest is embedded in a way that makes it backward compatible
  8. // with all prior versions.
  9. // This utility opens a Message Sniffer version 2 rule base file and authenticates
  10. // the file. If the authentiaction fails then the utility returns a nonzero value.
  11. // If the file autneticates correctly then the utility returns a zero value.
  12. //
  13. // This utility can be used to check that a download was performed properly so that
  14. // corrupted rule base files won't be placed into service in a Message Sniffer engine.
  15. #include <unistd.h>
  16. #include <cstdio>
  17. #include <cctype>
  18. #include <ctime>
  19. #include <cstdlib>
  20. #include <fstream>
  21. #include <iostream>
  22. #include <string>
  23. #include "SNFMulti/snf_engine.hpp"
  24. using namespace std; //introduces namespace std
  25. using namespace SNFMulti;
  26. const int RULEFILE_PARM = 1; // Rule Base File Parameter.
  27. const int AUTHENTICATION_PARM = 2; // Parm position of authentication.
  28. const int CORRECT_ARGS = 3; // Correct number of command line args.
  29. const int OK = 0; // OK result.
  30. const int ERROR_CMDLINE = 65; // Command line error result.
  31. const int ERROR_RULE_FILE = 67; // Rule file open error result.
  32. const int ERROR_RULE_DATA = 68; // Rule file authentication error result.
  33. const int ERROR_RULE_AUTH = 73; // Authentication failed on the rule base.
  34. const int ERROR_BAD_MATRIX = 71; // Something pushed us out of range.
  35. //////////////////////////////////////////////////////////////////////////////////////////
  36. // main()... well, that's where it all happens.
  37. int main( int argc, char* argv[] ) {
  38. // Check the command line and set option flags.
  39. // First, are the right number of parameters present?
  40. // If not then remind the user of what the command line should look like.
  41. if(argc != CORRECT_ARGS) {
  42. cerr << "snf2check: Bad Command Line" << endl
  43. << endl
  44. << "use snf2check.exe rulefilepath authentication" << endl
  45. << endl
  46. << "Version 2 w/Mangler Digest Checking - build "
  47. << __DATE__ << " " << __TIME__ << endl
  48. << endl
  49. << "Copyright (C) 2002-2008 MicroNeil Research Corporation"
  50. << endl;
  51. return ERROR_CMDLINE;
  52. }
  53. // Capture & rewrite path info...
  54. string RuleFileString(argv[RULEFILE_PARM]); // Rule file path...
  55. string Authentication(argv[AUTHENTICATION_PARM]); // Authentication...
  56. const int LicenseLength = 8; // License IDs are this long.
  57. string License(RuleFileString); // The license is in the rule file string.
  58. int LicensePosition = License.find_last_of("\\/"); // Find the license position.
  59. LicensePosition += 1; // Move past the stroke if present.
  60. string LicenseID( // Extract the licenseID.
  61. License.substr(LicensePosition,LicenseLength));
  62. ///////////////////////////////////////////////////////////////////////////////////////
  63. // Load the RuleBase.
  64. ///////////////////////////////////////////////////////////////////////////////////////
  65. TokenMatrix RuleBase; // Create the rule base object.
  66. try {
  67. RuleBase.Load(RuleFileString);
  68. }
  69. catch(TokenMatrix::BadAllocation) { // If we can't allocate memory report it.
  70. cerr << "snf2check: " << License << " ERROR_RULE_DATA!" << endl;
  71. return ERROR_RULE_DATA;
  72. }
  73. catch(...) { // If something else goes wrong report the following error...
  74. cerr << "snf2check: " << License << " ERROR_RULE_FILE!" << endl;
  75. return ERROR_RULE_FILE;
  76. }
  77. ///////////////////////////////////////////////////////////////////////////////////////
  78. // Validate the RuleBase is properly authenticated.
  79. ///////////////////////////////////////////////////////////////////////////////////////
  80. // SecurityKey starts with
  81. string SecurityKey(LicenseID); // the License ID then we add
  82. SecurityKey += string(argv[AUTHENTICATION_PARM]); // the authentication code.
  83. try {
  84. RuleBase.Validate(SecurityKey);
  85. }
  86. catch(...) { // If the validation fails then we produce this error...
  87. cerr << "snf2check: " << License << " ERROR_RULE_AUTH! " << endl;
  88. return ERROR_RULE_AUTH;
  89. }
  90. ///////////////////////////////////////////////////////////////////////////////////////
  91. // Verify the integrity of the RuleBase - this can only happen if Validated!
  92. ///////////////////////////////////////////////////////////////////////////////////////
  93. try {
  94. RuleBase.Verify(SecurityKey);
  95. }
  96. catch(...) { // If the verification process fails then we produce this error...
  97. cerr << "snf2check: " << License << " ERROR_BAD_MATRIX! " << endl;
  98. return ERROR_BAD_MATRIX;
  99. }
  100. // If we made it through all of that, then we're golden!
  101. return OK;
  102. }