Python Forum
Function / Arguments / Error Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function / Arguments / Error Help
#1
Hello! I'm new to python... so i did a TCP Packet Sniffer to have an idea on how python is on hacking since i wanna to do anti-TCP Packet sniffers etc.
So i got a python code for sniffing ;D great right?...right??

Anyway, i modified it so it can be like beautiful and nit for the user. So i decide to put arguments like:

--sniff-tcp - Sniff TCP packets / Hosts
--sniff-arp - Sniff ARP packets

So i found this module called ArgParse or argparse or whatever xD i imported it and i did the code so i made a function which will start TCP sniffing
if the user puts --sniff-tcp but everytime i do it i get an error which is haunting me cause this error i have saw it ON EACH OF EVERY SCRIPT I DID EVEN COPIED FROM INTERNET...

File "sniffer.py", line 25
try:
^

IndentslError: unindent does not match any outer indentation level

When i see this error i feel like getting a real python and burn it DOWN >:(

So here's my code:
import socket, sys, time, os
from struct import *
from termcolor import colored
from argparse import ArgumentParser

def helpArg():
	print "--sniff-arp"
	print "--sniff-tcp"

parser = ArgumentParser()

parser.add_argument("--sniff-tcp", action=tcpSniffer())

args = parser.parse_args()


#create an INET, STREAMing socket
def tcpSniffer():
	print colored("[+]", 'green') + (" Starting Sniffers...")
	time.sleep(2)
	print colored("[+]", 'green') + (" Creating socket...")
    try:
       s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
	except socket.error , msg:
   		print colored("[!]", 'red') + ('Socket could not be created. Error Code : ' + str(msg[0])) + (' Message ' + msg[1])
    	sys.exit()
    
	print colored("[+]", 'green'), ("Getting Ready Spoofers...")
	time.sleep(3)
 
	# receive a packet
	while True:
    	packet = s.recvfrom(65565)
     
    	#packet string from tuple
    	packet = packet[0]
     
    	#take first 20 characters for the ip header
    	ip_header = packet[0:20]
     
    	#now unpack them :)
    	iph = unpack('!BBHHHBBH4s4s' , ip_header)
     
    	version_ihl = iph[0]
    	version = version_ihl >> 4
    	ihl = version_ihl & 0xF
     
    	iph_length = ihl * 4
     
    	ttl = iph[5] 
    	protocol = iph[6]
    	s_addr = socket.inet_ntoa(iph[8]);
    	d_addr = socket.inet_ntoa(iph[9]);
    	hostname2solv = socket.gethostname()
    	host = socket.gethostbyname(hostname2solv)
     
    	print "############################# IP : " +str(s_addr) + " HOST : " +str(hostname2solv) + " #######################"
    	print 'Client Name : ' + str(hostname2solv)
        print 'Version : ' + str(version) 
        print 'IP Header Length : ' + str(ihl)
        print 'TTL : ' + str(ttl) 
        print 'Protocol : ' + str(protocol) 
        print 'Source Address : ' + str(s_addr) 
        print 'Destination Address : ' + str(d_addr)
     
        tcp_header = packet[iph_length:iph_length+20]
     
        #now unpack them :)
        tcph = unpack('!HHLLBBHHH' , tcp_header)
     
        source_port = tcph[0]
    	dest_port = tcph[1]
    	sequence = tcph[2]
    	acknowledgement = tcph[3]
    	doff_reserved = tcph[4]
    	tcph_length = doff_reserved >> 4
     
    	print 'Source Port : ' + str(source_port) 
    	print 'Dest Port : ' + str(dest_port) 
    	print 'Sequence Number : ' + str(sequence) 
		print 'Acknowledgement : ' + str(acknowledgement) 
    	print 'TCP header length : ' + str(tcph_length)
     
   		h_size = iph_length + tcph_length * 4
    	data_size = len(packet) - h_size
     
    	#get data from the packet
    	data = packet[h_size:]
     
    	print 'Data : ' + data
    	print "#######################################################################################"
   		print
Can anyone please help me with this error?
And if anyone could tell me how to do easy and working arguments in python that could be AWESOME!

Lots of love!
Reply
#2
your current code line#23 should be indented one more space
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
But when i use my notepad that support all programming and scripting languages it puts me that much spaces...
Reply
#4
(Jul-01-2018, 10:05 AM)Ghosty Wrote: And if anyone could tell me how to do easy and working arguments in python that could be AWESOME!
argparse (that's python3) should do. Here is the official tutorial
https://docs.python.org/3/howto/argparse.html

if you don't mind installing external packages, you can try Click
http://click.pocoo.org/5/

here is link to snippsat's tutorial
https://python-forum.io/Thread-Click-Click
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks so much but now i have

Error:
File "sniffer.py", line 23 time.sleep(2) ^ IndentationError: unexpected indent
:(
Reply
#6
(Jul-01-2018, 10:18 AM)Ghosty Wrote: But when i use my notepad that support all programming and scripting languages it puts me that much spaces...
Obviously you deleted one space by incident - don't you see it's unaligned with the rest code on the same level?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Jul-01-2018, 10:23 AM)Ghosty Wrote: Thanks so much but now i have

File "sniffer.py", line 23
time.sleep(2)
^
IndentationError: unexpected indent

:(
well, pay more attention and fix the indentation... You had to add one (1) space to the line, that is immediately after try:
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
The thing is i'm new to python xD i don't really know what i did wrong.. i tried re-importing but nah....

Python is hard for me in some way
Reply
#9
import socket, sys, time, os
from struct import *
from termcolor import colored
from argparse import ArgumentParser
 
def helpArg():
    print "--sniff-arp"
    print "--sniff-tcp"
 
parser = ArgumentParser()
 
parser.add_argument("--sniff-tcp", action=tcpSniffer())
 
args = parser.parse_args()
 
 
#create an INET, STREAMing socket
def tcpSniffer():
    print colored("[+]", 'green') + (" Starting Sniffers...")
    time.sleep(2)
    print colored("[+]", 'green') + (" Creating socket...")
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
    except socket.error , msg:
        print colored("[!]", 'red') + ('Socket could not be created. Error Code : ' + str(msg[0])) + (' Message ' + msg[1])
        sys.exit()
     
    print colored("[+]", 'green'), ("Getting Ready Spoofers...")
    time.sleep(3)
  
    # receive a packet
    while True:
        packet = s.recvfrom(65565)
      
        #packet string from tuple
        packet = packet[0]
      
        #take first 20 characters for the ip header
        ip_header = packet[0:20]
      
        #now unpack them :)
        iph = unpack('!BBHHHBBH4s4s' , ip_header)
      
        version_ihl = iph[0]
        version = version_ihl >> 4
        ihl = version_ihl & 0xF
      
        iph_length = ihl * 4
      
        ttl = iph[5] 
        protocol = iph[6]
        s_addr = socket.inet_ntoa(iph[8]);
        d_addr = socket.inet_ntoa(iph[9]);
        hostname2solv = socket.gethostname()
        host = socket.gethostbyname(hostname2solv)
      
        print "############################# IP : " +str(s_addr) + " HOST : " +str(hostname2solv) + " #######################"
        print 'Client Name : ' + str(hostname2solv)
        print 'Version : ' + str(version) 
        print 'IP Header Length : ' + str(ihl)
        print 'TTL : ' + str(ttl) 
        print 'Protocol : ' + str(protocol) 
        print 'Source Address : ' + str(s_addr) 
        print 'Destination Address : ' + str(d_addr)
      
        tcp_header = packet[iph_length:iph_length+20]
      
        #now unpack them :)
        tcph = unpack('!HHLLBBHHH' , tcp_header)
      
        source_port = tcph[0]
        dest_port = tcph[1]
        sequence = tcph[2]
        acknowledgement = tcph[3]
        doff_reserved = tcph[4]
        tcph_length = doff_reserved >> 4
      
        print 'Source Port : ' + str(source_port) 
        print 'Dest Port : ' + str(dest_port) 
        print 'Sequence Number : ' + str(sequence) 
        print 'Acknowledgement : ' + str(acknowledgement) 
        print 'TCP header length : ' + str(tcph_length)
      
        h_size = iph_length + tcph_length * 4
        data_size = len(packet) - h_size
      
        #get data from the packet
        data = packet[h_size:]
      
        print 'Data : ' + data
        print "#######################################################################################"
        print
This code is python2. If you are new to python, start with python3, not python2.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  calling external function with arguments Wimpy_Wellington 7 1,344 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,857 Oct-17-2022, 06:29 PM
Last Post: paulo79
  'namespace' shorthand for function arguments? shadowphile 5 2,541 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 2,111 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Possible to dynamically pass arguments to a function? grimm1111 2 2,124 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  How to pass multiple arguments into function Mekala 4 2,381 Jul-11-2020, 07:03 AM
Last Post: Mekala
  How to give a name to function arguments in C-API? WonszZeczny 0 1,313 Jun-22-2020, 10:20 AM
Last Post: WonszZeczny
  invalid keyword arguments error sagpal 3 2,337 Jun-04-2020, 10:24 PM
Last Post: bowlofred
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,206 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Pass Arguments to Function phillyfa 2 1,973 Mar-27-2020, 12:05 PM
Last Post: phillyfa

Forum Jump:

User Panel Messages

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