Advertisement
njunwa1

Java Anti-Sniff

Nov 28th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package sample;
  2.  
  3. import com.sun.jna.Native;
  4. import com.sun.jna.platform.win32.User32;
  5. import com.sun.jna.platform.win32.WinDef.HWND;
  6.  
  7. import java.util.TimerTask;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. public class EnumerateWindows extends TimerTask{
  12.  
  13.     private static final int MAX_TITLE_LENGTH = 1024;
  14.     //public static boolean exitStatus =false;
  15.  
  16.     /*
  17.     *  This Piece of code uses a Timer to schedule some TimerTask
  18.     *  The task itself is to get the active window
  19.     *  If the active window title matches the regular expression then the application is supposed to exit
  20.     *
  21.     * */
  22.  
  23.     @Override
  24.     public void run() {
  25.         char[] buffer = new char[MAX_TITLE_LENGTH * 2];
  26.  
  27.         HWND hwnd = User32.INSTANCE.GetForegroundWindow();
  28.         User32.INSTANCE.GetWindowText(hwnd, buffer, MAX_TITLE_LENGTH);
  29.         System.out.println("Active window title: " + Native.toString(buffer));
  30.  
  31.         Pattern p = Pattern.compile("(sniff|wireshark|dump|network analyzer|packet analyzer|packet tracer|network tracer)");
  32.         Matcher m = p.matcher(Native.toString(buffer));
  33.  
  34.         int start = 0;
  35.         while (m.find(start)) {
  36.             System.out.println("Match found: " + m.group(1) + " at position: " + m.start());
  37.             if((m.group(1)=="dump") || (m.group(1) =="sniff") || (m.group(1)=="wireshark") || (m.group(1)=="analyzer")){
  38.  
  39.  
  40.                 //Code To exit the program
  41.  
  42.             }
  43.             start = m.end();
  44.         }
  45.  
  46.     }
  47. }
  48.  
  49. ///This is piece of code that You need to put in any Initilizing method so as
  50. // To implement the Timertask of getting acitve window and exiting the application
  51. //It has been commented purposefuly cause the initilization method does not belong to this class under JavaFX
  52. /*
  53.     @Override
  54.     public void initialize(URL location, ResourceBundle resources) {
  55.         //EnumerateWindows.enumTitles();
  56.         Timer timer = new Timer();
  57.         timer.schedule(new EnumerateWindows(), 0, 5000);
  58.  
  59.     }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement