Advertisement
Guest User

exceptionally-obfuscated

a guest
Jul 12th, 2015
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <stdexcept>
  4.  
  5. using namespace std;
  6.  
  7. /*
  8.  * This exception is thrown to return the flag to main
  9.  */
  10. class FlagException : public exception
  11. {
  12. public:
  13.     FlagException(const string& flag) : flag(flag) {}
  14.  
  15.     const char *what() { return flag.c_str(); }
  16.  
  17.     virtual ~FlagException() throw() {}
  18.  
  19. private:
  20.     string flag;
  21. };
  22.  
  23. // This global variable just adds a little bit of obfuscation.
  24. // It is first used to hold 31, the size of the flag, and then
  25. // used as a convoluted way to just produce the number 7
  26. int phase=0;
  27.  
  28. /*
  29.  * These two functions keep summing the individual digits of a
  30.  * number till it's single digit. They are only ever called with
  31.  * the number 4165 and return 4+1+6+5=16 -> 1+6=7 so they return 7
  32.  * well, they throw it, instead of returning it
  33.  */
  34.  
  35. void check2525(int n)
  36. {
  37.     int acc=0;
  38.     while(n)
  39.     {
  40.         acc+=n % 10;
  41.         n/=10;
  42.     }
  43.     throw acc;
  44. }
  45.  
  46. void check3235(int n)
  47. {
  48.     for(;;)
  49.     {
  50.         if(n<10) throw n;
  51.         try {
  52.             check2525(n);
  53.         } catch(int& e) {
  54.             n=e;
  55.         }
  56.     }
  57. }
  58.  
  59. /*
  60.  * These functions validate the key and produce the flag
  61.  *
  62.  * Equivalent non obfuscated code
  63.     string flag;
  64.     int m=c.at(31)-'0'; //challenge is one char longer than key
  65.     for(int i=0;i<31;i++)
  66.     {
  67.         int a=c.at(i)-'0';
  68.         int b=k.at(i)-'0';
  69.         int t=(a+b)%10;
  70.         if(t!=7) throw runtime_error("");
  71.         flag+=static_cast<char>('0'+(b+m)%10);
  72.     }
  73.     throw FlagException(flag);
  74.  *
  75.  */
  76.  
  77. void check9786(const string& c)
  78. {
  79.     int m=c.at(31)-'0';
  80.     throw m;
  81. }
  82.  
  83. void check8546(const string& c, const string& k, int i)
  84. {
  85.     int a=c.at(i)-'0';
  86.     int b=k.at(i)-'0';
  87.     int t=(a+b)%10;
  88.     throw t;
  89. }
  90.  
  91. void check0564(const string& k, int m, int i)
  92. {
  93.     int b=k.at(i)-'0';
  94.     char r=static_cast<char>('0'+(b+m)%10);
  95.     throw r;
  96. }
  97.  
  98. void check7544(const string& c, const string& k, int i, int m)
  99. {
  100.     static string flag;
  101.    
  102.     // When we get here, phase is 4165
  103.     int tCheck;
  104.     try {
  105.         check3235(phase);
  106.     } catch(int& e) {
  107.         tCheck=e;
  108.     }
  109.    
  110.     try {
  111.         check8546(c,k,i);
  112.     } catch(int& e) {
  113.         if(e!=tCheck) throw runtime_error("");
  114.     }
  115.    
  116.     try {
  117.         check0564(k,m,i);
  118.     } catch(char& e) {
  119.         flag+=e;
  120.     }
  121.    
  122.     if(i==k.size()-1) throw FlagException(flag);
  123.     throw i;
  124. }
  125.  
  126. void check6553(const string& c, const string& k, int i, int m)
  127. {
  128.     phase+=32;
  129. }
  130.  
  131. void check2455(const string& c, const string& k, int i, int m)
  132. try {
  133.     check7544(c,k,i,m);
  134.     //This call is dead code
  135.     check6553(c,k,i,m);
  136. } catch(int& e) {
  137.     throw e+1;
  138. }
  139.  
  140. void check4564(const string& c, const string& k)
  141. {
  142.     int m;
  143.     try {
  144.         check9786(c);
  145.     } catch(int& e) {
  146.         m=e;
  147.     }
  148.    
  149.     for(int i=0;;)
  150.     {
  151.         try {
  152.             check2455(c,k,i,m);
  153.         } catch(int& e) {
  154.             i=e;
  155.         }
  156.     }
  157. }
  158.  
  159. /*
  160.  * This function just checks that the key is 31 char
  161.  */
  162.  
  163. void check5443(const string& c, const string& k)
  164. {
  165.     // When we get here, phase is 31
  166.     for(;;)
  167.     {
  168.         if(k.size()!=phase) throw runtime_error("");
  169.         phase+=4567;
  170.         throw 1;
  171.     }
  172. }
  173.  
  174. /*
  175.  * These three functions do the same of a single function that does
  176.  *
  177.  * void check4859(const string& c, const string& k)
  178.  * {
  179.  *     phase=31;
  180.  *     check5443(c,k);
  181.  *     phase=4165;
  182.  *     check4564(c,k);
  183.  * }
  184.  *
  185.  */
  186.  
  187. void check6845(const string& c, const string& k, int i)
  188. {
  189.     for(;;)
  190.     {
  191.         switch(i)
  192.         {
  193.             case 0:
  194.                 phase+=31;
  195.                 throw range_error("");
  196.             case 1:
  197.                 phase-=433;
  198.                 throw logic_error("");
  199.             default:
  200.                 throw runtime_error("");
  201.         }
  202.     }
  203. }
  204.  
  205. void check2838(const string& c, const string& k, int i)
  206. try {
  207.     for(;;) check6845(c,k,i);
  208. } catch(logic_error&) {
  209.     check4564(c,k);
  210. }
  211.  
  212. void check4859(const string& c, const string& k)
  213. {
  214.     for(int i=0;;i++)
  215.     {
  216.         try {
  217.             check2838(c,k,i);
  218.         } catch(range_error&) {
  219.             try {
  220.                 check5443(c,k);
  221.             } catch(int) {}
  222.         }
  223.     }
  224. }
  225.  
  226. /*
  227.  * This is simply the main
  228.  */
  229.  
  230. int main(int argc, char *argv[])
  231. try {
  232.     if(argc!=3) throw runtime_error("Wrong args");
  233.     string challenge=argv[1];
  234.     string key=argv[2];
  235.     if(challenge.substr(0,12)=="--challenge=") challenge=challenge.substr(12);
  236.     if(key.substr(0,6)=="--key=") key=key.substr(6);
  237.  
  238.     check4859(challenge,key);
  239.  
  240. } catch(FlagException& f) {
  241.     cout<<"flag{"<<f.what()<<"}"<<endl;
  242. } catch(...) {
  243.     cout<<"Wrong key"<<endl;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement