Dietzi

Untitled

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