mangle.cgi.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. HTML manglizer
  3. --------------
  4. Copyright (C) 2004 by Michal Zalewski <lcamtuf@coredump.cx>
  5. HTML manglizer library. Logs random seeds to error-log; find the last entry before
  6. crash, then pass it to remangle.cgi to reproduce the problem.
  7. */
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include "tags.h"
  14. #define R(x) (rand() % (x))
  15. #define MAXTCOUNT 100
  16. #define MAXPCOUNT 20
  17. #define MAXSTR2 80
  18. void make_up_value(void) {
  19. char c=R(2);
  20. if (c) putchar('"');
  21. switch (R(31)) {
  22. case 0: printf("javascript:"); make_up_value(); break;
  23. // case 1: printf("jar:"); make_up_value(); break;
  24. case 2: printf("mk:"); make_up_value(); break;
  25. case 3: printf("file:"); make_up_value(); break;
  26. case 4: printf("http:"); make_up_value(); break;
  27. case 5: printf("about:"); make_up_value(); break;
  28. case 6: printf("_blank"); break;
  29. case 7: printf("_self"); break;
  30. case 8: printf("top"); break;
  31. case 9: printf("left"); break;
  32. case 10: putchar('&'); make_up_value(); putchar(';'); break;
  33. case 11: make_up_value(); make_up_value(); break;
  34. case 12 ... 20: {
  35. int c = R(10) ? R(10) : (1 + R(MAXSTR2) * R(MAXSTR2));
  36. char* x = malloc(c);
  37. memset(x,R(256),c);
  38. fwrite(x,c,1,stdout);
  39. free(x);
  40. break;
  41. }
  42. case 21: printf("%s","%n%n%n%n%n%n"); break;
  43. case 22: putchar('#'); break;
  44. case 23: putchar('*'); break;
  45. default: if (R(2)) putchar('-'); printf("%d",rand()); break;
  46. }
  47. if (c) putchar('"');
  48. }
  49. void random_tag(void) {
  50. int tn, tc;
  51. do tn = R(MAXTAGS); while (!tags[tn][0]);
  52. tc = R(MAXPCOUNT) + 1;
  53. putchar('<');
  54. switch (R(10)) {
  55. case 0: putchar(R(256)); break;
  56. case 1: putchar('/');
  57. }
  58. printf("%s", tags[tn][0]);
  59. while (tc--) {
  60. int pn;
  61. switch (R(32)) {
  62. case 0: putchar(R(256));
  63. case 1: break;
  64. default: putchar(' ');
  65. }
  66. do pn = R(MAXPARS-1) + 1; while (!tags[tn][pn]);
  67. printf("%s", tags[tn][pn]);
  68. switch (R(32)) {
  69. case 0: putchar(R(256));
  70. case 1: break;
  71. default: putchar('=');
  72. }
  73. make_up_value();
  74. }
  75. putchar('>');
  76. }
  77. int main(int argc,char** argv) {
  78. int tc,seed;
  79. printf("Content-Type: text/html;charset=utf-8\nRefresh: 0;URL=mangle.cgi\n\n");
  80. printf("<HTML><HEAD><META HTTP-EQUIV=\"Refresh\" content=\"0;URL=mangle.cgi\">\n");
  81. printf("<script language=\"javascript\">setTimeout('window.location=\"mangle.cgi\"', 1000);</script>\n");
  82. seed = (time(0) ^ (getpid() << 16));
  83. fprintf(stderr,"[%u] Mangle attempt 0x%08x (%s) -- %s\n", (int)time(0), seed, getenv("HTTP_USER_AGENT"), getenv("REMOTE_ADDR"));
  84. srand(seed);
  85. tc = R(MAXTCOUNT) + 1;
  86. while (tc--) random_tag();
  87. fflush(0);
  88. return 0;
  89. }