miércoles, 23 de julio de 2008

[EN] Protocol introduction

In this first post I will cover the first part you need to know to talk to the MSN server.

In a normal session you will make requests to different servers, but one connection is basic and must be active through all the session, that is a connection with the Notification Server (NS), this connection is a socket connection initially made with the host messenger.hotmail.com on the port 1863, this connection is managed through commands written in plain ascii.

This commands have the following format

Command transaction_id [param1 param2 ... paramN] [payload_size]\r\n
[payload_body]

* command is generally a 3 letter word like VER, MSG etc.
* transaction_id is a number that represent the order of this command, start with the number 1 and is increased every time you send a new command. When you send a response to a command sent by the server you must set the tid (transaction_id for short from now on) to the number that the server sent. Some commands put other content as second parameter, but they are just a few exceptions.
* a number of optional parameters separated by a space come next and the number depends on the command. They can contain anything that may be useful for the command
* payload_size is the number of bytes of the payload that comes after the \r\n (the payload exists only on commands that contain payload like GCF, MSG and others..
* payload is the content of the command and can be anything that the command wants to send us.

Lets stop talking and get some action, lets send our first command to the server.
To make this post an introduction, I will just use netcat, the next post will teach you about sockets on python (they are a wrapper over C sockets so it's easy to follow for programmers of other languages).

open a terminal and write

$ netcat messenger.hotmail.com 1863[enter]

you will see a new line and nothing more, that means that you are connected to the server, now write:

VER 1 MSNP13 CVR0[enter]

then you will get:

VER 1 MSNP13 CVR0

(the last key is a zero)

what does it mean? that is the first message to the server and its response, the command is an introduction and says to the server something like "Hi i want to start a session, I speak the version 13 of the protocol (MSNP13)", then the server response is the same, that means that he speak that version, so we can continue.

you can send more than one version and he will return the version he prefers, we will just use the version 13 of the protocol, but here is an example of multiple versions:

lets send him 3 versions

mariano@mousehouse:~$ netcat messenger.hotmail.com 1863
VER 1 MSNP13 MSNP14 MSNP15 CVR0
VER 1 MSNP15 MSNP14 MSNP13 CVR0

seems that he prefers MSNP15 :D

lets see where are the MSN guys going

mariano@mousehouse:~$ netcat messenger.hotmail.com 1863
VER 1 MSNP13 MSNP14 MSNP15 MSNP16 MSNP17 CVR0
VER 1 MSNP17 MSNP16 MSNP15 MSNP14 MSNP13 CVR0

MSNP17?!? :D

even further...

mariano@mousehouse:~$ netcat messenger.hotmail.com 1863
VER 1 MSNP13 MSNP14 MSNP15 MSNP16 MSNP17 MSNP18 CVR0
VER 1 MSNP17 MSNP16 MSNP15 MSNP14 MSNP13 CVR0

nope, just MSNP17

just for you to see some more commands I will send some extra commands that will be explained later when we start a session using a programming language.

connect again with the server and send some commands

mariano@mousehouse:~$ netcat messenger.hotmail.com 1863
VER 1 MSNP13 CVR0
VER 1 MSNP13 CVR0
CVR 2 0x0c0a winnt 5.1 i386 MSNMSGR 8.0.0792 msmsgs xmxsxn@hotmail.com
CVR 2 8.1.0178 8.1.0178 8.1.0178 http://msgruser.dlservice.microsoft.com/download/5/6/4/5646481F-33EF-4B08-AF00-4904F7677B89/ES/Install_WLMessenger.exe http://get.live.com/es
USR 3 TWN I xmxsxn@hotmail.com
XFR 3 NS 207.46.110.94:1863 U D

lets see this by command

>>> are messages being sent
<<< are messages being received

>>> VER 1 MSNP13 CVR0
<<< VER 1 MSNP13 CVR0

VER is the same as before

>>> CVR 2 0x0c0a winnt 5.1 i386 MSNMSGR 8.0.0792 msmsgs xmxsxn@hotmail.com
<<< CVR 2 8.1.0178 8.1.0178 8.1.0178 http://msgruser.dlservice.microsoft.com/download/5/6/4/5646481F-33EF-4B08-AF00-4904F7677B89/ES/Install_WLMessenger.exe http://get.live.com/es

with the CVR command we send our version information to the server:

* 0x0c0a is a set of flags that indicate our capabilities, I will explain that later, with that flags you can keep coding without problem.
* "winnt i386" is our operating system :D
* 8.0.0792 is our client version ;)
* and the last one is the account we are trying to log in (that account is my test account)

we receive a response that is not really usefull, then we present ourselves, and get a response and we get disconnected (we get the shell again)

>>> USR 3 TWN I xmxsxn@hotmail.com
<<< XFR 3 NS 207.46.110.94:1863 U D

XFR is the transfer command, and means that the server wants us to connect to the server located at 207.46.110.94:1863

so we start netcat again pointing at that server, and do it all over again

mariano@mousehouse:~$ netcat 207.46.110.94 1863
VER 1 MSNP13 CVR0
VER 1 MSNP13 CVR0
CVR 2 0x0c0a winnt 5.1 i386 MSNMSGR 8.0.0792 msmsgs xmxsxn@hotmail.com
CVR 2 8.1.0178 8.1.0178 8.1.0178 http://msgruser.dlservice.microsoft.com/download/5/6/4/5646481F-33EF-4B08-AF00-4904F7677B89/ES/Install_WLMessenger.exe http://get.live.com/es
USR 3 TWN I xmxsxn@hotmail.com

until here all is like before but now we don't get a transfer (we can get more than one, but today we are lucky)

then we get
<<< GCF 0 6660

we receive the GCF command, with the payload_size 6660 and next we get 6660 bytes of xml that basically say which words we should filter on our client (nice :P)

<<< GCF 0 6660
[a lot of xml goes here]
<<< USR 3 TWN S ct=1216861501,rver=5.5.4177.0,wp=FS_40SEC_0_COMPACT,lc=1033,id=507,ru=http:%2F%2Fmessenger.msn.com,tw=0,kpp=1,kv=4,ver=2.1.6000.1,rn=1lgjBfIL,tpf=b0735e3a873dfb5e75054465095398e0

the last command will later be used to do the passport authentication.

ok, that's all for today, the next time I will write about how to do it with python code (maybe another language will be used too) wait for the translations on the weekend maybe :P

[DE] Erste Post!

Hallo!, diese ist meine Erste Post auf diese Blog, dass es zu lehren ist, wie man schreiben Sie Ihre eigene Bibliothek zur Interaktion mit dem MSN-Messenger-Netzwerk, die Gründe hinter diesem Blog sind:


  • Jemand hat mir bei Mail gefragt um ihn eine eigene Bibliothek zu bauen helfen mit ein Programmier Sprache die ich nicht kenne.
  • Hilfe diese Protokoll zu dokumentieren.
  • Hilfe dir lernen, wie Netzwerk Programme zu Programmieren.
  • Hilfe mir mein Deutsch zu verbessern (Ich hoffe dass jemand hilft mir diese Post zu korrigieren).
  • Hilfe Leute dass emesene helfen möchten (mein andere Projekt www.emesene.org).
  • Vielleicht ein Paar Münze verdienen durch adsense :P
Die Beispiele werden auf python geschrieben, es ist eine saubere Programmiersprache dass du lesen kannst ohne es zu kennen (jemand sagt dass es interpretiert Pseudocode :P)
Ich hoffe dass du diese blog befolgen und dass du auf deine bevorzugte Programmiersprache schreiben kannst!.

siehe den Kommentar-Link? benutze es, es ist hilfreich :P

[ES] Primer post!

Hola, este es el primer post (en castellano) de un blog que esta hecho con la intención de enseñar como escribir tu propia librería para interactuar con la red MSN Messenger, las razones detrás de este blog son:

  • Alguien me pidió por mail que lo ayude a crear una librería en un lenguaje que no conozco
  • Ayudar a documentar el protocolo
  • Ayudar a que aprendas como codificar cosas relacionadas con redes
  • Ayudarme a mejorar mi aleman (voy a poner traducciones al aleman, espero que algun aleman me ayude a corregir los horrores :D)
  • Ayudar a gente que quiere colaborar con emesene (mi otro proyecto www.emesene.org)
  • Quizas tener algunos centavos a traves de adsense :P

Los ejemplos serán provistos en python, es un lenguaje limpio y puede ser leido incluso si no sabes programar en el (alguien dice que es pseudocodigo interpretado :P)

Espero que sigan este blog y provean implementaciones en su lenguaje preferido!

Ven el link de comentarios? usenlo, es util :P

[EN] First post!

Hi, this is the first post of a blog that is intended to teach you how to write your own library to interact with the MSN Messenger network, the reasons behind this blog are:

  • Someone asked me by mail to help him to build a library on a language that I don't know
  • Help to document this protocol
  • Help you to learn how to code network stuff
  • Help me improve my german (I will provide german translations, I hope that some german will help me correct my horrors :D)
  • Help people that want to help emesene (my other project www.emesene.org)
  • maybe get some cents through adsense :P
The examples will be provided in python, it's a clean language and can be read even if you don't know it (someone says it's interpreted pseudocode :P)

I hope that you follow this blog and provide implementations in your favourite language!

see the comment link? use it, it's helpful :P