curl.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/python
  2. # -*- coding: iso-8859-15 -*-
  3. """
  4. This file is part of the cintruder project, http://cintruder.03c8.net
  5. Copyright (c) 2012/2016 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. try:
  19. from cStringIO import StringIO
  20. except ImportError:
  21. from StringIO import StringIO
  22. class CIntruderCurl(object):
  23. """
  24. Class to control curl on behalf of the application.
  25. """
  26. agent = 'Googlebot/2.1 (+http://www.google.com/bot.html)'
  27. referer = '127.0.0.1'
  28. proxy = None
  29. ignoreproxy = None
  30. def __init__(self, captcha="", ignoreproxy="", proxy=""):
  31. """
  32. Class init
  33. """
  34. self.handle = pycurl.Curl()
  35. self.verbosity = 0
  36. self.url = self.set_url(captcha)
  37. self.captcha = StringIO()
  38. self.proxy = self.set_proxy(ignoreproxy, proxy)
  39. self.set_option(pycurl.SSL_VERIFYHOST, 0)
  40. self.set_option(pycurl.SSL_VERIFYPEER, 0)
  41. #self.set_option(pycurl.SSLVERSION, pycurl.SSLVERSION_SSLv3)
  42. self.set_option(pycurl.COOKIEFILE, '/dev/null')
  43. self.set_option(pycurl.COOKIEJAR, '/dev/null')
  44. self.set_option(pycurl.NETRC, 1)
  45. def set_url(self, url):
  46. """
  47. Set the url.
  48. """
  49. self.url = url
  50. self.set_option(pycurl.URL, self.url)
  51. return url
  52. def set_agent(self, agent):
  53. """
  54. Set the user agent.
  55. """
  56. self.agent = agent
  57. self.set_option(pycurl.USERAGENT, self.agent)
  58. return agent
  59. def set_referer(self, referer):
  60. """
  61. Set the referer.
  62. """
  63. self.referer = referer
  64. self.set_option(pycurl.REFERER, self.referer)
  65. return referer
  66. def set_proxy(self, ignoreproxy, proxy):
  67. """
  68. Set the proxy to use.
  69. """
  70. self.proxy = proxy
  71. self.ignoreproxy = ignoreproxy
  72. if self.ignoreproxy == 1:
  73. self.set_option(pycurl.PROXY, "")
  74. else:
  75. self.set_option(pycurl.PROXY, self.proxy)
  76. return proxy
  77. def set_verbosity(self, level):
  78. """
  79. Set the verbosity level.
  80. """
  81. self.set_option(pycurl.VERBOSE, level)
  82. def set_option(self, *args):
  83. """
  84. Set the given option.
  85. """
  86. apply(self.handle.setopt, args)
  87. def request(self):
  88. """
  89. Perform a request and returns the payload.
  90. """
  91. if self.agent:
  92. self.set_option(pycurl.USERAGENT, self.agent)
  93. if self.referer:
  94. self.set_option(pycurl.REFERER, self.referer)
  95. if self.proxy:
  96. self.set_option(pycurl.PROXY, self.proxy)
  97. if self.ignoreproxy:
  98. self.set_option(pycurl.PROXY, "")
  99. if self.url:
  100. self.set_option(pycurl.URL, self.url)
  101. self.set_option(pycurl.SSL_VERIFYHOST, 0)
  102. self.set_option(pycurl.SSL_VERIFYPEER, 0)
  103. self.handle.setopt(self.handle.WRITEFUNCTION, self.captcha.write)
  104. try:
  105. self.handle.perform()
  106. print "[Info] Getting captcha...\n"
  107. return self.captcha
  108. except pycurl.error, error:
  109. errno, errstr = error
  110. print '\n[Error] Connection error!:', errstr, "\n"
  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:", self.agent
  124. print "[-]HTTP Referer:", self.referer
  125. if self.ignoreproxy:
  126. print "[-]Proxy:", "No proxy!"
  127. else:
  128. print "[-]Proxy:", self.proxy
  129. print "[-]URL:", self.url, "\n"