home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit 2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Texts / Improving-Security / security6.txt < prev    next >
Encoding:
Text File  |  1999-11-04  |  23.3 KB  |  479 lines

  1.  
  2.                            == Phrack Magazine ==
  3.                       Volume Seven, Issue Forty-Eight
  4.                        by daemon9 / route / infinity
  5.                             for Phrack Magazine
  6.                       June 1996 Guild Productions, kid
  7.  
  8.                       comments to route@infonexus.com
  9.  
  10. The purpose of this paper is to explain IP-spoofing to the masses. It
  11. assumes little more than a working knowledge of Unix and TCP/IP. Oh, and
  12. that yur not a moron...
  13.  
  14. IP-spoofing is complex technical attack that is made up of several
  15. components. (In actuality, IP-spoofing is not the attack, but a step in the
  16. attack. The attack is actually trust-relationship exploitation. However, in
  17. this paper, IP-spoofing will refer to the whole attack.) In this paper, I
  18. will explain the attack in detail, including the relevant operating system
  19. and networking information.
  20.  
  21.                      SECTION I. BACKGROUND INFORMATION
  22.  
  23.      --[ The Players ]--
  24.  
  25.         A:      Target host
  26.  
  27.         B:      Trusted host
  28.  
  29.         X:      Unreachable host
  30.  
  31.         Z:      Attacking host
  32.  
  33.         (1)2:   Host 1 masquerading as host 2
  34.  
  35.      --[ The Figures ]--
  36.  
  37. There are several figures in the paper and they are to be interpreted as
  38. per the following example:
  39.  
  40. ick   host a      control     host b
  41.  
  42. 1       A       ---SYN--->      B
  43.  
  44. tick: A tick of time. There is no distinction made as to how much time
  45. passes between ticks, just that time passes. It's generally not a great
  46. deal.
  47. host a: A machine particpating in a TCP-based conversation. control: This
  48. field shows any relevant control bits set in the TCP header and the
  49. direction the data is flowing
  50. host b: A machine particpating in a TCP-based conversation.
  51.  
  52. In this case, at the first refrenced point in time host a is sending a TCP
  53. segment to host b with the SYN bit on. Unless stated, we are generally not
  54. concerned with the data portion of the TCP segment.
  55.  
  56.      --[ Trust Relationships ]--
  57.  
  58. In the Unix world, trust can be given all too easily. Say you have an
  59. account on machine A, and on machine B. To facilitate going betwixt the two
  60. with a minimum amount of hassle, you want to setup a full-duplex trust
  61. relationship between them. In your home directory at A you create a .rhosts
  62. file: `echo "B username" > ~/.rhosts` In your home directory at B you
  63. create a .rhosts file: `echo "A username" > ~/.rhosts` (Alternately, root
  64. can setup similar rules in /etc/hosts.equiv, the difference being that the
  65. rules are hostwide, rather than just on an individual basis.) Now, you can
  66. use any of the r* commands without that annoying hassle of password
  67. authentication. These commands will allow address-based authentication,
  68. which will grant or deny access based off of the IP address of the service
  69. requestor.
  70.  
  71.      --[ Rlogin ]--
  72.  
  73. Rlogin is a simple client-server based protocol that uses TCP as it's
  74. transport. Rlogin allows a user to login remotely from one host to another,
  75. and, if the target machine trusts the other, rlogin will allow the
  76. convienience of not prompting for a password. It will instead have
  77. authenticated the client via the source IP address. So, from our example
  78. above, we can use rlogin to remotely login to A from B (or vice-versa) and
  79. not be prompted for a password.
  80.  
  81.      --[ Internet Protocol ]--
  82.  
  83. IP is the connectionless, unreliable network protocol in the TCP/IP suite.
  84. It has two 32-bit header fields to hold address information. IP is also the
  85. busiest of all the TCP/IP protocols as almost all TCP/IP traffic is
  86. encapsulated in IP datagrams. IP's job is to route packets around the
  87. network. It provides no mechanism for reliability or accountability, for
  88. that, it relies on the upper layers. IP simply sends out datagrams and
  89. hopes they make it intact. If they don't, IP can try to send an ICMP error
  90. message back to the source, however this packet can get lost as well. (ICMP
  91. is Internet Control Message Protocol and it is used to relay network
  92. conditions and different errors to IP and the other layers.) IP has no
  93. means to guarantee delivery. Since IP is connectionless, it does not
  94. maintain any connection state information. Each IP datagram is sent out
  95. without regard to the last one or the next one. This, along with the fact
  96. that it is trivial to modify the IP stack to allow an arbitrarily choosen
  97. IP address in the source (and destination) fields make IP easily
  98. subvertable.
  99.  
  100.      --[ Transmission Control Protocol ]--
  101.  
  102. TCP is the connection-oriented, reliable transport protocol in the TCP/IP
  103. suite. Connection-oriented simply means that the two hosts participating in
  104. a discussion must first establish a connection before data may change
  105. hands. Reliability is provided in a number of ways but the only two we are
  106. concerned with are data sequencing and acknowledgement. TCP assigns
  107. sequence numbers to every segment and acknowledges any and all data
  108. segments recieved from the other end. (ACK's consume a sequence number, but
  109. are not themselves ACK'd.) This reliability makes TCP harder to fool than
  110. IP.
  111.  
  112.      --[ Sequence Numbers, Acknowledgements and other flags ]--
  113.  
  114. Since TCP is reliable, it must be able to recover from lost, duplicated, or
  115. out-of-order data. By assigning a sequence number to every byte transfered,
  116. and requiring an acknowledgement from the other end upon receipt, TCP can
  117. guarantee reliable delivery. The receiving end uses the sequence numbers to
  118. ensure proper ordering of the data and to eliminate duplicate data bytes.
  119. TCP sequence numbers can simply be thought of as 32-bit counters. They
  120. range from 0 to 4,294,967,295. Every byte of data exchanged across a TCP
  121. connection (along with certain flags) is sequenced. The sequence number
  122. field in the TCP header will contain the sequence number of the first byte
  123. of data in the TCP segment. The acknowledgement number field in the TCP
  124. header holds the value of next expected sequence number, and also
  125. acknowledges all data up through this ACK number minus one.
  126. TCP uses the concept of window advertisement for flow control. It uses a
  127. sliding window to tell the other end how much data it can buffer. Since the
  128. window size is 16-bits a receiving TCP can advertise up to a maximum of
  129. 65535 bytes. Window advertisement can be thought of an advertisment from
  130. one TCP to the other of how high acceptable sequence numbers can be.
  131. Other TCP header flags of note are RST (reset), PSH (push) and FIN
  132. (finish). If a RST is received, the connection is immediately torn down.
  133. RSTs are normally sent when one end receives a segment that just doesn't
  134. jive with current connection (we will encounter an example below). The PSH
  135. flag tells the reciever to pass all the data is has queued to the
  136. aplication, as soon as possible. The FIN flag is the way an application
  137. begins a graceful close of a connection (connection termination is a 4-way
  138. process). When one end recieves a FIN, it ACKs it, and does not expect to
  139. receive any more data (sending is still possible, however).
  140.  
  141.      --[ TCP Connection Establishment ]--
  142.  
  143. In order to exchange data using TCP, hosts must establish a a connection.
  144. TCP establishes a connection in a 3 step process called the 3-way
  145. handshake. If machine A is running an rlogin client and wishes to conect to
  146. an rlogin daemon on machine B, the process is as follows:
  147.  
  148.                 fig(1)
  149.  
  150. 1       A       ---SYN--->      B
  151.  
  152. 2       A    <---SYN/ACK---     B
  153.  
  154. 3       A       ---ACK--->      B
  155.  
  156. At (1) the client is telling the server that it wants a connection. This is
  157. the SYN flag's only purpose. The client is telling the server that the
  158. sequence number field is valid, and should be checked. The client will set
  159. the sequence number field in the TCP header to it's ISN (initial sequence
  160. number). The server, upon receiving this segment (2) will respond with it's
  161. own ISN (therefore the SYN flag is on) and an ACKnowledgement of the
  162. clients first segment (which is the client's ISN+1). The client then ACK's
  163. the server's ISN (3). Now, data transfer may take place.
  164.  
  165.      --[ The ISN and Sequence Number Incrementation ]--
  166.  
  167. It is important to understand how sequence numbers are initially choosen,
  168. and how they change with respect to time. The initial sequence number when
  169. a host is bootstraped is initialized to 1. (TCP actually calls this
  170. variable 'tcp_iss' as it is the initial send sequence number. The other
  171. sequence number variable, 'tcp_irs' is the initial receive sequence number
  172. and is learned during the 3-way connection establishment. We are not going
  173. to worry about the distinction.) This practice is wrong, and is
  174. acknowledged as so in a comment the tcp_init() function where it appears.
  175. The ISN is incremented by 128,000 every second, which causes the 32-bit ISN
  176. counter to wrap every 9.32 hours if no connections occur. However, each
  177. time a connect() is issued, the counter is incremented by 64,000.
  178. One important reason behind this predictibility is to minimize the chance
  179. that data from an older stale incarnation (that is, from the same 4-tuple
  180. of the local and remote IP-addresses TCP ports) of the current connection
  181. could arrive and foul things up. The concept of the 2MSL wait time applies
  182. here, but is beyond the scope of this paper. If sequence numbers were
  183. choosen at random when a connection arrived, no guarantees could be made
  184. that the sequence numbers would be different from a previous incarnation.
  185. If some data that was stuck in a routing loop somewhere finally freed
  186. itself and wandered into the new incarnation of it's old connection, it
  187. could really foul things up.
  188.  
  189.      --[ Ports ]--
  190.  
  191. To grant simultaneous access to the TCP module, TCP provides a user
  192. interface called a port. Ports are used by the kernel to identify network
  193. processes. These are strictly transport layer entities (that is to say that
  194. IP could care less about them). Together with an IP address, a TCP port
  195. provides provides an endpoint for network communications. In fact, at any
  196. given moment all Internet connections can be described by 4 numbers: the
  197. source IP address and source port and the destination IP address and
  198. destination port. Servers are bound to 'well-known' ports so that they may
  199. be located on a standard port on different systems. For example, the rlogin
  200. daemon sits on TCP port 513.
  201.  
  202.                            SECTION II. THE ATTACK
  203.  
  204.                 ...The devil finds work for idle hands....
  205.  
  206.      --[ Briefly... ]--
  207.  
  208. IP-spoofing consists of several steps, which I will briefly outline here,
  209. then explain in detail. First, the target host is choosen. Next, a pattern
  210. of trust is discovered, along with a trusted host. The trusted host is then
  211. disabled, and the target's TCP sequence numbers are sampled. The trusted
  212. host is impersonated, the sequence numbers guessed, and a connection
  213. attempt is made to a service that only requires address-based
  214. authentication. If successful, the attacker executes a simple command to
  215. leave a backdoor.
  216.  
  217.      --[ Needful Things ]--
  218.  
  219. There are a couple of things one needs to wage this attack:
  220.  
  221.    * brain, mind, or other thinking device
  222.    * target host
  223.    * trusted host
  224.    * attacking host (with root access)
  225.    * IP-spoofing software
  226.  
  227. Generally the attack is made from the root account on the attacking host
  228. against the root account on the target. If the attacker is going to all
  229. this trouble, it would be stupid not to go for root. (Since root access is
  230. needed to wage the attack, this should not be an issue.)
  231.  
  232.      --[ IP-Spoofing is a 'Blind Attack' ]--
  233.  
  234. One often overlooked, but critical factor in IP-spoofing is the fact that
  235. the attack is blind. The attacker is going to be taking over the identity
  236. of a trusted host in order to subvert the security of the target host. The
  237. trusted host is disabled using the method described below. As far as the
  238. target knows, it is carrying on a conversation with a trusted pal. In
  239. reality, the attacker is sitting off in some dark corner of the Internet,
  240. forging packets puportedly from this trusted host while it is locked up in
  241. a denial of service battle. The IP datagrams sent with the forged
  242. IP-address reach the target fine (recall that IP is a
  243. connectionless-oriented protocol-- each datagram is sent without regard for
  244. the other end) but the datagrams the target sends back (destined for the
  245. trusted host) end up in the bit-bucket. The attacker never sees them. The
  246. intervening routers know where the datagrams are supposed to go. They are
  247. supposed to go the trusted host. As far as the network layer is concerned,
  248. this is where they originally came from, and this is where responses should
  249. go. Of course once the datagrams are routed there, and the information is
  250. demultiplexed up the protocol stack, and reaches TCP, it is discarded (the
  251. trusted host's TCP cannot respond-- see below). So the attacker has to be
  252. smart and know what was sent, and know what reponse the server is looking
  253. for. The attacker cannot see what the target host sends, but she can
  254. predict what it will send; that coupled with the knowledge of what it will
  255. send, allows the attacker to work around this blindness.
  256.  
  257.      --[ Patterns of Trust ]--
  258.  
  259. After a target is choosen the attacker must determine the patterns of trust
  260. (for the sake of argument, we are going to assume the target host does in
  261. fact trust somebody. If it didn't, the attack would end here). Figuring out
  262. who a host trusts may or may not be easy. A 'showmount -e' may show where
  263. filesystems are exported, and rpcinfo can give out valuable information as
  264. well. If enough background information is known about the host, it should
  265. not be too difficult. If all else fails, trying neighboring IP addresses in
  266. a brute force effort may be a viable option.
  267.  
  268.      --[ Trusted Host Disabling Using the Flood of Sins ]--
  269.  
  270. Once the trusted host is found, it must be disabled. Since the attacker is
  271. going to impersonate it, she must make sure this host cannot receive any
  272. network traffic and foul things up. There are many ways of doing this, the
  273. one I am going to discuss is TCP SYN flooding.
  274. A TCP connection is initiated with a client issuing a request to a server
  275. with the SYN flag on in the TCP header. Normally the server will issue a
  276. SYN/ACK back to the client identified by the 32-bit source address in the
  277. IP header. The client will then send an ACK to the server (as we saw in
  278. figure 1 above) and data transfer can commence. There is an upper limit of
  279. how many concurrent SYN requests TCP can process for a given socket,
  280. however. This limit is called the backlog, and it is the length of the
  281. queue where incoming (as yet incomplete) connections are kept. This queue
  282. limit applies to both the number of imcomplete connections (the 3-way
  283. handshake is not complete) and the number of completed connections that
  284. have not been pulled from the queue by the application by way of the
  285. accept() system call. If this backlog limit is reached, TCP will silently
  286. discard all incoming SYN requests until the pending connections can be
  287. dealt with. Therein lies the attack.
  288. The attacking host sends several SYN requests to the TCP port she desires
  289. disabled. The attacking host also must make sure that the source IP-address
  290. is spoofed to be that of another, currently unreachable host (the target
  291. TCP will be sending it's response to this address. (IP may inform TCP that
  292. the host is unreachable, but TCP considers these errors to be transient and
  293. leaves the resolution of them up to IP (reroute the packets, etc)
  294. effectively ignoring them.) The IP-address must be unreachable because the
  295. attacker does not want any host to recieve the SYN/ACKs that will be coming
  296. from the target TCP (this would result in a RST being sent to the target
  297. TCP, which would foil our attack). The process is as follows:
  298.  
  299.                 fig(2)
  300.  
  301. 1       Z(x)    ---SYN--->      B
  302.  
  303.         Z(x)    ---SYN--->      B
  304.  
  305.         Z(x)    ---SYN--->      B
  306.  
  307.         Z(x)    ---SYN--->      B
  308.  
  309.         Z(x)    ---SYN--->      B
  310.  
  311.                 ...
  312.  
  313. 2       X    <---SYN/ACK---     B
  314.  
  315.         X    <---SYN/ACK---     B
  316.  
  317.                 ...
  318.  
  319. 3       X      <---RST---       B
  320.  
  321. At (1) the attacking host sends a multitude of SYN requests to the target
  322. (remember the target in this phase of the attack is the trusted host) to
  323. fill it's backlog queue with pending connections. (2) The target responds
  324. with SYN/ACKs to what it believes is the source of the incoming SYNs.
  325. During this time all further requests to this TCP port will be ignored.
  326. Different TCP implementations have different backlog sizes. BSD generally
  327. has a backlog of 5 (Linux has a backlog of 6). There is also a 'grace'
  328. margin of 3/2. That is, TCP will allow up to backlog*3/2+1 connections.
  329. This will allow a socket one connection even if it calls listen with a
  330. backlog of 0.
  331.  
  332. AuthNote: [For a much more in-depth treatment of TCP SYN flooding, see my
  333. definitive paper on the subject. It covers the whole process in detail, in
  334. both theory, and practice. There is robust working code, a statistical
  335. analysis, and a legnthy paper. Look for it in issue 49 of Phrack. -daemon9
  336. 6/96]
  337.  
  338.      --[ Sequence Number Sampling and Prediction ]--
  339.  
  340. Now the attacker needs to get an idea of where in the 32-bit sequence
  341. number space the target's TCP is. The attacker connects to a TCP port on
  342. the target (SMTP is a good choice) just prior to launching the attack and
  343. completes the three-way handshake. The process is exactly the same as
  344. fig(1), except that the attacker will save the value of the ISN sent by the
  345. target host. Often times, this process is repeated several times and the
  346. final ISN sent is stored. The attacker needs to get an idea of what the RTT
  347. (round-trip time) from the target to her host is like. (The process can be
  348. repeated several times, and an average of the RTT's is calculated.) The RTT
  349. is necessary in being able to accuratly predict the next ISN. The attacker
  350. has the baseline (the last ISN sent) and knows how the sequence numbers are
  351. incremented (128,000/second and 64,000 per connect) and now has a good idea
  352. of how long it will take an IP datagram to travel across the Internet to
  353. reach the target (approximately half the RTT, as most times the routes are
  354. symmetrical). After the attacker has this information, she immediately
  355. proceeds to the next phase of the attack (if another TCP connection were to
  356. arrive on any port of the target before the attacker was able to continue
  357. the attack, the ISN predicted by the attacker would be off by 64,000 of
  358. what was predicted).
  359. When the spoofed segment makes it's way to the target, several different
  360. things may happen depending on the accuracy of the attacker's prediction:
  361.  
  362.    * If the sequence number is EXACTly where the receiving TCP expects it
  363.      to be, the incoming data will be placed on the next available position
  364.      in the receive buffer.
  365.    * If the sequence number is LESS than the expected value the data byte
  366.      is considered a retransmission, and is discarded.
  367.    * If the sequence number is GREATER than the expected value but still
  368.      within the bounds of the receive window, the data byte is considered
  369.      to be a future byte, and is held by TCP, pending the arrival of the
  370.      other missing bytes. If a segment arrives with a sequence number
  371.      GREATER than the expected value and NOT within the bounds of the
  372.      receive window the segment is dropped, and TCP will send a segment
  373.      back with the expected sequence number.
  374.  
  375.      --[ Subversion... ]--
  376.  
  377. Here is where the main thrust of the attack begins:
  378.  
  379.                 fig(3)
  380.  
  381. 1       Z(b)    ---SYN--->      A
  382.  
  383. 2       B     <---SYN/ACK---    A
  384.  
  385. 3       Z(b)    ---ACK--->      A
  386.  
  387. 4       Z(b)    ---PSH--->      A
  388.  
  389.                 [...]
  390.  
  391. The attacking host spoofs her IP address to be that of the trusted host
  392. (which should still be in the death-throes of the D.O.S. attack) and sends
  393. it's connection request to port 513 on the target (1). At (2), the target
  394. responds to the spoofed connection request with a SYN/ACK, which will make
  395. it's way to the trusted host (which, if it could process the incoming TCP
  396. segment, it would consider it an error, and immediately send a RST to the
  397. target). If everything goes according to plan, the SYN/ACK will be dropped
  398. by the gagged trusted host. After (1), the attacker must back off for a bit
  399. to give the target ample time to send the SYN/ACK (the attacker cannot see
  400. this segment). Then, at (3) the attacker sends an ACK to the target with
  401. the predicted sequence number (plus one, because we're ACKing it). If the
  402. attacker is correct in her prediction, the target will accept the ACK. The
  403. target is compromised and data transfer can commence (4).
  404. Generally, after compromise, the attacker will insert a backdoor into the
  405. system that will allow a simpler way of intrusion. (Often a `cat + + >>
  406. ~/.rhosts` is done. This is a good idea for several reasons: it is quick,
  407. allows for simple re-entry, and is not interactive. Remember the attacker
  408. cannot see any traffic coming from the target, so any reponses are sent off
  409. into oblivion.)
  410.  
  411.      --[ Why it Works ]--
  412.  
  413. IP-Spoofing works because trusted services only rely on network address
  414. based authentication. Since IP is easily duped, address forgery is not
  415. difficult. The hardest part of the attck is in the sequence number
  416. prediction, because that is where the guesswork comes into play. Reduce
  417. unknowns and guesswork to a minimum, and the attack has a better chance of
  418. suceeding. Even a machine that wraps all it's incoming TCP bound
  419. connections with Wietse Venema's TCP wrappers, is still vulnerable to the
  420. attack. TCP wrappers rely on a hostname or an IP address for
  421. authentication...
  422.  
  423.                      SECTION III. PREVENTITIVE MEASURES
  424.  
  425.                      ...A stich in time, saves nine...
  426.  
  427.      --[ Be Un-trusting and Un-trustworthy ]--
  428.  
  429. One easy solution to prevent this attack is not to rely on address-based
  430. authentication. Disable all the r* commands, remove all .rhosts files and
  431. empty out the /etc/hosts.equiv file. This will force all users to use other
  432. means of remote access (telnet, ssh, skey, etc).
  433.  
  434.      --[ Packet Filtering ]--
  435.  
  436. If your site has a direct connect to the Internet, you can use your router
  437. to help you out. First make sure only hosts on your internal LAN can
  438. particpate in trust-relationships (no internal host should trust a host
  439. outside the LAN). Then simply filter out all traffic from the outside (the
  440. Internet) that puports to come from the inside (the LAN).
  441.  
  442.      --[ Cryptographic Methods ]--
  443.  
  444. An obvious method to deter IP-spoofing is to require all network traffic to
  445. be encrypted and/or authenticated. While several solutions exist, it will
  446. be a while before such measures are deployed as defacto standards.
  447.  
  448.      --[ Initial Sequence Number Randomizing ]--
  449.  
  450. Since the sequence numbers are not choosen randomly (or incremented
  451. randomly) this attack works. Bellovin describes a fix for TCP that involves
  452. partitioning the sequence number space. Each connection would have it's own
  453. seperate sequence number space. The sequence numbers would still be
  454. incremented as before, however, there would be no obvious or implied
  455. relationship between the numbering in these spaces. Suggested is the
  456. following formula:
  457.  
  458.         ISN=M+F(localhost,localport,remotehost,remoteport)
  459.  
  460. Where M is the 4 microsecond timer and F is a cryptographic hash. F must
  461. not be computable from the outside or the attacker could still guess
  462. sequence numbers. Bellovin suggests F be a hash of the connection-id and a
  463. secret vector (a random number, or a host related secret combined with the
  464. machine's boot time).
  465.  
  466.                             SECTION IV. SOURCES
  467.  
  468.         -Books:         TCP/IP Illustrated vols. I, II & III
  469.  
  470.         -RFCs:          793, 1825, 1948
  471.  
  472.         -People:        Richard W. Stevens, and the users of the
  473.  
  474.                         Information Nexus for proofreading
  475.  
  476.         -Sourcecode:    rbone, mendax, SYNflood
  477.  
  478. This paper made possible by a grant from the Guild Corporation.
  479.