1

i have a problem when using my code with threading, the problem is apparently that the variables i define outside of the threading part aren't defined inside the threading part, here is my code:

import sys
import socket
from time import sleep
import threading

ip = raw_input ("please insert host ip: ")
port = input ("please insert port to fuzz: ")
header = raw_input ("please enter the header you want to fuzz, put & in the place you want to fuzz: ")
packet = raw_input ("what string would you like to fuzz the header with? : ")
multi = input ("in what jumps would you liike to multiply the string ? : ")
process = input ("please insert number of threads: ")
host = ip, port
char = packet * multi
a = 1

class ConnectionThread ( threading.Thread ):
    def run ( self ):
        while a > 0:
            try:
                s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
                s.connect((host))
                header = header.replace("&", packet)
                s.send(header)
                s.settimeout(7)
                data = s.recv(4)
                if data > 0:
                    print "got awnser"
                else:
                    print "no awnser"   
                sleep(0.1) 
                print "Fuzzing With:", header
                header = header.replace (packet, "&")
                packet = char + packet 
                s.close()
            except Exception as e:
                print e   
                s.close()
                sys.exit(0)
for x in xrange ( process ):
   ConnectionThread().start()

and i get this as return

local variable 'header' referenced before assignment
Ba7a7chy
  • 1,471
  • 4
  • 14
  • 29

2 Answers2

1

You need to use global to signify the variable is a global one. See, for example, here

For example, add the following to run (above the if):

global host, header, ... # All the other variables you use
Community
  • 1
  • 1
zmbq
  • 38,013
  • 14
  • 101
  • 171
  • i read your link for the global function discussion , but i dont understand how to use it in my code – Ba7a7chy Jul 17 '12 at 09:16
  • 1
    I think the essence of the discussion is: don't use it! Use a function for the gathering of all the parameters from the command line and let it return a dict or whatever you prefer, use it as parameter when you initialise the thread (add an __init__) and then access them as object attribute (self.header or self. parameters['header'] depending on the way you do) – Sebastian Blask Jul 17 '12 at 09:41
0

You will need to give your ConnectionThread class the attributes that you want to pass to it. At the top of the class just put in a init method, this will define the variable that you want to pass to the run method:

import sys
import socket
from time import sleep
import threading

class ConnectionThread ( threading.Thread ):
    def __init__(self):
        self.ip = raw_input ("please insert host ip: ")
        self.port = input ("please insert port to fuzz: ")
        self.header = raw_input ("please enter the header you want to fuzz, put & in the place you want to fuzz: ")
        self.packet = raw_input ("what string would you like to fuzz the header with? : ")
        self.multi = input ("in what jumps would you liike to multiply the string ? : ")
        self.process = input ("please insert number of threads: ")
        self.host = self.ip, self.port
        self.char = self.packet * self.multi
        self.a = 1

    def run ( self ):
        while self.a > 0:
            try:
                s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
                s.connect((self.host))
                self.header = self.header.replace("&", self.packet)
                s.send(self.header)
                s.settimeout(7)
                data = s.recv(4)
                if data > 0:
                    print "got awnser"
                else:
                    print "no awnser"   
                sleep(0.1) 
                print "Fuzzing With:", header
                self.header = self.header.replace (self.packet, "&")
                self.packet = self.char + self.packet 
                s.close()
            except Exception as e:
                print e   
                s.close()
                sys.exit(0)

for x in xrange ( ConnectionThread.process ):
   ConnectionThread().start()

have a look at how classes work at:

http://docs.python.org/tutorial/classes.html

for additional information.

Hope this helps :)

Nick
  • 354
  • 2
  • 6
  • this code for some odd reason wont work, it justs crash with this error : import: unable to grab mouse `': Resource temporarily unavailable @ error/xwindow.c/XSelectWindow/9052. from: can't read /var/mail/time import: unable to grab mouse `': Resource temporarily unavailable @ error/xwindow.c/XSelectWindow/9052. ./fuzzy.py: line 6: syntax error near unexpected token `(' ./fuzzy.py: line 6: `class ConnectionThread ( threading.Thread ):' from: can't read /var/mail/time ./fuzzy.py: line 6: syntax error near unexpected token `(' ./fuzzy.py: line 6: `class ConnectionThread ( threading.Thread ):' – Ba7a7chy Jul 17 '12 at 09:15