home *** CD-ROM | disk | FTP | other *** search
/ Cuteskunk BBS / cuteskunk.zip / cuteskunk / Unix-Hacking-Documents / X.security < prev   
Text File  |  2003-06-29  |  13KB  |  346 lines

  1. Crash Course in X Windows Security
  2.  
  3. 1. Motivation / introduction 
  4. 2. How open X displays are found 
  5. 3. The local-host problem 
  6. 4. Snooping techniques - dumping windows 
  7. 5. Snooping techniques - reading keyboard 
  8. 6. Xterm - secure keyboard option
  9. 7. Trojan X programs [xlock and xdm] 
  10. 8. X Security tools - xauth MIT-MAGIC-COOKIE 
  11. 9. Concluding remarks
  12.  
  13.  
  14.  
  15.  
  16.  
  17. 1. Motivation / introduction
  18.  
  19. X windows pose a security risk. Through a network, anyone can connect
  20. to an open X display, read the keyboard, dump the screen and windows
  21. and start applications on the unprotected display. Even if this is a
  22. known fact throughout the computer security world, few attempts on
  23. informing the user community of the security risks involved have been
  24. made.  This article deals with some of the aspects of X windows
  25. security. It is in no sense a complete guide to the subject, but
  26. rather an introduction to a not-so-known field of computer
  27. security. Knowledge of the basics of the X windows system is
  28. necessary, I haven't bothered including an introductory section to
  29. explain the fundamentals. I wrote some code during the research for
  30. this article, but none of it is included herein.  If the lingual flow
  31. of English seem mayhap strange and erroneous from byte to byte, this
  32. is due to the fact that I'm Scandinavian.  Bare with it. :)
  33.  
  34.  
  35.  
  36.  
  37.  
  38. 2. How open X displays are found
  39.  
  40. An open X display is in formal terms an X server that has its access
  41. control disabled. Disabling access control is normally done with the
  42. xhost command.
  43.  
  44. $ xhost +
  45.  
  46. allows connections from any host. A single host can be allowed
  47. connection with the command
  48.  
  49. $ xhost + ZZZ.ZZZ.ZZZ.ZZZ
  50.  
  51. where Z is the IP address or host-name. Access control can be enabled
  52. by issuing an
  53.  
  54. $ xhost - 
  55.  
  56. command. In this case no host but the local-host can connect to the
  57. display.  Period. It is as simple as that - if the display runs in
  58. 'xhost -' state, you are safe from programs that scans and attaches to
  59. unprotected X displays.  You can check the access control of your
  60. display by simply typing xhost from a shell. Sadly enough, most sites
  61. run their X displays with access control disabled as default. They are
  62. therefore easy prey for the various scanner programs circulating on
  63. the net.
  64.  
  65. Anyone with a bit of knowledge about Xlib and sockets programming can
  66. write an X scanner in a couple of hours. The task is normally
  67. accomplished by probing the port that is reserved for X windows,
  68. number 6000. If anything is alive at that port, the scanner calls
  69. XOpenDisplay("IP-ADDRESS:0.0") that will return a pointer to the
  70. display structure, if and only if the target display has its access
  71. control disabled. If access control is enabled, XOpenDisplay returns 0
  72. and reports that the display could not be opened.
  73.  
  74. E.g:
  75.  
  76. Xlib: connection to "display:0.0" refused by server
  77. Xlib: Client is not authorized to connect to Server
  78.  
  79. The probing of port 6000 is necessary because of the fact that calling
  80. XOpenDisplay() on a host that runs no X server will simply hang the
  81. calling process. So much for unix programming conventions. :)
  82.  
  83. I wrote a program called xscan that could scan an entire subnet or
  84. scan the entries in /etc/hosts for open X displays. My remark about
  85. most sites running X displays with access control disabled, originates
  86. from running xscan towards several sites on the internet.
  87.  
  88.  
  89.  
  90.  
  91.  
  92. 3. The localhost problem
  93.  
  94. Running your display with access control enabled by using 'xhost -'
  95. will guard you from XOpenDisplay attempts through port number
  96. 6000. But there is one way an eavesdropper can bypass this
  97. protection. If he can log into your host, he can connect to the
  98. display of the localhost. The trick is fairly simple. By issuing these
  99. few lines, dumping the screen of the host 'target' is accomplished:
  100.  
  101. $ rlogin target
  102. $ xwd -root -display localhost:0.0 > ~/snarfed.xwd
  103. $ exit
  104. $ xwud -in ~/snarfed.xwd
  105.  
  106. And voila, we have a screendump of the root window of the X server
  107. target. 
  108.  
  109. Of course, an intruder must have an account on your system and be able
  110. to log into the host where the specific X server runs. On sites with a
  111. lot of X terminals, this means that no X display is safe from those
  112. with access. If you can run a process on a host, you can connect to
  113. (any of) its X displays.
  114.  
  115. Every Xlib routine has the Display structure as it's first
  116. argument. By successfully opening a display, you can manipulate it
  117. with every Xlib call available. For an intruder, the most 'important'
  118. ways of manipulating is grabbing windows and keystrokes.
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. 4. Snooping techniques - dumping windows
  126.  
  127. The most natural way of snarfing a window from an X server is by using
  128. the X11R5 utility xwd or X Window System dumping utility. To get a
  129. grip of the program, here's a small excerpt from the man page
  130.  
  131.  DESCRIPTION
  132.       Xwd is an X Window System window dumping utility.  Xwd allows Xusers
  133.       to store window images in a specially formatted dump file.  This file
  134.       can then be read by various other X utilities for redisplay, printing,
  135.       editing, formatting, archiving, image processing, etc.  The target
  136.       window is selected by clicking the pointer in the desired window.  The
  137.       keyboard bell is rung once at the beginning of the dump and twice when
  138.       the dump is completed.
  139.  
  140. Shortly, xwd is a tool for dumping X windows into a format readable by
  141. another program, xwud. To keep the trend, here's an excerpt from the
  142. man page of xwud:
  143.  
  144.  
  145.  DESCRIPTION
  146.       Xwud is an X Window System image undumping utility.  Xwud allows X
  147.       users to display in a window an image saved in a specially formatted
  148.       dump file, such as produced by xwd(1).
  149.  
  150. I will not go in detail of how to use these programs, as they are both
  151. self-explanatory and easy to use. Both the entire root window, a
  152. specified window (by name) can be dumped, or a specified screen.  As a
  153. 'security measure' xwd will beep the terminal it is dumping from, once
  154. when xwd is started, and once when it is finished (regardless of the
  155. xset b off command). But with the source code available, it is a
  156. matter of small modification to compile a version of xwd that doesn't
  157. beep or otherwise identifies itself - on the process list e.g.  If we
  158. wanted to dump the root window or any other window from a host, we
  159. could simply pick a window from the process list, which often gives
  160. away the name of the window through the -name flag.  As before
  161. mentioned, to dump the entire screen from a host:
  162.  
  163. $ xwd -root localhost:0.0 > file
  164.  
  165. the output can be directed to a file, and read with
  166.  
  167. $ xwud -in file
  168.  
  169. or just piped straight to the xwud command.
  170.  
  171. Xterm windows are a different thing. You can not specify the name of
  172. an xterm and then dump it. They are somehow blocked towards the
  173. X_Getimage primitive used by xwd, so the following
  174.  
  175. $ xwd -name xterm
  176.  
  177. will result in an error. However, the entire root window (with Xterms
  178. and all) can still be dumped and watched by xwud. Some protection.
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. 5. Snooping techniques - reading keyboard
  186.  
  187. If you can connect to a display, you can also log and store every
  188. keystroke that passes through the X server. A program circulating the
  189. net, called xkey, does this trick. A kind of higher-level version of
  190. the infamous ttysnoop.c. I wrote my own, who could read the keystrokes
  191. of a specific window ID (not just every keystroke, as my version of
  192. xkey).  The window ID's of a specific root-window, can be acquired
  193. with a call to XQueryTree(), that will return the XWindowAttributes of
  194. every window present. The window manager must be able to control every
  195. window-ID and what keys are pressed down at what time.  By use of the
  196. window-manager functions of Xlib, KeyPress events can be captured, and
  197. KeySyms can be turned into characters by continuous calls to
  198. XLookupString. 
  199.  
  200. You can even send KeySym's to a Window. An intruder may therefore not
  201. only snoop on your activity, he can also send keyboard events to
  202. processes, like they were typed on the keyboard.  Reading/writing
  203. keyboard events to an xterm window opens new horizons in process
  204. manipulation from remote. Luckily, xterm has good protection
  205. techniques for prohibiting access to the keyboard events.
  206.  
  207.  
  208.  
  209.  
  210.  
  211. 6. Xterm - Secure keyboard option
  212.  
  213. A lot of passwords is typed in an xterm window. It is therefore
  214. crucial that the user has full control over which processes can read
  215. and write to an xterm.  The permission for the X server to send events
  216. to an Xterm window, is set at compile time. The default is false,
  217. meaning that all SendEvent requests from the X server to an xterm
  218. window is discarded. You can overwrite the compile-time setting with a
  219. standard resource definition in the .Xdefaults file:
  220.  
  221. xterm*allowSendEvents    True
  222.  
  223. or by selecting Allow Sendevents on the Xterm Main Options
  224. menu. (Accessed by pressing CTRL and the left mouse button But this is
  225. _not_ recommended. Neither by me, nor the man page. ;) Read access is
  226. a different thing. 
  227.  
  228. Xterms mechanism for hindering other X clients to read the keyboard
  229. during entering of sensitive data, passwords etc. is by using the
  230. XGrabKeyboard() call. Only one process can grab the keyboard at any
  231. one time. To activate the Secure Keyboard option, choose the Main
  232. Options menu in your Xterm window (CTRL+Left mouse button) and select
  233. Secure Keyboard.  If the colors of your xterm window inverts, the
  234. keyboard is now Grabbed, and no other X client can read the KeySyms.
  235.  
  236. The versions of Xterm X11R5 without patch26 also contain a rather
  237. nasty and very well known security hole that enables any user to
  238. become root through clever use of symbolic links to the password
  239. file. The Xterm process need to be setuid for this hole to be
  240. exploitable.  Refer to the Cert Advisory:
  241. CA-93:17.xterm.logging.vulnerability.
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. 7. Trojan X clients - xlock and X based logins
  249.  
  250. Can you think of a more suitable program for installing a
  251. password-grabbing trojan horse than xlock? I myself cannot. With a few
  252. lines added to the getPassword routine in xlock.c, the password of
  253. every user using the trojan version of xlock can be stashed away in a
  254. file for later use by an intruder. The changes are so minimal, only a
  255. couple of bytes will tell the real version from the trojan version.
  256.  
  257. If a user has a writable homedir and a ./ in her PATH environment
  258. variable, she is vulnerable to this kind of attack. Getting the
  259. password is achieved by placing a trojan version of Xlock in the users
  260. homedir and waiting for an invocation.  The functionality of the
  261. original Xlock is contained in the trojan version.  The trojan version
  262. can even tidy up and destroy itself after one succesfull attempt, and
  263. the user will not know that his password has been captured.
  264.  
  265. Xlock, like every password-prompting program, should be regarded with
  266. suspicion if it shows up in places it should not be, like in your own
  267. homedir.
  268.  
  269. Spoofed X based logins however are a bit more tricky for the intruder
  270. to accomplish.  He must simulate the login screen of the login program
  271. ran by XDM. The only way to ensure that you get the proper XDM login
  272. program (if you want to be really paranoid) is to restart the
  273. X-terminal, whatever key combination that will be for the terminal in
  274. question.
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282. 8. X Security tools - xauth MIT-MAGIC-COOKIE 
  283.  
  284. To avoid unathorized connections to your X display, the command xauth
  285. for encrypted X connections is widely used. When you login, xdm
  286. creates a file .Xauthority in your homedir. This file is binary, and
  287. readable only through the xauth command. If you issue the command
  288.  
  289. $ xauth list
  290.  
  291. you will get an output of:
  292.  
  293. your.display.ip:0  MIT-MAGIC-COOKIE-1  73773549724b76682f726d42544a684a
  294.  
  295.   display name     authorization type               key
  296.  
  297. The .Xauthority file sometimes contains information from older
  298. sessions, but this is not important, as a new key is created at every
  299. login session. To access a display with xauth active - you must have
  300. the current access key.
  301.  
  302. If you want to open your display for connections from a particular
  303. user, you must inform him of your key.
  304. He must then issue the command 
  305.  
  306. $ xauth add your.display.ip:0  MIT-MAGIC-COOKIE-1 73773549724b7668etc.
  307.  
  308. Now, only that user (including yourself) can connect to your display.
  309. Xauthority is simple and powerful, and eliminates many of the security
  310. problems with X.
  311.  
  312.  
  313.  
  314.  
  315.  
  316. 9. Concluding remarks
  317.  
  318. Thanks must go to Anthony Tyssen for sending me his accumulated info
  319. on X security issues from varius usenet discussions. I hope someone
  320. has found useful information in this text. It is released to the
  321. net.community with the idea that it will help the user to understand
  322. the security problems concerned with using X windows. Questions or
  323. remarks can be sent to the following address:
  324.  
  325. ______________________________________________________________________
  326. runeb / cF --- runeb@stud.cs.uit.no  ---   http://www.cs.uit.no/~runeb
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.