]> Pileus Git - ~andy/sfvlug/blob - awk/src/challenge.awk
Add sed and awk presentation
[~andy/sfvlug] / awk / src / challenge.awk
1 #!/bin/awk -f
2
3 function connect(method)
4 {
5         HOST = "ctf.nullcon.net"
6         FILE = "/inet/tcp/0/" HOST "/80"
7         PATH = "/challenges/programming/challenge.php"
8
9         print method " " PATH " HTTP/1.0" |& FILE
10         print "Host: " HOST ""            |& FILE
11
12         return FILE
13 }
14
15
16 BEGIN {
17         sock = connect("GET")
18         print |& sock
19         close(sock, "to")
20         while (sock |& getline line) {
21                 if (match(line, "Set-Cookie:"))
22                         cookie = line
23                 if (match(line, "span"))
24                         text = line
25         }
26         close(sock)
27
28         gsub(" ",    " ", text)
29         gsub("<[^>]*>",   " ", text)
30         gsub("^ *| *$",   "",  text)
31         gsub("  +",       "+", text)
32         gsub("^[^:]*: *", "",  cookie)
33         gsub(" *;.*",     "",  cookie)
34
35         sock = connect("POST")
36         resp = "answer=" text "&submit=Submit"
37         print "Content-Type: application/x-www-form-urlencoded" |& sock
38         print "Content-Length: " length(resp) |& sock
39         print "Cookie: " cookie |& sock
40         print |& sock
41         print resp |& sock
42         close(sock, "to")
43         while (sock |& getline line)
44                 print line
45         close(sock)
46 }