cutter-1.04/0000755000076400007640000000000012453543365011770 5ustar chrischriscutter-1.04/COPYING0000644000076400007640000004310507671211351013017 0ustar chrischris GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19yy name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. cutter-1.04/Makefile0000644000076400007640000000010707671210170013415 0ustar chrischriscutter: cutter.c cc cutter.c -o cutter clean: rm -f cutter.o cutter cutter-1.04/README0000644000076400007640000000364112453444330012644 0ustar chrischrisCUTTER 1.04 - Jan 2014 By Chris Lowth "Cutter" is an open source program that allows Linux firewall administrators to abort TCP/IP connections routed over the firewall or router on which it is run. Documentation and downloads are available at: http://www.digitage.co.uk/cutter ------------------------------------------------------------------------ IMPORTANT WARNING Cutter has been designed for use as a administrators tool for Linux firewalls. It's use (as is, or modified) for any other purpose is not sanctioned by the author. So - do not use this tool as a parachute, or to dry your cat, chill meat, answer your phone, drive you car, teach your kids to read or attack other people's computer systems or networks. This software has been designed for "legal" and "appropriate" use by network security administrators and the like. It has been written as part of a larger Linux firewall project, targetting at controlling traffic from peer-to-peer software such as Kazaa, iMesh and others into and out of a private network. It is not designed as a tool for malicious use and the author in no way sanctions such use. Users of the software should be aware that it's actions are easily detectable using a number of readily available network monitoring tools, and it makes no attempt to disguise it's actions. Malicious use of "cutter" could result in a jail sentance in a number of countries around the world. The author is not responsible for the results of using this software. It is provided "as is" in the hope that it will be useful, but no garantees are made about it's use (see "LICENSE", below). ------------------------------------------------------------------------- LICENSE Cutter 1.04 is released under the terms of GNU GENERAL PUBLIC LICENSE Version 2 (June 1991) and comes with all the usual freedom, openness and disclaimers associated with that license. Please see the "COPYING" file for details. cutter-1.04/cutter.c0000644000076400007640000004341212453543352013442 0ustar chrischris#define VERSION "1.04" /* * cutter.c Cut a NATted TCP/IP connection passing through * the IPTABLES firewall or router on which the * utility is executed. * * Date Jan 2015 * * Author Chris Lowth * * Copyright GNU GENERAL PUBLIC LICENSE Version 2, June 1991 * (see "COPYING" file for details). * * Docs & updates * Can be found at http://www.digitage.co.uk/cutter */ /* Change Log 1.04 - Jan 2015 - Work with kernels that support either 'nf_contrack' or 'ip_contrack' - Work with kernels that support ipv6 (though we only cut ipv4 connections) - Give more info in error messages to avoid common sources of confusion about how cutter is designed to be used (in particular: the common error or running it on the server rather than the firewall). - Check that the caller is 'root' - Check that the machine we're running on is a router or firewall (ie that ip_forward enabled) - Correct routing table scan - Read all connections in before attempting to kill any */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* the L2 protocols */ #include #include #include #include #include #include #define ETHHDR sizeof(struct ethhdr) #define TCPHDR sizeof(struct tcphdr) #define IPHDR sizeof(struct iphdr) #define PACKETSIZE TCPHDR + IPHDR #ifndef EXIT_FAILURE #define EXIT_FAILURE 2 #define EXIT_WARNING 1 #define EXIT_SUCCESS 0 #endif typedef unsigned char uchar; #define TRUE 1 #define FALSE 0 struct rpack{ struct iphdr ip; struct tcphdr tcp; char space[8192]; }; struct tpack{ struct iphdr ip; struct tcphdr tcp; }; struct pseudo_header{ // pseudo header 4 the checksum unsigned source_address; unsigned dest_address; unsigned char placeholder; unsigned char protocol; unsigned short tcp_length; struct tcphdr tcp; }; struct ifreq ifreq[32]; struct ifconf ifconf; /***************************************************************************/ /* char *ip2str(in_addr_t ip) { struct in_addr a; a.s_addr = ip; return strdup(inet_ntoa(a)); } */ /**************************************************************************** * Get the IP address of the next hop device for the specified IP (or * 0.0.0.0 if there isnt one - ie we are directly connected). And the local * interface that we should use to get there. */ int getnexthop(in_addr_t ip, char *intf, in_addr_t *gateway) { FILE *id = fopen( "/proc/net/route", "r" ); char buff[500]; char iface[32]; in_addr_t dest_ip, gateway_ip, mask; int flags, refcnt, use, metric, mtu, window, irtt; int found = FALSE; char found_iface[32] = ""; in_addr_t found_gw = 0; in_addr_t found_mask = 0;; if ( id == NULL ) return FALSE; while (fgets(buff, sizeof(buff), id) != NULL) { memset(iface, 0, sizeof(iface)); dest_ip = gateway_ip = flags = refcnt = use = metric = mask = mtu = window = irtt = -1; if (sscanf(buff, "%s %8lx %8lx %4x %d %d %d %8lx %d %d %d", iface, &dest_ip, &gateway_ip, &flags, &refcnt, &use, &metric, &mask, &mtu, &window, &irtt ) == 11) { dest_ip = dest_ip; gateway_ip = gateway_ip; mask = mask; if ( iface[0] != '*' && // not a rejected interface (flags & 0x0001) && // route is UP (flags & 0x0200) == 0 && // not a "reject" (ip & mask) == dest_ip && // IP match (!found || mask > found_mask) ) { strcpy(found_iface, iface); found_gw = gateway_ip; found_mask = mask; found = TRUE; } } } fclose(id); if (found) { *gateway = found_gw; strcpy(intf, found_iface); return TRUE; } else { return FALSE; } } /**************************************************************************** * Get the MAC address (in binary format) for a neighbouring IP */ int getmac(in_addr_t ip, uchar *mac) { FILE *id = fopen( "/proc/net/arp", "r" ); union { uchar c[4]; in_addr_t n; } ipu; in_addr_t ipn; int mac0, mac1, mac2, mac3, mac4, mac5; int hwtype, flags; char dev[32], mask[32], arpip[32]; char buff[200]; int n; if (id == NULL) return FALSE; while (fgets(buff, sizeof(buff), id) != NULL) { n = sscanf(buff, "%s 0x%x 0x%x %x:%x:%x:%x:%x:%x %s %s", arpip, &hwtype, &flags, &mac0,&mac1,&mac2,&mac3,&mac4,&mac5, mask,dev ); if (n == 11 ) { ipn = inet_addr(arpip); if (ipn == ip) { mac[0]=mac0; mac[1]=mac1; mac[2]=mac2; mac[3]=mac3; mac[4]=mac4; mac[5]=mac5; fclose(id); return TRUE; } } } fclose(id); return FALSE; } unsigned short in_cksum(unsigned short *ptr,int nbytes){ register long sum; // assumes long == 32 bits u_short oddbyte; register u_short answer; // assumes u_short == 16 bits sum = 0; while (nbytes > 1) { sum += *ptr++; nbytes -= 2; } if (nbytes == 1) { oddbyte = 0; // make sure top half is zero *((u_char *) &oddbyte) = *(u_char *)ptr; // one byte only sum += oddbyte; } sum = (sum >> 16) + (sum & 0xffff); // add high-16 to low-16 sum += (sum >> 16); // add carry answer = ~sum; // ones-complement, then truncate to 16 bits return(answer); } int getifconfig() { int rtn; int raw_sock; if ((raw_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) == -1) { perror("open packet socket"); return FALSE; } memset(ifreq, 0, sizeof(ifreq)); ifconf.ifc_len = sizeof(ifreq); ifconf.ifc_req = ifreq; rtn = ioctl(raw_sock, SIOCGIFCONF, &ifconf); close(raw_sock); return TRUE; } int localip(in_addr_t ip) { int i; struct sockaddr_in *sa; for (i=0; isin_addr.s_addr == ip) return TRUE; } return FALSE; } int send_rst( char *from_ip_str, u_short fromport, char *to_ip_str, u_short toport ) { int i_result, raw_sock, rtn; in_addr_t gateway_ip; struct sockaddr_ll myaddr, hisaddr; struct tpack tpack; struct rpack rpack; struct pseudo_header pheader; struct ifreq ifr; int ifindex; unsigned char mac[6]; char interf[80]; int from_ip = inet_addr(from_ip_str); int to_ip = inet_addr(to_ip_str); time_t tstart; memset( &tpack, 0, sizeof(tpack) ); // TCP header tpack.tcp.source=htons(fromport); // 16-bit Source port number tpack.tcp.dest=htons(toport); // 16-bit Destination port tpack.tcp.seq=0; // 32-bit Sequence Number */ tpack.tcp.ack_seq=0; // 32-bit Acknowledgement Number */ tpack.tcp.doff=5; // Data offset */ tpack.tcp.res1=0; // reserved */ tpack.tcp.urg=0; // Urgent offset valid flag */ tpack.tcp.ack=0; // Acknowledgement field valid flag */ tpack.tcp.psh=0; // Push flag */ tpack.tcp.rst=0; // Reset flag */ tpack.tcp.syn=0; // Synchronize sequence numbers flag */ tpack.tcp.fin=1; // Finish sending flag */ tpack.tcp.window=0; // 16-bit Window size */ tpack.tcp.check=0; // space for 16-bit checksum tpack.tcp.urg_ptr=0; // 16-bit urgent offset */ // IP header tpack.ip.version=4; // 4-bit Version */ tpack.ip.ihl=5; // 4-bit Header Length */ tpack.ip.tos=0x10; // 8-bit Type of service */ tpack.ip.tot_len=htons(IPHDR+TCPHDR); // 16-bit Total length */ tpack.ip.id=0; // 16-bit ID field */ tpack.ip.frag_off=htons(0x4000); // 13-bit Fragment offset */ tpack.ip.ttl=0xff; // 8-bit Time To Live */ tpack.ip.protocol=IPPROTO_TCP; // 8-bit Protocol */ tpack.ip.check=0; // space for 16-bit Header checksum tpack.ip.saddr = from_ip; tpack.ip.daddr = to_ip; // IP header checksum tpack.ip.check=in_cksum((unsigned short *)&tpack.ip,IPHDR); // TCP header checksum pheader.source_address=(unsigned)tpack.ip.saddr; pheader.dest_address=(unsigned)tpack.ip.daddr; pheader.placeholder=0; pheader.protocol=IPPROTO_TCP; pheader.tcp_length=htons(TCPHDR); bcopy((char *)&tpack.tcp,(char *)&pheader.tcp,TCPHDR); tpack.tcp.check=in_cksum((unsigned short *)&pheader,TCPHDR+12); /* * Open a PACKET (layer 2) socket. */ if ((raw_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) == -1) { perror("open packet socket"); return FALSE; } if (!getnexthop(to_ip, interf, &gateway_ip)) { fprintf(stderr, "Cant find next hop gateway in routing table\n"); close(raw_sock); return FALSE; } /* * Determine the ifindex of the interface we are going to use with * our layer 2 comms. */ memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, interf, sizeof(ifr.ifr_name)); if (ioctl(raw_sock, SIOCGIFINDEX, &ifr) == -1) { perror("ioctl - get ifindex"); close(raw_sock); return FALSE; } ifindex = ifr.ifr_ifindex; if (!getmac( gateway_ip == 0 ? to_ip : gateway_ip, mac) ) { memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, interf, sizeof(ifr.ifr_name)); if (ioctl(raw_sock, SIOCGIFHWADDR, &ifr) == -1) { perror("ioctl - get hwaddr"); close(raw_sock); return FALSE; } memcpy(mac, ifr.ifr_hwaddr.sa_data, 6); } /* * Bind to the interface. We can use a null MAC address because the * system will fill it in for us. */ memset(&myaddr, 0, sizeof(myaddr)); myaddr.sll_family = AF_PACKET; myaddr.sll_ifindex = ifindex; if(bind(raw_sock, (struct sockaddr*)(&myaddr), sizeof(myaddr)) != 0) { perror("bind"); close(raw_sock); return FALSE; } memset(&hisaddr, 0, sizeof(hisaddr)); hisaddr.sll_family = AF_PACKET; hisaddr.sll_protocol = htons(ETH_P_IP); hisaddr.sll_ifindex = ifindex; hisaddr.sll_halen = 6; memcpy(hisaddr.sll_addr, mac, 6); /* Send our invalid FIN packet */ printf( "sending FIN from %s:%d to %s:%d\n", from_ip_str, fromport, to_ip_str, toport); i_result = sendto(raw_sock,&tpack,PACKETSIZE,0,(void*)&hisaddr,sizeof(hisaddr)); if (i_result != PACKETSIZE) { perror("sendto - fin packet"); close(raw_sock); return FALSE; } /* Wait for the SYN we expect back from the peer, and send an RST with * the sequence numbers gleaned from the SYN */ tstart = time(0); for ( ; time(0) < tstart + 15; ) { // give the peer 15 seconds to respond struct sockaddr_ll gotaddr; int addrlen = sizeof(gotaddr); fd_set readfds; struct timeval tv; FD_ZERO(&readfds); FD_SET(raw_sock, &readfds); tv.tv_sec = 1; tv.tv_usec = 0; if (select(raw_sock + 1, &readfds, NULL, NULL, &tv) == 0) continue; memcpy(&gotaddr, &hisaddr, addrlen); memset(&rpack, 0, sizeof(rpack)); i_result = recvfrom(raw_sock, &rpack, sizeof(rpack), 0, (void*)&gotaddr, &addrlen); if (i_result < 16) { perror( "recvfrom - waiting for SYN" ); close(raw_sock); return FALSE; } if ( rpack.ip.version == 4 && rpack.ip.ihl == 5 && rpack.ip.protocol == IPPROTO_TCP && rpack.ip.saddr == tpack.ip.daddr && rpack.ip.daddr == tpack.ip.saddr && rpack.tcp.source == tpack.tcp.dest && rpack.tcp.dest == tpack.tcp.source && rpack.tcp.ack == 1 ) { tpack.tcp.seq = rpack.tcp.ack_seq; tpack.tcp.ack_seq = 0; tpack.tcp.fin = 0; tpack.tcp.rst = 1; tpack.tcp.check = 0; pheader.source_address=(unsigned)tpack.ip.saddr; pheader.dest_address=(unsigned)tpack.ip.daddr; pheader.placeholder=0; pheader.protocol=IPPROTO_TCP; pheader.tcp_length=htons(TCPHDR); bcopy((char *)&tpack.tcp,(char *)&pheader.tcp,TCPHDR); tpack.tcp.check=in_cksum((unsigned short *)&pheader,TCPHDR+12); printf( "got SYN .. sending RST from %s:%d to %s:%d\n", from_ip_str, fromport, to_ip_str, toport); i_result = sendto(raw_sock,&tpack,PACKETSIZE,0,(void*)&hisaddr,sizeof(hisaddr)); if (i_result != PACKETSIZE) { perror("sendto - reset packet"); close(raw_sock); return FALSE; } close(raw_sock); return TRUE; } } printf("SYN not seen so RST not sent\n"); close(raw_sock); return FALSE; } int match(in_addr_t match_ip, int match_port, in_addr_t found_ip, int found_port) { if ( (match_ip == 0 || match_ip == found_ip) && (match_port == 0 || match_port == found_port) ) return TRUE; else return FALSE; } int get_str_field(char **p, char *key, char *out, int outlen) { char *found = strstr(*p, key); int i; memset(out, 0, outlen); if (found == NULL) return FALSE; found += strlen(key); for (i=0; inext)) { if (strcmp((*cp)->a1, a1)==0 && (*cp)->p1 == p1 && strcmp((*cp)->a2, a2)==0 && (*cp)->p2 == p2) return; } *cp = malloc(sizeof(struct connection_s)); if (*cp == NULL) { fprintf(stderr, "Out of memory\n"); exit(EXIT_FAILURE); } (*cp)->a1 = strdup(a1); (*cp)->p1 = p1; (*cp)->a2 = strdup(a2); (*cp)->p2 = p2; (*cp)->next = NULL; if ((*cp)->a1 == NULL || (*cp)->a2 == NULL) { fprintf(stderr, "Out of memory\n"); exit(EXIT_FAILURE); } } /* void dump_connection_list(void) { struct connection_s **cp; for (cp = &connections; *cp != NULL; cp = &((*cp)->next)) { printf("%s %d %s %d\n", (*cp)->a1, (*cp)->p1, (*cp)->a2, (*cp)->p2); } } */ int send_rsts(void) { struct connection_s **cp; int ok = TRUE; for (cp = &connections; *cp != NULL; cp = &((*cp)->next)) { ok = send_rst((*cp)->a1, (*cp)->p1, (*cp)->a2, (*cp)->p2) && ok; } return ok; } int scan_conntrack(in_addr_t ip1, int port1, in_addr_t ip2, int port2) { char *file1 = "/proc/net/ip_conntrack"; char *file2 = "/proc/self/net/nf_conntrack"; FILE *id = NULL; char src1[32], dst1[32], src2[32], dst2[32]; int sport1, dport1, sport2, dport2, i; int packets1, packets2, bytes1, bytes2; in_addr_t src1n, src2n, dst1n, dst2n; char buff[1024], *p; int found = 0; int found_but_local = 0; int ok = TRUE; id = fopen(file1, "r"); if (id == NULL) id = fopen(file2, "r"); if (id == NULL) { perror( "openning ip_conntrack or nf_conntrack (is kernel module 'conntrack' loaded?)" ); return FALSE; } while (fgets(buff, sizeof(buff), id) != NULL) { int is_ipv4_tcp = memcmp(buff, "tcp ", 4) == 0 || ( memcmp(buff, "ipv4 ", 5) == 0 && strstr(buff, " tcp ") != NULL ); if (!is_ipv4_tcp || strstr(buff, " ESTABLISHED ") == NULL) continue; p = buff; if ( !get_str_field(&p, " src=", src1, sizeof(src1)) || !get_str_field(&p, " dst=", dst1, sizeof(dst1)) || !get_int_field(&p, " sport=", &sport1) || !get_int_field(&p, " dport=", &dport1) || !get_str_field(&p, " src=", src2, sizeof(src2)) || !get_str_field(&p, " dst=", dst2, sizeof(dst2)) || !get_int_field(&p, " sport=", &sport2) || !get_int_field(&p, " dport=", &dport2) ) continue; src1n = inet_addr(src1); src2n = inet_addr(src2); dst1n = inet_addr(dst1); dst2n = inet_addr(dst2); if ( (match(ip1,port1,src2n,sport2) && match(ip2,port2,dst2n,dport2)) || (match(ip1,port1,dst2n,dport2) && match(ip2,port2,src2n,sport2)) || (match(ip1,port1,src1n,sport1) && match(ip2,port2,dst1n,dport1)) || (match(ip1,port1,dst1n,dport1) && match(ip2,port2,src1n,sport1)) ) { /* * local network to public network - forwarded connection */ if (!localip(src1n) && !localip(dst1n) && !localip(src2n) && localip(dst2n)) { found ++; add_to_connection_list(dst1, dport1, src1, sport1); add_to_connection_list(dst2, dport2, src2, sport2); } /* Inbound connection forwarded to private network device */ else if (!localip(src1n) && localip(dst1n) && !localip(src2n) && !localip(dst2n)) { found ++; add_to_connection_list(dst1, dport1, src1, sport1); add_to_connection_list(dst2, dport2, src2, sport2); } /* We appear to be running in the server or client, not in a router/firewall */ else found_but_local ++; } } if (found == 0) { if (found_but_local > 0) { fprintf(stderr, "Error: The matching connection terminates on THIS computer.\n\n"); fprintf(stderr, "Note:\n"); fprintf(stderr, " cutter can only cut connections running over the router or firewall\n"); fprintf(stderr, " on which it is run. It cannot cut connections that terminate locally.\n"); fprintf(stderr, " So: you should run cutter on the firewall/router, not on the client\n"); fprintf(stderr, " or server machine.\n"); } else { fprintf(stderr, "No matching connections found\n"); } return FALSE; } return send_rsts(); } int main(int argc, char *argv[]) { FILE *id = NULL; char buff[10]; in_addr_t ip1 = argc > 1 ? inet_addr(argv[1]) : 0; int port1 = argc > 2 ? atoi(argv[2]) : 0; in_addr_t ip2 = argc > 3 ? inet_addr(argv[3]) : 0; int port2 = argc > 4 ? atoi(argv[4]) : 0; if (argc < 2) { fprintf(stderr, "\ncutter - TCP/IPv4 connection cutter for linux firewalls\n\n"); fprintf(stderr, "Version : " VERSION "\n"); fprintf(stderr, "Home page : http://www.digitage.co.uk/cutter\n\n"); fprintf(stderr, "usage is: cutter ip [ port [ ip [ port ] ] ]\n\n"); exit(EXIT_FAILURE); } if (getuid() != 0 || geteuid() != 0) { fprintf(stderr, "Refused: cutter must be run by 'root'\n"); exit(EXIT_FAILURE); } id = fopen("/proc/sys/net/ipv4/ip_forward", "r"); if (id == NULL) { perror("openning ip_forward"); exit(EXIT_FAILURE); } memset(buff, 0, sizeof(buff)); fgets(buff, sizeof(buff), id); if (atoi(buff) != 1) { fprintf(stderr, "This does not appear to be a firewall or router (ipv4 forwarding is disabled)\n"); exit(EXIT_FAILURE); } fclose(id); if (ip1 == -1 || ip2 == -1) { fprintf(stderr, "Invalid IP address\n"); exit(EXIT_FAILURE); } getifconfig(); if (scan_conntrack(ip1, port1, ip2, port2)) return EXIT_SUCCESS; else return EXIT_FAILURE; }