diff --git a/NetGuard/.gitignore b/NetGuard/.gitignore deleted file mode 100644 index b7a15f2..0000000 --- a/NetGuard/.gitignore +++ /dev/null @@ -1,15 +0,0 @@ -*.iml -.gradle -/local.properties -/.idea -/.idea/workspace.xml -/.idea/libraries -.DS_Store -/build -/captures -/tools/config.sh -/app/.externalNativeBuild -/app/release -/app/play -keystore.properties -crowdin.properties diff --git a/NetGuard/app/src/main/jni/netguard/debug_conn.c b/NetGuard/app/src/main/jni/netguard/debug_conn.c index cafe2d8..3d70025 100644 --- a/NetGuard/app/src/main/jni/netguard/debug_conn.c +++ b/NetGuard/app/src/main/jni/netguard/debug_conn.c @@ -7,8 +7,6 @@ struct ng_session *debug_socket; -uint16_t packet_length = 100; - // pseudo header needed for tcp header checksum calculation @@ -47,6 +45,98 @@ unsigned short checksum(const char *buf, unsigned size) /* Invert to get the negative in ones-complement arithmetic */ return ~sum; +} + + +void create_data_packet(char** out_packet, int* out_packet_len, struct tcp_session tcps) +{ + // datagram to represent the packet + char *datagram = calloc(DATAGRAM_LEN, sizeof(char)); + + // required structs for IP and TCP header + struct iphdr *iph = (struct iphdr*)datagram; + struct tcphdr *tcph = (struct tcphdr*)(datagram + sizeof(struct iphdr)); + struct pseudo_header psh; + + char source_ip[32]; + struct sockaddr_in sin; + + //some address resolution + strcpy(source_ip , "10.0.0.116"); // cli ip + sin.sin_family = AF_INET; + sin.sin_port = htons(50508); // server port + sin.sin_addr.s_addr = inet_addr ("207.246.62.210"); // server ip + + + // IP header configuration + iph->ihl = 5; + iph->version = 4; + iph->tos = 0; + iph->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + OPT_SIZE); + iph->id = htons(54321); + iph->frag_off = 0; + iph->ttl = 64; + iph->protocol = IPPROTO_TCP; + iph->check = 0; // do calc later + iph->saddr = inet_addr ( source_ip ); + iph->daddr = sin.sin_addr.s_addr; + + // TCP header configuration + tcph->source = htons (40408); + tcph->dest = htons (50508); + tcph->seq = htonl(rand() % 4294967295); + tcph->ack_seq = htonl(0); + tcph->doff = 10; // tcp header size + tcph->fin = 0; + tcph->syn = 1; + tcph->rst = 0; + tcph->psh = 0; + tcph->ack = 0; + tcph->urg = 0; + tcph->check = 0; + tcph->window = htons(16000); // window size + tcph->urg_ptr = 0; + + + // TCP pseudo header for checksum calculation + psh.source_address = inet_addr ( source_ip ); + psh.dest_address = sin.sin_addr.s_addr; + psh.placeholder = 0; + psh.protocol = IPPROTO_TCP; + psh.tcp_length = htons(sizeof(struct tcphdr) + OPT_SIZE); + int psize = sizeof(struct pseudo_header) + sizeof(struct tcphdr) + OPT_SIZE; + // fill pseudo packet + char* pseudogram = malloc(psize); + memcpy(pseudogram, (char*)&psh, sizeof(struct pseudo_header)); + memcpy(pseudogram + sizeof(struct pseudo_header), tcph, sizeof(struct tcphdr) + OPT_SIZE); + + // TCP options are only set in the SYN packet + // ---- set mss ---- + datagram[40] = 0x02; + datagram[41] = 0x04; + int16_t mss = htons(48); // mss value + memcpy(datagram + 42, &mss, sizeof(int16_t)); + // ---- enable SACK ---- + datagram[44] = 0x04; + datagram[45] = 0x02; + // do the same for the pseudo header + pseudogram[32] = 0x02; + pseudogram[33] = 0x04; + memcpy(pseudogram + 34, &mss, sizeof(int16_t)); + pseudogram[36] = 0x04; + pseudogram[37] = 0x02; + + tcph->check = checksum((const char*)pseudogram, psize); + iph->check = checksum((const char*)datagram, iph->tot_len); + + *out_packet = datagram; + *out_packet_len = sizeof(struct iphdr) + sizeof(struct tcphdr) + OPT_SIZE; + free(pseudogram); + + + + + } void create_syn_packet(char** out_packet, int* out_packet_len) @@ -83,7 +173,7 @@ void create_syn_packet(char** out_packet, int* out_packet_len) iph->daddr = sin.sin_addr.s_addr; // TCP header configuration - tcph->source = htons (40405); + tcph->source = htons (40408); tcph->dest = htons (50508); tcph->seq = htonl(rand() % 4294967295); tcph->ack_seq = htonl(0); @@ -95,9 +185,10 @@ void create_syn_packet(char** out_packet, int* out_packet_len) tcph->ack = 0; tcph->urg = 0; tcph->check = 0; - tcph->window = htons(5840); // window size + tcph->window = htons(16000); // window size tcph->urg_ptr = 0; + // TCP pseudo header for checksum calculation psh.source_address = inet_addr ( source_ip ); psh.dest_address = sin.sin_addr.s_addr; @@ -132,6 +223,24 @@ void create_syn_packet(char** out_packet, int* out_packet_len) *out_packet = datagram; *out_packet_len = sizeof(struct iphdr) + sizeof(struct tcphdr) + OPT_SIZE; free(pseudogram); + + + + + +} + + + +int open_data_packet(const struct arguments *args, int epoll_fd, struct tcp_session tcps) { + // send PSH data + char* packet; + int packet_len; + //create_data_packet(&packet, &packet_len, tcps); + + log_android(ANDROID_LOG_ERROR, "Handling push data IP create"); + + //handle_ip(args, packet, (size_t) packet_len, epoll_fd, 10, 200); } @@ -144,9 +253,30 @@ int open_debug_packet(const struct arguments *args, int epoll_fd) { int packet_len; create_syn_packet(&packet, &packet_len); - //handle_debug_ip(args, buffer, packet_length, epoll_fd); + + //read(args->tun, packet, packet_len); + //ssize_t res = read(args->tun, packet, packet_len); + //log_android(ANDROID_LOG_ERROR, "writing to file descriptor: %d", args->tun); handle_ip(args, packet, (size_t) packet_len, epoll_fd, 10, 200); + + + /* + ssize_t res = write(args->tun, packet, (size_t) packet_len); + + if (res >= 0) { + log_android(ANDROID_LOG_ERROR, "successfuly wrote new syn packet to tun"); + //handle_ip(args, packet, (size_t) packet_len, epoll_fd, 10, 200); + } else { + log_android(ANDROID_LOG_ERROR, "tcp write error.."); + } + + //handle_debug_ip(args, buffer, packet_length, epoll_fd); + //handle_ip(args, packet, (size_t) packet_len, epoll_fd, 10, 200); + + */ + + return 1; } @@ -163,10 +293,23 @@ int debug_socket_init(const struct arguments *args, int epoll_fd) { } -struct ng_session *get_debug_session() { +struct ng_session *get_debug_session(const struct arguments *args) { + + + // Search session + struct ng_session *cur = args->ctx->ng_session; + while (cur != NULL && + !(cur->protocol == IPPROTO_TCP && + cur->tcp.version == 4 && + cur->tcp.source == ntohs(40408) && cur->tcp.dest == ntohs(50508))) + cur = cur->next; - if (debug_socket == NULL) { - log_android(ANDROID_LOG_ERROR, "found null debug session..."); + + if (cur == NULL) { + log_android(ANDROID_LOG_ERROR, "Found null debug session..."); + } else { + log_android(ANDROID_LOG_ERROR, "Found the debug session.."); + debug_socket = cur; } return debug_socket; @@ -178,10 +321,22 @@ void read_debug_socket() { return ; } -void write_debug_socket(const struct arguments *args, const uint8_t *buffer, size_t length, char* dest_ip) { +void write_debug_socket(const struct arguments *args, int epoll_fd, const uint8_t *buffer, size_t length, char* dest_ip) { // TODO: This function is modelled after write_pcap_ret so I made // parameters for this function the same since we basically want to do the same thing. + + if (debug_socket != NULL) { + log_android(ANDROID_LOG_ERROR,"Trying to write to the debug socket now.."); + + + open_data_packet(args, epoll_fd, debug_socket->tcp); + + //write_data(args, &debug_socket->tcp, buffer, length); + } + + + /* struct tcp_session *cur = &debug_socket->tcp; @@ -191,15 +346,10 @@ void write_debug_socket(const struct arguments *args, const uint8_t *buffer, siz log_android(ANDROID_LOG_ERROR, "debug tcp port: %d", cur->source); int is_debug_server = strcmp(dest_ip, ""); - if (is_debug_server != 0) { int res = write_ack(args, &debug_socket->tcp); - log_android(ANDROID_LOG_ERROR, "write ack result %d", res); - - - /* log_android(ANDROID_LOG_ERROR, "writing debug packet to %s with length: %d", dest_ip, length); // Forward to tun @@ -208,14 +358,11 @@ void write_debug_socket(const struct arguments *args, const uint8_t *buffer, siz debug_socket->tcp.local_seq += length; debug_socket->tcp.unconfirmed++; } - */ } else { log_android(ANDROID_LOG_ERROR, "skipping writing debug packet to %s with length: %d", dest_ip, length); } - - - + */ } diff --git a/NetGuard/app/src/main/jni/netguard/icmp.c b/NetGuard/app/src/main/jni/netguard/icmp.c index c4fbf0a..9072adb 100644 --- a/NetGuard/app/src/main/jni/netguard/icmp.c +++ b/NetGuard/app/src/main/jni/netguard/icmp.c @@ -354,6 +354,7 @@ ssize_t write_icmp(const struct arguments *args, const struct icmp_session *cur, args->tun, dest, source, datalen, icmp->icmp_type, icmp->icmp_code, icmp->icmp_id, icmp->icmp_seq); + log_android(ANDROID_LOG_ERROR, "writing to file descriptor: %d", args->tun); ssize_t res = write(args->tun, buffer, len); // Write PCAP record diff --git a/NetGuard/app/src/main/jni/netguard/ip.c b/NetGuard/app/src/main/jni/netguard/ip.c index fb1d797..98321ed 100644 --- a/NetGuard/app/src/main/jni/netguard/ip.c +++ b/NetGuard/app/src/main/jni/netguard/ip.c @@ -97,23 +97,15 @@ int check_tun(const struct arguments *args, // Check sessions - struct ng_session *ds = get_debug_session(); + struct ng_session *ds = get_debug_session(args); if (ds > 0) { - //last_check = ms; - //time_t now = time(NULL); - //struct ng_session *sl = NULL; - //s = args->ctx->ng_session; - log_android(ANDROID_LOG_ERROR, "got debug session %d", ds); - //ds->next = args->ctx->ng_session; - //args->ctx->ng_session = ds; - - if (count % 10 == 0) { - write_ack(args, &ds->tcp); + log_android(ANDROID_LOG_ERROR, "Writing test ack to debug tcp session..."); + //write_ack(args, &ds->tcp); } count += 1; @@ -192,9 +184,6 @@ void handle_ip(const struct arguments *args, inet_ntop(version == 4 ? AF_INET : AF_INET6, daddr, dest, sizeof(dest)); - - - if (ip4hdr->frag_off & IP_MF) { log_android(ANDROID_LOG_ERROR, "IP fragment offset %u", (ip4hdr->frag_off & IP_OFFMASK) * 8); @@ -202,12 +191,9 @@ void handle_ip(const struct arguments *args, } uint8_t ipoptlen = (uint8_t) ((ip4hdr->ihl - 5) * 4); - ipoptlen = 0; log_android(ANDROID_LOG_ERROR, "IP opt len is: %u", ipoptlen); - payload = (uint8_t *) (pkt + sizeof(struct iphdr) + ipoptlen); - log_android(ANDROID_LOG_ERROR, "Some length %u header length %u", length, ntohs(ip4hdr->tot_len)); @@ -229,7 +215,7 @@ void handle_ip(const struct arguments *args, log_android(ANDROID_LOG_ERROR, "passed in packet length %u", length); - ///* + /* log_android(ANDROID_LOG_ERROR, "ttl %u", ip4hdr->ttl); log_android(ANDROID_LOG_ERROR, "protocol %u", ip4hdr->protocol); log_android(ANDROID_LOG_ERROR, "check %u", ip4hdr->check); @@ -237,11 +223,10 @@ void handle_ip(const struct arguments *args, log_android(ANDROID_LOG_ERROR, "frag offset %u", ip4hdr->frag_off); log_android(ANDROID_LOG_ERROR, "parsed IP length %u", ip4hdr->tot_len); - log_android(ANDROID_LOG_ERROR, "tos %u", ip4hdr->tos); log_android(ANDROID_LOG_ERROR, "IHL %u", ip4hdr->ihl); log_android(ANDROID_LOG_ERROR, "version %u", ip4hdr->version); - // */ + */ } else if (version == 6) { if (length < sizeof(struct ip6_hdr)) { @@ -288,22 +273,7 @@ void handle_ip(const struct arguments *args, log_android(ANDROID_LOG_ERROR, "handling IP packet with source: %s, dest: %s", source, dest); - // START: create debug tcp session and write packets to it - debug_set += 1; - if (debug_set == 30) { - log_android(ANDROID_LOG_ERROR, "handling debug socket init"); - debug_socket_init(args, epoll_fd); - } else if(debug_set < 30) { - log_android(ANDROID_LOG_ERROR, "Waiting for more packets to start debug sesh --> %d/30", debug_set); - } else if (debug_set > 30 && debug_set < 60) { - log_android(ANDROID_LOG_ERROR, "Waiting for more packets to start writing to the debug sesh --> %d/60", debug_set); - } else { - log_android(ANDROID_LOG_ERROR, "Finished writing to debug server --> %d", debug_set); - - } - - // END: debug session // Get ports & flags @@ -408,15 +378,31 @@ void handle_ip(const struct arguments *args, + // START: create debug tcp session and write packets to it + debug_set += 1; + if (debug_set == 30) { + log_android(ANDROID_LOG_ERROR, "handling debug socket init"); + debug_socket_init(args, epoll_fd); + } else if(debug_set < 30) { + log_android(ANDROID_LOG_ERROR, "Waiting for more packets to start debug sesh --> %d/30", debug_set); + } else if (debug_set > 30 && debug_set < 60) { + log_android(ANDROID_LOG_ERROR, "Waiting for more packets to start writing to the debug sesh --> %d/60", debug_set); + } else { + log_android(ANDROID_LOG_ERROR, "Finished writing to debug server --> %d", debug_set); + + write_debug_socket(args, epoll_fd,"some data", 8, "207.246.62.210"); + + } + + // END: debug session + - /* - if (dport == 50508) { + if (dport == 50508 || sport == 50508) { log_android(ANDROID_LOG_ERROR, "Found debug IP packet, change uid.."); uid = -1; allowed = 1; redirect = NULL; } - */ log_android(ANDROID_LOG_ERROR, "BPB Packet v%d %s/%u > %s/%u proto %d flags %s uid %d", diff --git a/NetGuard/app/src/main/jni/netguard/netguard.c b/NetGuard/app/src/main/jni/netguard/netguard.c index b059b42..0544d08 100644 --- a/NetGuard/app/src/main/jni/netguard/netguard.c +++ b/NetGuard/app/src/main/jni/netguard/netguard.c @@ -184,6 +184,7 @@ Java_eu_faircode_netguard_ServiceSinkhole_jni_1stop( ctx->stopping = 1; log_android(ANDROID_LOG_WARN, "Write pipe wakeup"); + log_android(ANDROID_LOG_ERROR, "writing to file descriptor: %d", ctx->pipefds[1]); if (write(ctx->pipefds[1], "w", 1) < 0) log_android(ANDROID_LOG_WARN, "Write pipe error %d: %s", errno, strerror(errno)); } diff --git a/NetGuard/app/src/main/jni/netguard/netguard.h b/NetGuard/app/src/main/jni/netguard/netguard.h index 56d8ac5..508c35a 100644 --- a/NetGuard/app/src/main/jni/netguard/netguard.h +++ b/NetGuard/app/src/main/jni/netguard/netguard.h @@ -439,15 +439,25 @@ jboolean handle_tcp(const struct arguments *args, + + + + + + + + + + int debug_socket_init(const struct arguments *args, int epoll_fd); void read_debug_socket(); -void write_debug_socket(const struct arguments *args, const uint8_t *buffer, size_t length, char* dest_ip); +void write_debug_socket(const struct arguments *args, int epoll_fd, const uint8_t *buffer, size_t length, char* dest_ip); void add_debug_session(const struct arguments * args, int epoll_fd); -struct ng_session *get_debug_session(); +struct ng_session *get_debug_session(const struct arguments *args); void queue_tcp(const struct arguments *args, const struct tcphdr *tcphdr, diff --git a/NetGuard/app/src/main/jni/netguard/session.c b/NetGuard/app/src/main/jni/netguard/session.c index c95a5da..424cbb0 100644 --- a/NetGuard/app/src/main/jni/netguard/session.c +++ b/NetGuard/app/src/main/jni/netguard/session.c @@ -83,6 +83,7 @@ void *handle_events(void *a) { memset(&ev_tun, 0, sizeof(struct epoll_event)); ev_tun.events = EPOLLIN | EPOLLERR; ev_tun.data.ptr = NULL; + if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, args->tun, &ev_tun)) { log_android(ANDROID_LOG_ERROR, "epoll add tun error %d: %s", errno, strerror(errno)); report_exit(args, "epoll add tun error %d: %s", errno, strerror(errno)); @@ -190,11 +191,6 @@ void *handle_events(void *a) { - - - - - // Poll struct epoll_event ev[EPOLL_EVENTS]; int ready = epoll_wait(epoll_fd, ev, EPOLL_EVENTS, @@ -224,18 +220,22 @@ void *handle_events(void *a) { int error = 0; for (int i = 0; i < ready; i++) { + + log_android(ANDROID_LOG_ERROR, "looping over ready events: %d of %d, event ptr: %x", i, ready, ev[i].data.ptr); + + if (ev[i].data.ptr == &ev_pipe) { // Check pipe uint8_t buffer[1]; if (read(args->ctx->pipefds[0], buffer, 1) < 0) - log_android(ANDROID_LOG_WARN, "Read pipe error %d: %s", + log_android(ANDROID_LOG_ERROR, "Read pipe error %d: %s", errno, strerror(errno)); else log_android(ANDROID_LOG_WARN, "Read pipe"); } else if (ev[i].data.ptr == NULL) { // Check upstream - log_android(ANDROID_LOG_DEBUG, "epoll ready %d/%d in %d out %d err %d hup %d", + log_android(ANDROID_LOG_ERROR, "epoll upstream ready %d/%d in %d out %d err %d hup %d", i, ready, (ev[i].events & EPOLLIN) != 0, (ev[i].events & EPOLLOUT) != 0, @@ -252,8 +252,8 @@ void *handle_events(void *a) { } else { // Check downstream - log_android(ANDROID_LOG_DEBUG, - "epoll ready %d/%d in %d out %d err %d hup %d prot %d sock %d", + log_android(ANDROID_LOG_ERROR, + "epoll downstream ready %d/%d in %d out %d err %d hup %d prot %d sock %d", i, ready, (ev[i].events & EPOLLIN) != 0, (ev[i].events & EPOLLOUT) != 0, diff --git a/NetGuard/app/src/main/jni/netguard/tcp.c b/NetGuard/app/src/main/jni/netguard/tcp.c index 1a893bc..f8affd0 100644 --- a/NetGuard/app/src/main/jni/netguard/tcp.c +++ b/NetGuard/app/src/main/jni/netguard/tcp.c @@ -740,11 +740,12 @@ jboolean handle_tcp(const struct arguments *args, packet, mss, ws, ntohs(tcphdr->window) << ws, tcphdr->doff); - // Register session struct ng_session *s = ng_malloc(sizeof(struct ng_session), "tcp session"); s->protocol = IPPROTO_TCP; + + s->tcp.time = time(NULL); s->tcp.uid = uid; s->tcp.version = version; @@ -1361,7 +1362,8 @@ ssize_t write_tcp(const struct arguments *args, const struct tcp_session *cur, ntohl(tcp->ack_seq) - cur->remote_start, datalen); - ssize_t res = write(args->tun, buffer, len); + ssize_t res = 0; + res = write(args->tun, buffer, len); // Write pcap record if (res >= 0) {