tgalal / yowsup

The WhatsApp lib
GNU General Public License v3.0
7.09k stars 2.23k forks source link

Can't receive Encrypt image #1400

Closed Dimelay closed 5 years ago

Dimelay commented 8 years ago

File "/home/steck/whatsappMy/yowsup/yowsup/layers/protocol_media/layer.py", line 41, in recvMessageStanza if mediaNode.getAttributeValue("type") == "image": AttributeError: 'NoneType' object has no attribute 'getAttributeValue'

DATA Decrypt body: yowsup/layers/axolotl/layer.py: def handleWhisperMessage(self, node): encMessageProtocolEntity = EncryptedMessageProtocolEntity.fromProtocolTreeNode(node) ``` whisperMessage = WhisperMessage(serialized=encMessageProtocolEntity.getEncData()) sessionCipher = self.getSessionCipher(encMessageProtocolEntity.getFrom(False)) plaintext = sessionCipher.decryptMsg(whisperMessage) if encMessageProtocolEntity.getVersion() == 2: plaintext = self.unpadV2Plaintext(plaintext) //PRINT DECRYPT BODY print plaintext // bodyNode = ProtocolTreeNode("body", data = plaintext) node.addChild(bodyNode) self.toUpper(node) ``` Link to my file https://mmi488.whatsapp.net/d/0QVa5QLVNRiH0HZ_gU-W5FbzX-4/AhbVJi2WS1Yia7EklnoFua1fwMezYiqzH4BQsv53_63Z.enc ┴�K\←c��� *�/R���Zc�T㡪Z│ܒM���O·�┘┌��±b�c�H�─�π─U��┌\└ǎ�K91b��←?:◆�;±�⎺��cM&RF���_�M��1���┤4?���d��K'E��̏�
Dimelay commented 8 years ago

from axolotl.kdf.hkdfv3 import HKDFv3 from axolotl.util.byteutil import ByteUtil in layer.py

payamazadi commented 8 years ago

sry, didn't see the full thing. got it, it's downloading for me now too!! very nice!!!

Dimelay commented 8 years ago

We did a good job =)

yniv commented 8 years ago

Sound good! can someone arrange a pull request with the full fix for image encryption?? thank you all for the effort

payamazadi commented 8 years ago

@jlguardi on your comments in #1387 you mentioned a more proper integration of this code into the Yowsup layers - can you clarify? also here is a cleaner version of the code with some of the node attributes corrected:

def handleWhisperMessage(self, node):
        encMessageProtocolEntity = EncryptedMessageProtocolEntity.fromProtocolTreeNode(node)

        whisperMessage = WhisperMessage(serialized=encMessageProtocolEntity.getEncData())
        sessionCipher = self.getSessionCipher(encMessageProtocolEntity.getFrom(False))
        plaintext = sessionCipher.decryptMsg(whisperMessage)

        if encMessageProtocolEntity.getVersion() == 2:
            plaintext = self.unpadV2Plaintext(plaintext)

        #get preview
        pos = plaintext.upper().find(binascii.unhexlify('ffd8ffe0'.upper()))
        preview = ""
        if pos > 0:
            preview = plaintext[pos:]
        else:
            preview = "lol"

        #decode schema from encrypted message
        media = encrypted_media_pb2.Media()
        media.ParseFromString(plaintext)
        self.decryptAndSaveImage(media)

        bodyNode = ProtocolTreeNode("media", data = preview)
        bodyNode.setAttribute("type", "image")
        bodyNode.setAttribute("file", "/tmp/my.jpg")
        bodyNode.setAttribute("size", media.length)
        bodyNode.setAttribute("width", media.width)
        bodyNode.setAttribute("height", media.height)
        bodyNode.setAttribute("encoding", "unknown")
        bodyNode.setAttribute("caption", media.caption)
        bodyNode.setAttribute("mimetype", media.mimetype)
        bodyNode.setAttribute("filehash", media.sha256)
        bodyNode.setAttribute("url", media.url)
        bodyNode.setAttribute("ip", "idk")
        node.setAttribute("preview", preview)

        node.addChild(bodyNode)

        self.toUpper(node)

    def decryptAndSaveImage(self, media):
        encimg = urllib2.urlopen(media.url).read()
        derivative = HKDFv3().deriveSecrets(media.refkey, binascii.unhexlify('576861747341707020496d616765204b657973'), 112)
        parts = ByteUtil.split(derivative, 16, 32)
        iv = parts[0]
        cipherKey = parts[1]
        e_img = encimg[:-10]
        AES.key_size=128
        cr_obj = AES.new(key=cipherKey,mode=AES.MODE_CBC,IV=iv)
        myimg = cr_obj.decrypt(e_img)
        f=open('/tmp/my.jpg','wb')
        f.write(bytearray(myimg))
        f.close()

