1

Hi guys,
I hope you can help me. Sorry for my english, it's not my first language. I'm new to programming and I'm stuck in my first project. I try to program a simple data traffic generator with Tkinter and Python 2.7.
I use the dpkt library as a basis for sending packets. In the beginn I wrote a script that I can run in the terminal and it works totally fine.

import dpkt
import socket,  random

# input
ip_dst = raw_input('IP-address of destination: ')
n = input('number of packets 2^n: ')
num =2**(n)
# packet size
b = input('size of packets (number between 1 and 5950): ')
while (b<1 or b>5950):
    print('Invalid number!')
    b = input('size of packets (number between 1 and 5950): ')
# script for sending packets
z=0
while z<=num:
    print z
    echo = dpkt.icmp.ICMP.Echo()
    echo.id = random.randint(0,0xffff)
    echo.seq = random.randint(0,0xffff)

    echo.data  = 'hello world'*b                    # changing size with factor b

    icmp = dpkt.icmp.ICMP()
    icmp.type = dpkt.icmp.ICMP_ECHO
    icmp.data = echo

    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)
    s.connect((ip_dst,1))                           # ip-address of destination
    sent = s.send(str(icmp))
    print 'sent %d bytes' %sent
    z=z+1

In my next step I tried to create a GUI to make it more handy and also useable for other users. So I created a mask in Tkinter, where I can enter the IP-address, select the size and the number of packets. Further I want the option that I can choose the type of the packet (ICMP/UDP). The code for sending UDP-packets I want to add later as a function.

from Tkinter import *
import ttk
import tkFont
import dpkt
import socket, random

sent_packets = 0

# send icmp
def send_icmp(sent_packets, s1,ip_dst):
     while (sent_packets<=n):
         print sent_packets
         echo = dpkt.icmp.ICMP.Echo()
         echo.id = random.randint(0,0xffff)
         echo.seq = random.randint(0,0xffff)

         echo.data  = 'hello world'*(s1)

         icmp = dpkt.icmp.ICMP()
         icmp.type = dpkt.icmp.ICMP_ECHO
         icmp.data = echo

         s = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)
         s.connect((ip_dst,1))
         sent = s.send(str(icmp))
         print 'sent %d bytes' %sent
         sent_packets += 1
         mainWin.update()


mainWin= Tk()
mainWin.title('Traffic Generator')
mainWin.columnconfigure (0, weight=1)
mainWin.rowconfigure(0, weight=1)

ip_dst = StringVar()

# Frame settings
mainFrame = Frame(mainWin, width=200, height=100)
mainFrame.grid_propagate(0)
mainFrame.grid()
mainFrame.columnconfigure(0, weight=1)
mainFrame.columnconfigure(1, weight=1)
mainFrame.columnconfigure(2, weight=1)
mainFrame.rowconfigure(0, weight=1)
mainFrame.rowconfigure(1, weight=1)
mainFrame.rowconfigure(2, weight=1)
mainFrame.rowconfigure(3, weight=1)


# Headline
label_1 = Label(mainWin, text="Traffic Generator", font="Helvetica 11 bold")
label_1.grid(column=0, row=0, padx=15, pady=5, sticky=(N,W), columnspan=2)

# Input

# IP-address:
label_2 = Label(mainWin, text="Please enter IP-address of destination: ", font=tkFont.Font(family="Helvetica", size=10))
label_2.grid(column=0, row=1, padx=15, pady=5, sticky=(N,W))

entry_1 = Entry(mainWin, textvariable=ip_dst, width=13)
entry_1.grid(column=1, row=1, padx=15, pady=5, sticky=(N,E))
entry_1.focus()

# type:
label_4 = Label(mainWin, text="Select packet type: ", font=tkFont.Font(family="Helvetica", size=10))
label_4.grid(column=0, row=2, padx=15, pady=5, sticky=(N,W))
type = IntVar()
Radiobutton(mainWin, text="ICMP", variable=type, value="send_icmp").grid(row=2, column=1, sticky=(N,W), padx=15, pady=5, columnspan=2)
Radiobutton(mainWin, text="UDP", variable=type, value="send_udp").grid(row=2, column=1, sticky=(N,W), padx=70, pady=5, columnspan=2)

# size:
label_3 = Label(mainWin, text="Select packet size: ", font=tkFont.Font(family="Helvetica", size=10))
label_3.grid(column=0, row=3, padx=15, pady=5, sticky=(N,W))

size1 = IntVar()
size_slide1 = Scale(mainWin, from_=1, to=5900, orient='horizontal', variable= size1, length=200, resolution=10, showvalue=0)
size_slide1.set(3000)
size_slide1.grid(column=1, row=3, padx=15, pady=5, sticky=(N,E))

s1= str(size1)

Label(mainWin, textvariable= size1).grid(column=2, row=3, padx=15, pady=5, sticky=(N,E)) 


# number:
label_4 = Label(mainWin, text="Select number of packets: ",font=tkFont.Font(family="Helvetica", size=10))
label_4.grid(column=0, row=4, padx=15, pady=5, sticky=(N,W))

num1 = int()
Label(mainWin, text="2^n packets: ", font=tkFont.Font(family="Helvetica", size=10)).grid(column=1, row=4, padx=15, pady=5, sticky=(W,E))
num_slide1 = Scale(mainWin, from_=1, to=500, orient='horizontal', variable = num1, length=200, resolution=2, showvalue=0)
num_slide1.set(32)
num_slide1.grid(column=1, row=5, padx=15, pady=5, sticky=(N,E))
Label(mainWin, textvariable= num1).grid(column=2, row=5, padx=15, pady=5, sticky=(N,E))
number1 = 2**(num1)

# send:
Button(mainWin, text="Send packets", command=type, width=13).grid(column=2, row=6) # radiobuttons select value of variable 'type' which executes selected function (e.g. def send_icmp) of selected type


mainWin.mainloop()

The problem is, that when I hit send there is no reaction in the terminal. I don't understand why the script doesn't run. This is my first code and I'm working on this already quite a time. I probably make a little detour but I hope you can help me out and can tell me what the problem is. Thank you so much already. Alex

junker1248
  • 11
  • 1

1 Answers1

2

The script runs perfectly fine , its just the button you need to tweak.Probably you need to highlight the button when mouse is on it, use something like activebackground or activeforeground

I checked via tcpdump or you can use wireshark on the remote IP where you are sending the packet.

Nishant Singh
  • 3,055
  • 11
  • 36
  • 74
  • Sry for my late response. I didn't have the time to continue my project. Even when I tweak the button with activebackground, I don't get any reaction while pressing send. Is it possible that you cannot run such a script (for sending packets) in a function? Or do I have to work with classes? – junker1248 May 17 '16 at 10:39
  • ofcourse you can , just identify the main working blocks and create `def` for them..and as u already have used the function `send_icmp` its already modular , so u don't need to do much work on it – Nishant Singh May 17 '16 at 17:07
  • Thank you! I got it running. Now I use only the socket library for creating packets, its much faster. My only problem is that I cannot really change the type of the packets with the radiobuttons. Is it better to use different functions for each type, or just one function with changeable variables via radiobutton? – junker1248 May 24 '16 at 09:20
  • break it into more functions , this way you will be able to debug more properly too – Nishant Singh May 24 '16 at 12:18