0

I was making a site component scanner with Python. Unfortunately, something goes wrong when I added another value to my script. This is my script:

#!/usr/bin/python


import sys
import urllib2
import re
import time
import httplib
import random

# Color Console
W  = '\033[0m'  # white (default)
R  = '\033[31m' # red
G  = '\033[1;32m' # green bold
O  = '\033[33m' # orange
B  = '\033[34m' # blue
P  = '\033[35m' # purple
C  = '\033[36m' # cyan
GR = '\033[37m' # gray

#Bad HTTP Responses 
BAD_RESP = [400,401,404]

def main(path):
print "[+] Testing:",host.split("/",1)[1]+path
try:
    h = httplib.HTTP(host.split("/",1)[0])
    h.putrequest("HEAD", "/"+host.split("/",1)[1]+path)
    h.putheader("Host", host.split("/",1)[0])
    h.endheaders()
    resp, reason, headers = h.getreply()
    return resp, reason, headers.get("Server")
except(), msg: 
    print "Error Occurred:",msg
    pass

def timer():
    now = time.localtime(time.time())
    return time.asctime(now)

def slowprint(s):
    for c in s + '\n':
        sys.stdout.write(c)
        sys.stdout.flush() # defeat buffering
        time.sleep(8./90)

print G+"\n\t                 Whats My Site Component Scanner"

coms = { "index.php?option=com_artforms" : "com_artforms" + "link1","index.php?option=com_fabrik" : "com_fabrik" + "ink"}

if len(sys.argv) != 2:
    print "\nUsage: python jx.py <site>"
    print "Example: python jx.py www.site.com/\n"
    sys.exit(1)

host = sys.argv[1].replace("http://","").rsplit("/",1)[0]
if host[-1] != "/":
    host = host+"/"

print "\n[+] Site:",host
print "[+] Loaded:",len(coms) 

print "\n[+] Scanning Components\n"
for com,nme,expl in coms.items():
    resp,reason,server = main(com)
    if resp not in BAD_RESP:
        print ""
        print G+"\t[+] Result:",resp, reason
        print G+"\t[+] Com:",nme
        print G+"\t[+] Link:",expl
        print W
    else:
        print ""
        print R+"\t[-] Result:",resp, reason
        print W
print "\n[-] Done\n"

And this is the error message that comes up:

      Traceback (most recent call last):
        File "jscan.py", line 69, in <module>
        for com,nme,expl in xpls.items():
      ValueError: need more than 2 values to unpack

I already tried changing the 2 value into 3 or 1, but it doesn't seem to work.

1
  • 1
    As an aside, check out the blessings module Commented Feb 27, 2014 at 12:14

4 Answers 4

2

xpls.items returns a tuple of two items, you're trying to unpack it into three. You initialize the dict yourself with two pairs of key:value:

coms = { "index.php?option=com_artforms" : "com_artforms" + "link1","index.php?option=com_fabrik" : "com_fabrik" + "ink"}

besides, the traceback seems to be from another script - the dict is called xpls there, and coms in the code you posted...

Sign up to request clarification or add additional context in comments.

1 Comment

sorry my bad. I've change it to coms
1

you can try

for (xpl, poc) in xpls.items():
    ...
    ...

because dict.items will return you tuple with 2 values.

Comments

0

You have all the information you need. As with any bug, the best place to start is the traceback. Let's:

  for com,poc,expl in xpls.items():
ValueError: need more than 2 values to unpack

Python throws ValueError when a given object is of correct type but has an incorrect value. In this case, this tells us that xpls.items is an iterable an thus can be unpacked, but the attempt failed.

The description of the exception narrows down the problem: xpls has 2 items, but more were required. By looking at the quoted line, we can see that "more" is 3.

In short: xpls was supposed to have 3 items, but has 2.

Note that I never read the rest of the code. Debugging this was possible using only those 2 lines.

Learning to read tracebacks is vital. When you encounter an error such as this one again, devote at least 10 minutes to try to work with this information. You'll be repayed tenfold for your effort.

Comments

0

As already mentioned, dict.items() returns a tuple with two values. If you use a list of strings as dictionary values instead of a string, which should be split anyways afterwards, you can go with this syntax:

coms = { "index.php?option=com_artforms" : ["com_artforms", "link1"],
         "index.php?option=com_fabrik" : ["com_fabrik", "ink"]}

for com, (name, expl) in coms.items(): 
    print com, name, expl

>>> index.php?option=com_artforms com_artforms link1
>>> index.php?option=com_fabrik com_fabrik ink

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.