turning in for the night!

jlguardi commented 8 years ago

@payamazadi I'm integrating in the layers. Thx for the work of last hours!

bdbais commented 8 years ago

somebody could to merge all patch in 1 thread :) I'm lost :)

jlguardi commented 8 years ago

@bdbais jlguardi/master for all but encrypted_media and we have a branch for tha propose (https://github.com/jlguardi/yowsup/tree/encrypted_media)

bdbais commented 8 years ago

cloned, builded, installed, very good, it works! :) thanks @jlguardi and @payamazadi !

thundergreen commented 8 years ago

@bdbais how it works? i can apparently send messages but wont get ack. in my client and can'T receive messages

yniv commented 8 years ago

@jlguardi the encrypted_media branch is already working? supporting encrypted media completely?

jlguardi commented 8 years ago

@yniv not yet! It's just a proof of concept. I need to adapt to layers but WIP.

thundergreen commented 8 years ago

I checked and pulled last master but it won't work :(

Am 25.03.2016 um 12:30 schrieb jlguardi:

@yniv https://github.com/yniv not yet! It's just a proof of concept. I need to adapt to layers but WIP.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/tgalal/yowsup/issues/1400#issuecomment-201245495

Dimelay commented 8 years ago

Who adapts the code in layers? I'm doing this, slowly

Dimelay commented 8 years ago

I adapted the code in layers. While I test. But it works! )

jlguardi commented 8 years ago

I GOT IT!!!!

Well, I've adapted images at least. Other media would be soon. It's not too clean code but I'll refactor slowly.

Check my branch jlguardi/encrypted_media

I have included a new method in all downloadable medias: getMediaContent which gets the file and decrypts it if necessary.

Just tested with images. Video crashes all other media, not tested!

jlguardi commented 8 years ago

Fixed Video!!!

Dimelay commented 8 years ago

@jodersus I've done a little differently but this also works well)

Dimelay commented 8 years ago

Thanks to all! branch can be closed) Patches are ready

jlguardi commented 8 years ago

Missing location and audio. I'll port them tonight. But I'll merge to master now.

thundergreen commented 8 years ago

Messages still not receiving .. are u also working on this?

Am 25.03.2016 um 16:49 schrieb jlguardi:

I GOT IT!!!!

Well, I've adapted images at least. Other media would be soon. It's not too clean code but I'll refactor slowly.

Check my branch jlguardi/encrypted_media

I have included a new method in all downloadable medias: getMediaContent which gets the file and decrypts it if necessary.

Just tested with images. Video crashes all other media, not tested!

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/tgalal/yowsup/issues/1400#issuecomment-201340288

jlguardi commented 8 years ago

@thundergreen Messages? Please, refer issue (if any) with logs.

thundergreen commented 8 years ago

I'm using transwhat with yowsup. I can send messages, get also ackn. messages but no hooks back in conversations. People can read my messages but can't reply to me. messages wont be received.

My debug of transwhat doesn't show that much.

Am 25.03.2016 um 18:20 schrieb jlguardi:

@thundergreen https://github.com/thundergreen Messages? Please, refer issue (if any) with logs.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/tgalal/yowsup/issues/1400#issuecomment-201375784

jlguardi commented 8 years ago

Open a new issue and post logs of yowsup-cli with your credentials in debug mode.

thundergreen commented 8 years ago

never used cli..... will try it. how to enable the debug mode?

Am 25.03.2016 um 18:36 schrieb jlguardi:

Open a new issue and post logs of yowsup-cli with your credentials in debug mode.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/tgalal/yowsup/issues/1400#issuecomment-201381040

