SRL Archive - Other Scripts

SOCKS5 proxy library


Yakman

2009-05-03 01:37:20

scar socks5 proxy library

iv used it to load google through tor, and to connect to irc through tor



{
scar socks5 proxy library by yakman

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}

{
read rfc1928 and rfc1929
}

type
RemoteAddress = record
ipv4: Boolean;
host: string; //used if ipv4=false
ip: array[0..3] of byte; //used it ipv4=true
end;

SocketBuffer = record
fd: Integer;
pos, len: Integer;
buf: string;
end;

Socks5Auth = record
UseAuth: Boolean;
user, pass: string;
end;

//have to make a simple buffering interface here..
procedure StartSocketByteBuffer(var buffer: SocketBuffer; fd: integer);
begin
buffer.fd:= fd;
buffer.pos:= 1;
end;

function SocketRead(var buffer: SocketBuffer): Char;
var
b: string;
begin
if(buffer.pos >= buffer.len+1)then
begin
ReadConnectionData(buffer.fd, b);
buffer.buf:= b;
buffer.pos:= 1;
buffer.len:= Length(buffer.buf);
end;
Result:= buffer.buf[buffer.pos];
buffer.pos:= buffer.pos + 1;
end;

{
Start a socks5 connection

fd - socket handle
addr - address the socks5 server should connect to
when this function returns, the values in the record
will be filled with the addr that the socket
on the socks5 server is bounded to.
port - port the socks5 server should connect to
when this function returns, it will be filled with
the port that the socket on the socks5 server is
bounded to.
auth - username/password to be offered, if any

return true on success
}
function InitSocks5(fd: Integer; var addr: RemoteAddress; var port: Integer; var auth: Socks5Auth): Boolean;
var
p: string;
buffer: SocketBuffer;
c: char;
f: Integer;
begin
Result:= True;
StartSocketByteBuffer(buffer, fd);

//send handshake
p:= #05; //version
if(auth.UseAuth)then
p:= p
+ #02 //number of methods
+ #00 //method no auth
+ #02 //username/password
else
p:= p
+ #01 //number of methods
+ #00; //method no auth
SendConnectionData(fd, p);

//read version
c:= SocketRead(buffer);
if(c <> #05)then
begin
Result:= False;
Writeln('Socks wrong version: ' + inttostr(ord(c)));
Exit;
end;

//read method
c:= SocketRead(buffer);
if(auth.UseAuth)then
begin
if(c <> #00)and(c <> #02)then
begin
Result:= False;
Writeln('Socks wrong method: ' + inttostr(ord(c)));
Exit;
end;

if(c = #02)then //user/pass auth
begin
p:= #05
+ Chr(Length(auth.user) and $ff) + auth.user
+ Chr(Length(auth.pass) and $ff) + auth.pass;
SendConnectionData(fd, p);

SocketRead(buffer); //reading version again
if(SocketRead(buffer) <> #00)then //status
begin
Result:= False;
writeln('Failed auth');
Exit;
end;
end;
end
else
begin
if(c <> #00)then
begin
Result:= False;
Writeln('Socks wrong method: ' + inttostr(ord(c)));
Exit;
end;
end;

//send request
p:= #05 + //version
#01 + //cmd connect
#00;//reserved
if(addr.ipv4)then
begin
p:= p + #01 + //ipv4
Chr(addr.ip[0]) + Chr(addr.ip[1]) + Chr(addr.ip[2]) + Chr(addr.ip[3]);
end
else
begin
p:= p + #03 + //domain name
Chr(Length(addr.host) and $ff) + addr.host;
end;

//add port
p:= p + Chr((port shr 8) and $ff) + Chr(port and $ff);
SendConnectionData(fd, p);


//reading reply
SocketRead(buffer); //version
c:= SocketRead(buffer);
if(c <> #00)then
begin
Result:= False;
Writeln('Socks reply not equal to success: ' + inttostr(ord(c)));
Exit;
end;
SocketRead(buffer); //reserved
c:= SocketRead(buffer); //address type
case c of
#01: begin //ipv4 address
addr.ipv4:= True;
for f:=0 to 3 do
addr.ip[f]:= Ord(SocketRead(buffer)); //read ipv4 addr
end;
#03: begin //domain name
addr.ipv4:= False;
addr.host:= '';
c:= SocketRead(buffer);
for f:=1 to ord(c) do
addr.host:= addr.host + SocketRead(buffer);
end;
#04: begin //ipv6
//writeln('warning - ipv6 not implemented');
for f:=1 to 16 do
SocketRead(buffer); //read ipv6 addr
end;
end;

port:= (Ord(SocketRead(buffer)) shl 8) or (Ord(SocketRead(buffer)));
end;


procedure LoadGoogle;
var
fd: Integer;
s: string;
addr: RemoteAddress;
port: Integer;
auth: Socks5Auth;
begin
writeln('connecting');
fd:= OpenConnection('localhost', 9050, 10000);
if(fd < 0)then
begin
writeln('OpenConnection()');
Exit;
end;

writeln('negociating proxy server');
{
addr.ipv4:= True;
addr.ip[0]:= 209;
addr.ip[1]:= 85;
addr.ip[2]:= 229;
addr.ip[3]:= 147;
}
addr.ipv4:= False;
addr.host:= 'www.google.co.uk';
port:= 80;

auth.UseAuth:= True;
auth.user:= 'socks5user';
auth.pass:= 'socks5pass';
if(not InitSocks5(fd, addr, port, auth))then
begin
FreeConnection(fd);
writeln('error starting socks');
Exit;
end;
if(addr.ipv4)then
s:= Inttostr(addr.ip[0]) + '.'
+ Inttostr(addr.ip[1]) + '.'
+ Inttostr(addr.ip[2]) + '.'
+ Inttostr(addr.ip[3])
else
s:= addr.host;
writeln('remote socket bound to ' + s + ':' + Inttostr(port));

SendConnectionData(fd,
'GET / HTTP/1.1' + #13 + #10 +
'Accept: */*' + #13 + #10 +
'User-Agent: scar' + #13 + #10 +
'Host: [url]www.google.co.uk[/url]' + #13 + #10 +
'Connection: close' + #13 + #10 +
#13 + #10
);

writeln('waiting for responce');
repeat
ReadConnectionData(fd, s);
Writeln(s);
Sleep(50);
until(s = '');

writeln('end');
FreeConnection(fd);
end;

begin
LoadGoogle;
end.

Harry

2009-05-03 04:26:58

Line 188: [Error] (188:15): Type mismatch in script

Used SCAR 3.15b now, worked.


Successfully compiled (78 ms)
connecting
negociating proxy server
remote socket bound to 0.0.0.0:0
waiting for responce
HTTP/1.1 302 Found
Location: [url]http://www.google.es/[/url]
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=8399bdc668b70999:TM=1241311069:LM=1241311069:S=h6A96JV_fadpFalv; expires=Tue, 03-May-2011 00:37:49 GMT; path=/; domain=.google.com
Date: Sun, 03 May 2009 00:37:49 GMT
Server: gws
Content-Length: 218
Connection: close


<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.es/">here</A>.
</BODY></HTML>


end
Successfully executed

Naice.

Yakman

2009-05-03 04:33:40

its cause you used scar 3.20 and freddy fails

R0b0t1

2009-05-03 04:39:51

Fail troll is fail.

mixster

2009-05-03 13:45:04

You rocks my socks (please don't hurt me).
As always, a useful piece of random scripting - nice commenting, clear code and more!
How would we get by without you :)

Shuttleu

2009-05-03 13:51:42

Scar 3.15b
Successfully compiled (53 ms)
connecting
OpenConnection()
Successfully executed
:(

~shut

EDIT: Instructions please?

ShowerThoughts

2009-05-03 14:26:37

From: Yakman
its cause you used scar 3.20 and freddy fails


/facepalm, Scar 3.15b is also made by Freddy1990, ASAP you should shut up and only say freddy fails if you have uninstalled all your program from Freddy1990 from your pc.

Freddy1990 > Yakman
Mature > Immature.

If you give a statement give arguments.

Nice code.

~Hermpie

kingarabian

2009-05-03 14:47:41

From: Hermpie
/facepalm, Scar 3.15b is also made by Freddy1990, ASAP you should shut up and only say freddy fails, if you have uninstalled all your program from Freddy1990 from your pc.

Freddy1990 > Yakman
Mature > Immature.

If you give a statement give arguments.

Nice code.

~Hermpie


Calm down. Im sure he doesnt mean it personally. But I agree that 3.15b > then Scar 3.20.

Yakman

2009-05-03 19:59:29

Shuttleu, you must point the scar script to a socks5 proxy server.

by default, it points to localhost:9050 which is used when you have tor running.
you can change it in the procedure LoadGoogle;


From: Hermpie
/facepalm, Scar 3.15b is also made by Freddy1990, ASAP you should shut up and only say freddy fails, if you have uninstalled all your program from Freddy1990 from your pc.

Freddy1990 > Yakman
Mature > Immature.

If you give a statement give arguments.

Nice code.

~Hermpie


i was refering to how freddy didnt keep backward compatability in this case, although maybe i should be a bit easier on him, this just sounds like a bug.

ShowerThoughts

2009-05-03 20:36:45

You said something about SCAR 3.20 then you said Freddy fails.
Sounds flaming to me.

I want to ask you something nicely do you please want to stop thinking and doing like the world is black white.
It's like you always think black or white, I like you but your always like I am right and you're not, IDC the rest.

Good luck anyways ;)

~Hermpie

Shuttleu

2009-05-04 00:51:09

now i get this
Successfully compiled (47 ms)
connecting
negociating proxy server
Failed auth
error starting socks
Successfully executed
idk what it does but i wanna see but i cant seem to get it to work :/

~shut

EDIT: never mind i got it to work
Successfully compiled (62 ms)
connecting
negociating proxy server
remote socket bound to 81.20.129.28:2333
waiting for responce
HTTP/1.1 302 Found
Location: http://www.google.de/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=869cec4ae7cf3876:TM=1241384037:LM=1241384037:S=ubiCdXscFGQiOMm0; expires=Tue, 03-May-2011 20:53:57 GMT; path=/; domain=.google.com
Date: Sun, 03 May 2009 20:53:57 GMT
Server: gws
Content-Length: 218
Connection: close

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.de/">here</A>.
</BODY></HTML>


end
Successfully executed
so what does it acctually do?

Harry

2009-05-04 01:00:37

It is just a library for loading a random socks5 proxy, I think. So you can use it for other projects, etc.

Smartzkid

2009-05-04 01:42:40

IE getting around the RS account creation time limit :)

Nice code, yak! Thanks!