Search code examples
javascripthtmlbrowserwebrtc

Port Knocking UDP with Javascript in Browser


So I am trying to send a port knock sequence from javascript.

The TCP part is simple enough with websockets.

I've read that WebRTC is the closest thing the browser offers to sending a UDP packet... but WebRTC is a lot to digest just to attempt to send a UDP "knock".

Is it even possible to "knock" via UDP using WebRTC? If so, please provide a simple example. I am just looking for a simplistic working example. I.e. to "knock" via TCP you can use the following:

var sock = new WebSocket("ws://"+ host +":"+ port);

Solution

  • It is in fact possible to send UDP packets with the browser.

    You could either write a chrome extension (app) which would give you access to https://developer.chrome.com/apps/sockets_udp ("sockets": {...} in your manifest.json).

    Or, as far as WebRTC goes:

    var pc = new webkitRTCPeerConnection(
        { "iceServers": [{ "url": "stun:localhost:1234" }] }
    );
    
    pc.createOffer(function (sessionDescription) {
        pc.setLocalDescription(sessionDescription);
    
    }, function(error) {
        alert(error);
    }, { 'mandatory': { 'OfferToReceiveAudio': true, 'OfferToReceiveVideo': true } });
    

    And a quick ruby-script

    require 'socket'

    $port = 1234
    
    t = Thread.start do
      server = UDPSocket.open
      server.bind(nil, $port)
      4.times do
        a = server.recvfrom(12364)
        p a
        p server.send "ping", 0, a[1][2], a[1][1]
      end
    end
    
    t.join
    

    ["\x00\x01\x00\x00!\x12\xA4B0PgJcgcqalrO", ["AF_INET", 51881, "192.168.0.27", "192.168.0.27"]] 4 ["\x00\x01\x00\x00!\x12\xA4Bbmfxf2ABsZws", ["AF_INET", 53092, "192.168.0.27", "192.168.0.27"]] 4 ["\x00\x01\x00\x00!\x12\xA4B0PgJcgcqalrO", ["AF_INET", 51881, "192.168.0.27", "192.168.0.27"]] 4 ["\x00\x01\x00\x00!\x12\xA4Bbmfxf2ABsZws", ["AF_INET", 53092, "192.168.0.27", "192.168.0.27"]]

    I'm not sure though if that qualifies as "port knock sequence."

    Ref: https://www.webrtc-experiment.com/docs/webrtc-for-beginners.html