yowsup icon indicating copy to clipboard operation
yowsup copied to clipboard

Send media - Request upload for file [email protected] failed [BOUNTY FOR SOLUTION]

Open sealabr opened this issue 9 years ago • 287 comments

Hello I'm trying to upload media by yowsup, using RequestUploadIqProtocolEntity but it's not possible because lambda function allways direct to onRequestUploadError, and cant send file ...

Request upload for file /tmp/jpeg.jpg for [email protected] failed

I also tried with yowsupCLI using image send

/image send 5524XXXXXX /tmp/3DCDA9A9436B01F9A3.jfif

ERROR:yowsup.demos.cli.layer:Request upload for file /tmp/3DCDA9A9436B01F9A3.jfif for [email protected] failed
DEBUG:yowsup.layers.logger.layer:rx:
<iq type="error" from="s.whatsapp.net" id="1">
<error text="feature-not-implemented" code="501">
</error>

sealabr avatar Jul 12 '17 20:07 sealabr

I too have same problem.

pabloruan0710 avatar Jul 12 '17 20:07 pabloruan0710

I have the same problem, looks like a problem where whatsapp changed their method..

visiks avatar Jul 13 '17 10:07 visiks

Just solved part of the issue:

The tag is no longer supported. Now you must send encrypted images with <encr_media>.

