#define _GNU_SOURCE #include #include #include #include #include #include // This one has been removed from the library due to security issues... char* gets(char* s); ////////// // Forward/external declarations ////////// /** The gadget */ void getFlag(); /** Print help */ void help(); /** Prints the contents of main's stack frame (defined externally) */ void dump_mem(); /** Check if 's' matches 'command_name'; abbreviations allowed */ bool is_command(const char* command_name, const char* s); // In Java, Python, etc. an object variable is always a *reference* to an object. // C doesn't have references, so we have to use pointers instead. typedef struct PondAnimal* PondAnimal; // PondAnimal type is now a pointer to a PondAnimal typedef struct Pond* Pond; // Pond type is now a pointer to a Pond ////////// // *** Class interface declaration for Pond ////////// struct Pond { //// fields /** Pond size */ long width; /** Points to the resident animal */ PondAnimal animal; //// "methods" – we have to pass 'this' manually when we call them /** Do one step of the pond simulation, return true if we should continue */ bool (*step)(const Pond this); /** Returns false if animal is outside the bounds of the pond */ bool (*contains)(const Pond this, const PondAnimal animal); /** Print the pond to the console. Animals will be displayed using their get_icon() method */ void (*draw)(const Pond pond); }; // constructors (defined externally) Pond Pond_init(Pond this); ////////// // *** Class interface declaration for PondAnimal ////////// struct PondAnimal { //// fields /* Current position */ float x; /* Current velocity */ float dx; /* Name of the (individual) animal */ char name[16]; // *** perhaps we can overflow this buffer? //// "methods" – we have to pass 'this' manually when we call them /** Print whatever the animal says */ void (*make_noise)(const PondAnimal this); /** Returns an icon to be displayed on screen */ const char* (*get_icon)(const PondAnimal this); /** Move and maybe make some noise */ void (*step)(PondAnimal this); }; // constructors for two variants ("subclasses") of PondAnimal PondAnimal Duck_init(PondAnimal this, const Pond pond); PondAnimal Frog_init(PondAnimal this, const Pond pond); // ignore – these are used by dump_mem() void* bottom_of_main_stackframe; const int stackframe_size_in_words = 20; int main(int argc, char** argv) { bottom_of_main_stackframe = &argv; srand(time(NULL)); // With HARD_MODE, limit reads into the command buffer so it can't overflow int BUFFER_SIZE = getenv("HARD_MODE") ? 16 : 128; // Put local vars in a struct so they are more buffer-overflowable... struct { char buffer[16]; // *** perhaps we can overflow this buffer? struct Pond pond; // *** interesting function pointers here? struct PondAnimal animal; // *** interesting function pointers here? } data; register Pond pond = &data.pond; // just convenient short-hand register PondAnimal animal = &data.animal;// just convenient short-hand // "Construct" the pond Pond_init(pond); // Fill it with a duck or a frog pond->animal = rand() % 2 == 0 ? Duck_init(animal, pond) : Frog_init(animal, pond); printf("Welcome to THiS, Inc.'s Object-Oriented Pond Simulator.\n"); help(); printf("\n"); while (true) { // draw pond and prompt for command pond->draw(pond); // calls animal->get_icon() to draw animal printf(" command? "); fflush(stdout); // read command into buffer if (!fgets(data.buffer, BUFFER_SIZE, stdin)) // *** overflowable in easy mode break; // evaluate command if (is_command("exit", data.buffer)) { break; } else if (is_command("mem", data.buffer)) { dump_mem(); } else if (is_command("name", data.buffer)) { printf("This animal's current name is '%s'. What do you want to call it? ", animal->name); fflush(stdout); if (!gets(animal->name)) // *** uses unsafe gets() break; printf("PondAnimal renamed to '%s'.\n", animal->name); } else if (is_command("step", data.buffer) || is_command("", data.buffer)) { // pond->step() will call pond->contains() and animal->step() (which might call animal->make_noise()) if (!pond->step(pond)) break; } else if (is_command("help", data.buffer)) { help(); } else { printf("Unknown command %s", data.buffer); } } printf("Exiting\n"); fflush(stdout); return 0; } // Call this to win! void getFlag() { if (getenv("TASK_NAME")) { printf("Success!\n"); fflush(stdout); system("python ctf.py success"); exit(0); } else { printf("Success!\n"); fflush(stdout); exit(0); } } void help() { printf("\n" // "(e)xit -- end the simulation\n" "(h)elp -- print help\n" "(m)em -- dump memory\n" "(n)ame -- rename animal\n" "(s)tep, NEWLINE -- move one step\n" "(e)xit, EOF -- exit\n" // ); }