| 1 | |
|---|
| 2 | #include <string> |
|---|
| 3 | #include <sstream> |
|---|
| 4 | #include <iostream> |
|---|
| 5 | |
|---|
| 6 | #include <cstring> |
|---|
| 7 | #include <cstdlib> |
|---|
| 8 | |
|---|
| 9 | #include <unistd.h> /* setregid() */ |
|---|
| 10 | #include <signal.h> /* raise() */ |
|---|
| 11 | #include <time.h> /* localtime_r() */ |
|---|
| 12 | |
|---|
| 13 | #include "md5.h" |
|---|
| 14 | #include "lmkeywords.h" |
|---|
| 15 | |
|---|
| 16 | class Random |
|---|
| 17 | { |
|---|
| 18 | public: |
|---|
| 19 | static std::string ServerName() |
|---|
| 20 | { |
|---|
| 21 | static char const *server[] = |
|---|
| 22 | { |
|---|
| 23 | "Apache", |
|---|
| 24 | "Apache/1.3.33 (Unix) mod_gzip/1.3.26.1a mod_ssl/2.8.23 OpenSSL/0.9.7e", |
|---|
| 25 | "Apache/1.3.42 (Unix) mod_perl/1.31", |
|---|
| 26 | "Apache/1.3.42 (Unix) PHP/5.3.8 with Suhosin-Patch", |
|---|
| 27 | "Apache/2.0.52 (Red Hat)", |
|---|
| 28 | "Apache/2.0.54 (Fedora)", |
|---|
| 29 | "Apache/2.2.9", |
|---|
| 30 | "Apache/2.2.10 (Unix) PHP/5.2.6", |
|---|
| 31 | "Apache/2.2.16 (Debian)", |
|---|
| 32 | "Apache/2.2.20 (Unix) mod_ssl/2.2.20 OpenSSL/0.9.8e-fips-rhel5 PHP/5.2.5", |
|---|
| 33 | "Apache/2.2.20 (Unix) PHP/5.2.17", |
|---|
| 34 | "Apache/2.2.21 (Unix) PHP/5.2.17", |
|---|
| 35 | "Apache/2.2.3 (CentOS)", |
|---|
| 36 | "Apache/2.2.3 (Oracle)", |
|---|
| 37 | "Apache-Coyote/1.1", |
|---|
| 38 | "lighttpd/1.4.28", |
|---|
| 39 | "lighttpd/1.4.30", |
|---|
| 40 | "Microsoft-IIS/6.0", |
|---|
| 41 | "Microsoft-IIS/7.5", |
|---|
| 42 | "nginx", |
|---|
| 43 | "nginx/0.7.64", |
|---|
| 44 | "nginx/0.8.32", |
|---|
| 45 | "nginx/0.8.54", |
|---|
| 46 | "nginx/1.0.11", |
|---|
| 47 | "nginx/1.1.8", |
|---|
| 48 | "PWS/1.7.3.7", |
|---|
| 49 | "squid/2.7.STABLE9", |
|---|
| 50 | "squid/3.1.10", |
|---|
| 51 | }; |
|---|
| 52 | |
|---|
| 53 | return server[rand() % (sizeof(server) / sizeof(*server))]; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | static std::string Title() |
|---|
| 57 | { |
|---|
| 58 | std::stringstream ret(""); |
|---|
| 59 | |
|---|
| 60 | ret << titles[rand() % (sizeof(titles) / sizeof(*titles))]; |
|---|
| 61 | |
|---|
| 62 | return ret.str(); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | static std::string Keywords(size_t count) |
|---|
| 66 | { |
|---|
| 67 | std::stringstream ret(""); |
|---|
| 68 | |
|---|
| 69 | for (size_t i = 0; i < count; i++) |
|---|
| 70 | { |
|---|
| 71 | if (i) |
|---|
| 72 | ret << ", "; |
|---|
| 73 | ret << keywords[rand() % (sizeof(keywords) / sizeof(*keywords))]; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | return ret.str(); |
|---|
| 77 | } |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | class Utils |
|---|
| 81 | { |
|---|
| 82 | public: |
|---|
| 83 | static void Chroot() |
|---|
| 84 | { |
|---|
| 85 | /* FIXME: the return value is ignored; we can't chroot if we |
|---|
| 86 | * weren't launched setuid, but that's not too bad */ |
|---|
| 87 | chroot("."); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | static void DropPrivileges() |
|---|
| 91 | { |
|---|
| 92 | setregid(getgid(), getgid()); |
|---|
| 93 | setreuid(getuid(), getuid()); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | static std::string Time() |
|---|
| 97 | { |
|---|
| 98 | char now[128]; |
|---|
| 99 | struct tm tm; |
|---|
| 100 | time_t t = time(NULL); |
|---|
| 101 | strftime(now, 128, "%a, %d %b %Y %T %z", localtime_r(&t, &tm)); |
|---|
| 102 | return now; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | static void OhShitShitShit(char const *message) |
|---|
| 106 | { |
|---|
| 107 | /* We don't need to try to recover and be exploited through some |
|---|
| 108 | * shitty vuln I may have forgotten, so just fucking die already */ |
|---|
| 109 | std::cerr << "fatal: " << message << std::endl; |
|---|
| 110 | raise(SIGTERM); |
|---|
| 111 | } |
|---|
| 112 | /** |
|---|
| 113 | * FIXME: I'm not even sure if these functions work right. |
|---|
| 114 | * (Or at all... It's very likely that they don't do anything right.) |
|---|
| 115 | * I stuck the original PHP code in a comment above each function for reference. |
|---|
| 116 | * -- Leon |
|---|
| 117 | */ |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Generate a unique key from the current time and an IP address. It starts |
|---|
| 121 | * with -time() so that newest hits are stored first in alphabetical order. |
|---|
| 122 | */ |
|---|
| 123 | //return sprintf('%08x', 0xffffffff - time()) . substr(md5($ip . rand()), 0, 8); |
|---|
| 124 | static std::string GenKey(char const *client_ip) |
|---|
| 125 | { |
|---|
| 126 | time_t t = time(NULL); |
|---|
| 127 | srand(t); |
|---|
| 128 | |
|---|
| 129 | char buffer[16]; |
|---|
| 130 | char iphash[37]; |
|---|
| 131 | char hash[8]; |
|---|
| 132 | |
|---|
| 133 | sprintf(iphash,"%s%d",client_ip,rand()%32767); |
|---|
| 134 | |
|---|
| 135 | md5((unsigned char*)iphash,8,(unsigned char*)hash); |
|---|
| 136 | |
|---|
| 137 | sprintf(buffer, "%lx", (0xffffffff - t)); |
|---|
| 138 | strcat(buffer,hash); |
|---|
| 139 | |
|---|
| 140 | return buffer; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /* Get back the date value from a key */ |
|---|
| 144 | //return 0xffffffff - hexdec(substr($key, 0, 8)); |
|---|
| 145 | static std::string KeyToDate(char const *key) |
|---|
| 146 | { |
|---|
| 147 | char buffer[8]; |
|---|
| 148 | char prekey[8]; |
|---|
| 149 | |
|---|
| 150 | strncpy(prekey,key,8); |
|---|
| 151 | int hexkey = atoi(prekey); |
|---|
| 152 | sprintf(buffer, "%c", 0xffffffff - hexkey); |
|---|
| 153 | |
|---|
| 154 | return buffer; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | static char * ParseQuery(char toparse[]) |
|---|
| 158 | { |
|---|
| 159 | char * pch = strtok(toparse, "&"); |
|---|
| 160 | |
|---|
| 161 | while (pch != NULL) |
|---|
| 162 | { |
|---|
| 163 | printf("%s\n",pch); |
|---|
| 164 | pch = strtok(NULL, "&"); |
|---|
| 165 | } |
|---|
| 166 | return pch; // TODO return an array/list of somesort. |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | }; |
|---|
| 170 | |
|---|
| 171 | template<int CAPACITY> class Queue |
|---|
| 172 | { |
|---|
| 173 | public: |
|---|
| 174 | Queue() |
|---|
| 175 | { |
|---|
| 176 | m_start = m_count = 0; |
|---|
| 177 | m_poppers = m_pushers = 0; // i like poppers -- Leon |
|---|
| 178 | pthread_mutex_init(&m_mutex, NULL); |
|---|
| 179 | pthread_cond_init(&m_empty_cond, NULL); |
|---|
| 180 | pthread_cond_init(&m_full_cond, NULL); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | ~Queue() |
|---|
| 184 | { |
|---|
| 185 | pthread_cond_destroy(&m_empty_cond); |
|---|
| 186 | pthread_cond_destroy(&m_full_cond); |
|---|
| 187 | pthread_mutex_destroy(&m_mutex); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | void Push(int value) |
|---|
| 191 | { |
|---|
| 192 | pthread_mutex_lock(&m_mutex); |
|---|
| 193 | m_pushers++; |
|---|
| 194 | |
|---|
| 195 | while (m_count == CAPACITY) |
|---|
| 196 | pthread_cond_wait(&m_full_cond, &m_mutex); |
|---|
| 197 | |
|---|
| 198 | m_pushers--; |
|---|
| 199 | m_values[(m_start + m_count) % CAPACITY] = value; |
|---|
| 200 | m_count++; |
|---|
| 201 | |
|---|
| 202 | if (m_poppers) |
|---|
| 203 | pthread_cond_signal(&m_empty_cond); |
|---|
| 204 | |
|---|
| 205 | pthread_mutex_unlock(&m_mutex); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | int Pop() |
|---|
| 209 | { |
|---|
| 210 | pthread_mutex_lock(&m_mutex); |
|---|
| 211 | m_poppers++; |
|---|
| 212 | |
|---|
| 213 | while (m_count == 0) |
|---|
| 214 | pthread_cond_wait(&m_empty_cond, &m_mutex); |
|---|
| 215 | |
|---|
| 216 | m_poppers--; |
|---|
| 217 | int ret = m_values[m_start]; |
|---|
| 218 | m_start = (m_start + 1) % CAPACITY; |
|---|
| 219 | m_count--; |
|---|
| 220 | |
|---|
| 221 | if (m_pushers) |
|---|
| 222 | pthread_cond_signal(&m_full_cond); |
|---|
| 223 | |
|---|
| 224 | pthread_mutex_unlock(&m_mutex); |
|---|
| 225 | return ret; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | private: |
|---|
| 229 | int m_values[CAPACITY]; |
|---|
| 230 | size_t m_start, m_count; |
|---|
| 231 | size_t m_poppers, m_pushers; |
|---|
| 232 | pthread_mutex_t m_mutex; |
|---|
| 233 | pthread_cond_t m_empty_cond, m_full_cond; |
|---|
| 234 | }; |
|---|
| 235 | |
|---|