jlguardi commented 8 years ago

yowsup-cli demos -y -d -l phonewithcc:password

thundergreen commented 8 years ago

Am 25.03.2016 um 18:52 schrieb jlguardi:

Auth: Logged in! DEBUG:yowsup.layers.logger.layer:rx: <message participant="17863821714às.whatsapp.net" t="1458846489" from="33674514813-1446536444àg.us" offline="3" type="text" id="33E891E4922A3C9F9A" notify="Gilles Hurtaud">

»‚Ëè ChåsüdI†ö±vJßÅýÛ9¡ùÞc±Ñ½áCJqç'ö)L<àé«hYqg ñª¨ïÍv†—Ó§’Œš´IðºØØwVÕð”ÞÑýV äÆe‘gáÀÌÑcӂ†V¼­qdm¿;Q ʳFVé"·êÆZ×jzÓZ懑ãYl-Çb¸&þéR;®=³Ü¾›sè4h*us ñÿ6mìFmTU³VÛö±¯¥éQj;d¿þL6(ôa–­5¢ÛÒêç+pü;ÔUä §û” HEX:3308bb82cbe801100c1ad001dbb9b1e0219df1f1c3b79569b94de4d5d092880278f670537cf49230dde445ca4142f4040e71671ca0f103aaa8efcd768697d3a7928c9a1510b41e49f0bad8d87756d5f094ded1fd560d438f68e51b737d644986f6b106764a05dfc5fddb39a123de63b1d1bde1434a715c277c0729104c023c40e9ab68590c7bc6659167e1c0ccd163d3828656bcad71641d6d1dbf9d3b510cf1ff366dec466d5455b356db7cb1afa55b516a3b6414bffe4c01362860610e96ad35a2dbd21eeae72b707d3bd455e40df1b9a4f020d9dbb7ff202649d77fb109db1cff97d5f4d8fb62b826fe5b523bae3db3dcbe9b73e834682a75730d5a0dcab34656e922b7eac65ad76a7ad35ae68791e307596c2dc77f0aa7fb9407

Traceback (most recent call last): File "/usr/local/bin/yowsup-cli", line 4, in èèimportèè('pkgèresources').runèscript('yowsup2==2.4.48', 'yowsup-cli') File "/usr/local/lib/python2.7/dist-packages/pkgèresources/èèinitèè.py", line 726, in runèscript self.require(requires)é0ê.runèscript(scriptèname, ns) File "/usr/local/lib/python2.7/dist-packages/pkgèresources/èèinitèè.py", line 1484, in runèscript exec(code, namespace, namespace) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/EGG-INFO/scripts/yowsup-cli", line 323, in if not parser.process(): File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/EGG-INFO/scripts/yowsup-cli", line 227, in process self.startCmdline() File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/EGG-INFO/scripts/yowsup-cli", line 255, in startCmdline stack.start() File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/demos/cli/stack.py", line 25, in start self.stack.loop(timeout = 0.5, discrete = 0.5) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/stacks/yowstack.py", line 187, in loop asyncore.loop(_args, *_kwargs) File "/usr/lib/python2.7/asyncore.py", line 216, in loop pollèfun(timeout, map) File "/usr/lib/python2.7/asyncore.py", line 156, in poll read(obj) File "/usr/lib/python2.7/asyncore.py", line 87, in read obj.handleèerror() File "/usr/lib/python2.7/asyncore.py", line 83, in read obj.handleèreadèevent() File "/usr/lib/python2.7/asyncore.py", line 449, in handleèreadèevent self.handleèread() File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/network/layer.py", line 86, in handleèread self.receive(data) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/network/layer.py", line 94, in receive self.toUpper(data) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/èèinitèè.py", line 59, in toUpper self.èèupper.receive(data) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/stanzaregulator/layer.py", line 28, in receive self.processReceived() File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/stanzaregulator/layer.py", line 48, in processReceived self.toUpper(oneMessageData) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/èèinitèè.py", line 59, in toUpper self.èèupper.receive(data) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/auth/layerècrypt.py", line 63, in receive self.toUpper(payload) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/èèinitèè.py", line 59, in toUpper self.èèupper.receive(data) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/coder/layer.py", line 35, in receive self.toUpper(node) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/èèinitèè.py", line 59, in toUpper self.èèupper.receive(data) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/logger/layer.py", line 14, in receive self.toUpper(data) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/èèinitèè.py", line 59, in toUpper self.èèupper.receive(data) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/axolotl/layer.py", line 118, in receive self.onMessage(protocolTreeNode) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/axolotl/layer.py", line 140, in onMessage self.handleEncMessage(protocolTreeNode) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/axolotl/layer.py", line 225, in handleEncMessage self.handleWhisperMessage(node) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/axolotl/layer.py", line 271, in handleWhisperMessage encMessageProtocolEntity = EncryptedMessageProtocolEntity.fromProtocolTreeNode(node) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/axolotl/protocolentities/messageèencrypted.py", line 53, in fromProtocolTreeNode encNode.data.encode('latin-1') if sys.versionèinfo >= (3,0) else encNode.data) File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.4.48-py2.7.egg/yowsup/layers/axolotl/protocolentities/messageèencrypted.py", line 24, in setEncProps assert encType in "pkmsg", "msg" AssertionError: msg

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/tgalal/yowsup/issues/1400#issuecomment-201385449

