Help with proxy code

10/16/2010 19:05 tkblackbelt#1
Ok so I started to make my own proxy and have got the client to connect to it but when I connect to the AuthServer I get the output -597210518-11065-488, I'm not sure if thats the right output. Ill post my code so far. I would appreciate it if someone pointed out what I'm doing wrong. thanks :)

Java

Code:
package lightproxy;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main{

    public static void main(String args[]) throws IOException{



        //Client Port number
        final int PORT = 9959;
        String AuthIp = "208.96.34.46";

        //Connects to client
        ServerSocket Client = new ServerSocket(PORT);
        System.out.println(getTime() + "Waiting for connections on Port : " + PORT);
        Socket Clientsoc = Client.accept();
        
        
        System.out.println(getTime() +  "Client connected");

        //Connects to Auth Server
        Socket s = new Socket(AuthIp,PORT);
     
        BufferedInputStream in = new BufferedInputStream(s.getInputStream());

        while(true){
            int end = in.read();
            if(end == -1)
                break;
            else{
            byte b = (byte)end;
            System.out.print(b);
            }
        }

       }

    public static String getTime(){
        Calendar cal = java.util.GregorianCalendar.getInstance (  ) ;
        SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy/MM/dd hh:mm:ss" ) ;
        return (sdf.format(cal.getTime())) + " : ";
    }
}
10/16/2010 21:28 pro4never#2
First of all I don't know java syntax but it looks like you're combining the entire values for the packet and printing them out...

#1 It's a byte array which then needs to be structured and read using REVERSED BYTE ORDER

Eg

uint with value of 1 is

1 0 0 0

IIRC java's default 'readers' for that type of stuff (binary reader for files did it) where it wouldn't reverse the order so you get funky values. You need java's equivalent of a bitconverter. You will also need to think of an efficient method for writing packets. Something like writeuint32(value, offset, destination); is what I use... one of impulse's methods (and others use it) but anything will work really.

