Dietzi

Untitled

Aug 11th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 11.01 KB | None | 0 0
  1. diff --git a/yowsup/layers/axolotl/layer_send.py b/yowsup/layers/axolotl/layer_send.py
  2. index bbeacd8..74fae49 100644
  3. --- a/yowsup/layers/axolotl/layer_send.py
  4. +++ b/yowsup/layers/axolotl/layer_send.py
  5. @@ -9,6 +9,8 @@ from axolotl.axolotladdress import AxolotlAddress
  6.  from axolotl.protocol.whispermessage import WhisperMessage
  7.  from axolotl.groups.senderkeyname import SenderKeyName
  8.  from axolotl.groups.groupsessionbuilder import GroupSessionBuilder
  9. +from yowsup.structs import ProtocolTreeNode
  10. +from yowsup.layers.protocol_iq.protocolentities import IqProtocolEntity
  11.  
  12.  import logging
  13.  from random import randint
  14. @@ -45,18 +47,19 @@ class AxolotlSendLayer(AxolotlBaseLayer):
  15.  
  16.      def send(self, node):
  17.          if node.tag == "message" and node["to"] not in self.skipEncJids and not node.getChild("enc") and (not node.getChild("media") or node.getChild("media")["mediakey"]):
  18. -            self.processPlaintextNodeAndSend(node)
  19. -        # elif node.tag == "iq" and node["xmlns"] == "w:m":
  20. -        #     mediaNode = node.getChild("media")
  21. -        #     if mediaNode and mediaNode["type"] == "image":
  22. -        #         iqNode = IqProtocolEntity.fromProtocolTreeNode(node).toProtocolTreeNode()
  23. -        #         iqNode.addChild(ProtocolTreeNode(
  24. -        #             "encr_media", {
  25. -        #                 "type": mediaNode["type"],
  26. -        #                 "hash": mediaNode["hash"]
  27. -        #             }
  28. -        #         ))
  29. -        #         self.toLower(iqNode)
  30. +        #if node.tag == "message" and node["to"] not in self.skipEncJids and not node.getChild("enc"):
  31. +         self.processPlaintextNodeAndSend(node)
  32. +        #elif node.tag == "iq" and node["xmlns"] == "w:m":
  33. +        #    mediaNode = node.getChild("media")
  34. +        #    if mediaNode and mediaNode["type"] == "image":
  35. +        #        iqNode = IqProtocolEntity.fromProtocolTreeNode(node).toProtocolTreeNode()
  36. +        #        iqNode.addChild(ProtocolTreeNode(
  37. +        #            "encr_media", {
  38. +        #                "type": mediaNode["type"],
  39. +        #                "hash": mediaNode["hash"]
  40. +        #            }
  41. +        #        ))
  42. +        #        self.toLower(iqNode)
  43.          else:
  44.              self.toLower(node)
  45.  
  46. diff --git a/yowsup/layers/protocol_media/mediauploader.py b/yowsup/layers/protocol_media/mediauploader.py
  47. index da9b3d1..6750b31 100644
  48. --- a/yowsup/layers/protocol_media/mediauploader.py
  49. +++ b/yowsup/layers/protocol_media/mediauploader.py
  50. @@ -5,6 +5,22 @@ from time import sleep
  51.  import threading
  52.  import logging
  53.  from yowsup.common.tools import MimeTools
  54. +from axolotl.kdf.hkdfv3 import HKDFv3
  55. +import binascii
  56. +from axolotl.util.byteutil import ByteUtil
  57. +import Crypto.Cipher.AES as AES
  58. +from axolotl.ecc.curve import Curve
  59. +from axolotl.sessionbuilder import SessionBuilder
  60. +from axolotl.state.sessionstate import SessionState
  61. +from axolotl.protocol.whispermessage import WhisperMessage
  62. +from axolotl.protocol.prekeywhispermessage import PreKeyWhisperMessage
  63. +from axolotl.nosessionexception import NoSessionException
  64. +from axolotl.invalidmessageexception import InvalidMessageException
  65. +from axolotl.duplicatemessagexception import DuplicateMessageException
  66. +import hashlib
  67. +import base64
  68. +import hmac
  69. +from yowsup.common.tools import WATools
  70.  
  71.  logger = logging.getLogger(__name__)
  72.  
  73. @@ -29,6 +45,20 @@ class MediaUploader(WARequest, threading.Thread):
  74.  
  75.          self.sock = socket.socket()
  76.  
  77. +    def pad(self, s):
  78. +        return s + ((16-len(s) % 16) * '{')
  79. +
  80. +    def encryptImg(self, img, refkey):
  81. +        #refkey = binascii.hexlify(os.urandom(112))
  82. +        img = self.pad(img)
  83. +        derivative = HKDFv3().deriveSecrets(binascii.unhexlify(refkey), binascii.unhexlify("576861747341707020496d616765204b657973"), 112)
  84. +        parts = ByteUtil.split(derivative, 16, 32)
  85. +        iv = parts[0]
  86. +        cipherKey = parts[1]
  87. +        cipher = AES.new(key=cipherKey, mode=AES.MODE_CBC, IV=iv)
  88. +        imgEnc=cipher.encrypt(img)
  89. +        return imgEnc[:-6]
  90. +
  91.      def start(self):
  92.          if self.async:
  93.              threading.Thread.__init__(self)
  94. @@ -46,9 +76,25 @@ class MediaUploader(WARequest, threading.Thread):
  95.  
  96.  
  97.          try:
  98. +            #filename = os.path.basename(sourcePath)
  99. +            #filetype = MimeTools.getMIME(filename)
  100. +            #filesize = os.path.getsize(sourcePath)
  101.              filename = os.path.basename(sourcePath)
  102.              filetype = MimeTools.getMIME(filename)
  103. -            filesize = os.path.getsize(sourcePath)
  104. +            f = open(sourcePath, 'rb')
  105. +            stream = f.read()
  106. +            f.close()
  107. +            refkey = binascii.hexlify(os.urandom(112))
  108. +            stream=self.encryptImg(stream,refkey)
  109. +            fenc = open(filename + ".enc", 'wb')  
  110. +            fenc.write(stream)
  111. +            fenc.seek(0, 2)
  112. +            filesize=fenc.tell()
  113. +            fenc.close()
  114. +
  115. +            sha1 = hashlib.sha256()
  116. +            sha1.update(stream)
  117. +            b64Hash = base64.b64encode(sha1.digest())
  118.  
  119.              self.sock.connect((self.url, self.port))
  120.              ssl_sock = ssl.wrap_socket(self.sock)
  121. @@ -60,23 +106,34 @@ class MediaUploader(WARequest, threading.Thread):
  122.              boundary = "zzXXzzYYzzXXzzQQ"#"-------" + m.hexdigest() #"zzXXzzYYzzXXzzQQ"
  123.              contentLength = 0
  124.  
  125. +            digTo = hmac.new("".encode("utf-8"), self.jid.replace("@s.whatsapp.net","@c.us").encode("utf-8"), hashlib.sha256).digest()[:20]
  126. +            refTo = base64.b64encode(digTo).decode()
  127. +            digFrom = hmac.new("".encode("utf-8"), self.accountJid.replace("@s.whatsapp.net","@c.us").encode("utf-8"), hashlib.sha256).digest()[:20]
  128. +            refFrom = base64.b64encode(digFrom).decode()
  129.              hBAOS = "--" + boundary + "\r\n"
  130. -            hBAOS += "Content-Disposition: form-data; name=\"to\"\r\n\r\n"
  131. -            hBAOS += self.jid + "\r\n"
  132. +            #hBAOS += "Content-Disposition: form-data; name=\"to\"\r\n\r\n"
  133. +            #hBAOS += self.jid + "\r\n"
  134. +            #hBAOS += "--" + boundary + "\r\n"
  135. +            #hBAOS += "Content-Disposition: form-data; name=\"from\"\r\n\r\n"
  136. +            #hBAOS += self.accountJid.replace("@whatsapp.net","") + "\r\n"
  137. +            #hBAOS += "--" + boundary + "\r\n"
  138. +            hBAOS += "Content-Disposition: form-data; name=\"hash\"\r\n\r\n"
  139. +            hBAOS += WATools.getFileHashForUpload(filename + ".enc") + "\r\n"
  140.              hBAOS += "--" + boundary + "\r\n"
  141. -            hBAOS += "Content-Disposition: form-data; name=\"from\"\r\n\r\n"
  142. -            hBAOS += self.accountJid.replace("@whatsapp.net","") + "\r\n"
  143. +            hBAOS += "Content-Disposition: form-data; name=\"refs\"\r\n\r\n"
  144. +            hBAOS += refFrom + "\r\n"
  145. +            hBAOS += refTo + "\r\n"
  146.  
  147.              hBAOS += "--" + boundary + "\r\n"
  148.              hBAOS += "Content-Disposition: form-data; name=\"file\"; filename=\"" + crypto + "\"\r\n"
  149. -            hBAOS  += "Content-Type: " + filetype + "\r\n\r\n"
  150. +            hBAOS += "Content-Type: application/octet-stream\r\n\r\n"
  151.  
  152.              fBAOS = "\r\n--" + boundary + "--\r\n"
  153.  
  154.              contentLength += len(hBAOS)
  155.              contentLength += len(fBAOS)
  156.              contentLength += filesize
  157. -
  158. +            
  159.              POST = "POST %s\r\n" % uploadUrl
  160.              POST += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n"
  161.              POST += "Host: %s\r\n" % self.url
  162. @@ -88,12 +145,12 @@ class MediaUploader(WARequest, threading.Thread):
  163.  
  164.              totalsent = 0
  165.              buf = 1024
  166. -            f = open(sourcePath, 'rb')
  167. -            stream = f.read()
  168. -            f.close()
  169. +            #f = open(sourcePath, 'rb')
  170. +            #stream = f.read()
  171. +            #stream = self.encryptImg(stream1)
  172. +            #f.close()
  173.              status = 0
  174.              lastEmit = 0
  175. -
  176.              while totalsent < int(filesize):
  177.                  ssl_sock.write(stream[:buf])
  178.                  status = totalsent * 100 / filesize
  179. @@ -125,6 +182,9 @@ class MediaUploader(WARequest, threading.Thread):
  180.              result = None
  181.  
  182.              for l in lines:
  183. +                logger.warning("################################")
  184. +                logger.warning(l)
  185. +                logger.warning("################################")
  186.                  if l.startswith("{"):
  187.                      result = self.parser.parse(l, self.pvars)
  188.                      break
  189. diff --git a/yowsup/layers/protocol_media/protocolentities/iq_requestupload.py b/yowsup/layers/protocol_media/protocolentities/iq_requestupload.py
  190. index bdd1e69..7a2a1a8 100644
  191. --- a/yowsup/layers/protocol_media/protocolentities/iq_requestupload.py
  192. +++ b/yowsup/layers/protocol_media/protocolentities/iq_requestupload.py
  193. @@ -62,7 +62,7 @@ class RequestUploadIqProtocolEntity(IqProtocolEntity):
  194.          }
  195.          if self.origHash:
  196.              attribs["orighash"] = self.origHash
  197. -        mediaNode = ProtocolTreeNode("media", attribs)
  198. +        mediaNode = ProtocolTreeNode("encr_media", attribs)
  199.          node.addChild(mediaNode)
  200.          return node
  201.  
  202. @@ -71,7 +71,7 @@ class RequestUploadIqProtocolEntity(IqProtocolEntity):
  203.          assert node.getAttributeValue("type") == "set", "Expected set as iq type in request upload, got %s" % node.getAttributeValue("type")
  204.          entity = IqProtocolEntity.fromProtocolTreeNode(node)
  205.          entity.__class__ = RequestUploadIqProtocolEntity
  206. -        mediaNode = node.getChild("media")
  207. +        mediaNode = node.getChild("encr_media")
  208.          entity.setRequestArguments(
  209.              mediaNode.getAttributeValue("type"),
  210.              mediaNode.getAttributeValue("hash"),
  211. diff --git a/yowsup/layers/protocol_media/protocolentities/iq_requestupload_result.py b/yowsup/layers/protocol_media/protocolentities/iq_requestupload_result.py
  212. index 45e7eb8..982249f 100644
  213. --- a/yowsup/layers/protocol_media/protocolentities/iq_requestupload_result.py
  214. +++ b/yowsup/layers/protocol_media/protocolentities/iq_requestupload_result.py
  215. @@ -35,7 +35,7 @@ class ResultRequestUploadIqProtocolEntity(ResultIqProtocolEntity):
  216.          node = super(ResultRequestUploadIqProtocolEntity, self).toProtocolTreeNode()
  217.  
  218.          if not self.isDuplicate():
  219. -            mediaNode = ProtocolTreeNode("media", {"url": self.url})
  220. +            mediaNode = ProtocolTreeNode("encr_media", {"url": self.url})
  221.              if self.ip:
  222.                  mediaNode["ip"] = self.ip
  223.  
  224. @@ -52,8 +52,11 @@ class ResultRequestUploadIqProtocolEntity(ResultIqProtocolEntity):
  225.          entity= ResultIqProtocolEntity.fromProtocolTreeNode(node)
  226.          entity.__class__ = ResultRequestUploadIqProtocolEntity
  227.          mediaNode = node.getChild("media")
  228. +        encrMediaNode = node.getChild("encr_media")
  229.          if mediaNode:
  230.              entity.setUploadProps(mediaNode["url"], mediaNode["ip"], mediaNode["resume"])
  231. +        elif encrMediaNode:
  232. +            entity.setUploadProps(encrMediaNode["url"], encrMediaNode["ip"], encrMediaNode["resume"])
  233.          else:
  234.              duplicateNode = node.getChild("duplicate")
  235.              if duplicateNode:
Advertisement
Add Comment
Please, Sign In to add comment