If you make the needed changes in iq_requestupload.py and iq_requestupload_result.py (replace "media" with "encr_media", you can get to the upload step.

I could not, however, get the upload to actually work. Server always gives 400 error.

Maybe the server checks if the image is actually encrypted?

Gotta check some of the forks which can actually decrypt the image to see if we can figure out a procedure for encryption.

I'm trying to get the correct POST format from the decompiled java source, with no success.

If anyone has any idea how to proceed, I'd be immensely grateful.

etinin avatar Jul 13 '17 17:07 etinin

Glad to hear that someone is trying to do something, now I know that is needed to send encryped media, instead of plain text media, as lguardi commented on

#2123

Basically, you need to implement the inverse of the send procedure: 
- Generate a random key for encryption 
- Encrypt media and upload to the server 
- send encrypted url to recipient and the encryption key in a protobuf message

I'm looking forward to understand more about encrypted protocol in yowsup, to actually do something about send encrypted media protocol, I will be glad to any help given.

sealabr avatar Jul 13 '17 17:07 sealabr

@etinin , i follow you to change "media" to "encr_media", got the upload URL, but then got this post & response:

POST: https://mmg.whatsapp.net/u/f/FIcI1bQcJGbRXaGee0_cSllrJNoABVRrIxaEqA/AkObQlFqUS5DA21KPJHyYOnL47WKzVMSGN8Jf0SaiBE8 Content-Type: multipart/form-data; boundary=zzXXzzYYzzXXzzQQ Host: mmg.whatsapp.net User-Agent: WhatsApp/2.16.11 S40Version/14.26 Device/Nokia-302 Content-Length: 4880

HEADER & FOOTER: --zzXXzzYYzzXXzzQQ Content-Disposition: form-data; name="from"

[email protected] --zzXXzzYYzzXXzzQQ Content-Disposition: form-data; name="file"; filename="d6d02600f4f65c6733303c90a821800e.jpg" Content-Type: image/jpeg

RESPONSE: HTTP/1.1 400 Bad Request Content-Type: application/json Date: Sat, 15 Jul 2017 17:10:05 GMT Connection: close Content-Length: 0

It seems enc image is send in the protobuf message, like when extracted image from receiving image. I think @jlguardi , can help give us some more clues.

bahtiarp avatar Jul 16 '17 00:07 bahtiarp

I'm currently short of the required time, but if someone could take a look at jiguardi's fork, this might be solved. As I understand it, his fork has the decryption method properly implemented.

https://github.com/jlguardi/yowsup/tree/master/yowsup

In a parallel note, maybe we could try uploading a pre-encrypted image file? As I understand, the server should probably accept it, since it doesn't really have the key. But, again, I haven't studied the proper code.

It seems you just have to send yourself an encrypted image and check the specifics of the AES encryption being employed.

I've tried intercepting the whatsapp app requests to solve this issue but it's harder than I believed. The APP both on iPhone and Android seems to ignore proxy settings entirely when it can connect straight to the internet. And even some requests that get through the proxy are closed by Whatsapp when the app sees the selfsigned SSL certificate (even though the cert is properly added to the system.

Anyone has knowledge any reliable procedure for intercepting requests? Med school is keeping me way too busy to keep working blindly.

etinin avatar Jul 16 '17 20:07 etinin

I made some modifications, to send back received image with enc, But recipient only got this message in WA instead of the image: Waiting for this message. This may take a while. Learn More

This my debug log:

[message to="[email protected]" type="media" id="1500356450-2"] [enc type="msg" mediatype="image" v="2"] 3! �ƕ�u����[ -�J�,��q���z�/�6wE"�� �MQ�H�x�Y�6��� .....BLABLA HEX:330a2105845ae2d89594e ...BLABLA [/enc] [/message]

2017-07-18 12:40:51,414 [MainThread ] [DEBUG] rx: [ack t="1500356451" from="[email protected]" class="message" id="1500356450-2"] [/ack]

2017-07-18 12:40:52,778 [MainThread ] [DEBUG] rx: [receipt type="retry" from="[email protected]" id="1500356450-2" t="1500356452"] [retry count="1" t="1500356451" id="1500356450-2" v="1"] [/retry] [registration] ('+ê HEX:28272bea [/registration] [/receipt]

2017-07-18 12:40:52,779 [MainThread ] [DEBUG] tx: [ack to="[email protected]" type="retry" id="1500356450-2" class="receipt"] [/ack]

bahtiarp avatar Jul 18 '17 05:07 bahtiarp

@bahtiarp, you've got to properly encrypt the image and send the encryption key. How did you get to this point? Are you uploading the image or just resending something you received?

Could you please send the patch?

etinin avatar Jul 18 '17 16:07 etinin

https://github.com/tgalal/yowsup/issues/2016#issuecomment-317543592

langioletto avatar Jul 25 '17 13:07 langioletto

@etinin

This is how to encrypt:

def encryptImg(img, refkey): img = pad(img) derivative = HKDFv3().deriveSecrets(binascii.unhexlify(refkey), binascii.unhexlify("576861747341707020496d616765204b657973"), 112) parts = ByteUtil.split(derivative, 16, 32) iv = parts[0] cipherKey = parts[1] cipher = AES.new(key=cipherKey, mode=AES.MODE_CBC, IV=iv) imgEnc=cipher.encrypt(img) return imgEnc[:-6]

refkey = binascii.hexlify(os.urandom(112))

bahtiarp avatar Aug 05 '17 17:08 bahtiarp

Where should this be? In mediauploader.py after "stream = f.read()" ? I got this error: global name pad is not defined. Should I also change the media-tag to encr_media ?

dietzi avatar Aug 07 '17 18:08 dietzi

@dietzi try this

def pad(self, s):
        return s + ((16-len(s) % 16) * '{')

pabloruan0710 avatar Aug 07 '17 18:08 pabloruan0710

Here is my diff: https://pastebin.com/mWvjhSUX Now I get "json data not found" at mediauploader.py line 160

dietzi avatar Aug 07 '17 19:08 dietzi

try this:

after ssl_sock.write(bytearray(fBAOS.encode()))
dataux = ssl_sock.recv()

lines = dataux.decode().splitlines() 

pabloruan0710 avatar Aug 07 '17 19:08 pabloruan0710

@dietzi

If you see the log, you got this when you upload the data:

RESPONSE: HTTP/1.1 400 Bad Request Content-Type: application/json Date: Sun, 16 Jul 2017 13:24:23 GMT Connection: close Content-Length: 0

(so you dont get the json data, as expected by the next processed).

I've checked from wa web version (chrome).

I see the header when uploading encr image is diffrent from yowsup mediauploader done.

yowsup mediauploader.py version for Header POST:

POST https://mmg.whatsapp.net/u/f/Gqp_7Z4ZN1PsLrg8b3xHGllraQYABVRvM1QKAg/AkObQlFqUS5DA21KPJHyYOnL47WKzVMSGN8Jf0SaiBE8 Content-Type: multipart/form-data; boundary=zzXXzzYYzzXXzzQQ Host: mmg.whatsapp.net User-Agent: WhatsApp/2.16.11 S40Version/14.26 Device/Nokia-302 Content-Length: 4892

--zzXXzzYYzzXXzzQQ Content-Disposition: form-data; name="to"

[email protected] --zzXXzzYYzzXXzzQQ Content-Disposition: form-data; name="from"

[email protected] --zzXXzzYYzzXXzzQQ Content-Disposition: form-data; name="file"; filename="AkObQlFqUS5DA21KPJHyYOnL47WKzVMSGN8Jf0SaiBE8.enc" Content-Type: image/jpeg

Response from Whatsapp: HTTP/1.1 400 Bad Request Content-Type: application/json Date: Sun, 16 Jul 2017 13:24:23 GMT Connection: close Content-Length: 0

Whatsapp WEB (chrome) version for Header POST:

------WebKitFormBoundary83mOQO9W0A1qhgX7 Content-Disposition: form-data; name="hash"

xxfuy7CpXoeRjSI321m74C8sXG/u3Bls8niwzzdGxuE= ------WebKitFormBoundary83mOQO9W0A1qhgX7 Content-Disposition: form-data; name="refs"

UL9-EipnOTCQuDjlaN-C5_pwW5k= p64bypqos9A0WxZrNLYkUOg2oZ4= ------WebKitFormBoundary83mOQO9W0A1qhgX7-- Content-Disposition: form-data; name="file"; filename="blob" Content-Type: application/octet-stream

Whatsapp WEB (chrome) RESPONSE: {"type":"encrypted","mimetype":"application/octet-stream","size":"7034","filehash":"xxfuy7CpXoeRjSI321m74C8sXG/u3Bls8niwzzdGxuE=","url":"https://mmg-fna.whatsapp.net/d/f/AsL21rfdOsW-bDtnaMAHOcSsmWgt2gjQAtCfwiELT_Mj.enc"}

i've checked that this: xxfuy7CpXoeRjSI321m74C8sXG/u3Bls8niwzzdGxuE= is the b64Hash of image file.

While this two (Content-Disposition: form-data; name="refs"): UL9-EipnOTCQuDjlaN-C5_pwW5k= p64bypqos9A0WxZrNLYkUOg2oZ4= I still dont know how to genereate.

But although we got the correct response from whatsapp when uploaded, we have totally have to change the next process in yowsup. because it's diffrent from old responses that yowsup processed.

bahtiarp avatar Aug 08 '17 04:08 bahtiarp

@bahtiarp The refs is generated using the send number and recipient number with encripty:

  • This is in JS, need rewrite in Python
https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js
https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/sha256.min.js
https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/enc-utf8.min.js
https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/enc-base64.min.js
https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/lib-typedarrays.min.js

e = "55628226****@c.us" 
for (var t = e.length, n = [], r = 0; t > r; r++)
n[r >>> 2] |= (255 & e.charCodeAt(r)) << 24 - r % 4 * 8;

a = new Uint8Array(); 
b = CryptoJS.lib.WordArray.create(a)

var t = CryptoJS.HmacSHA256(CryptoJS.enc.Utf8.parse(e), b); 
t.sigBytes = 20
t.clamp()
var e = t.toString(CryptoJS.enc.Base64)
alert(e) 
console.log(e)

screen shot 2017-08-02 at 16 10 54

pabloruan0710 avatar Aug 08 '17 11:08 pabloruan0710

If I add "hash" to the header with a random key then I get "unsupported media type". I think if we could get the hash-code then we will be able to send media. Does anybody know about to generate the hash?

dietzi avatar Aug 08 '17 18:08 dietzi

@pabloruan0710 using your js script i still cannot get the correct refs;

from: 62851xxxxxx to: 62895330xxxxx

refs should be:

UL9-EipnOTCQuDjlaN-C5_pwW5k= qohsee679xXFE8N_aXqB40PLX1U=

can you try to get the same result with your js script?

bahtiarp avatar Aug 08 '17 19:08 bahtiarp

@bahtiarp This numbers ref is random Cripty, not is same number, check post with sniffer in Whatsapp web.

pabloruan0710 avatar Aug 08 '17 19:08 pabloruan0710

I can Send, and received this response:

Content-Type: multipart/form-data; boundary=zzXXzzYYzzXXzzQQ
Host: mmg.whatsapp.net
User-Agent: WhatsApp/2.16.12 S40Version/14.26 Device/Nokia-302
Content-Length: 41905
HTTP/1.1 200 OK
Content-Type: application/json
Date: Tue, 08 Aug 2017 19:19:51 GMT
Connection: close
Content-Length: 221
{"type":"encrypted","mimetype":"application/octet-stream","size":"41517","filehash":"Ll9P92iz1ERZHgM1pSflrGT0UU6RUEzU4fTAE95JkA8=","url":"https://mmg-fna.whatsapp.net/d/f/AoEGRkSJG36LMUwEjvkGtUyjcNLUq8ftLcqu2Xf9EMZt.enc"}

However didn't received image in recipient number

pabloruan0710 avatar Aug 08 '17 19:08 pabloruan0710