/* 1*/ /********************************************************************************* /* 2*/ * /* 3*/ * PacketFiltering クラス /* 4*/ * /* 5*/ * 設定されたフィルタでパケットフィルタリングを行う /* 6*/ * /* 7*/ * /* 8*/ * インバウンド・アウトバウンドのインタフェースハンドルを作成する /* 9*/ * インタフェースハンドルはブラックリスト方式(原則許可)か /* 10*/ * ホワイトリスト方式(原則禁止)を設定する /* 11*/ * 両インタフェースハンドルにフィルタを加える /* 12*/ * ブラックリスト・ハンドルの場合、フィルタに合致するパケットは破棄される /* 13*/ * ホワイトリスト・ハンドルの場合、フィルタに合致するパケットは通過する /* 14*/ * /* 15*/ * /* 16*/ * /* 17*/ * public メソッド /* 18*/ * ・void BindInterfaceToIPAddress(InetAddress adrs, int adrsType, /* 19*/ * INTERFACE_HANDLE inbound, INTERFACE_HANDLE outbound) /* 20*/ * IPアドレスにインタフェースハンドルをバインドする /* 21*/ * (フィルタリングの実行) /* 22*/ * /* 23*/ * ・void UnBindInterface() /* 24*/ * バインドされているインタフェースハンドルをアンバインドする /* 25*/ * (フィルタリングを停止) /* 26*/ * /* 27*/ * ・FILTER_DESCRIPTOR getICMPfilter() /* 28*/ * ICMPフィルタの生成 /* 29*/ * /* 30*/ * ・FILTER_DESCRIPTOR getIPfilter() /* 31*/ * IPフィルタの生成 /* 32*/ * /* 33*/ * ・FILTER_DESCRIPTOR getTCPfilter() /* 34*/ * TCPフィルタの生成 /* 35*/ * /* 36*/ * ・FILTER_DESCRIPTOR getUDPfilter() /* 37*/ * UDPフィルタの生成 /* 38*/ * /* 39*/ * /* 40*/ * private メソッド /* 41*/ * ・String PfCreateInterface(int dwName, boolean inAction, /* 42*/ * boolean outAction, boolean bUseLog, boolean bMustBeUnique) /* 43*/ * フィルタリングインタフェースの作成 /* 44*/ * /* 45*/ * ・String PfAddFiltersToInterfaceIN(int addrType, byte[] SrcAddr, /* 46*/ * byte[] SrcMask, byte[] DstAddr, byte[] DstMask, int srcPort, /* 47*/ * int dstPort, int ceilingSrcPort, int ceilingDstPort, int protocol) /* 48*/ * フィルタをインバウンド・インタフェースに加える /* 49*/ * /* 50*/ * ・String PfAddFiltersToInterfaceOUT(int addrType, byte[] SrcAddr, byte[] SrcMask, /* 51*/ * byte[] DstAddr, byte[] DstMask, int srcPort, int dstPort, /* 52*/ * int ceilingSrcPort, int ceilingDstPort, int protocol) /* 53*/ * フィルタをアウトバウンド・インタフェースに加える /* 54*/ * /* 55*/ * ・String PfBindInterfaceToIPAddress(int pfatType, byte[] IPAddress) /* 56*/ * フィルタリングインタフェースをIPアドレスにバインド /* 57*/ * /* 58*/ * ・String PfUnBindInterface(); /* 59*/ * フィルタリングインタフェースのアンバインド /* 60*/ * /* 61*/ * ・String PfDeleteInterface() /* 62*/ * フィルタリングインタフェースの除去 /* 63*/ * /* 64*/ * ・FILTER_DESCRIPTOR setBasicDescriptor(FILTER_DESCRIPTOR descriptor) /* 65*/ * フィルタの基本設定を行う /* 66*/ * /* 67*/ *********************************************************************************/ /* 68*/ /* 69*/ package WinPacketFiltering; /* 70*/ /* 71*/ import java.net.InetAddress; /* 72*/ import java.net.UnknownHostException; /* 73*/ import WinPacketFiltering.FILTER_DESCRIPTOR; /* 74*/ /* 75*/ public class PacketFiltering /* 76*/ { /* 77*/ /* Native Interface */ /* 78*/ /* 79*/ private static final boolean DEBUG = false; // デバッグフラグ /* 80*/ /* 81*/ private static final int inull = 0; /* 82*/ private static final String NO_ERROR = "NO_ERROR"; /* 83*/ private static final String NATIVE = "NATIVE : "; /* 84*/ /* 85*/ // フィルタリングの実行 /* 86*/ public static void BindInterfaceToIPAddress(InetAddress adrs, /* 87*/ int adrsType, INTERFACE_HANDLE inbound, INTERFACE_HANDLE outbound) /* 88*/ throws PacketFilteringException /* 89*/ { /* 90*/ String result; /* 91*/ /* 92*/ // Netive: インタフェースの生成 /* 93*/ result = PfCreateInterface( /* 94*/ inull, inbound.rule, outbound.rule, false, true); /* 95*/ /* 96*/ if(DEBUG) System.out.println("CreateInterface : "+result); // デバッグ /* 97*/ /* 98*/ failureProcessor(result); /* 99*/ /*100*/ if(DEBUG) // デバッグ /*101*/ { /*102*/ System.out.println("filter num"); /*103*/ System.out.println("in : "+inbound.getSize()); /*104*/ System.out.println("out : "+outbound.getSize()); /*105*/ } /*106*/ /*107*/ // Native: インバウンドフィルタの追加 /*108*/ for(int i=0;i<inbound.getSize();i++) /*109*/ { /*110*/ FILTER_DESCRIPTOR filter = (FILTER_DESCRIPTOR)inbound.getFilter(i); /*111*/ /*112*/ result = PfAddFiltersToInterfaceIN( /*113*/ filter.AddrType, /*114*/ filter.SrcAddr.getAddress(), /*115*/ filter.SrcMask.getAddress(), /*116*/ filter.DstAddr.getAddress(), /*117*/ filter.DstMask.getAddress(), /*118*/ filter.SrcPort, /*119*/ filter.DstPort, /*120*/ filter.CeilingSrcPort, /*121*/ filter.CeilingDstPort, /*122*/ filter.Protocol /*123*/ ); /*124*/ /*125*/ failureProcessor(result); /*126*/ } /*127*/ /*128*/ if(DEBUG) // デバッグ /*129*/ { /*130*/ System.out.println("AddFiltersToInterfaceIN : "+result); /*131*/ } /*132*/ /*133*/ // Native: アウトバウンドフィルタの追加 /*134*/ for(int i=0;i<outbound.getSize();i++) /*135*/ { /*136*/ FILTER_DESCRIPTOR filter = (FILTER_DESCRIPTOR)outbound.getFilter(i); /*137*/ /*138*/ result = PfAddFiltersToInterfaceOUT( /*139*/ filter.AddrType, /*140*/ filter.SrcAddr.getAddress(), /*141*/ filter.SrcMask.getAddress(), /*142*/ filter.DstAddr.getAddress(), /*143*/ filter.DstMask.getAddress(), /*144*/ filter.SrcPort, /*145*/ filter.DstPort, /*146*/ filter.CeilingSrcPort, /*147*/ filter.CeilingDstPort, /*148*/ filter.Protocol /*149*/ ); /*150*/ /*151*/ failureProcessor(result); /*152*/ } /*153*/ /*154*/ if(DEBUG) System.out.println("AddFiltersToInterfaceOUT : "+result); // デバッグ /*155*/ /*156*/ // IPアドレスにバインド /*157*/ result = PfBindInterfaceToIPAddress(adrsType, adrs.getAddress()); /*158*/ failureProcessor(result); /*159*/ /*160*/ if(DEBUG) System.out.println("BindInterfaceToIPAddress : "+result); // デバッグ /*161*/ } /*162*/ /*163*/ // フィルタリングの停止 /*164*/ public static void UnBindInterface() throws PacketFilteringException /*165*/ { /*166*/ String result; /*167*/ /*168*/ result = PfUnBindInterface(); /*169*/ failureProcessor(result); /*170*/ /*171*/ result = PfDeleteInterface(); /*172*/ failureProcessor(result); /*173*/ } /*174*/ /*175*/ // ネイティブメソッド失敗処理 /*176*/ private static void failureProcessor(String result) /*177*/ throws PacketFilteringException /*178*/ { /*179*/ if(!result.equals(NO_ERROR)) /*180*/ { /*181*/ PfDeleteInterface(); /*182*/ throw new PacketFilteringException(NATIVE+result); /*183*/ } /*184*/ } /*185*/ /*186*/ /*187*/ // (Native) フィルタリングインタフェースの作成 /*188*/ private static native String PfCreateInterface( /*189*/ int dwName, /*190*/ // PFFORWARD_ACTION /*191*/ boolean inAction, /*192*/ // PFFORWARD_ACTION /*193*/ boolean outAction, /*194*/ boolean bUseLog, /*195*/ boolean bMustBeUnique /*196*/ ); /*197*/ /*198*/ // (Native) フィルタをインバウンド・インタフェースに加える /*199*/ private static native String PfAddFiltersToInterfaceIN( /*200*/ int addrType, /*201*/ byte[] SrcAddr, /*202*/ byte[] SrcMask, /*203*/ byte[] DstAddr, /*204*/ byte[] DstMask, /*205*/ int srcPort, /*206*/ int dstPort, /*207*/ int ceilingSrcPort, /*208*/ int ceilingDstPort, /*209*/ int protocol /*210*/ ); /*211*/ /*212*/ // (Native) フィルタをアウトバウンド・インタフェースに加える /*213*/ private static native String PfAddFiltersToInterfaceOUT( /*214*/ int addrType, /*215*/ byte[] SrcAddr, /*216*/ byte[] SrcMask, /*217*/ byte[] DstAddr, /*218*/ byte[] DstMask, /*219*/ int srcPort, /*220*/ int dstPort, /*221*/ int ceilingSrcPort, /*222*/ int ceilingDstPort, /*223*/ int protocol /*224*/ ); /*225*/ /*226*/ // (Native) フィルタリングインタフェースをIPアドレスにバインド /*227*/ private static native String PfBindInterfaceToIPAddress( /*228*/ int pfatType, /*229*/ byte[] IPAddress /*230*/ ); /*231*/ /*232*/ // (Native) フィルタリングインタフェースのアンバインド /*233*/ private static native String PfUnBindInterface(); /*234*/ /*235*/ // (Native) フィルタリングインタフェースの除去 /*236*/ private static native String PfDeleteInterface(); /*237*/ /*238*/ static /*239*/ { /*240*/ System.loadLibrary("PacketFiltering"); // dllのロード /*241*/ } /*242*/ /*243*/ /************************************************/ /*244*/ /*245*/ /* Java Methods */ /*246*/ /*247*/ /** FIELD VALUE **/ /*248*/ // AddrType /*249*/ public static final int IPv4_FLAG = 4; /*250*/ public static final int IPv6_FLAG = 6; /*251*/ // SrcMask, DstMask /*252*/ private static final byte[] ALL_FFv4 /*253*/ = {(byte)255, (byte)255, (byte)255, (byte)255}; /*254*/ public static InetAddress NO_MASKv4;// = InetAddress.getByAddress(ALL_FFv4); /*255*/ private static final byte[] ALL_00v4 /*256*/ = {(byte)0, (byte)0, (byte)0, (byte)0}; /*257*/ public static InetAddress ALL_IPv4;// = InetAddress.getByAddress(ALL_00v4); /*258*/ private static final byte[] ALL_FFv6 /*259*/ = { (byte)255, (byte)255, (byte)255, (byte)255, /*260*/ (byte)255, (byte)255, (byte)255, (byte)255, /*261*/ (byte)255, (byte)255, (byte)255, (byte)255, /*262*/ (byte)255, (byte)255, (byte)255, (byte)255}; /*263*/ public static InetAddress NO_MASKv6;// = InetAddress.getByAddress(ALL_FFv6); /*264*/ private static final byte[] ALL_00v6 /*265*/ = { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, /*266*/ (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0}; /*267*/ public static InetAddress ALL_IPv6;// = InetAddress.getByAddress(ALL_00v6); /*268*/ // SrcPort, DstPort /*269*/ public static final int NO_SET = -1; /*270*/ public static final int ANY_PORT = 0; /*271*/ // CeilingSrcPort, CeilingDstPort /*272*/ public static final int ONLY_1_PORT = -1; /*273*/ // Protocol /*274*/ public static final int ANY_PROTOCOL = 1; /*275*/ public static final int ICMP = 2; /*276*/ public static final int TCP = 3; /*277*/ public static final int UDP = 4; /*278*/ /*279*/ public static InetAddress LOCAL_HOST;// = InetAddress.getLocalHost(); /*280*/ /* */ /*281*/ /*282*/ // フィルタディスクリプタの基本設定 /*283*/ private static FILTER_DESCRIPTOR setBasicDescriptor(FILTER_DESCRIPTOR descriptor) /*284*/ { /*285*/ descriptor.AddrType = IPv4_FLAG; /*286*/ descriptor.SrcAddr = null; /*287*/ descriptor.SrcMask = NO_MASKv4; /*288*/ descriptor.DstAddr = null; /*289*/ descriptor.DstMask = NO_MASKv4; /*290*/ descriptor.SrcPort = NO_SET; /*291*/ descriptor.DstPort = NO_SET; /*292*/ descriptor.CeilingSrcPort = ONLY_1_PORT; /*293*/ descriptor.CeilingDstPort = ONLY_1_PORT; /*294*/ descriptor.Protocol = ANY_PROTOCOL; /*295*/ /*296*/ return descriptor; /*297*/ } /*298*/ /*299*/ // ICMPフィルタを新規作成する /*300*/ public static FILTER_DESCRIPTOR getICMPfilter() /*301*/ { /*302*/ FILTER_DESCRIPTOR descriptor = setBasicDescriptor(new FILTER_DESCRIPTOR()); /*303*/ descriptor.Protocol = ICMP; /*304*/ /*305*/ return descriptor; /*306*/ } /*307*/ /*308*/ // IPフィルタを新規作成する /*309*/ public static FILTER_DESCRIPTOR getIPfilter() /*310*/ { /*311*/ FILTER_DESCRIPTOR descriptor = setBasicDescriptor(new FILTER_DESCRIPTOR()); /*312*/ descriptor.SrcPort = ANY_PORT; /*313*/ descriptor.DstPort = ANY_PORT; /*314*/ descriptor.CeilingSrcPort = ANY_PORT; /*315*/ descriptor.CeilingDstPort = ANY_PORT; /*316*/ /*317*/ return descriptor; /*318*/ } /*319*/ /*320*/ // TCPフィルタを新規作成する /*321*/ public static FILTER_DESCRIPTOR getTCPfilter() /*322*/ { /*323*/ FILTER_DESCRIPTOR descriptor = setBasicDescriptor(new FILTER_DESCRIPTOR()); /*324*/ descriptor.Protocol = TCP; /*325*/ /*326*/ return descriptor; /*327*/ } /*328*/ /*329*/ // UDPフィルタを新規作成する /*330*/ public static FILTER_DESCRIPTOR getUDPfilter() /*331*/ { /*332*/ FILTER_DESCRIPTOR descriptor = setBasicDescriptor(new FILTER_DESCRIPTOR()); /*333*/ descriptor.Protocol = UDP; /*334*/ /*335*/ return descriptor; /*336*/ } /*337*/ /*338*/ static /*339*/ { /*340*/ try /*341*/ { /*342*/ NO_MASKv4 = InetAddress.getByAddress(ALL_FFv4); /*343*/ ALL_IPv4 = InetAddress.getByAddress(ALL_00v4); /*344*/ NO_MASKv6 = InetAddress.getByAddress(ALL_FFv6); /*345*/ ALL_IPv6 = InetAddress.getByAddress(ALL_00v6); /*346*/ LOCAL_HOST = InetAddress.getLocalHost(); /*347*/ } /*348*/ catch (UnknownHostException e) /*349*/ { /*350*/ e.printStackTrace(); /*351*/ } /*352*/ } /*353*/ /*****************************************************************/ /*354*/ }