Tuesday, April 25, 2017

Starting python programming

Programming is easy. Really easy.

Many years ago programming requires good skills in hardware and software. "How computers works" was really important question 30 years ago. Cracking Enigma computer or first computer at a nuclear submarine were more simplest the modern cell phone.  This is a reason why building mathematical solution requires perfect engineering skill.
Computer is more powerful now and  tarting programming does not requires long term eduction. For example: driving car does not requires deep knowing in  engines and physics, you are driver not mechanics.

Downloading software. 

First  download  python.python 
Also text editor  or powerful IDE is needed. I like Emacs but a lot of peoples love pycharm. You can download it  from PyCharm. Python is really popular and a lot of system have it included. I'm planning  to use some additional libraries  for our application. It is good idea do not touch your original software. Correct  way is virtual environment usage. 
 Linux people can take a look at : Virtual environment for Linux and IMac
Windows  people  should take a look at : Virtual environment for Windows


First our python  program

Using your favorite editor  type next code: 



#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Small  script for plarium game.
This application has education purposes only.
"""

def main():
    """main function for this one"""
    print("Hello!")
    return 0

if __name__=="__main__":
    main()


First string  informs system that python will be used. 
Second one  is setting code page for our script.  My system  uses utf-8.  Next one  is python doc string. This can help other guys to understand your code.  Of course,   you can move out  this string. 
Starting  from def  to the end it is default python code  structure.   Also you can delete  all code except string print("Hello!")  and result is a same.   I've added some additional string for making good programmer's writing skill.

Working with files

Of course, writing 'hello'  is really simple and everyone can do this.  More interested is working with files. Writing simple  files  is simple but interesting from performance point of view as I wrote early List in python performance.  I think it is not a bad idea to start from writing and reading configurations files. One of a simplest modules for this task is ConfigParser in python 3.0  it has been renamed to configparser. 
add please to our script after doc string next code: 
import os, ConfigParser

def readconfigfile():
    """ Readinf importamt values from config files"""
    configparser=ConfigParser.RawConfigParser()
    configparser.readfp(open('./config.cfg'))
    userid=configparser.get('user', 'userid')
    authkey=configparser.get('user', 'authkey')
    return userid, authkey
 and update main function to  the next one:

def main():
    """main function for this one"""
    userid,authkey=readconfigfile()
    print("Hello!")
    
    print('userid:{0}, authkey:{1}'.format(userid,authkey))
    return 0
also create a simple file config.cfg with simple data:

[user]
userid=12344
authkey=34fewfsad

Of course, you can set those values using our investigation from firefox development tools: getting UserID and authkey Run it and our script print values from our config file. Also we can use those data for future programming.

Thursday, April 20, 2017

Plarium part two. Working with swf

Flash is old but still usable. 

Flash is very old  but still  usable. Few year ago I was sure that this product be killed by WebGL or other modern technology. Now Flash is alive and  very popular for some application.  Flash  contains not only sprites and images ,  actionscript code is also included in swf file. By running  this code flsdh has ability to  work with  user control and send command via network. 

Start JPEXS Free Decompiler  and open previous downloaded swf file. Navigate to script section:
  Unfortunately my Imac has problem with text searhing(searching is stopped with 'out of memory' error). In this case I'm looking  for potential file using personal meaning. File which has "sign-code" reference is placed at next path: scripts-> model-> logic->server->commands->JsonCallCmd
Function name is call() and contain  another one fuction generateRequestSignature(data,paramTextForSignatureCalculation) where  data are data which are send to server and paramTextForSignatureCalculation is sum of wellknown string started from "The Matrix" , method, userid , and usersocailauthkey.  Last three strings are also added to HTTP Header. 
you can verify this investigation by python interpretator:
python
Python 2.7.10 (default, Oct 13 2015, 09:42:49) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> matrix_string = "The Matrix has you..."
>>> http_data= """{"s":{"s":true,"o":1,"l":true,"f":true,"c":"en-US","p":0,"m":true,"z":0.6361773255813953,"g":false,"a":true},"u":1492615673749,"r":77,"o":{"i":44,"b":null},"y":[518],"sm":[],"km":9,"g":21193,"t":1492615694942,"q":[64,1,44,55,56,57,58,59,60,61,62,63]}"""
>>> http_command="CloseQuest"
>>> authkey='secretkey'
>>> userid='itsasecret'
>>> print hashlib.md5((matrix_string+http_data+http_command+userid+authkey).encode('ascii')).hexdigest()
8917c927db57bf6d7dbcb7a72b25151d
If you have received the same value as in http header then you have done your job well.

Plarium again. How to start programming due to game playing.

Plarium again. How to start programming due to game playing. 

I would like to say that writing a small script which is helping to have advantages against other peoples is not good. Writing  this post I have a goal to prepare a way to the fanny world of the programming.  I hope this article helps to use computer not a game machine only. 
Previous article related to this one  is placed at :old script

Requirements

  1. Internet browser with developer tools. Firefox or chrome are a good choice.  Both of them can be installed at the any popular operations system.  Download links are there:  https://www.mozilla.org/uk/firefox/new/ and https://www.google.com/chrome/browser/desktop/index.html. Also developer extension is needed: this is for firefox
  2. Plarium company uses flash and flash decompiler (in some case debugger) is required. I've used   JPEXS Free Flash Decompiler . Download link is here : https://www.free-decompiler.com/flash/download/  
  3.  I've used  python for writing some code. I'm lazy man and  've used my investigation and python 2.7 . You can use any it is not so important. https://www.python.org/downloads/
  4. Time and personal challenge.  Main our goal is programming starting. 

First step using Firefox. 

1. Start firefox and developer  tool window. 
2. Navigate to plarium.com
3. Take a look at network windows at firefox developer tools page. It is looks like : 

left side of the page contains request from your browser to server. Right side contains detail of the request. At this  moment only html and Flash  are been interested for investigation. 
Press "HTML" and "Flash" at the menu bar. After this network page is look like : 
Take a look at the left side. 
200 and GET inform as about succesful  request (status 200) and used http method GET.  List of codes  are preset at https://en.wikipedia.org/wiki/List_of_HTTP_status_codes. 200 means that all are OK.    HTTP  has eight method but   ussualy GET and POST are used.  This image inform as that  our browser download game from next URL : https://cdn01.x-plarium.com/pirates/client/ppen/uc_04_11_0957_636275014576166798.swf .
 Keep in mind this URL and file . We need  it for future investigation. 
Next one  is a POST  request to segment.ashx:
 Lets  try to understand this request:
First string is flash file which produces our request. 
  • sign-code is special string with control code. 
  • signin-authSeed and signin-authKey are credential string. I hope you understand  why this image  has this black color. 
  • signin-userId is local login name 
  • sever-method is special field selecting user activity. (I've finished quest and browser  add 'CloseQuest')
  • last one string is data which is sending by browser. look like "t" is time but i'm not sure at this moment.  
Next part  contains working with swf file