jlguardi commented 8 years ago

@thundergreen group encrypted messages aren't supported yet.

payamazadi commented 8 years ago

@jlguardi i've been having an issue w the branch around axolotl's layer, and the handlePreKeyWhisperMessage function.

can you do me a favor and run the echo client and tell me if you get errors?

when the layer sends the media message to handlePreKeyWhisperMessage function, with the code exactly how it is in the encrypted-media branch, and i run the echo client, i get infinite output, looks like this: ERROR:yowsup.layers.axolotl.layer:Unexpected end-group tag. ERROR:yowsup.layers.axolotl.layer:Unexpected end-group tag. ERROR:yowsup.layers.axolotl.layer:Unexpected end-group tag. ERROR:yowsup.layers.axolotl.layer:Truncated message. ERROR:yowsup.layers.axolotl.layer:Truncated message. ERROR:yowsup.layers.axolotl.layer:Truncated message. ERROR:yowsup.layers.axolotl.layer:Tag had invalid wire type. ERROR:yowsup.layers.axolotl.layer:Truncated message. ERROR:yowsup.layers.axolotl.layer:Unexpected end-group tag.

when i essentially replicate the actions of handlePreKeyWhisperMessage into handleWhisperMessage, things seem to work fine.

my workaround is this:

if node.getChild("enc")["v"] == "2" and node["from"] not in self.v2Jids:
                self.v2Jids.append(node["from"])

            if node.getChild("enc")["type"] == "pkmsg":
                self.handlePreKeyWhisperMessage(node)
            elif node.getChild("enc")["type"] == "msg" and node.getChild("enc")["mediatype"] == "image":
                self.handleWhisperMessage(node) #what should this be!?
            else:
                self.handleWhisperMessage(node)

..

def handleWhisperMessage(self, node):
        encMessageProtocolEntity = EncryptedMessageProtocolEntity.fromProtocolTreeNode(node)

        whisperMessage = WhisperMessage(serialized=encMessageProtocolEntity.getEncData())
        sessionCipher = self.getSessionCipher(encMessageProtocolEntity.getFrom(False))
        plaintext = sessionCipher.decryptMsg(whisperMessage)

        if encMessageProtocolEntity.getVersion() == 2:
            plaintext = self.unpadV2Plaintext(plaintext)

        if node.getAttributeValue("type") == 'text':
            bodyNode = ProtocolTreeNode("body", data = plaintext)
        if node.getAttributeValue("type") == 'media':
            # get preview
            pos = plaintext.upper().find(binascii.unhexlify('ffd8ffe0'.upper()))
            preview = plaintext[pos:] if pos > 0 else ""

            #decode schema from encrypted message
            media = encrypted_media_pb2.Media()
            media.ParseFromString(plaintext)

            #logger.debug("compare: " + str(plaintext[pos:] != media.thumbnail))
            bodyNode = ProtocolTreeNode("media", data = preview)
            bodyNode.setAttribute("type", node.getChild("enc").getAttributeValue("mediatype"))
            bodyNode.setAttribute("size", str(media.length))
            bodyNode.setAttribute("width", str(media.width))
            bodyNode.setAttribute("height", str(media.height))
            bodyNode.setAttribute("encoding", "unknown")
            bodyNode.setAttribute("caption", media.caption)
            bodyNode.setAttribute("mimetype", media.mimetype)
            bodyNode.setAttribute("filehash", base64.b64encode(media.sha256))
            bodyNode.setAttribute("url", media.url)
            bodyNode.setAttribute("refkey", base64.b64encode(media.refkey))
            bodyNode.setAttribute("file", "wat")

        node.addChild(bodyNode)
        self.toUpper(node)

