Help C# clientless Problem

05/29/2015 20:54 theking200051#1
hey guys ,
i wanna to build an clientless based tool for one of the private servers , i alrdy made some Clientless tools by Autoit i got the concept but i faced some problems with the c# .

i think the problem in the 0xA102 packet or the TCP socket redirction method i used.
Here is the working Autoit code ;

And here is the non working C# code

i just read some others clientless sources and i Noticed that they use multiThreads for receiving.

The Question : my redirection mechanism is correct or i miss some thing or i must build receiving Multithreads ?

Thanks in advance .
05/29/2015 23:00 vorosmihaly#2
I'd love to help you,but with this small piece of code I can't really say anything.. :D
05/30/2015 03:26 theking200051#3
ty for try helping ,the code is large so i just ignore some unimportant methods and unimportant OPcodes like 0xA100,0x2322,0xA103,0x6323.... and so on , if it still not enough i can upload the full project .

Ty again for response and sry for the bad design.


Code:
static private Socket s;
        static private TransferBuffer recv_buffer = new TransferBuffer(4096, 0, 0);
        static private Security security = new Security();
        static private List<Packet> packets = new List<Packet>();
public void beginconnect(string ips,int port)     \\ this method for intiate the TCP session ;
         {
              s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
             IPAddress ip = IPAddress.Parse(ips);
             bool stoploop = false;
             while (stoploop == false)
             {
                 try
                 {
                     s.Connect(ip, port);
                     s.Blocking = false;
                     s.NoDelay = true;
                     stoploop = true;
                     //GUI.Debugmsg("Intiate TCP Session ......");
                 }
                 catch 
                 {
                     GUI.Debugmsg("Intiate TCP Session Failed !");
                     Thread.Sleep(3000);
                      stoploop = false;
                 }
                 Thread.Sleep(1000);
             }
         }

public void DoRecv()                   \\\\ tell the program to start receving 
        {
            SocketError err;
            
            while (Stop_recv == false )
	       {
	        recv_buffer.Size = s.Receive(recv_buffer.Buffer, 0, recv_buffer.Buffer.Length, SocketFlags.None, out err);
               if (err !=SocketError.Success)
	             {
                     if (err !=SocketError.WouldBlock)
                     {
                         GUI.Debugmsg("TCP Disconnected !" + err);
                         Restartconnection();
                         break; 
                     }
	             }
                else
	             {
                   if (recv_buffer.Size > 0)
                        {
                            security.Recv(recv_buffer);
                        }
                        //else
                        //{
                        //    GUI.Debugmsg("TCP Not receiving Error !");
                        //    Restartconnection();             
                        //    break;
                        //}
	             }
               ///////////////////////////////////////////////////
                    List<Packet> tmp_packets = security.TransferIncoming();
                    if (tmp_packets != null)
                    {
                        packets.AddRange(tmp_packets);
                    }
                     if (packets.Count>0)
	                {
                       RX_login()                  
	                }
                   Thread.Sleep(10);    
           }

          }

 private void RX_login()                    \\\\\ contain the received packets and switch the opcode to find the response  
        {
            foreach (Packet packet in packets)
              {   
                switch (packet.Opcode)
                {
                    case 0x2001:
                        string gate = packet.ReadAscii();
                        if (gate =="GatewayServer")
                        {
                            TX_response(0x6100,true,false);
                            //GUI.Debugmsg("6100 !");
                        }
                        if (gate=="AgentServer")
	                     {
                             TX_response(0x6103, true, false);
                             GUI.Debugmsg("Redirected !");
	                     }
                    break;
                  
                    case 0xA102:
                    byte result = packet.ReadUInt8();
                    if (result==0x01)
                    {
                         ID = packet.ReadUInt32();
                         IP = packet.ReadAscii();
                         port = packet.ReadUInt16();
                       IPAdress _ip = IPAdress.parse(IP
                         s.connect(_ip,port);
                        GUI.Debugmsg("0XA102");
                    }
                    else
                    {
                        GUI.Debugmsg("Failed to Login !");
                    }
                    break;
                   
            }
            DoSend();
        }

 private void TX_response(ushort opcode,bool Encrypt,bool Massive)     ////// contain the response function to the received Packets 
        {
            Packet response = new Packet(opcode, Encrypt, Massive);

            switch (opcode)
            {
                case 0x6100 :
                     response.WriteUInt8(0x16);
                     response.WriteAscii("SR_Client");
                     response.WriteUInt32(0x2E01);
                    break;
               
                
                case 0x6103 :
                response.WriteUInt32(ID);
                response.WriteAscii(Globals.username);
                response.WriteAscii(Globals.Password);
                response.WriteUInt8(0x16);
                response.WriteUInt32(0);
                response.WriteUInt16(0);
                break;
            }

            security.Send(response);
        }

private void DoSend()                \\\\\\\\\\\ Transfer the packets from Silkroadsecurity APi to send it
        {
            List<KeyValuePair<TransferBuffer, Packet>> tmp_buffers = security.TransferOutgoing();
            if (tmp_buffers != null)
            {
                foreach (var kvp in tmp_buffers)
                {
                    TransferBuffer buffer = kvp.Key;
                    Packet packet = kvp.Value;

                    SocketError err = SocketError.Success;

                    while (buffer.Offset != buffer.Size)
                    {
                        byte[] packet_bytes = packet.GetBytes();
                       
                       
                        int sent = s.Send(buffer.Buffer, buffer.Offset, buffer.Size - buffer.Offset, SocketFlags.None, out err);
                        if (err != SocketError.Success)
                        {
                            if (err != SocketError.WouldBlock)
                            {
                                // error ///////////////////
                                break;
                            }
                        }
                        buffer.Offset += sent;
                        Thread.Sleep(10);
                        
                    }
                }
06/07/2015 09:42 theking200051#4
i Found the solution .
to redirect u must create a new socket not change the old one.
request to Close .
06/07/2015 19:39 Nectix#5
#closed