initial
[udavac.git] / udavac.lua
blob0f3909e09cf066bdf66f70432f63e567b470d66e
1 #!/usr/bin/lua
2 -- udavacz v1.0
3 --
4 -- de-anonimize users behind nat.
5 -- returns first 9 chars of dns, i.e. your dns
6 -- better be accurate.
7 --
8 -- (c) 2008 kt@leet.cz
9 --
11 require "socket"
12 serv=socket.tcp()
13 serv:settimeout(0)
14 serv:setoption("reuseaddr",true)
15 assert(serv:bind("0.0.0.0",113))
16 serv:listen(100)
18 ins={serv}
20 while true do
21 local r = socket.select(ins, nil)
22 if r[serv] then
23 local newc=serv:accept()
24 table.insert(ins,newc)
25 else
26 for _,v in ipairs(r) do
27 local ln = v:receive("*l") or ""
28 local p1, p2 = ln:match("^(%d*)[ \t]*,[ \t]*(%d*)")
29 if ln and p1 and p2 then socket.protect(function()
30 p1 = tonumber(p1)
31 p2 = tonumber(p2)
32 local remhost, remport = socket.try(v:getpeername())
33 local rp=io.popen("conntrack -L conntrack -p tcp --state ESTABLISHED --orig-port-dst "..p2.." --reply-port-dst "..p1.." -d "..remhost,"r")
34 local rpl = rp:read("*l") or ""
35 rp:close()
36 local h=rpl:match(".-src%=([^ ]*) ")
37 if h then
38 local hn=socket.try(socket.dns.tohostname(h or ""))
39 local rhn=hn:match("^([^.]*)"):sub(1,9)
40 socket.try(v:setoption("linger", {on=true,timeout=5}))
41 socket.try(v:send(p1.." , "..p2.." : USERID : UNIX : "..rhn.."\r\n"))
42 end
43 end)() end
45 for idx,tv in ipairs(ins) do
46 if tv==v then
47 v:close()
48 table.remove(ins,idx)
49 end
50 end
51 end
52 end
53 end