Also keep in mind you will need to decrypt the incoming information using AuthEncryption (it's posted in every source ever and is incredibly simple) and Blowfish for the game server.

Check out the proxy sticky if you need more information. It's all been posted basically.
10/16/2010 22:33 tkblackbelt#3
Ok so if I'm understanding you correctly every time I get incoming byte I have to reverse it and not add bytes together but have the bytes go one after another, then I have to structure and decrypt the array of bytes.

Actually I'm struggling with the encryption in java. So I think I'm going to learn c# since you were saying theres lots info and sources about proxies, Thanks for your help though pro4never.

Sweet I just started c# and its almost exactly like java. I've already made a tic tac toe game xD
10/17/2010 05:05 pro4never#4
C# and Java are VERY similar.

And you... sort of place them in order. It's not quite that simple.

Convert your bytes to hex and it will make more sense than as dec. Something like this..

String Output = "";
for(int I = 0; I < Data.Length; I++)
Output += Convert.ToString(Data[I], 16) + " " ;
Console.WriteLine("Packet dump for type: " + BitConverter.ToUInt16(Data, 2) + " \n" + Output);

That will give you a hex dump of your packet including the packet type (assuming it's already decrypted!)



If you look at the proxy sticky I link to a VERY good explanation of packet structuring. Basically you 'chunk' things together. This is done through logic and trial and error mostly. I go into it a little in that thread but basically each packet has various values you are looking for. These include but are not limited to...

Packet Length (always there, always first 2 bytes. USHORT)
Packet type (always there, always second 2 bytes. USHORT)
Character UID UINT
Target UID UINT
X/Y/Map USHORT
TimeStamp UINT
String (usually contains a length byte preceding it. EG names have a byte listing length of string to read)

There are other things you may want to 'chunk' out of packets but that's the main stuff. Just look at the hex and what you are doing. That + wiki makes things easy to figure out. Once you know your UID in one packet you can block it off in ANY other packet based on that character (easy removal of 4 unknown bytes). Same with target uid, target a mob and if the value you are reading is between 300-500k you know it's a mob uid. etc
10/17/2010 05:26 _tao4229_#5
Quote:
Originally Posted by pro4never View Post
First of all I don't know java syntax but it looks like you're combining the entire values for the packet and printing them out...

#1 It's a byte array which then needs to be structured and read using REVERSED BYTE ORDER

Eg

uint with value of 1 is

1 0 0 0

IIRC java's default 'readers' for that type of stuff (binary reader for files did it) where it wouldn't reverse the order so you get funky values. You need java's equivalent of a bitconverter. You will also need to think of an efficient method for writing packets. Something like writeuint32(value, offset, destination); is what I use... one of impulse's methods (and others use it) but anything will work really.

Also keep in mind you will need to decrypt the incoming information using AuthEncryption (it's posted in every source ever and is incredibly simple) and Blowfish for the game server.

Check out the proxy sticky if you need more information. It's all been posted basically.
Endianness - Wikipedia, the free encyclopedia


To OP: add me on MSN, I'll PM you
10/17/2010 05:45 pro4never#6
My bad saint. I've never done anything in Java and when Noah was attempting to read values using it we were having some issues where the reader he was using was not reversing the bytes. I was positive that there were functions built in to java to complete this... I just know that when he was writing the code to read dmaps it was pulling funky values when he used the default binary reader wheras when he read a single byte it read correctly (the only obvious explanation I could think of at the time was endianness)
10/17/2010 18:33 tkblackbelt#7
Ok I rewrote the proxy in c# and got the encryption and decryption working for Auth. but when I get the password seed. and relay it to the client and get back the response I get the same packet as I sent not the response with the acc pass and etc.

Code:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace LightProxy
{
    class Program
    {
        static void Main(string[] args)
        {

            IPAddress ip;
            TcpListener ClientListner;
            TcpClient ClientSocket;
            Socket ProxyToServerSocket;
            AuthProtocolCryptographer cryptor;
            byte[] buffer = new byte[255];
            Program proxy = new Program();

            ProxyToServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            ip = IPAddress.Parse("192.168.1.100");
            ClientListner = new TcpListener(ip, 9959);

            ClientListner.Start();
            Console.WriteLine("Server Started");

            ClientSocket = ClientListner.AcceptTcpClient();
            Console.WriteLine("Server: Client Connected");

            try
            {
                ProxyToServerSocket.Connect("208.96.34.46", 9959);
                Console.WriteLine("Auth: Client Connected to Auth");
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not connect to Auth server");
            }
            cryptor = new AuthProtocolCryptographer();

            ProxyToServerSocket.Receive(buffer);
            
            cryptor.Decrypt(buffer);
            cryptor.Encrypt(buffer);
            proxy.DumpHex(buffer);

            ClientSocket.Client.Send(buffer);
            ProxyToServerSocket.Send(buffer);
            proxy.DumpHex(buffer);

            
        }
10/19/2010 08:07 xmen01235#8
Quote:
Originally Posted by tkblackbelt View Post
Ok I rewrote the proxy in c# and got the encryption and decryption working for Auth. but when I get the password seed. and relay it to the client and get back the response I get the same packet as I sent not the response with the acc pass and etc.

Code:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace LightProxy
{
    class Program
    {
        static void Main(string[] args)
        {

            IPAddress ip;
            TcpListener ClientListner;
            TcpClient ClientSocket;
            Socket ProxyToServerSocket;
            AuthProtocolCryptographer cryptor;
            byte[] buffer = new byte[255];
            Program proxy = new Program();

            ProxyToServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            ip = IPAddress.Parse("192.168.1.100");
            ClientListner = new TcpListener(ip, 9959);

            ClientListner.Start();
            Console.WriteLine("Server Started");

            ClientSocket = ClientListner.AcceptTcpClient();
            Console.WriteLine("Server: Client Connected");

            try
            {
                ProxyToServerSocket.Connect("208.96.34.46", 9959);
                Console.WriteLine("Auth: Client Connected to Auth");
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not connect to Auth server");
            }
            cryptor = new AuthProtocolCryptographer();

            ProxyToServerSocket.Receive(buffer);
            
            cryptor.Decrypt(buffer);
            cryptor.Encrypt(buffer);
            proxy.DumpHex(buffer);

            ClientSocket.Client.Send(buffer);
            ProxyToServerSocket.Send(buffer);
            proxy.DumpHex(buffer);

            
        }
You're code is so confusing and it is not the right way to set up your proper proxy. You need to study further on how to create a client server application for the first place.

The process inside the proxy such as encryption and decryption will be done on the receive event of the packets coming from either conquer client or TQ authentication and game server.
10/19/2010 16:02 pro4never#9
Your problem is you haven't actually setup any sort of receive functions.

I don't know about Java but in C# you have to either make a thread to block off looking for connections which then on successful connection spawns a new thread to listen for data (sync sockets... not a great option) or an event based socket system which on receive/on connect performs some sort of action (async sockets)

Also as stated before, crypt will be stored PER CLIENT. Auth encryption uses a counter meaning you MUST encrypt/decrypt each packet that goes through or else it will lose 'sync' and stop working
10/19/2010 16:24 Trigorio#10
Why are you messing with the password seed unless you are trying to create a standalone bot?
Just let the client do it for you, if it's a proxy you are after.
10/19/2010 17:13 tkblackbelt#11
Thanks for helping me bu ya I think I'll study c# for a while before I go any further with this.
10/19/2010 17:41 xmen01235#12
Quote:
Originally Posted by tkblackbelt View Post
Thanks for helping me bu ya I think I'll study c# for a while before I go any further with this.
Don't give up too early you can study my own socket wrapper for your reference

[Only registered and activated users can see links. Click Here To Register...]
10/19/2010 18:39 tkblackbelt#13
Sweet I'll have a look at but I was programming in java for a about 4-5 months, and decided to try c# so I only have about a week of experience with it (although the syntax is very similar to java so I'm learning quit quickly) so I'm gonna finish reading my c# book and take a few C# networking tuts and then get back to my proxy, and I will complete it. Also would you recommend reading a book on cryptography to help understanding encrypting and decrypting packets?
10/20/2010 02:49 xmen01235#14
Quote:
Originally Posted by tkblackbelt View Post
Sweet I'll have a look at but I was programming in java for a about 4-5 months, and decided to try c# so I only have about a week of experience with it (although the syntax is very similar to java so I'm learning quit quickly) so I'm gonna finish reading my c# book and take a few C# networking tuts and then get back to my proxy, and I will complete it. Also would you recommend reading a book on cryptography to help understanding encrypting and decrypting packets?
As for my socket class I am using vb dot net on it but it is easy for you to convert it into c# if you really want. And about the crypthography I also learn those stuff from epvp and codexplosion, I really not that very proficient on those fields but the you can study the 2 specific crypthography that conquer has been using. I have my own wrapper for both crypthograhpy also but I want you to learn it by yourself. The first one for authentication is not that hard and it is like an XOR method only but the second one for game server is a bit tricky and you need to study blowfish and DH key exchange. If you reach at this point you should study the best method of defeating this crypthography which is know as man in the middle attack.
10/20/2010 04:29 pro4never#15
[Only registered and activated users can see links. Click Here To Register...]

Has some links to info on encryption. I generally just use public encryption methods for both as they 'work' perfectly fine in C#.

If you want to write your own though there is a bunch of info on the dhkey/man in the middle attack and also a nice bit of example code for blowfish on korv's wiki. All this is already linked in the proxy thread though.



[Only registered and activated users can see links. Click Here To Register...]
Diffie?Hellman key exchange - Wikipedia, the free encyclopedia
Man-in-the-middle attack - Wikipedia, the free encyclopedia

Also points for you for actually being willing to put work into LEARNING. I'm shocked. 99 pct of the posts around here has been "omg how do I make proxy?!?" and then as soon as they run into an obstacle they give up and ask for a working one to use/leach/etc.