Posts Tagged - http

Java Damn Simple HTTP Proxy

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HttpProxy {

  private static byte[] CONNECTION_ESTABLISHED =
      "HTTP/1.0 200 Connection established\r\nProxy-Agent: HTTP Proxy/1.0\r\n\r\n".getBytes();

  public static void main(String[] args) throws IOException {
    ExecutorService threadPool = Executors.newCachedThreadPool();
    ServerSocket serverSocket = new ServerSocket(8080);
    while (true) {
      threadPool.submit(new UpstramHandler(serverSocket.accept()));
    }
  }

  static class UpstramHandler implements Runnable {

    private final Socket socket;
    private final ExecutorService executorService;

    UpstramHandler(Socket socket) {
      this.socket = socket;
      this.executorService = Executors.newSingleThreadExecutor();
    }

    @Override
    public void run() {
      byte[] buffer = new byte[2048];
      while (true) {
        try {
          Socket remoteSocket = null;
          int recv = 1;
          while (recv > 0) {
            recv = socket.getInputStream().read(buffer);
            if (recv > 0) {
              if (recv > 8) {
                String s = new String(buffer, 0, 8);
                if (s.contains("CONNECT")) {
                  int endlIndx = 0;
                  for (int i = 8; i < recv - 1; i++) {
                    if (buffer[i] == '\r' && buffer[i + 1] == '\n') {
                      endlIndx = i + 1;
                      break;
                    }
                  }
                  if (endlIndx > 8) {
                    String request = new String(buffer, 0, endlIndx + 1);
                    int connectIndex = request.indexOf("CONNECT");
                    int startIndex = request.indexOf(" ", connectIndex) + 1;
                    int endIndex = request.indexOf(" ", startIndex);
                    String[] hostnameAndPort;
                    if (endIndex < startIndex) {
                      hostnameAndPort = request.substring(startIndex).split(":");
                    } else {
                      hostnameAndPort = request.substring(startIndex, endIndex).split(":");
                    }
                    String hostname = hostnameAndPort[0];
                    int port = Integer.parseInt(hostnameAndPort[1]);
                    remoteSocket = new Socket(hostname, port);
                    socket.getOutputStream().write(CONNECTION_ESTABLISHED);
                    socket.getOutputStream().flush();
                    executorService.submit(new DownstreamHandler(socket, remoteSocket));
                    continue;
                  }
                }
              }
              if (remoteSocket != null) {
                remoteSocket.getOutputStream().write(buffer, 0, recv);
                remoteSocket.getOutputStream().flush();
              }
            }
          }
        } catch (IOException e) {
          System.err.println(e);
        }
      }
    }
  }

  static class DownstreamHandler implements Runnable {

    private final Socket socket;
    private final Socket remoteSocket;

    DownstreamHandler(Socket socket, Socket remoteSocket) {
      this.socket = socket;
      this.remoteSocket = remoteSocket;
    }

    @Override
    public void run() {
      byte[] buffer = new byte[2048];
      int recv = 1;
      while (recv > 0) {
        try {
          recv = remoteSocket.getInputStream().read(buffer);
          if (recv > 0 && remoteSocket != null) {
            socket.getOutputStream().write(buffer, 0, recv);
            socket.getOutputStream().flush();
          }
        } catch (IOException e) {
          System.err.println(e);
        }
      }
    }
  }
}

Read More

SSH tunneling over HTTP Proxy

sudo apt update && sudo apt install gcc

sudo vim http-injector-unix-client.c

/*
MIT License

Copyright (c) [2020] [Ardika Rommy Sanjaya]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stddef.h>

#if (__STDC_VERSION__ >= 199901L)
#include <stdint.h>
#endif

// https://creativeandcritical.net/str-replace-c
char *repl_str(const char *str, const char *from, const char *to) {

	/* Adjust each of the below values to suit your needs. */

	/* Increment positions cache size initially by this number. */
	size_t cache_sz_inc = 16;
	/* Thereafter, each time capacity needs to be increased,
	 * multiply the increment by this factor. */
	const size_t cache_sz_inc_factor = 3;
	/* But never increment capacity by more than this number. */
	const size_t cache_sz_inc_max = 1048576;

	char *pret, *ret = NULL;
	const char *pstr2, *pstr = str;
	size_t i, count = 0;
	#if (__STDC_VERSION__ >= 199901L)
	uintptr_t *pos_cache_tmp, *pos_cache = NULL;
	#else
	ptrdiff_t *pos_cache_tmp, *pos_cache = NULL;
	#endif
	size_t cache_sz = 0;
	size_t cpylen, orglen, retlen, tolen, fromlen = strlen(from);

	/* Find all matches and cache their positions. */
	while ((pstr2 = strstr(pstr, from)) != NULL) {
		count++;

		/* Increase the cache size when necessary. */
		if (cache_sz < count) {
			cache_sz += cache_sz_inc;
			pos_cache_tmp = realloc(pos_cache, sizeof(*pos_cache) * cache_sz);
			if (pos_cache_tmp == NULL) {
				goto end_repl_str;
			} else pos_cache = pos_cache_tmp;
			cache_sz_inc *= cache_sz_inc_factor;
			if (cache_sz_inc > cache_sz_inc_max) {
				cache_sz_inc = cache_sz_inc_max;
			}
		}

		pos_cache[count-1] = pstr2 - str;
		pstr = pstr2 + fromlen;
	}

	orglen = pstr - str + strlen(pstr);

	/* Allocate memory for the post-replacement string. */
	if (count > 0) {
		tolen = strlen(to);
		retlen = orglen + (tolen - fromlen) * count;
	} else	retlen = orglen;
		ret = malloc(retlen + 1);
	if (ret == NULL) {
		goto end_repl_str;
	}

	if (count == 0) {
		/* If no matches, then just duplicate the string. */
		strcpy(ret, str);
	} else {
		/* Otherwise, duplicate the string whilst performing
		 * the replacements using the position cache. */
		pret = ret;
		memcpy(pret, str, pos_cache[0]);
		pret += pos_cache[0];
		for (i = 0; i < count; i++) {
			memcpy(pret, to, tolen);
			pret += tolen;
			pstr = str + pos_cache[i] + fromlen;
			cpylen = (i == count-1 ? orglen : pos_cache[i+1]) - pos_cache[i] - fromlen;
			memcpy(pret, pstr, cpylen);
			pret += cpylen;
		}
		ret[retlen] = '\0';
	}

end_repl_str:
	/* Free the cache and return the post-replacement string,
	 * which will be NULL in the event of an error. */
	free(pos_cache);
	return ret;
}

/* connecting to http proxy */

void printHelp() {
	printf("ssh [email protected] -o ProxyCommand=\"./http-injector-client -x 192.168.43.172:44533 -P \'CONNECT 103.129.220.168:22 HTTP/1.1[crlf*2]\'\"\n\n");
	printf("-x: HTTP Proxy.\n");
	printf("-P: HTTP Payload.\n");
	printf("-s: Buffer size (Optional, default: 1023).\n");
}

void *reader(void *args) {
	int fd = *(int *) args;
	char ch;
	while (1) {
    		fread(&ch, 1, 1, stdin);
		send(fd, (void *) &ch, 1, 0);
	}
}

void *writer(void *args) {
	int fd = *(int *) args;
	char ch;
	while (1) {
		recv(fd, (void *) &ch, 1, 0);
		fwrite(&ch, 1, 1, stdout);
		fflush(stdout);
	}
}


int main(int argc, char* argv[]) {

	int fd;
	struct sockaddr_in remote; 

	char *proxy_host = NULL;
	int proxy_port = -1;

	char *payload = NULL;

	int buf_size = 1024;
	int opt;
  	while ((opt = getopt (argc, argv, "x:h:s:P:")) != -1) {
		switch (opt) {
			case 'x':
				proxy_host = strtok(optarg, ":");
				if (proxy_host != NULL) {
					proxy_port = atoi(strtok(NULL, ":"));
				}
				break;
			case 'h':
				printHelp();
				break;
			case 's':
				buf_size = atoi(optarg);
				break;
			case 'P':
				payload = optarg;
				payload = repl_str(payload, "[cr]", "\r");
				payload = repl_str(payload, "[lf]", "\n");
				payload = repl_str(payload, "[crlf]", "\r\n");
				payload = repl_str(payload, "[crlf*2]", "\r\n\r\n");
				payload = repl_str(payload, "[lfcr]", "\n\r");
				break;
		}
	}
	
	if (proxy_host == NULL || proxy_port < 0 || payload == NULL) {
		printHelp();
		exit(-1);
	}

	char buffer[buf_size];
	int sent, read = 0;
	pthread_t worker;


	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		printf("\nFailed to open socket file descriptor.\n");
		return -1;
	}

	remote.sin_family = AF_INET;
	remote.sin_port = htons(proxy_port);

	if (inet_pton(AF_INET, proxy_host, &remote.sin_addr) <= 0) {
        	printf("\nInvalid address/Address not supported.\n");
		close(fd);
        	return -1;
    	}

	if (connect(fd, (struct sockaddr *) &remote, sizeof(remote)) < 0) {
        	printf("\nConnection Failed.\n");
		close(fd);
        	return -1;
	}

	if ((sent = send(fd, payload, strlen(payload), 0)) < 0) {
		printf("\nFailed to send HTTP payload.\n");
		close(fd);
		return -1;
	}

	memset(buffer, '\0', buf_size);
	while (read == 0) {
		read = recv(fd, buffer, buf_size, 0);
		for (int i = 0; i < read; i++) {
			if (buffer[read - 4] == '\r' && buffer[read - 3] == '\n' && buffer[read - 2] == '\r' && buffer[read - 1] == '\n') {
				read == 0;
				break;
			}
		}
	}

	pthread_create(&worker, NULL, reader, (void*) &fd); 
	writer((void*) &fd);

	close(fd);
	return 0;
}

gcc http-injector-unix-client.c -o http-injector-client -lpthread

ssh [email protected] -o "ProxyCommand=./http-injector-client -x 192.168.43.173:44355 -P 'CONNECT 103.129.220.168:22 HTTP/1.1[crlf*2]'"

*) Parameters

  • -x: HTTP Proxy

  • -P: HTTP request payload (Request connection to ssh server)

  • -s: Buffer size (Optional), default value is 1024.

Read More