def enable_RST(): execCommand("sudo iptables -I OUTPUT -p tcp --tcp-flags ALL RST -j ACCEPT", shell=True) execCommand( "sudo iptables -I OUTPUT -p tcp --tcp-flags ALL RST,ACK -j ACCEPT", shell=True) time.sleep(THREAD_SYNC_TIME)
def sniff_ackseq(i, srcIP): try: tf = tempfile.NamedTemporaryFile() execCommand("sudo tcpdump -c 1 -w " + tf.name + ".cap -i " + i + " \"src net " + srcIP + "\" 2>/dev/null", shell = True) scan = rdpcap("" + tf.name + ".cap") finally: execCommand("rm -f " + tf.name + ".cap", shell = True) return scan[0]
def get_MPTCP_syn(i): try: tf = tempfile.NamedTemporaryFile() execCommand("sudo tcpdump -c 1 -w " + tf.name + ".cap -i " + i + " \"tcp[tcpflags] & tcp-syn != 0\" 2>/dev/null", shell = True) scan = rdpcap("" + tf.name + ".cap") finally: execCommand("rm -f " + tf.name + ".cap", shell = True) return scan[0]
def get_MPTCP_ack(i, dstIP): try: tf = tempfile.NamedTemporaryFile() execCommand("sudo tcpdump -c 1 -w " + tf.name + ".cap -i " + i + " \"tcp[tcpflags] & (tcp-ack) != 0 and tcp[tcpflags] & (tcp-syn) == 0 and dst net " + dstIP + "\" 2>/dev/null", shell = True) scan = rdpcap("" + tf.name + ".cap") finally: execCommand("rm -f " + tf.name + ".cap", shell = True) return scan[0]
def get_MPTCP_syn(i): try: tf = tempfile.NamedTemporaryFile() execCommand("sudo tcpdump -c 1 -w " + tf.name + ".cap -i " + i + " \"tcp[tcpflags] & tcp-syn != 0\" 2>/dev/null", shell=True) scan = rdpcap("" + tf.name + ".cap") finally: execCommand("rm -f " + tf.name + ".cap", shell=True) return scan[0]
def get_MPTCP_ack(i, dstIP): try: tf = tempfile.NamedTemporaryFile() execCommand( "sudo tcpdump -c 1 -w " + tf.name + ".cap -i " + i + " \"tcp[tcpflags] & (tcp-ack) != 0 and tcp[tcpflags] & (tcp-syn) == 0 and dst net " + dstIP + "\" 2>/dev/null", shell=True) scan = rdpcap("" + tf.name + ".cap") finally: execCommand("rm -f " + tf.name + ".cap", shell=True) return scan[0]
def get_MPTCP_synack(i, srcIP): try: tf = tempfile.NamedTemporaryFile() execCommand( "sudo tcpdump -c 1 -w " + tf.name + ".cap -i " + i + ' "tcp[tcpflags] & (tcp-syn) != 0 and src net ' + srcIP + '" 2>/dev/null', shell=True, ) scan = rdpcap("" + tf.name + ".cap") finally: execCommand("rm -f " + tf.name + ".cap", shell=True) return scan[0]
def main(): args = parse_args() if (int(args.samples) < 10): print "Samples must be at least 10" return execCommand("rm -f tmp", shell = True) # PID Checking execCommand("top -p " + str(args.pid) + " -n1 -b | awk '/" + str(args.pid) + "/{print $12\"\t\" $10}' >> tmp", shell = True) if len(tuple(open('./tmp', 'r'))) == 0: execCommand("rm -f tmp", shell = True) print "Error: PID not found" return execCommand("rm -f graph.png", shell = True) print "Start scanning..." for i in range(0, int(args.samples)): execCommand("top -p " + str(args.pid) + " -n1 -b | awk '/" + str(args.pid) + "/{print \"" + str(i) + " \t\" $9}' >> tmp", shell = True) sys.stdout.write("\rScanning [[ %d%% ]]" % (i*100/int(args.samples))) sys.stdout.flush() print "" # Generating the graph execCommand("sudo gnuplot gplotscript", shell = True) # Check sample size a posteriori if len(tuple(open('./tmp', 'r'))) < 10: print "Error: Not enough samples were generated" return # Generating the average value lines = tuple(open('./tmp', 'r')) count = 0 mysum = 0.0 for line in lines: mysum += float(line.split('\t')[1].replace(',','.')) count += 1 print "Average CPU load: " + str(float(mysum)/float(count))
def graph_bw(a1, a2, f): try: tf1 = tempfile.NamedTemporaryFile() tf2 = tempfile.NamedTemporaryFile() tf3 = tempfile.NamedTemporaryFile() execCommand("echo set terminal png > " + tf3.name, shell = True) execCommand("echo set grid >> " + tf3.name, shell = True) execCommand("echo set yrange [ -10 : ] >> " + tf3.name, shell = True) execCommand("echo set title \\\"Protocol breakdown in the last hour\\\" >> " + tf3.name, shell = True) execCommand("echo set xlabel \\\"seconds\\\" >> " + tf3.name, shell = True) execCommand("echo set ylabel \\\"packets/s\\\" >> " + tf3.name, shell = True) execCommand("echo plot \\\"" + tf1.name + "\\\" using 1:\\(\\$2/60\\) smooth csplines title \\\"TCP1\\\" \\\ >> " + tf3.name, shell = True) execCommand("echo ,\\\"" + tf2.name + "\\\" using 1:\\(\\$2/60\\) smooth csplines title \\\"TCP2\\\" >> " + tf3.name, shell = True) execCommand("tcpstat -r " + f + " -f \"net " + a1 + "\" -o \"%R\\t%T\\n\" 0.1 > " + tf1.name + " && tcpstat -r " + f + " -f \"net " + a2 + "\" -o \"%R\\t%T\\n\" 0.1 > " + tf2.name + " && gnuplot " + tf3.name + " > bw.png", shell = True) # execCommand("cat " + tf1.name + " > out", shell = True) # execCommand("cat " + tf2.name + " >> out", shell = True) finally: return
def enable_RST(): execCommand("sudo iptables -I OUTPUT -p tcp --tcp-flags ALL RST -j ACCEPT", shell=True) execCommand("sudo iptables -I OUTPUT -p tcp --tcp-flags ALL RST,ACK -j ACCEPT", shell=True) time.sleep(THREAD_SYNC_TIME)
def graph_bw(a1, a2, f): try: tf1 = tempfile.NamedTemporaryFile() tf2 = tempfile.NamedTemporaryFile() tf3 = tempfile.NamedTemporaryFile() execCommand("echo set terminal png > " + tf3.name, shell=True) execCommand("echo set grid >> " + tf3.name, shell=True) execCommand("echo set yrange [ -10 : ] >> " + tf3.name, shell=True) execCommand( "echo set title \\\"Protocol breakdown in the last hour\\\" >> " + tf3.name, shell=True) execCommand("echo set xlabel \\\"seconds\\\" >> " + tf3.name, shell=True) execCommand("echo set ylabel \\\"packets/s\\\" >> " + tf3.name, shell=True) execCommand( "echo plot \\\"" + tf1.name + "\\\" using 1:\\(\\$2/60\\) smooth csplines title \\\"TCP1\\\" \\\ >> " + tf3.name, shell=True) execCommand( "echo ,\\\"" + tf2.name + "\\\" using 1:\\(\\$2/60\\) smooth csplines title \\\"TCP2\\\" >> " + tf3.name, shell=True) execCommand("tcpstat -r " + f + " -f \"net " + a1 + "\" -o \"%R\\t%T\\n\" 0.1 > " + tf1.name + " && tcpstat -r " + f + " -f \"net " + a2 + "\" -o \"%R\\t%T\\n\" 0.1 > " + tf2.name + " && gnuplot " + tf3.name + " > bw.png", shell=True) # execCommand("cat " + tf1.name + " > out", shell = True) # execCommand("cat " + tf2.name + " >> out", shell = True) finally: return