const char rodata[] = "abcd"; #include #include #define _GNU_SOURCE #include #include #include struct account { long money; long ducks; const char *name; }; void retrieve_account(struct account *acc); void check_flag(const struct account *acc); int main(int argc, char **argv) { struct account acc; // current user and their money retrieve_account(&acc); srand((int)time(NULL)); /////////// INTERESTING CODE BELOW ////////////// // these are the interesting local variables, packed in a struct so the layout doesn't change struct Order { char buffer[12]; int nDucks; long unitPrice; } order; order.unitPrice = 20 + rand() % 10; setbuf(stdout, NULL); // turn off output buffering printf("Welcome to *Acme Rubber Ducks, Inc.*!……" "For a limited time only, we'd like to *personally* offer *you*, %s, quality rubber ducks at the low, low price of only $%ld per unit!……" "(NOTE!! This offer is NON-TRANSFERABLE and for a LIMITED TIME ONLY. Special TERMS AND CONDITIONS may apply.)……" "You have $%ld in your account, and you have %ld ducks. How many rubber ducks would you like to order? ", acc.name, order.unitPrice, acc.money, acc.ducks); fgets(order.buffer, 512, stdin); order.nDucks = atoi(order.buffer); // atoi converts a string to an int if (order.nDucks < 1) { // we're certainly not going to get tricked by anyone ordering a negative amount of ducks! printf("Sorry, minimum order size is 1\n"); return 0; } long total = order.nDucks * order.unitPrice; if (total > acc.money) { // input validation is important. we're checking everything here! printf("%d ducks @ $%ld/unit makes $%ld, but you only have $%ld! Sorry!\n", order.nDucks, order.unitPrice, total, acc.money); } else { acc.money -= total; acc.ducks += order.nDucks; printf("Thank you for your order of %d ducks @ $%ld/unit!…" "We have charged your account $%ld, and you now have $%ld left and you have %ld rubber ducks in your collection.\n", order.nDucks, order.unitPrice, total, acc.money, acc.ducks); check_flag(&acc); } return 0; } void retrieve_account(struct account *acc) { const char *m = getenv("MONEY"); if (m != NULL) acc->money = atol(m); else acc->money = 1000; const char *d = getenv("DUCKS"); if (d != NULL) acc->ducks = atol(d); else acc->ducks = 0; acc->name = getenv("USER") ? getenv("USER") : "anonymous"; // ^^^ the arrow operator (->) gets a field value from a *pointer to a struct*; it is // equivalent to dereferencing the pointer (*acc) and then selecting the field: (*acc).name } // check if the flag was captured // ctf.py will check if money > 1000 and if so issue a success token void check_flag(const struct account *acc) { if (getenv("TASK_NAME")) { char cmdline[48]; snprintf(cmdline, 47, "python ctf.py check %ld %ld", acc->money, acc->ducks); system(cmdline); } else { printf("Success!\n"); } }