telnet.exp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/expect -f
  2. # This requires tcl, expect, and tcllib
  3. # Features:
  4. # Auto logs in with support for the auth system
  5. # Auto rewrites .cmds and /cmds into proper dccprefix'd cmd
  6. # Prevents password going into partyline (wraith does this as well)
  7. # Ctrl+c/Ctrl+z works in telnet
  8. # TODO: Try every hub until one connects
  9. # TODO: Auto login via relay
  10. # TODO: Scripting interface to botnet (.foreach bot chattr $bot +c) (.foreach channel chanset $chan +fastop) (ban shit?)
  11. set timeout 10
  12. match_max 5000
  13. set authFile "${env(HOME)}/.wraith/auth"
  14. set args [split $argv " "]
  15. if {[llength $args] < 1 || $args == "-h"} {
  16. puts "Usage: wraith-connect.exp pack.cfg \[user\] \[pass\]"
  17. puts "For authing you need a ~/.wraith/auth file with the following contents:"
  18. puts "packname authkey secpass"
  19. exit
  20. }
  21. set cfgfile [lindex $args 0]
  22. set username [lindex $args 1]
  23. set password [lindex $args 2]
  24. # Get packname / hub info
  25. if {[catch {set fd [open $cfgfile]}]} {
  26. puts "Pack config not found."
  27. exit 1
  28. }
  29. set packdata [read $fd]
  30. close $fd
  31. set packname ""
  32. set hub(addr) ""
  33. set hub(port) ""
  34. set dccprefix "."
  35. # Parse the pack config
  36. foreach line [split $packdata \n] {
  37. set lineSplit [split $line " "]
  38. set opcode [lindex $lineSplit 0]
  39. switch [string tolower $opcode] {
  40. "packname" { set packname [string tolower [lindex $lineSplit 1]] }
  41. "dccprefix" { set dccprefix [lindex $lineSplit 1] }
  42. "hub" {
  43. if {$hub(addr) == ""} {
  44. set hub(addr) [lindex $lineSplit 2]
  45. set hub(port) [lindex $lineSplit 3]
  46. }
  47. }
  48. default {
  49. continue;
  50. }
  51. }
  52. }
  53. proc logshit {} {
  54. interact -nobuffer -re "(.*)\r" return
  55. puts "LOG: $interact_out(1,string)"
  56. }
  57. proc get_up_bots {} {
  58. global dccprefix username
  59. set bots [list]
  60. log_user 0
  61. send "${dccprefix}bots\n"
  62. expect "${dccprefix}bots"
  63. #{send_user "GOT '$expect_out(buffer)'\n"}
  64. expect {
  65. -gl "*#${username}# bots" {
  66. # send_user "GOT '$expect_out(buffer)'\n"
  67. exp_continue
  68. }
  69. -re {Up bots[^ ]*([^\n]*)\n} {
  70. # send_user "GOT '$expect_out(1,string)'\n"
  71. foreach bot [join $expect_out(1,string)] { lappend bots $bot }
  72. exp_continue
  73. }
  74. -gl "*Total*up*" ;#{send_user "DONE\n"}
  75. }
  76. log_user 1
  77. return $bots
  78. }
  79. set on_a_cmd_line 0
  80. proc connected {} {
  81. global dccprefix password on_a_cmd_line
  82. # Start by getting a list of bots
  83. send_user "Linked Bots: [get_up_bots]\n\n"
  84. ## Fixme: expect a relay and do an auto login
  85. interact {
  86. # Ctrl+Z Suspend..
  87. -reset \032 {
  88. exec kill -STOP [pid]
  89. }
  90. # Ctrl+C Sigint
  91. \003 {
  92. exit
  93. }
  94. # Rewrite /commands
  95. # Rewrite .commands (Only useful if they dont have . as a dccprefix)
  96. -re {([./])( ?.*)} {
  97. ### Only rewrite the first character in the line
  98. if {$on_a_cmd_line == 0} {
  99. set on_a_cmd_line 1
  100. send "${dccprefix}${interact_out(2,string)}"
  101. } else {
  102. send "${interact_out(1,string)}${interact_out(2,string)}"
  103. }
  104. }
  105. ### End of line, safe to start rewriting cmd prefix again
  106. -nobuffer -re "(.*)\r" {
  107. set on_a_cmd_line 0
  108. }
  109. # Dont send your password on_a_cmd_line!
  110. ${password} {
  111. send_user "Stopped password from going into partyline."
  112. }
  113. # Open the expect interactive prompt
  114. ~~
  115. # Log the rest
  116. # -nobuffer -re "(.*)" logshit
  117. }
  118. exit 0
  119. }
  120. # Check for an auth file containing packname/authkey/secpass
  121. # Connect
  122. spawn telnet $hub(addr) $hub(port)
  123. # Send username on blank space
  124. expect {
  125. "Escape character is '^\]'.\r\r\n \r\n" {
  126. if {$username == ""} {
  127. send_user "Enter your username\n"
  128. expect_user -re "(.*)\n"
  129. send_user "\n"
  130. set username $expect_out(1,string)
  131. }
  132. send -- "${username}\n"
  133. }
  134. }
  135. # Send password when asked
  136. expect {
  137. "Enter your password\r\n" {
  138. if {$password == ""} {
  139. stty -echo
  140. expect_user -re "(.*)\n"
  141. stty echo
  142. set password $expect_out(1,string)
  143. }
  144. send -- "${password}\n"
  145. }
  146. "Connection closed by foreign host." exit
  147. }
  148. expect {
  149. # AuthSystem Protocol v1
  150. -gl "-Auth * ${packname}" {
  151. package require md5
  152. # Get secpass / authkey
  153. if {[catch {set fd [open $authFile]}]} {
  154. puts "No auth file found!!"
  155. puts "For authing you need a ~/.wraith/auth file with the following contents:"
  156. puts "packname authkey secpass"
  157. exit 1
  158. }
  159. set packdata [read $fd]
  160. close $fd
  161. foreach line [split $packdata \n] {
  162. set lineSplit [split $line " "]
  163. if {[string tolower [lindex $lineSplit 0]] == $packname} {
  164. set authkey [lindex $lineSplit 1]
  165. set secpass [lindex $lineSplit 2]
  166. }
  167. }
  168. set randstring [lindex [split $expect_out(buffer) " "] 1]
  169. set md5 [string tolower [md5::md5 -hex "${randstring}${secpass}${authkey}"]]
  170. send -- "+Auth ${md5}\n"
  171. }
  172. # "Connected to *"
  173. -ex "*** ${username} joined the party line." {
  174. connected
  175. exit 0
  176. }
  177. }
  178. expect {
  179. -ex "*** ${username} joined the party line." {
  180. # Hand control over to telnet
  181. connected
  182. exit 0
  183. }
  184. "Connection closed by foreign host." {
  185. puts "Failed to login."
  186. exit 1
  187. }
  188. }