KISS

Keep It Simple Stupid

Sending HTTP POST request with netcat

| comments

I have a D-Link Wi-Fi router that sometimes loses the PPPoE connection and thus requires reconnect. The standard way is to go to the web interface, login, and press the button. But it’s a long way, especially on an android device. Wouldn’t it be much nicer and faster to simply run a script?

I’ve installed busybox with the Busybox installer, and gotten wget, although very limited, it can’t send POST requests. curl is not there. So we’ll have to do with netcat. Here’s how to login:

1
2
3
BODY="html_response_page=login_fail.asp&login_name=login&login_pass=pass&graph_id=${GRAPH_ID}&login_n=login&log_pass=pass&graph_code=&login=Login"
BODY_LEN=$( busybox echo -n ${BODY} | wc -c )
busybox echo -ne "POST /login.cgi HTTP/1.0\r\nHost: 199.188.1.99\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: ${BODY_LEN}\r\n\r\n${BODY}" | nc -i 3 199.188.1.99 80

Here, $BODY contains the form data which is sent in the POST body. We then calculate its length, because it’s required from the Content-Length HTTP header, as well as the Content-Type header. That’s what echo generates and sends to the socket:

1
2
3
4
5
6
POST /login.cgi HTTP/1.0
Host: 199.188.1.99
Content-Type: application/x-www-form-urlencoded
Content-Length: 134

html_response_page=login_fail.asp&login_name=login&login_pass=pass&graph_id=abcdef&login_n=login&log_pass=pass&graph_code=&login=Login

The -i 3 parameter to nc is important because otherwise it doesn’t wait for a response. All in all, nothing special in this script, but gives an example how to send a POST with netcat. BTW, this may not be so easy with HTTP 2.0 if it’s going to be binary.

For the sake of completeness, you can find the scripts to restart the PPPoE connection for OS X, Linux, and Android below:

OS X (rpppoe_osx.sh) download
1
2
3
4
5
#!/bin/bash

GRAPH_ID=$( curl 'http://199.188.1.99/login_auth.asp' --http1.0 | sed -nEe '/id="graph_id"/{s/.*value="([^"]+)".*/\1/;p;}' )
curl 'http://199.188.1.99/login.cgi' --http1.0 -H 'Host: 199.188.1.99' --data "html_response_page=login_fail.asp&login_name=login&login_pass=pass&graph_id=${GRAPH_ID}&login_n=login&log_pass=pass&graph_code=&login=Login"
curl --http1.0 -X POST http://199.188.1.99/pppoe_00_disconnect.cgi
Linux (rpppoe_linux.sh) download
1
2
3
4
5
6
#!/bin/bash

URL="http://199.188.1.99/"
GRAPH_ID=$( wget "${URL}login_auth.asp" -O- | sed -nE '/id="graph_id"/{s/.*value="([^"]+)".*/\1/;p;}' )
wget "${URL}login.cgi" --header='Host: 199.188.1.99' --post-data="html_response_page=login_fail.asp&login_name=login&login_pass=pass&graph_id=${GRAPH_ID}&login_n=login&log_pass=pass&graph_code=&login=Login" -O-
wget "${URL}pppoe_00_disconnect.cgi" --post-data='' -O-
Android (rpppoe_android.sh) download
1
2
3
4
5
6
7
#!/system/bin/sh
URL="http://199.188.1.99/"
GRAPH_ID=$( wget "${URL}login_auth.asp" -O- | sed -nE '/id="graph_id"/{s/.*value="([^"]+)".*/\1/; p;}' )
BODY="html_response_page=login_fail.asp&login_name=login&login_pass=pass&graph_id=${GRAPH_ID}&login_n=login&log_pass=pass&graph_code=&login=Login"
BODY_LEN=$( busybox echo -n ${BODY} | wc -c )
busybox echo -ne "POST /login.cgi HTTP/1.0\r\nHost: 199.188.1.99\r\nContent-Type:application/x-www-form-urlencoded\r\nContent-Length: ${BODY_LEN}\r\n\r\n${BODY}" | nc -i 3 199.188.1.99 80
wget "${URL}pppoe_00_disconnect.cgi" -O-

android, bash, linux, terminal

Don't hesitate to leave a comment below. NB! If you don't see a comment form under the post, it's most likely that an extension (such as Ghostery, NoScript, or AdBlock) of your browser blocks the scripts from disqus.com, and you can unblock that.

« Neurobics Xcode and AppleScript »

Comments