curl.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/python3
  2. # -*- coding: iso-8859-15 -*-
  3. """
  4. This file is part of the cintruder project, https://cintruder.03c8.net
  5. Copyright (c) 2012/2020 psy <epsylon@riseup.net>
  6. cintruder is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation version 3 of the License.
  9. cintruder is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  12. details.
  13. You should have received a copy of the GNU General Public License along
  14. with cintruder; if not, write to the Free Software Foundation, Inc., 51
  15. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. """
  17. import pycurl
  18. from io import BytesIO
  19. class CIntruderCurl(object):
  20. """
  21. Class to control curl on behalf of the application.
  22. """
  23. agent = 'Googlebot/2.1 (+http://www.google.com/bot.html)'
  24. referer = '127.0.0.1'
  25. proxy = None
  26. ignoreproxy = None
  27. def __init__(self, captcha="", ignoreproxy="", proxy=""):
  28. """
  29. Class init
  30. """
  31. self.handle = pycurl.Curl()
  32. self.verbosity = 0
  33. self.url = self.set_url(captcha)
  34. self.captcha = BytesIO()
  35. self.proxy = self.set_proxy(ignoreproxy, proxy)
  36. self.set_option(pycurl.SSL_VERIFYHOST, 0)
  37. self.set_option(pycurl.SSL_VERIFYPEER, 0)
  38. #self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_SSLv3)
  39. self.set_option(pycurl.COOKIEFILE, '/dev/null')
  40. self.set_option(pycurl.COOKIEJAR, '/dev/null')
  41. self.set_option(pycurl.NETRC, 1)
  42. def set_url(self, url):
  43. """
  44. Set the url.
  45. """
  46. self.url = url
  47. self.set_option(pycurl.URL, self.url)
  48. return url
  49. def set_agent(self, agent):
  50. """
  51. Set the user agent.
  52. """
  53. self.agent = agent
  54. self.set_option(pycurl.USERAGENT, self.agent)
  55. return agent
  56. def set_referer(self, referer):
  57. """
  58. Set the referer.
  59. """
  60. self.referer = referer
  61. self.set_option(pycurl.REFERER, self.referer)
  62. return referer
  63. def set_proxy(self, ignoreproxy, proxy):
  64. """
  65. Set the proxy to use.
  66. """
  67. self.proxy = proxy
  68. self.ignoreproxy = ignoreproxy
  69. if self.ignoreproxy == 1:
  70. self.set_option(pycurl.PROXY, "")
  71. else:
  72. self.set_option(pycurl.PROXY, self.proxy)
  73. return proxy
  74. def set_verbosity(self, level):
  75. """
  76. Set the verbosity level.
  77. """
  78. self.set_option(pycurl.VERBOSE, level)
  79. def set_option(self, *args):
  80. """
  81. Set the given option.
  82. """
  83. self.handle.setopt(*args)
  84. def request(self):
  85. """
  86. Perform a request and returns the payload.
  87. """
  88. if self.agent:
  89. self.set_option(pycurl.USERAGENT, self.agent)
  90. if self.referer:
  91. self.set_option(pycurl.REFERER, self.referer)
  92. if self.proxy:
  93. self.set_option(pycurl.PROXY, self.proxy)
  94. if self.ignoreproxy:
  95. self.set_option(pycurl.PROXY, "")
  96. if self.url:
  97. self.set_option(pycurl.URL, self.url)
  98. self.set_option(pycurl.SSL_VERIFYHOST, 0)
  99. self.set_option(pycurl.SSL_VERIFYPEER, 0)
  100. self.handle.setopt(self.handle.WRITEFUNCTION, self.captcha.write)
  101. try:
  102. self.handle.perform()
  103. print("\n[Info] Getting captcha...")
  104. return self.captcha
  105. except pycurl.error as error:
  106. try:
  107. errno, errstr = error
  108. except:
  109. errno, errstr = "unknown!", "unknown!"
  110. print('\n[Error] Connection error... '+ str(errstr))
  111. return "exit"
  112. def close(self):
  113. """
  114. Close the curl handle.
  115. """
  116. self.handle.close()
  117. self.captcha.close()
  118. def print_options(self):
  119. """
  120. Print selected options.
  121. """
  122. print("\n[-] Verbose: active")
  123. print("[-] HTTP User Agent: "+str(self.agent))
  124. print("[-] HTTP Referer: "+ str(self.referer))
  125. if self.ignoreproxy:
  126. print("[-] Proxy: No proxy!")
  127. else:
  128. print("[-] Proxy: "+ str(self.proxy))
  129. print("[-] URL: "+ str(self.url)+ "\n")