Last active
December 30, 2015 22:09
-
-
Save NusZzz/7891962 to your computer and use it in GitHub Desktop.
Network.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class Network{ | |
// ip method | |
public void ip(String newString){ | |
// make a position of each string | |
int numberPosition[] = new int[newString.length()]; | |
for(int i=0;i<=newString.length()-1;i++){ | |
numberPosition[i] = Character.getNumericValue(newString.charAt(i)); | |
} | |
// type of field that we use to calculate and show to user | |
int version = numberPosition[0]; | |
int HeaderLength = numberPosition[0]*numberPosition[1]; | |
int totalLength = (int)(numberPosition[4]*4096 + numberPosition[5]*256 + numberPosition[6]*16 + numberPosition[7]*1); | |
String identification = "0x"+newString.charAt(8)+newString.charAt(9)+newString.charAt(10)+newString.charAt(11); | |
int timeToLive = (int)(numberPosition[16]*16 + (numberPosition[17]*1)); | |
int Protocol = (int)(numberPosition[19]*1); | |
String headerCheckSum = "0x"+newString.charAt(20)+newString.charAt(21)+newString.charAt(22)+newString.charAt(23); | |
int sourceIP1 = (int)(numberPosition[24]*16 + (numberPosition[25]*1)); | |
int sourceIP2 = (int)(numberPosition[26]*16 + (numberPosition[27]*1)); | |
int sourceIP3 = (int)(numberPosition[28]*16 + (numberPosition[29]*1)); | |
int sourceIP4 = (int)(numberPosition[30]*16 + (numberPosition[31]*1)); | |
int destinationIP1 = (int)(numberPosition[32]*16 + (numberPosition[33]*1)); | |
int destinationIP2 = (int)(numberPosition[34]*16 + (numberPosition[35]*1)); | |
int destinationIP3 = (int)(numberPosition[36]*16 + (numberPosition[37]*1)); | |
int destinationIP4 = (int)(numberPosition[38]*16 + (numberPosition[39]*1)); | |
// check protocol name | |
String protoColName = ""; | |
if(Protocol == 4){ | |
protoColName = "(IPv4)"; | |
}else if(Protocol==6){ | |
protoColName = "(TCP)"; | |
}else{ | |
protoColName = ""; | |
} | |
// print output of each field | |
System.out.println("\n====================== IP Analyzer ======================"); | |
System.out.println("\n\tVersion \t\t: " + version); | |
System.out.println("\tHeader Length \t\t: " + HeaderLength); | |
System.out.println("\tTotal Length \t\t: " + totalLength); | |
System.out.println("\tIdentification \t\t: " + identification); | |
System.out.println("\tTime to Live \t\t: " + timeToLive); | |
System.out.println("\tProtocol \t\t: " + Protocol + protoColName); | |
System.out.println("\tHeader Checksum \t: " + headerCheckSum); | |
System.out.println("\tSource IP \t\t: " +sourceIP1 +"."+ sourceIP2 +"."+ sourceIP3 +"."+ sourceIP4); | |
System.out.println("\tDestination IP \t\t: " +destinationIP1 +"."+ destinationIP2 +"."+ destinationIP3 +"."+ destinationIP4); | |
} | |
// tcp method | |
public void tcp(String newString){ | |
// make a position of each string | |
int numberPosition[] = new int[newString.length()]; | |
for(int i=0;i<=newString.length()-1;i++){ | |
numberPosition[i] = Character.getNumericValue(newString.charAt(i)); | |
} | |
// type of field that we use to calculate and show to user | |
int soucePortTCP = (int)(numberPosition[40]*4096 + numberPosition[41]*256 + numberPosition[42]*16 + numberPosition[43]*1); | |
int destinationPortTCP = (int)(numberPosition[44]*4096 + numberPosition[45]*256 + numberPosition[46]*16 + numberPosition[47]*1); | |
long sequenceNumber = (long)(numberPosition[48]*Math.pow(16, 7) + numberPosition[49]*Math.pow(16, 6) + numberPosition[50]*Math.pow(16, 5) + numberPosition[51]*Math.pow(16, 4) + numberPosition[52]*Math.pow(16, 3) + numberPosition[53]*Math.pow(16, 2) + numberPosition[54]*16 + numberPosition[55]*1); | |
long acknowledgeMentNumber = (long)(numberPosition[56]*Math.pow(16, 7) + numberPosition[57]*Math.pow(16, 6) + numberPosition[58]*Math.pow(16, 5) + numberPosition[59]*Math.pow(16, 4) + numberPosition[60]*Math.pow(16, 3) + numberPosition[61]*Math.pow(16, 2) + numberPosition[62]*16 + numberPosition[63]*1); | |
int headerLength = (int)(numberPosition[64]*4); | |
String flag = "0x"+numberPosition[65]+numberPosition[66]+numberPosition[67]; | |
String checkSumTCP = "0x"+newString.charAt(72)+newString.charAt(73)+newString.charAt(74)+newString.charAt(75); | |
int windowSizeValue = (int)(numberPosition[68]*4096 + numberPosition[69]*256 + numberPosition[70]*16 + numberPosition[71]*1); | |
int flagPositionOne = Character.getNumericValue(flag.charAt(3)); | |
int flagPositionTwo = Character.getNumericValue(flag.charAt(4)); | |
String flagName = null; | |
String destinationPortName,sourcePortName = ""; | |
// check port to print out. | |
if(soucePortTCP==80){ | |
sourcePortName = " (https)"; | |
}else if(soucePortTCP==443){ | |
sourcePortName = " (http)"; | |
}else{ | |
sourcePortName = ""; | |
} | |
// check port to print out. | |
if(destinationPortTCP==80){ | |
destinationPortName = " (https)"; | |
}else if(destinationPortTCP==443){ | |
destinationPortName = " (http)"; | |
}else{ | |
destinationPortName = ""; | |
} | |
// check flag | |
if(flagPositionOne==1 && flagPositionTwo==0){ | |
flagName = " (ACK)"; | |
}else if(flagPositionOne==0 && flagPositionTwo==1){ | |
flagName = " (FIN)"; | |
}else if(flagPositionOne==0 && flagPositionTwo==2){ | |
flagName = " (SYN)"; | |
}else if(flagPositionOne==0 && flagPositionTwo==4){ | |
flagName = " (RST)"; | |
}else if(flagPositionOne==1 && flagPositionTwo==1){ | |
flagName = " (FIN, ACK)"; | |
}else if(flagPositionOne==1 && flagPositionTwo==2){ | |
flagName = " (SYN, ACK)"; | |
}else if(flagPositionOne==1 && flagPositionTwo==4){ | |
flagName = " (RST, ACK)"; | |
}else if(flagPositionOne==1 && flagPositionTwo==8){ | |
flagName = " (PSH, ACK)"; | |
}else if(flagPositionOne==2 && flagPositionTwo==1){ | |
flagName = " (FIN, URG)"; | |
}else if(flagPositionOne==2 && flagPositionTwo==2){ | |
flagName = " (SYN, URG)"; | |
}else if(flagPositionOne==2 && flagPositionTwo==4){ | |
flagName = " (RSP, URG)"; | |
}else if(flagPositionOne==2 && flagPositionTwo==4){ | |
flagName = " (PSH, URG)"; | |
} | |
// print output of each field | |
System.out.println("\n====================== TCP Analyzer ======================"); | |
System.out.println("\n\tSource Port \t\t: " + soucePortTCP + sourcePortName); | |
System.out.println("\tDestination Port \t: " + destinationPortTCP + destinationPortName); | |
System.out.println("\tSequence Number\t\t: " + sequenceNumber); | |
System.out.println("\tAcknowledgement Number\t: " + acknowledgeMentNumber); | |
System.out.println("\tHeader Length \t\t: " + headerLength + " bytes"); | |
System.out.println("\tFlag \t\t\t: " + flag + "" + flagName); | |
System.out.println("\tWindow Size Value \t: " + windowSizeValue); | |
System.out.println("\tCheck Sum \t\t: " + checkSumTCP); | |
} | |
public static void main(String[] args){ | |
// create onject called network | |
Network nwtwork = new Network(); | |
Scanner kb = new Scanner(System.in); | |
// input from keyboard | |
System.out.print("Enter Hexadecimal : "); | |
// input to HEX | |
String HEX = kb.nextLine(); | |
// try to get input that have space to | |
String[] SplitedString = HEX.split("\\s"); | |
String newString = ""; | |
// try to connect new string | |
for(int i=0;i<SplitedString.length;i++){ | |
newString = newString+SplitedString[i]; | |
} | |
// check character that in a-f or A-F or 0-9 or not. | |
if(HEX.matches("[a-fA-F0-9]+")){ | |
// check length of header | |
if(HEX.length()<80){ | |
System.out.println("Header should more than 80 characters"); | |
main(args); | |
}else{ | |
// calculate 2 values | |
nwtwork.ip(HEX); | |
nwtwork.tcp(HEX); | |
} | |
}else{ | |
// try to call main() again | |
System.out.println("Please enter set of characters between A-F or number 0-9"); | |
main(args); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment