Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Https requests via 2 socks5 proxies

novice

New Coder
I am trying to achieve an experimental setup in nodejs as illustrated below:

https-server --> local socks5 proxy (Tor) --> my external socks5 proxy --> webserver.

The existing https-server was written in nodejs and it intercepts requests from the clients firefox browser, modifies the headers, and uses the request module to fetch the requested url provided by the client.
I would like the https request to tunnel through Tor, then through my external proxy (necessary for data collection for my experiments), and then to the webserver.

I found that the socks module has a feature for chaining proxies but it has no Agent to send the https request. The code below works with the chain of proxies and with http and not https.

JavaScript:
const SocksClient = require('socks').SocksClient;

const options = {
  destination: {
    host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
    port: 80
  },
  command: 'connect', // Only the connect command is supported when chaining proxies.
  proxies: [ // The chain order is the order in the proxies array, meaning the last proxy will establish a connection to the destination.
    {
      ipaddress: '127.0.0.1', // ipv4, ipv6, or hostname
      port: 9050,
      type: 5
    },
    {
      ipaddress: 'my external proxy ip', // ipv4, ipv6, or hostname
      port: 1080,
      type: 5
    }
  ]
};


var socket = SocksClient.createConnectionChain(options)
  .then(info => {
    //console.log(info.socket);
    console.log(info.socket.remoteAddress);
    info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
    info.socket.on('data', (data) => {
      console.log(data.toString()); // ip-api.com sees that the last proxy in the chain is connected to it.   
    });
  })
  .catch(err => {
    // Handle errors
    console.log(err);
  });

Also, there is a module called socks5-https-client that is able to send https requests over a single socks5 proxy (see code below). It uses the request module and the socks5-https-client as an agent, a solution I would prefer. Unfortunately, it does not support proxy chaining (more than 1 proxy).

JavaScript:
var Agent = require('socks5-https-client/lib/Agent');

request({
    url: 'https://encrypted.google.com/',
    strictSSL: true,
    agentClass: Agent,
    agentOptions: {
        socksHost: 'my-tor-proxy-host', // Defaults to 'localhost'.
        socksPort: 9050, // Defaults to 1080.

        // Optional credentials that I don't need
        //socksUsername: 'proxyuser',
        //socksPassword: 'p@ssw0rd',
    }
}, function(err, res) {
    console.log(err || res.body);
});

I am a beginner in node programming but these are my general thoughts to a solution. An https agent can be added to the existing socks module -- the module already supports chaining of proxies -- such that it can be parsed to the request module as an agent, just like in the second code. Alternatively, the socks5-https-client can be modified to have a function like the createConnectionChain() in the socks module to support multiple proxies.

I will be grateful if anyone can assist me with a solution or materials that I can follow to get this done. Thanks
 
I am trying to achieve an experimental setup in nodejs as illustrated below:

https-server --> local socks5 proxy (Tor) --> my external socks5 proxy --> webserver.

The existing https-server was written in nodejs and it intercepts requests from the clients firefox browser, modifies the headers, and uses the request module to fetch the requested url provided by the client.
I would like the https request to tunnel through Tor, then through my external proxy (necessary for data collection for my experiments), and then to the webserver.

I found that the socks module has a feature for chaining proxies but it has no Agent to send the https request. The code below works with the chain of proxies and with http and not https.

JavaScript:
const SocksClient = require('socks').SocksClient;

const options = {
  destination: {
    host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
    port: 80
  },
  command: 'connect', // Only the connect command is supported when chaining proxies.
  proxies: [ // The chain order is the order in the proxies array, meaning the last proxy will establish a connection to the destination.
    {
      ipaddress: '127.0.0.1', // ipv4, ipv6, or hostname
      port: 9050,
      type: 5
    },
    {
      ipaddress: 'my external proxy ip', // ipv4, ipv6, or hostname
      port: 1080,
      type: 5
    }
  ]
};


var socket = SocksClient.createConnectionChain(options)
  .then(info => {
    //console.log(info.socket);
    console.log(info.socket.remoteAddress);
    info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
    info.socket.on('data', (data) => {
      console.log(data.toString()); // ip-api.com sees that the last proxy in the chain is connected to it.  
    });
  })
  .catch(err => {
    // Handle errors
    console.log(err);
  });

Also, there is a module called socks5-https-client that is able to send https requests over a single socks5 proxy (see code below). It uses the request module and the socks5-https-client as an agent, a solution I would prefer. Unfortunately, it does not support proxy chaining (more than 1 proxy).

JavaScript:
var Agent = require('socks5-https-client/lib/Agent');

request({
    url: 'https://encrypted.google.com/',
    strictSSL: true,
    agentClass: Agent,
    agentOptions: {
        socksHost: 'my-tor-proxy-host', // Defaults to 'localhost'.
        socksPort: 9050, // Defaults to 1080.

        // Optional credentials that I don't need
        //socksUsername: 'proxyuser',
        //socksPassword: 'p@ssw0rd',
    }
}, function(err, res) {
    console.log(err || res.body);
});

I am a beginner in node programming but these are my general thoughts to a solution. An https agent can be added to the existing socks module -- the module already supports chaining of proxies -- such that it can be parsed to the request module as an agent, just like in the second code. Alternatively, the socks5-https-client can be modified to have a function like the createConnectionChain() in the socks module to support multiple proxies.

I will be grateful if anyone can assist me with a solution or materials that I can follow to get this done. Thanks
Hi there, if I may ask, what was the inspiration for this project?
 
I am trying to research end-to-end flow correlation attacks against Tor and possible defences. Therefore, I have to record traffic before entering the Tor network and after exiting the Tor network. Without any defence in place, I use tcpdump to capture the traffic on the client machine with source or destination localhost:9050 (where the Tor onion proxy listens). Thus, I capture the traffic before entering Tor on the client. I also use proxychains version 4 software to tunnel all firefox traffic through Tor first and then my external proxy with a strict chain configuration. This ensures that the Tor exit node tunnels the traffic through my external proxy to the webserver. There also, I record the traffic with tcpdump.

Now , there is a defence strategy that tries to modify the HTTP requests made by the client in order to obfuscate the traffic. This defence is written in nodejs as an HTTPS proxy. The HTTPS proxy intercepts the traffic and modify the headers before sending out new requests. Therefore, proxychains software does not work like it did for non-defence. Here, I must create a tunnel between the HTTPS server and the destination webserver to send all traffic through Tor and then my external proxy. That way, I will be able to record traffic on both the client (before entering Tor) and on my external proxy (after exiting Tor). I hope this is clear
 
I am trying to research end-to-end flow correlation attacks against Tor and possible defences. Therefore, I have to record traffic before entering the Tor network and after exiting the Tor network. Without any defence in place, I use tcpdump to capture the traffic on the client machine with source or destination localhost:9050 (where the Tor onion proxy listens). Thus, I capture the traffic before entering Tor on the client. I also use proxychains version 4 software to tunnel all firefox traffic through Tor first and then my external proxy with a strict chain configuration. This ensures that the Tor exit node tunnels the traffic through my external proxy to the webserver. There also, I record the traffic with tcpdump.

Now , there is a defence strategy that tries to modify the HTTP requests made by the client in order to obfuscate the traffic. This defence is written in nodejs as an HTTPS proxy. The HTTPS proxy intercepts the traffic and modify the headers before sending out new requests. Therefore, proxychains software does not work like it did for non-defence. Here, I must create a tunnel between the HTTPS server and the destination webserver to send all traffic through Tor and then my external proxy. That way, I will be able to record traffic on both the client (before entering Tor) and on my external proxy (after exiting Tor). I hope this is clear
I figured you were going down that route lol. Just trynna gauge where you're at. Although I have a feeling you have good intentions with your research, something this on a forum like this will get you flagged, and anyone with malicious intent may take the opportunity to be a dum-dum and decide to go down the skiddie path. If you are interested in learning more about the ethical side and development side of things feel free to reach out and we can take things on a more professional level. Wouldn't want you to get flagged as a potential skid
 
I figured you were going down that route lol. Just trynna gauge where you're at. Although I have a feeling you have good intentions with your research, something this on a forum like this will get you flagged, and anyone with malicious intent may take the opportunity to be a dum-dum and decide to go down the skiddie path. If you are interested in learning more about the ethical side and development side of things feel free to reach out and we can take things on a more professional level. Wouldn't want you to get flagged as a potential skid
Thank you. I can provide email for private chat if you know more about this and can help me
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom