Openkore.com

OpenKore Forums
It is currently 18 Aug 2018, 06:32

All times are UTC - 5 hours [ DST ]





Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: 02 - kali's beginning programming guide
PostPosted: 22 Jul 2017, 07:31 
Offline
OpenKore Monk
OpenKore Monk

Joined: 04 Apr 2008, 10:10
Posts: 474
In OpenKore's source code you'll sometimes see packing strings like
Code:
V Z40 a32 v
or
Code:
V Z24 Z24 C


What does this mean?

As a refresher, look up http://www.rubydoc.info/stdlib/core/Array%3Apack

So from that link you'll see that the packing string
Code:
V Z40 a32 v
means:
Code:
V         | Integer | 32-bit unsigned, VAX (little-endian) byte order
a         | String  | same as ``a'', except that null is added with *
Z         | String  | arbitrary binary string (null padded, count is width)
v         | Integer | 16-bit unsigned, VAX (little-endian) byte order


Now we can recreate our login packet.

Like I mentioned in the previous section as well as the wiki entry on the network subsystem, Ragnarok's packets are heralded by a packet switch (aka message ID). In this particular case we're interested in something called the "master_login" packet, which is "0A76" in phRO and thRO, and "0064" in iRO restart.

Time to do some code to test this out :)

But first, if you're in windows you'll need the Windows Subsystem for Linux https://msdn.microsoft.com/en-us/comman ... tall_guide so you can follow along. Mac users already have ruby installed, and Linux users can just use apt-get to install ruby.

You'll also need to install libmcrypt-dev in linux to make use of the cryptography library.

We're gonna need a few gems (which are libraries; code that has been packaged to be reused by other developers) installed, so type these in:
Code:
gem install ruby-mcrypt
gem install hexdump


Then in a file called ragnarok.rb paste these in:
Code:
require "socket"
require "mcrypt"
require "hexdump"

packet_switch = [0x0a76].pack("v")
version = [0x0018].pack("V")
username = "username".ljust(40, "\0")
password = "password".ljust(16, "\0")
crypt_key = [0x06, 0xA9, 0x21, 0x40, 0x36, 0xB8, 0xA1, 0x5B, 0x51, 0x2E, 0x03, 0xD5, 0x34, 0x12, 0x00, 0x06, 0x06, 0xA9, 0x21, 0x40, 0x36, 0xB8, 0xA1, 0x5B, 0x51, 0x2E, 0x03, 0xD5, 0x34, 0x12, 0x00, 0x06].pack("C32")
cipher = Mcrypt.new(:rijndael_256, :ecb, crypt_key, nil, :zeros)
password_rijndael = cipher.encrypt(password)
master_version = [0x000f].pack("v")

packet = packet_switch + version + username + password_rijndael + master_version
Hexdump.dump(packet) # dumps the packet to the screen

ip_address = "116.93.119.116" # replace with the correct ip address; this one is for phRO
ip_port = 6900 # same here, replace with the correct port
socket = TCPSocket.new ip_address, ip_port
puts "sending"
socket.write(packet)
puts "receiving"
data = socket.recvfrom(1024)
Hexdump.dump(data.pack("A*"))
data = socket.recvfrom(1024)
puts data.pack("H*")
Hexdump.dump(data.pack("A*"))


Then to run the file, type
Code:
ruby ragnarok.rb


Try it out and see that you just communicated with the server. You should see output similar to this:
Code:
0>   b5 01 fe ff ff ff fe ff ff ff 00 00 00 00 00 00  |................|
16>  00 00                                            |..|

0>   76 02 73 00 00 00 00 00    00 00 00 00 00 00 00 00    v.s.............
16>  00 00 00 00 00 00 00 00    00 00 00 00 00 00 00 00    ........ .......
32>  00 00 00 00 00 00 00 00    00 00 00 00 00 00 00 64    ...............d
48>  00 00 00 74 5D 77 1E 94    11 54 68 6F 72 20 28 4E    ...t]w...Thor (N
64>  65 77 29 00 00 00 00 00    00 00 00 00 00 7B 1A 00    ew)..........{..
80>  00 00 00 74 5D 78 86 94    11 4C 6F 6B 69 00 00 00    ...t]x...Loki...
96>  00 00 00 00 00 00 00 00    00 00 00 00 00 25 37 00    .............%7.
112> 00 00 00                                              ...

Here's the quiz: what do these two packets contain? Hint: you can use the OpenKore source code to figure out the packing strings and match the portion to its description.

I understand that this is all so complicated with all the encryption and such, so next time let's try working on a less protected server, like iRO restart.

_________________
Got your topic trashed by a mod?

Trashing topics is one click, and moving a topic to its proper forum is a lot harder. You expend the least effort in deciding where to post, mods expend the least effort by trashing.

Have a nice day.


Top
 Profile  
 
 Post subject: Re: 02 - kali's beginning programming guide
PostPosted: 22 Jul 2017, 09:59 
Offline
Been there done that!
Been there done that!

Joined: 24 Mar 2012, 04:13
Posts: 116
Can you give explanation for "x" ?

here's my problem

my char is female but when i type "s" , it is shown "Boy"

the code from serverType0.pm
Code:
my ($cID,$exp,$zeny,$jobExp,$jobLevel, $opt1, $opt2, $option, $stance, $manner, $statpt,
$hp,$maxHp,$sp,$maxSp, $walkspeed, $jobId,$hairstyle, $weapon, $level, $skillpt,$headLow, $shield,$headTop,$headMid,$hairColor,$clothesColor,$name,$str,$agi,$vit,$int,$dex,$luk,$slot, $rename, $unknown, $mapname, $deleteDate) =
         unpack($unpack_string, substr($args->{RAW_MSG}, $i));


and from receive.pm
Code:
return 'a4 V9 v V2 v14 Z24 C8 v Z16 V x4 x4 x4 C' if $_ == 145;


this part of the 099D data , i don't want to list the char id, so i cut the first 368 byte and change name to ABCD
Code:

368>  00 00 00 00 41 42 43 44    00 00 00 00 00 00 00 00    ....ABCD........
384>  00 00 00 00 00 00 00 00    00 00 00 00 01 01 01 01    ................
400>  01 01 02 00 00 00 6E 65    77 5F 31 2D 31 2E 67 61    ......new_1-1.ga
416>  74 00 00 00 00 00 00 00    00 00 00 00 00 00 00 00    t...............
432>  00 00 00 00 00 00 00


$name is Z24 (41 42 43 44 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 )

$str,$agi,$vit,$int,$dex,$luk,$slot, $rename, $unknown, are C8 v ( 01 01 01 01 01 01 02 00 00 00 )

$mapname is Z16 (6E 65 77 5F 31 2D 31 2E 67 61 00 00 00 00 00 00 )

and $deleteDate is V ( 00 00 00 00 )

and the rest "x4 x4 x4 C" ( 00 00 00 00 00 00 00 00 00 00 00 00 00 )

I believe gender is the "last byte" , the last 00 (is it 'C' ?)
so i use this , which works fine now
Code:
$chars[$slot]{sex} = unpack( 'C', substr($args->{RAW_MSG}, $i + $blockSize -1));



but i don't understand the 'x4' , Can you give explanation for "x4"
is "x" works like "a" ?


Top
 Profile  
 
 Post subject: Re: 02 - kali's beginning programming guide
PostPosted: 22 Jul 2017, 10:49 
Offline
OpenKore Monk
OpenKore Monk

Joined: 04 Apr 2008, 10:10
Posts: 474
x means to just read the byte as is, but don't parse it. it is openkore specific, but yes you can treat it as an a

_________________
Got your topic trashed by a mod?

Trashing topics is one click, and moving a topic to its proper forum is a lot harder. You expend the least effort in deciding where to post, mods expend the least effort by trashing.

Have a nice day.


Top
 Profile  
 
 Post subject: Re: 02 - kali's beginning programming guide
PostPosted: 22 Jul 2017, 10:51 
Offline
OpenKore Monk
OpenKore Monk

Joined: 04 Apr 2008, 10:10
Posts: 474
great work btw figuring it all out by yourself. would you mind sending a pull request so others can benefit too? :)

_________________
Got your topic trashed by a mod?

Trashing topics is one click, and moving a topic to its proper forum is a lot harder. You expend the least effort in deciding where to post, mods expend the least effort by trashing.

Have a nice day.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group