Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- using namespace std;
- /*
- * This exception is thrown to return the flag to main
- */
- class FlagException : public exception
- {
- public:
- FlagException(const string& flag) : flag(flag) {}
- const char *what() { return flag.c_str(); }
- virtual ~FlagException() throw() {}
- private:
- string flag;
- };
- // This global variable just adds a little bit of obfuscation.
- // It is first used to hold 31, the size of the flag, and then
- // used as a convoluted way to just produce the number 7
- int phase=0;
- /*
- * These two functions keep summing the individual digits of a
- * number till it's single digit. They are only ever called with
- * the number 4165 and return 4+1+6+5=16 -> 1+6=7 so they return 7
- * well, they throw it, instead of returning it
- */
- void check2525(int n)
- {
- int acc=0;
- while(n)
- {
- acc+=n % 10;
- n/=10;
- }
- throw acc;
- }
- void check3235(int n)
- {
- for(;;)
- {
- if(n<10) throw n;
- try {
- check2525(n);
- } catch(int& e) {
- n=e;
- }
- }
- }
- /*
- * These functions validate the key and produce the flag
- *
- * Equivalent non obfuscated code
- string flag;
- int m=c.at(31)-'0'; //challenge is one char longer than key
- for(int i=0;i<31;i++)
- {
- int a=c.at(i)-'0';
- int b=k.at(i)-'0';
- int t=(a+b)%10;
- if(t!=7) throw runtime_error("");
- flag+=static_cast<char>('0'+(b+m)%10);
- }
- throw FlagException(flag);
- *
- */
- void check9786(const string& c)
- {
- int m=c.at(31)-'0';
- throw m;
- }
- void check8546(const string& c, const string& k, int i)
- {
- int a=c.at(i)-'0';
- int b=k.at(i)-'0';
- int t=(a+b)%10;
- throw t;
- }
- void check0564(const string& k, int m, int i)
- {
- int b=k.at(i)-'0';
- char r=static_cast<char>('0'+(b+m)%10);
- throw r;
- }
- void check7544(const string& c, const string& k, int i, int m)
- {
- static string flag;
- // When we get here, phase is 4165
- int tCheck;
- try {
- check3235(phase);
- } catch(int& e) {
- tCheck=e;
- }
- try {
- check8546(c,k,i);
- } catch(int& e) {
- if(e!=tCheck) throw runtime_error("");
- }
- try {
- check0564(k,m,i);
- } catch(char& e) {
- flag+=e;
- }
- if(i==k.size()-1) throw FlagException(flag);
- throw i;
- }
- void check6553(const string& c, const string& k, int i, int m)
- {
- phase+=32;
- }
- void check2455(const string& c, const string& k, int i, int m)
- try {
- check7544(c,k,i,m);
- //This call is dead code
- check6553(c,k,i,m);
- } catch(int& e) {
- throw e+1;
- }
- void check4564(const string& c, const string& k)
- {
- int m;
- try {
- check9786(c);
- } catch(int& e) {
- m=e;
- }
- for(int i=0;;)
- {
- try {
- check2455(c,k,i,m);
- } catch(int& e) {
- i=e;
- }
- }
- }
- /*
- * This function just checks that the key is 31 char
- */
- void check5443(const string& c, const string& k)
- {
- // When we get here, phase is 31
- for(;;)
- {
- if(k.size()!=phase) throw runtime_error("");
- phase+=4567;
- throw 1;
- }
- }
- /*
- * These three functions do the same of a single function that does
- *
- * void check4859(const string& c, const string& k)
- * {
- * phase=31;
- * check5443(c,k);
- * phase=4165;
- * check4564(c,k);
- * }
- *
- */
- void check6845(const string& c, const string& k, int i)
- {
- for(;;)
- {
- switch(i)
- {
- case 0:
- phase+=31;
- throw range_error("");
- case 1:
- phase-=433;
- throw logic_error("");
- default:
- throw runtime_error("");
- }
- }
- }
- void check2838(const string& c, const string& k, int i)
- try {
- for(;;) check6845(c,k,i);
- } catch(logic_error&) {
- check4564(c,k);
- }
- void check4859(const string& c, const string& k)
- {
- for(int i=0;;i++)
- {
- try {
- check2838(c,k,i);
- } catch(range_error&) {
- try {
- check5443(c,k);
- } catch(int) {}
- }
- }
- }
- /*
- * This is simply the main
- */
- int main(int argc, char *argv[])
- try {
- if(argc!=3) throw runtime_error("Wrong args");
- string challenge=argv[1];
- string key=argv[2];
- if(challenge.substr(0,12)=="--challenge=") challenge=challenge.substr(12);
- if(key.substr(0,6)=="--key=") key=key.substr(6);
- check4859(challenge,key);
- } catch(FlagException& f) {
- cout<<"flag{"<<f.what()<<"}"<<endl;
- } catch(...) {
- cout<<"Wrong key"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement