首页 > CShap > Switch TOR to a new identity

Switch TOR to a new identity

2009年9月23日 发表评论 阅读评论

在程序中使用Tor 的一个关键问题就是如何在程序中强制Tor 使用新的身份(切换代理),如果不能在程序中更换身份那使用Tor 的意义也就不大了。
Tor 默认HTTP代理端口是8118,Socket代理端口是9050,控制端口是9051。我们可以通过向Tor 控制端口发送命令来强制Tor 使用新的身份。
下面有三段代码,分别由shell、php和c#实现切换Tor 使用新的身份。

shell代码片段:

spawn telnet 127.0.0.1 9051
expect "Escape character is'^]'."
send "AUTHENTICATE\r"
expect "250 OK"
send "signal NEWNYM\r"
expect "250 OK"
send "quit\r"

php版本:


tor_new_identity() takes three optional parameters –

o $tor_ip – The IP address of the TOR server you want to access. Defaults to 127.0.0.1
o $control_port – The TOR control port. Defaults to 9051.
o $auth_code – The authentication code. Defaults to an empty string. See the next section for more details on this parameter.
The function returns TRUE if it successfuly sent the “new identity” command, FALSE otherwise. If your TOR proxy doesn’t require authentication you can force a new identity very easily :

 if (tor_new_identity('127.0.0.01', '9051')) {
	echo "Identity switched!";
}

c#版本代码片段:

TcpClient client = new TcpClient();
client.Connect("127.0.0.1", Convert.ToInt32(9051));
NetworkStream stream = client.GetStream();
byte[] cmd = null;
string strCmd = string.Format("AUTHENTICATE \"{0}\"\r\n",password);
cmd = System.Text.Encoding.Default.GetBytes(strCmd);
stream.Write(cmd, 0, cmd.Length);
byte[] buffer = new byte[2];
int count = 0;
string strResponse = "";
count = stream.Read(buffer, 0, buffer.Length);
strResponse += System.Text.Encoding.Default.GetString(buffer, 0, count);
while (count != 0 && stream.DataAvailable)
{
count = stream.Read(buffer, 0, buffer.Length);
strResponse += System.Text.Encoding.Default.GetString(buffer, 0, count);
if (count < buffer.Length)
break;
}
if (strResponse.IndexOf("250") == -1)
{
MessageBox.Show("AUTHENTICATE Fail!");
return;
}
buffer = new byte[1024];
strCmd = "signal NEWNYM\r\n";
cmd = System.Text.Encoding.Default.GetBytes(strCmd);
stream.Write(cmd, 0, cmd.Length);
strResponse = "";
count = stream.Read(buffer, 0, buffer.Length);
strResponse += System.Text.Encoding.Default.GetString(buffer, 0, count);
while (count != 0 && stream.DataAvailable)
{
count = stream.Read(buffer, 0, buffer.Length);
strResponse += System.Text.Encoding.Default.GetString(buffer, 0, count);
if (count < buffer.Length)
break;
}
if (strResponse.IndexOf("250") == 1)
MessageBox.Show("Switch Identity OK!");

C#完整测试代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.IO;

namespace TORDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.ip-number.com/index.asp");

            WebProxy proxy = new WebProxy("127.0.0.1",8118);
            req.Proxy = proxy;
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            using (Stream stream = res.GetResponseStream())
            {
                StreamReader sr = new StreamReader(stream);
                string str = sr.ReadToEnd();
                sr.Close();

                string pattern = @".*(?((0|1[0-9]{0,2}|2[0-9]{0,1}|2[0-4][0-9]|25[0-5]|[3-9][0-9]{0,1})\.){3}(0|1[0-9]{0,2}|2[0-9]{0,1}|2[0-4][0-9]|25[0-5]|[3-9][0-9]{0,1})).*";
                Regex reg = new Regex(pattern);
                if(reg.IsMatch(str))
                {
                    Match m = reg.Match(str);
                    MessageBox.Show(m.Value);
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                TcpClient client = new TcpClient();
                client.Connect("127.0.0.1", Convert.ToInt32(tbPort.Text));
                NetworkStream stream = client.GetStream();

                byte[] cmd = null;
                string strCmd = string.Format("AUTHENTICATE \"{0}\"\r\n",tbPwd.Text);
                cmd = System.Text.Encoding.Default.GetBytes(strCmd);

                stream.Write(cmd, 0, cmd.Length);

                byte[] buffer = new byte[2];
                int count = 0;
                string strResponse = "";
                count = stream.Read(buffer, 0, buffer.Length);
                strResponse += System.Text.Encoding.Default.GetString(buffer, 0, count);
                while (count != 0 && stream.DataAvailable)
                {
                    count = stream.Read(buffer, 0, buffer.Length);
                    strResponse += System.Text.Encoding.Default.GetString(buffer, 0, count);
                    if (count < buffer.Length)
                    {
                        break;
                    }
                }
                if (strResponse.IndexOf("250") == -1)
                {
                    MessageBox.Show("AUTHENTICATE Fail!");
                    return;
                }

                buffer = new byte[1024];
                strCmd = "signal NEWNYM\r\n";
                cmd = System.Text.Encoding.Default.GetBytes(strCmd);
                stream.Write(cmd, 0, cmd.Length);

                strResponse = "";
                count = stream.Read(buffer, 0, buffer.Length);
                strResponse += System.Text.Encoding.Default.GetString(buffer, 0, count);
                while (count != 0 && stream.DataAvailable)
                {
                    count = stream.Read(buffer, 0, buffer.Length);
                    strResponse += System.Text.Encoding.Default.GetString(buffer, 0, count);
                    if (count < buffer.Length)
                    {
                        break;
                    }
                }
                if (strResponse.IndexOf("250") == -1)
                {
                    MessageBox.Show("Switch Identity Fail!");
                    return;
                }
                else
                {
                    MessageBox.Show("Switch OK!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("error:" + ex.Message);
            }
        }
    }

参考文章地址:
http:// linux.chinaunix.net/bbs/thread-1014409-1-1.html
http:// w-shadow.com/blog/2008/06/20/tor-how-to-new-identity-with-php
http:// www.cnblogs.com/sxlfybb/archive/2008/07/07/1237662.html

分类: CShap 标签: ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.

注意: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。例如, ABC是本文的评论者之一,则使用'@ABC '(不包括单引号)将会自动将您的评论发送给ABC。使用'@all ',将会将评论发送给之前所有其它评论者。请务必注意user必须和评论者名相匹配(大小写一致)。直接点击评论上方的回复实现此功能