Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class Arp {
  5.  
  6. private int ip;
  7.  
  8. private int mac;
  9.  
  10. private Map<Integer, Integer> arptable;
  11.  
  12. // for simplicity sake the ARP packet format is assumed to be <ip-address>-<mac-address>
  13. private static final String PACKET_DELIMITER = "-";
  14.  
  15. private static final String SPOOF_ALERT_STATUS = "-1";
  16. private static final String IP_ALREADY_IN_USE_STATUS = "-2";
  17. private static final String NOT_FOUND = "-3";
  18.  
  19. // constructor
  20. public Arp(int mac, int ip) {
  21. this.mac = mac;
  22. this.ip = ip;
  23. arptable = new HashMap<>();
  24. arptable.put(ip, mac);
  25. }
  26.  
  27. // This function returns a spoofed ARP packet:
  28. // The argument passed to this function is the IP address that you want to
  29. // impersonate.
  30. public String spoofArp(int spoofIP) {
  31. return spoofIP + PACKET_DELIMITER + mac;
  32. }
  33.  
  34. // Receive a message and provide the response. This function returns either
  35. // a packet, or a status code.
  36. public String receiveArp(String message) {
  37. String[] tokens = message.split(PACKET_DELIMITER);
  38. int receivedIp = Integer.parseInt(tokens[0].trim());
  39. int receivedMac = -1;
  40. if(tokens.length > 1) {
  41. receivedMac = Integer.parseInt(tokens[1].trim());
  42. }
  43. if(receivedMac == mac && receivedIp != ip) {
  44. return SPOOF_ALERT_STATUS;
  45. }
  46.  
  47. Integer knownCachedMac = arptable.get(receivedIp);
  48. if(knownCachedMac != null && receivedMac != -1 && knownCachedMac != receivedMac) {
  49. return IP_ALREADY_IN_USE_STATUS;
  50. }
  51.  
  52. if(knownCachedMac == null) {
  53. // cache
  54. if(receivedMac == -1) {
  55. message = NOT_FOUND;
  56. }
  57. else {
  58. arptable.put(receivedIp, receivedMac);
  59. }
  60. }
  61. else {
  62. message = receivedIp + PACKET_DELIMITER + knownCachedMac;
  63. }
  64.  
  65. return message;
  66. }
  67.  
  68. public static void main(String[] args) {
  69. Arp arp = new Arp(1, 1);
  70. String spoofPacket = arp.spoofArp(2);
  71. if(!spoofPacket.equals("2-1")) {
  72. System.out.println("Improper spoofing, expected 2-1 but received " + spoofPacket);
  73. }
  74.  
  75. String receivedMessage = arp.receiveArp("6-1");
  76. if(!receivedMessage.equals(SPOOF_ALERT_STATUS)) {
  77. System.out.println("expected a spoofing alert but didn't arrive");
  78. }
  79.  
  80. receivedMessage = arp.receiveArp("2-2");
  81. if(!receivedMessage.equals("2-2")) {
  82. System.out.println("expected the same message to be returned");
  83. }
  84.  
  85. receivedMessage = arp.receiveArp("2-");
  86. if(!receivedMessage.equals("2-2")) {
  87. System.out.println("expected cached message to be returned");
  88. }
  89.  
  90. receivedMessage = arp.receiveArp("2-5");
  91. if(!receivedMessage.equals(IP_ALREADY_IN_USE_STATUS)) {
  92. System.out.println("expected IP_ALREADY_IN_USE_STATUS");
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement