Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void mod_pcap_loop(struct syn_t *syn) {
- struct pcap_pkthdr *phead = calloc(1, sizeof(*phead));
- const u_char *packet = NULL;
- while(1) {
- packet = pcap_next(syn->pd, phead);
- if (packet) break;
- }
- const struct sniff_ip *ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
- u_int size_ip = IP_HL(ip)*4;
- const struct sniff_tcp *tcp = (struct sniff_tcp*)(packet + SIZE_ETHERNET + size_ip);
- printf("reply! (%d bytes): (seq %d) (ack %d) (ports %d -> %d) (flags ", (int)phead->caplen,
- ntohl(tcp->th_seq),ntohl(tcp->th_ack),ntohs(tcp->th_sport),
- ntohs(tcp->th_dport));
- if (tcp->th_flags & TH_URG) printf("URG ");
- if (tcp->th_flags & TH_SYN) printf("SYN ");
- if (tcp->th_flags & TH_ACK) printf("ACK ");
- if (tcp->th_flags & TH_PUSH)printf("PSH ");
- if (tcp->th_flags & TH_RST) printf("RST ");
- if (tcp->th_flags & TH_FIN) printf("FIN ");
- printf(")\n");
- if (tcp->th_flags & TH_ACK && tcp->th_flags & TH_SYN) {
- syn->syn_data.port = ntohs(tcp->th_dport);
- syn->syn_data.seq = ntohl(tcp->th_seq);
- syn->syn_data.ack = ntohl(tcp->th_ack);
- }
- }
- void init_pcap(struct syn_t *syn) {
- char errbuf[PCAP_ERRBUF_SIZE];
- char filter_exp[40];
- char *dev = (char *)calloc(1, 4);
- #ifdef __APPLE__
- memcpy(dev, "en1", 3);
- #elif __linux__
- memcpy(dev, "eth1", 4);
- #else
- memcpy(dev, "lo0", 3);
- #endif
- struct bpf_program fp;
- bpf_u_int32 mask, net;
- sprintf(filter_exp, "src host %s", syn->dst_ip);
- pcap_lookupnet(dev, &net, &mask, errbuf);
- syn->pd = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
- pcap_compile(syn->pd, &fp, filter_exp, 0, net);
- pcap_setfilter(syn->pd, &fp);
- }
- void *func_probe(void *vd) {
- struct syn_t *syn = (struct syn_t *)vd;
- pthread_mutex_lock(&syn->_b_mutex[1]);
- while(syn->tcm != 2) pthread_cond_wait(&syn->_b_cond[1], &syn->_b_mutex[1]);
- pthread_mutex_unlock(&syn->_b_mutex[1]);
- printf("Thread %#x reached (%s)\n",(unsigned)pthread_self(),__func__);
- struct pkt_summary seed;
- seed.seq = (int)(arc4random() % MAX_SEQ);
- seed.port = (int)abs((arc4random() % (MAX_P-MIN_P)) + MIN_P-1);
- seed.ack = 0;
- pthread_mutex_lock(&syn->_b_mutex[0]);
- printf("Sending SYN probe on port %d with seq %d and ack %d\n",seed.port,seed.seq,seed.ack);
- send_probe(TH_SYN,htonl(seed.seq),htonl(seed.ack),INADDR_LOCAL,syn->dst_ip,seed.port,80);
- syn->state = S_SYN_SENT;
- pthread_cond_signal(&syn->_b_cond[0]);
- while (syn->state != S_SYN_RCVD) pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
- pthread_cond_signal(&syn->_b_cond[0]);
- pthread_mutex_unlock(&syn->_b_mutex[0]);
- pthread_mutex_lock(&syn->_b_mutex[0]);
- seed.seq = seed.seq + 1;
- seed.ack = syn->syn_data.seq + 1;
- printf("Sending ACK probe on port %d with seq %d and ack %d\n",seed.port,seed.seq,seed.ack);
- send_probe(TH_ACK,htonl(seed.seq),htonl(seed.ack),INADDR_LOCAL,syn->dst_ip,seed.port,80);
- syn->state = S_ACK_SENT;
- pthread_cond_signal(&syn->_b_cond[0]);
- while (syn->state != S_ACK_RCVD) pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
- pthread_cond_signal(&syn->_b_cond[0]);
- pthread_mutex_unlock(&syn->_b_mutex[0]);
- return vd;
- }
- void *func_check(void *vd) {
- struct syn_t *syn = (struct syn_t *)vd;
- pthread_mutex_lock(&syn->_b_mutex[1]);
- while(syn->tcm != 2) pthread_cond_wait(&syn->_b_cond[1], &syn->_b_mutex[1]);
- pthread_mutex_unlock(&syn->_b_mutex[1]);
- printf("Thread %#x reached (%s)\n",(unsigned)pthread_self(),__func__);
- init_pcap(syn);
- pthread_mutex_lock(&syn->_b_mutex[0]);
- while(syn->state != S_SYN_SENT)
- pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
- mod_pcap_loop(syn);
- syn->state = S_SYN_RCVD;
- pthread_cond_signal(&syn->_b_cond[0]);
- pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
- pthread_cond_signal(&syn->_b_cond[0]);
- pthread_mutex_unlock(&syn->_b_mutex[0]);
- pthread_mutex_lock(&syn->_b_mutex[0]);
- while(syn->state != S_ACK_SENT)
- pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
- mod_pcap_loop(syn);
- syn->state = S_ACK_RCVD;
- pthread_cond_signal(&syn->_b_cond[0]);
- pthread_cond_wait(&syn->_b_cond[0],&syn->_b_mutex[0]);
- pthread_cond_signal(&syn->_b_cond[0]);
- pthread_mutex_unlock(&syn->_b_mutex[0]);
- return vd;
- }
- int main(int argc, char **argv) {
- struct syn_t *syn;
- signal(SIGINT, &h_mac_quit);
- signal(SIGUSR1,&h_mac_quit);
- signal(SIGSEGV,&h_mac_quit);
- if (getuid() && geteuid()) { die_with_signal("not root"); }
- syn = calloc(1, sizeof(struct syn_t));
- syn->dst_ip = fillstruct("www.google.com");
- pthread_mutex_init(&syn->_b_mutex[0], NULL);
- pthread_mutex_init(&syn->_b_mutex[1], NULL);
- pthread_cond_init(&syn->_b_cond[0], NULL);
- pthread_cond_init(&syn->_b_cond[1], NULL);
- pthread_mutex_lock(&syn->_b_mutex[1]);
- pthread_create(&syn->thrs[0],NULL,func_probe, (void*)syn);
- printf("Spawning thread probe (%#x) (%d)\n",(unsigned)syn->thrs[0],syn->tcm++);
- pthread_create(&syn->thrs[1],NULL,func_check, (void*)syn);
- printf("Spawning check probe (%#x) (%d)\n",(unsigned)syn->thrs[1],syn->tcm++);
- pthread_cond_broadcast(&syn->_b_cond[1]);
- pthread_mutex_unlock(&syn->_b_mutex[1]);
- pthread_join(syn->thrs[1],NULL);
- pthread_join(syn->thrs[0],NULL);
- printf("connection established!\n");
- pthread_mutex_destroy(&syn->_b_mutex[0]);
- pthread_mutex_destroy(&syn->_b_mutex[1]);
- pthread_cond_destroy(&syn->_b_cond[0]);
- pthread_cond_destroy(&syn->_b_cond[1]);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment