Advertisement
Guest User

Untitled

a guest
Nov 30th, 2011
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.07 KB | None | 0 0
  1. #ifndef WIN32
  2.     #define _GNU_SOURCE
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. #include <time.h>
  10. #include <sys/stat.h>
  11. #include <winsock.h>
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include <process.h>
  15.  
  16. #ifdef WIN32
  17.     #include <winsock.h>
  18.     #include <windows.h>
  19.     #include <stdio.h>
  20.     #include <stdlib.h>
  21.     #include <string.h>
  22.     #include <stdint.h>
  23.     #include <time.h>
  24.     #include <sys/stat.h>
  25.     #include <stdlib.h>
  26.     #include <process.h>
  27.     typedef int socklen_t;
  28. #else
  29.     #include <unistd.h>
  30.     #include <sys/socket.h>
  31.     #include <sys/types.h>
  32.     #include <arpa/inet.h>
  33.     #include <netinet/in.h>
  34.     #include <netdb.h>
  35.     #include <dlfcn.h>  // -ldl
  36.     #define myrecvfrom recvfrom
  37.     static ssize_t (*old_recvfrom)(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) = NULL;
  38. #endif
  39.  
  40. typedef uint8_t     u8;
  41. typedef uint16_t    u16;
  42. typedef uint32_t    u32;
  43.  
  44.  
  45. /*
  46.     DEKLARACIJA VARIJABLI
  47. */
  48. #define MAXIPS 256  // Maksimalan broj IP adresa u tabelama
  49.  
  50. #define GETSTATUS "\xFF\xFF\xFF\xFFgetstatus"
  51. #define GETSTATUS_LEN 13
  52.  
  53. //****************************************************************************************************
  54.  
  55. // TABELA ZA GETSTATUS
  56. typedef struct {
  57.     u32     ip;
  58.     DWORD   zadnjigetstatus,
  59.             brojac;
  60. } getstatust_t;
  61.  
  62. // TABELA ZA BANOVE
  63. typedef struct {
  64.     u32    ip;
  65.     DWORD  banirando;
  66. } banovit_t;
  67.  
  68.  
  69. //****************************************************************************************************
  70.  
  71. static getstatust_t     getstatust[MAXIPS] = {{0,0,0}};
  72. static banovit_t        banovit[MAXIPS] = {{0,0}};
  73.  
  74. //****************************************************************************************************
  75. // PRIMLJENI PAKETI
  76. ssize_t myrecvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) {
  77.     DWORD  sada;
  78.     u32     ip;
  79.     int     i,
  80.             stari;
  81.  
  82. #ifndef WIN32
  83.     if(!old_recvfrom) old_recvfrom = dlsym(RTLD_NEXT, "recvfrom");
  84.     len = old_recvfrom(s, buf, len, flags, from, fromlen);
  85. #endif
  86.  
  87.         if(!from) return(len);
  88.         if((int)len <= 0) return(len);
  89.  
  90.     if((int)len > 0)
  91.     {
  92.         ip   = ((struct sockaddr_in *)from)->sin_addr.s_addr;
  93.  
  94.         //****************************************************************************************************     
  95.         //  GETSTATUS
  96.         if(!strncasecmp(GETSTATUS,buf,GETSTATUS_LEN)) {
  97.             for(i = 0; i < MAXIPS; i++) {   // Provjeri je li IP u tabeli
  98.                 if(ip != getstatust[i].ip) continue; // Ako to nije IP iz tabele prijedi na sljedeci
  99.                
  100.                 if(GetTickCount() < (getstatust[i].zadnjigetstatus + 250)) { // provjeri je li prošlo 250 ms od zadnjeg getstatusa
  101.                     //len = 0;                // postavi paket na nulu za disconnect
  102.                     getstatust[i].brojac++; // povecaj brojac
  103.                 }
  104.                
  105.                 if(getstatust[i].brojac > 10){ // ako je više od 10 zahtjeva, dodaj IP u ban listu
  106.                     banovit[i].ip = getstatust[i].ip; // dodaj IP u bazu banova
  107.                     banovit[i].banirando = GetTickCount() + 10000; // dodaj 10 sekundi na vrijeme za ban
  108.                     getstatust[i].brojac = 0; // resetiraj brojac da se nanovo provjeri nastavak flooda
  109.                 }
  110.                 getstatust[i].zadnjigetstatus = GetTickCount(); // postavi vrijeme zadnjeg upcita na trenutno
  111.                 return(len); // vrati paket
  112.             }
  113.             stari = 0;
  114.             for(i = 0; i < MAXIPS; i++) {   // dodaj ili obnovi IP
  115.                 if(!getstatust[i].ip) {     // prazno mjesto
  116.                     stari = i;
  117.                     break;
  118.                 }
  119.                 if(getstatust[i].zadnjigetstatus < getstatust[stari].zadnjigetstatus) {
  120.                     stari = i; // nadi najstariji IP
  121.                 }
  122.             }
  123.             getstatust[stari].ip = ip;
  124.             getstatust[stari].zadnjigetstatus = GetTickCount();
  125.             getstatust[stari].brojac = 0;
  126.         }
  127.         return(len);
  128.     }
  129. }
  130.  
  131. // POSLANI PAKETI
  132. int __cdecl mysendto(SOCKET s, u_char **retbuf, int len, int flags, const struct sockaddr *to, int tolen) {
  133.     u_char  *buf = *retbuf;
  134.  
  135.     DWORD   sada;
  136.     u32     ip;
  137.     int     i;
  138.  
  139.     ip   = ((struct sockaddr_in *)to)->sin_addr.s_addr;
  140.  
  141.     for(i = 0; i < MAXIPS; i++) {                   // provjeri da li je IP u tabeli baniranih
  142.         if(ip != banovit[i].ip) continue;           // ako IP nije u polju baniranih, nastavi pretragu
  143.         if(GetTickCount() < banovit[i].banirando) { // ne vracaj ništa dok ne prode vrijeme hladenja
  144.             return(-0x77777777);                    // vrati ništa i izbaci petlju
  145.         }
  146.     }
  147.     *retbuf = buf;
  148.     return(len);
  149. }
  150.  
  151.  
  152. #ifdef WIN32
  153. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
  154.     switch(fdwReason) {
  155.         case DLL_PROCESS_ATTACH: {
  156.             DisableThreadLibraryCalls(hinstDLL);
  157.             break;
  158.         }
  159.         case DLL_PROCESS_DETACH: {
  160.             break;
  161.         }
  162.         default: break;
  163.     }
  164.     return(TRUE);
  165. }
  166. #endif
  167.  
  168. /*
  169.   example of fake number of players on the own server
  170.   by Luigi Auriemma
  171.   e-mail: aluigi@autistici.org
  172.   web:    aluigi.org
  173.  
  174.   this plugin for Proxocket (http://aluigi.org/mytoolz.htm#proxocket) shows
  175.   how to send a false number of players to the clients that query the server
  176.   on which is running this plugin.
  177.  
  178.   how to use:
  179.   - copy ws2_32.dll of Proxocket in the folder of your server
  180.   - copy THIS myproxocket.dll in the same folder (you must compile it if the
  181.     file not available)
  182.   - start your server
  183.   - query the server with a client to know if it's working, in which case
  184.     it will appear non-empty
  185.  
  186.   oh and yes, this example looks simple and basic (or even "lame") because...
  187.   it's just an example!!!
  188. */
  189. // win:   gcc -o myproxocket.dll myproxocket.c -shared
  190. // linux can NOT be implemented because mysendto uses a different prototype (**buf)!
  191.  
  192. #ifndef WIN32
  193.     #define _GNU_SOURCE
  194. #endif
  195.  
  196. #include <stdio.h>
  197. #include <stdlib.h>
  198. #include <string.h>
  199. #include <time.h>
  200.  
  201. #ifdef WIN32
  202.     #include <winsock.h>
  203.     #include <windows.h>
  204.     typedef int socklen_t;
  205. #else
  206.     #include <unistd.h>
  207.     #include <sys/socket.h>
  208.     #include <sys/types.h>
  209.     #include <arpa/inet.h>
  210.     #include <netinet/in.h>
  211.     #include <netdb.h>
  212.     #include <dlfcn.h>  // -ldl
  213.     #define mysendto sendto
  214.     static ssize_t (*old_sendto)(int s, void *buf, size_t len, int flags, struct sockaddr *to, int tolen) = NULL;
  215. #endif
  216.  
  217.  
  218.  
  219. #define VER         "0.1"
  220. #define PLAYERS     6
  221.  
  222.  
  223.  
  224. u_char *mymemmem(u_char *ptr, int offset, u_char *value, int valsz, int num) {
  225.     int     i;
  226.  
  227.     if(valsz < 0) valsz = strlen(value);
  228.     for(i = offset; i <= (num - valsz); i++) {
  229.         if(!memcmp(ptr + i, value, valsz)) return(ptr + i + valsz);
  230.     }
  231.     return(NULL);
  232. }
  233.  
  234.  
  235.  
  236. int myrand(void) {
  237.     static int  rnd = 0;
  238.     if(!rnd) rnd = time(NULL);
  239.     rnd = ((rnd * 0x343FD) + 0x269EC3) >> 1;
  240.     return(rnd & 0x7fffffff);
  241. }
  242.  
  243.  
  244.  
  245. ssize_t __cdecl mysendto(int s, u_char **retbuf, int len, int flags, const struct sockaddr *to, int tolen) {
  246.     int     i,
  247.             chr  = '\\',    // default delimiter
  248.             num  = 0,       // 0 players in the reply
  249.             lame = 0;
  250.     u_char  *new,           // it's automatically freed by proxocket
  251.             *p;
  252.     u_char  *buf = *retbuf; // do NOT touch this
  253.  
  254. #ifndef WIN32
  255.     if(!old_sendto) old_sendto = dlsym(RTLD_NEXT, "sendto");
  256.     len = old_sendto(s, buf, len, flags, to, tolen);
  257. #endif
  258.  
  259.     if(len < 30) return(len);
  260.  
  261.     // Quake 3 getstatus
  262.     if(!memcmp(buf, "\xff\xff\xff\xff" "statusResponse", 18)) {
  263.         p = mymemmem(buf, 18 + 1, "\n", 1, len);
  264.         if(p) {
  265.             for(i = p - buf; i < len; i++) {
  266.                 if(p[i] == '\n') num++;
  267.             }
  268.         }
  269.         if(num >= PLAYERS) return(len);
  270.  
  271.         new = malloc(len + 1024);
  272.         memcpy(new, buf, len);
  273.         buf = new;
  274.  
  275.         p = buf + len;
  276.         for(; num < PLAYERS; num++) {
  277.             p += sprintf(p, "%d %d \"BOssMan\"\n", myrand() % 50, num);
  278.         }
  279.         len = p - buf;
  280.  
  281.     } else {
  282.         // Quake 3 getinfo
  283.         if(!memcmp(buf, "\xff\xff\xff\xff" "infoResponse", 16)) {
  284.             p = mymemmem(buf, 16, "\\clients\\", -1, len);
  285.             if(!p) p = mymemmem(buf, 16, "\\players\\", -1, len);
  286.             if(!p) p = mymemmem(buf, 16, "\\privateClients\\", -1, len);
  287.  
  288.         // Gamespy 2 and 3
  289.         } else if(!memcmp(buf + 5, "hostname\0", 9)) {
  290.             p = mymemmem(buf, 5, "\0""numplayers""\0", 1+10+1, len);
  291.             chr = 0;
  292.  
  293.         // Half-Life
  294.         } else if(!memcmp(buf, "\xff\xff\xff\xff" "infostringresponse", 22)) {
  295.             p = mymemmem(buf, 14, "\\players\\", -1, len);
  296.  
  297.         // Half-Life (m)
  298.         } else if(!memcmp(buf, "\xff\xff\xff\xff" "m", 5)) {
  299.             p = buf + 5;
  300.             for(i = 0; i < 5; i++) {
  301.                 p += strlen(p) + 1;
  302.                 if(p >= (buf + len)) return(len);
  303.             }
  304.             // p[0] is the number of players, trick
  305.             chr = p[1]; // trick
  306.  
  307.         // Source (I)
  308.         } else if(!memcmp(buf, "\xff\xff\xff\xff" "I", 5)) {
  309.             p = buf + 5 + 1;
  310.             for(i = 0; i < 6; i++) {
  311.                 p += strlen(p) + 1;
  312.                 if(p >= (buf + len)) return(len);
  313.             }
  314.             // p[0] is the number of players, trick
  315.             chr = p[1]; // trick
  316.  
  317.         /*
  318.         // Source (E): no players info in "rules"
  319.         } else if(!memcmp(buf, "\xff\xff\xff\xff" "E", 5)) {
  320.             p = mymemmem(buf, 5, "\0""clients""\0", 1+7+1, len);
  321.             if(!p) p = mymemmem(buf, 5, "\0""players""\0", 1+7+1, len);
  322.             chr = 0;
  323.         */
  324.  
  325.         /*
  326.         // Doom 3: not supported
  327.         } else if(!memcmp(buf, "\xff\xff" "infoResponse", 14)) {
  328.         */
  329.  
  330.         // Gamespy 1
  331.         } else if(buf[0] == '\\') {
  332.             p = mymemmem(buf, 0, "\\numplayers\\", -1, len);
  333.  
  334.         // All seeying eye
  335.         } else if(!memcmp(buf, "EYE1", 4)) {
  336.             p = buf + 4;
  337.             for(i = 0; i < 7; i++) {
  338.                 if(!((p < (buf + len)) && (*p > 1))) return(len);
  339.                 p += *p;
  340.             }
  341.             p++;
  342.             // p[0] is the number of players, trick
  343.             chr = p[*p];    // trick
  344.  
  345.         } else {
  346.             return(len);
  347.         }
  348.         if(!p) return(len); // boring
  349.  
  350.         if(*p < '0') lame = 1;  // it's only a lame trick for non textual players (max 47)
  351.  
  352.         if(lame) {
  353.             num = *p;
  354.         } else {
  355.             num = atoi(p);
  356.         }
  357.         if((num < 0) || (num >= PLAYERS)) return(len);
  358.  
  359.         i = p - buf;
  360.         new = malloc(len + 16);
  361.         memcpy(new, buf, i);                // copy till the players parameter (included)
  362.         p = new + i;
  363.         if(lame) {
  364.             *p++ = PLAYERS;
  365.         } else {
  366.             p += sprintf(p, "%d", PLAYERS); // put the new one
  367.         }
  368.         while(buf[i] != chr) i++;           // skip the old value
  369.         memcpy(p, buf + i, len - i);        // copy the rest
  370.         p += (len - i);
  371.         buf = new;
  372.         len = p - buf;
  373.     }
  374.  
  375.     *retbuf = buf;  // do NOT touch this
  376.     return(len);
  377. }
  378.  
  379.  
  380.  
  381. #ifdef WIN32
  382. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
  383.     if(fdwReason == DLL_PROCESS_ATTACH) DisableThreadLibraryCalls(hinstDLL);
  384.     return(TRUE);
  385. }
  386. #endif
  387.  
  388.  
  389.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement