shbnc.c allows you to use a SQUID bouncer with applications which are configured to use a SOCKS proxy.
f01eb2b1304f99ba8f00fcffc17ecad885fd78033d57685382285705a434801f
/* socks->http proxy bouncer
* Wildandi <andi@void.at>
*
* Use vulnerable SQUID server (= allows CONNECT method) as an SOCKS server for your application
*
* Example: ./shbnc 0 1080 httpproxy 8080
*
* http://void.at:8080
*/
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
struct socks4 {
char vn;
char cd;
unsigned short dstport;
unsigned int dstip;
char *data;
};
int conn(int *sock, char *server, int port);
int list(char *server, int port);
int main(int argc, char **argv) {
int serversock,
clientsock,
disconnected,
greater;
fd_set fs;
struct socks4 stype, ctype;
char buffer[2048]="";
if (argc>4) {
clientsock = list(argv[1], atoi(argv[2]));
if (clientsock==-1) return 0;
disconnected = 1;
if (!conn(&serversock,argv[3], atoi(argv[4]))) {
/* Data from client */
memset(&stype, 0, sizeof(stype));
recv(clientsock, &stype, sizeof(stype), 0x4);
ctype.vn = 0;
ctype.cd = 90;
ctype.dstport = 0;
ctype.dstip = 0;
ctype.data = NULL;
send(clientsock, &ctype, sizeof(ctype), 0x4);
snprintf(buffer, sizeof(buffer), "CONNECT %s:%d HTTP/1.0\n\n", inet_ntoa(stype.dstip), ntohs(stype.dstport));
send(serversock, buffer, strlen(buffer), 0x4);
recv(serversock, buffer, sizeof(buffer), 0x4);
greater = (serversock > clientsock ? serversock : clientsock);
while (disconnected) {
FD_ZERO(&fs);
FD_SET(clientsock, &fs);
FD_SET(serversock, &fs);
select(greater + 1, &fs, NULL, NULL, NULL);
if (FD_ISSET(serversock, &fs)) {
memset(buffer, 0, sizeof(buffer));
disconnected = recv(serversock, buffer, sizeof(buffer), 0x4);
if (disconnected) send(clientsock, buffer, disconnected, 0x4);
} else if (FD_ISSET(clientsock, &fs)) {
memset(&buffer, 0, sizeof(buffer));
disconnected = recv(clientsock, buffer, sizeof(buffer), 0x4);
if (disconnected) send(serversock, buffer, disconnected, 0x4);
}
}
} else {
stype.vn = 0;
stype.cd = 91;
stype.dstport = 0;
stype.dstip = 0;
stype.data = NULL;
send(clientsock, &stype, sizeof(stype), 0x4);
}
close(clientsock);
close(serversock);
} else {
fprintf(stderr,"%s bindhost bindport httpproxy httpproxyport\n", *argv);
}
return 0;
}
int conn(int *sock, char *server, int port)
{
struct sockaddr_in s_in;
struct hostent *he;
char *ip;
if ((*sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) /* TCP */
{
fprintf(stderr,"Cant create TCP socket\n");
return -1;
}
bzero((char *)&s_in,sizeof(s_in));
s_in.sin_family=AF_INET;
s_in.sin_addr.s_addr=inet_addr(server);
s_in.sin_port=htons(port);
if ((he=gethostbyname(server))!=NULL)
bcopy(he->h_addr,(char *)&s_in.sin_addr,he->h_length);
else if ((s_in.sin_addr.s_addr=inet_addr(server))<0) return 2;
if (connect(*sock,(struct sockaddr *)&s_in,16)==-1)
{
fprintf(stderr,"Cant connect to %s:%d\n",server,port);
close(*sock);
return 3;
}
return 0;
}
int list(char *server, int port) {
struct sockaddr_in s_in;
struct hostent *he;
int sock=0, len=0;
if ((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))==-1) {
fprintf(stderr,"Cant create TCP socket\n");
return -1;
}
bzero((char *)&s_in,sizeof(s_in));
s_in.sin_family=AF_INET;
s_in.sin_addr.s_addr=inet_addr(server);
s_in.sin_port=htons(port);
if ((he=gethostbyname(server))!=NULL)
bcopy(he->h_addr,(char *)&s_in.sin_addr,he->h_length);
else if ((s_in.sin_addr.s_addr=inet_addr(server))<0) return 2;
if (bind(sock, (struct sockaddr *)&s_in, sizeof(s_in))) {
fprintf(stderr,"Cant bind\n");
return -2;
}
if (listen(sock, 1)) {
fprintf(stderr, "listen error\n");
return -3;
}
return accept(sock, (struct sockaddr *)&s_in, &len);
}