by the way, there is also an error being thrown (somewhere, due to a logger.debug line) because the node's file attribute was not being set. i added that line in. you may want to commit that to the branch

jlguardi commented 8 years ago

@payamazadi Ups I read now your comment. Meet me at IRC (yowsup@chat.noip.me) if you prefer. I have just patched file token and reordered due to msg encrypted media wasn't managed.

payamazadi commented 8 years ago

@jlguardi thanks for that. #1406 i'm in the IRC which they posted as being on freenode. what do you use / how do i get on it?

payamazadi commented 8 years ago

btw, great work on this refactor, it works great now out of the box with the echo demo and the code is pretty clean. i can help refactor some of that too

pag16 commented 8 years ago

hey guys @payamazadi @jlguardi what is the branch to be used?

pag16 commented 8 years ago

hey guys can u please help ? which branch should be used?

Dimelay commented 8 years ago

Tomorrow I'll share my code. I do not use yowsup-cli. I use my cli. I write only for personal use, but I can share code.

Dimelay commented 8 years ago
[Online ](Brain) Чем занимаешься?)
[Online ](Brain) read Brain 
27-03-2016 13:40 <green>Brain</green> <- color ;)
<yellow>С Блонди прописи делаем,еще школа не началась а уже уроки делаем </yellow>
[Online ]Brain)^D
[Online ](-->)

[Online ](Brain) img <- Press TAB
12.png        IMG_0604.JPG  IMG_0646.JPG  scr1.jpg  <-- autocomplete for send img =))
[Online ](Brain)
[Online ]Brain) /list <- phonebook
7XXXXXXXXXX - Name_1
7XXXXXXXXXX -Name_2
7XXXXXXXXXX - Name_3
7XXXXXXXXXX - Name_4
7XXXXXXXXXX -Name_5
7XXXXXXXXXX -Name_6
....etc
[Online ](Brain) Name_4
[Online ](Name_4) 

[27-03-2016_18-28-45] Name_8 send(а) image //the image is stored in the directory specified in the settings

Screenshot )

It is very convenient to use with the phone via ssh

jlguardi commented 8 years ago

New version with media and group messages!!! There are too many issues and I don't know where to report.

My branch https://github.com/jlguardi/yowsup/ works with personal/group text/media. Sending isn't encrypted in groups but works with plain send.

Test it and enjoy it (if it works).

biji commented 8 years ago

@jlguardi which patch fix receiving encrypted image? thanks

jodersus commented 8 years ago

@jlguardi Great stuff! Many thanks for your great efforts. Which branch should we use? master or encrypted_media?

jlguardi commented 8 years ago

@jodersus master. See my wiki for some details.

bartoszhernas commented 8 years ago

@jlguardi I am using your master currently. Could you tell me how to decrypt downloaded media files?

jlguardi commented 8 years ago

@bartoszhernas message.getMediaContent() returns unencrypted media but just images work for now, WIP.

bartoszhernas commented 8 years ago

@jlguardi You are a life saver. Thanks a lot. Will check it out

oip976 commented 8 years ago

Receiving of decrypted images is no more possible. All receveid images are still encrypted, implemented your method message.getMediaContent(). Are there any open issues?

brunodeandrade commented 7 years ago

Are there any open issues? All receveid images are still encrypted in most recent branch...

apevzner-dp commented 7 years ago

Hello guys,

So is there any piece of news? Is it possible now to open the decrypted images?

Natureshadow commented 7 years ago

So, what needs to be done to get all of this trogether?

tgalal commented 5 years ago

v3.2.0