1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>nexp</title><link rel="stylesheet" href="nexp.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.70.1"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en"><a name="nexp"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>nexp —
A framework for crafting network packets and processing responses
</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">nexp</code> [<code class="option">-V</code>] [<code class="option">-v</code>] [<code class="option">-t</code>] [<code class="option">-c</code> <em class="replaceable"><code>cmd</code></em>] [<code class="option">-s</code> <em class="replaceable"><code>seed</code></em>] [<code class="option">-h</code>] [<em class="replaceable"><code>cmdfile</code></em>] [<em class="replaceable"><code>args</code></em>]</p></div></div><div class="refsect1" lang="en"><a name="id2487792"></a><h2>INTRODUCTION</h2><p>
<span><strong class="command">Network Expect</strong></span> (<span><strong class="command">nexp</strong></span>) is a
framework that allows to easily build tools that can interact
with network traffic. Following a script, traffic can be
injected into the network, and decisions can be taken, and acted
upon, based on received network traffic. An interpreted language
provides branching and high-level control structures to direct
the interaction with the network.
</p><p>
<span><strong class="command">Network Expect</strong></span> was heavily influenced and
inspired on the Expect program written by Don Libes, which
allows to "talk" to interactive programs in a scripted
fashion. Because of this, you will find a lot of similarities
between commands in <span><strong class="command">Network Expect</strong></span> and
commands in Don Libes' Expect. If you are a regular Expect user,
it should not be very difficult to start writing
<span><strong class="command">Network Expect</strong></span> scripts because the basics are
the same.
</p><p>
In Don Libes' Expect, scripts can send data to a process just as
if a user were interactively typing commands. Then, the script
would read the responses send by the application and take
decisions accordingly. In <span><strong class="command">Network Expect</strong></span>'s
case, a script could send traffic to a network device and then
take decisions based on the received network traffic. The type
of things that <span><strong class="command">Network Expect</strong></span> can do are
usually very low level network operations, which usually require
writing a custom application in a language like C.
</p><p>
<span><strong class="command">Network Expect</strong></span>'s philosophy is based on the
observation that network applications always operate on an
action-reaction principle in which something is sent to an
application running on a remote host and a response is then
expected.
</p><p>
Some of the things that <span><strong class="command">Network Expect</strong></span> can do
include:
</p><div class="itemizedlist"><ul type="disc"><li><p>
Generate arbitrary network traffic and inject it into a
network at layer 2 or layer 3.
</p><p>
A wide range of protocols is supported, including IP version
6 as well as protocol options like IPv4, IPv6 and TCP
options, something that regular tools don't offer. For
example, <span><strong class="command">Network Expect</strong></span> supports TCP MD5
signatures (RFC 2385).
</p><p>
This <span><strong class="command">Network Expect</strong></span> functionality is very
similar to the functionality provided by several packet
crafting and forging open source tools like Nemesis, Packit,
hping, Scapy, and others.
</p></li><li><p>
Listen for network traffic and take decisions based on the
type of traffic received.
</p></li><li><p>
Open a sniffer trace in PCAP format and replay it after
changing some values in the original packet capture.
</p></li><li><p>
Emulate network protocols to see how they interact with
other speakers of that protocol. For example, emulating a
TCP server to investigate approaches to randomization of TCP
Initial Sequence Numbers (ISN) can be easily done in
<span><strong class="command">Network Expect</strong></span>.
</p></li></ul></div></div><div class="refsect1" lang="en"><a name="id2487934"></a><h2>USAGE</h2><p>
<span><strong class="command">Network Expect</strong></span> reads cmdfile for a list of
commands to execute. <span><strong class="command">Network Expect</strong></span> may also
be invoked implicitly on systems which support the #! notation
by marking the script executable, and making the first line in
your script:
</p><pre class="programlisting">
#!/usr/bin/nexp -f</pre><p>
Of course, the path must accurately describe where
<span><strong class="command">Network Expect</strong></span> lives. /usr/bin is just an
example.
</p><p>
The <code class="option">-c</code> flag prefaces a command to be executed
before any in the script. The command should be quoted to
prevent being broken up by the shell. This option may be used
multiple times. Multiple commands may be executed with a single
<code class="option">-c</code> by separating them with semicolons.
Commands are executed in the order they appear.
</p><p>
<code class="option">-V</code> causes <span><strong class="command">Network Expect</strong></span> to
print its version number and exit.
</p><p>
The <code class="option">-s</code> flag allows to specify a random seed
that will cause predicatibility of pseudo-random numbers
generated by <span><strong class="command">Network Expect</strong></span> during execution
of a script. In cases where <span><strong class="command">Network Expect</strong></span> is
used as a protocol fuzzer, this option is useful to be able to
re-generate a specific test case.
</p><p>
<code class="option">-v</code> increases the verbosity level. Some commands
display additional information when the verbosity level is
higher.
</p><p>
The <code class="option">-t</code> flag changes the display format used by
commands that display dates or generate strings that represent
dates.
</p><p>
Optional args are constructed into a list and stored in the
variable named argv. argc is initialized to the length of argv.
</p><p>
argv0 is defined to be the name of the script (or binary if no
script is used). For example, the following prints out the name
of the script and the first three arguments:
</p><pre class="programlisting">
puts "$argv0 [lrange $argv 0 2]"</pre></div><div class="refsect1" lang="en"><a name="id2488044"></a><h2>NETWORK LISTENERS AND SPEAKERS</h2><p>
An integral part of <span><strong class="command">Network Expect</strong></span> is the
concept of network <span class="emphasis"><em>listeners</em></span> and network
<span class="emphasis"><em>speakers</em></span>, which are the <span><strong class="command">Network
Expect</strong></span> equivalent to spawned processes in Don Libes'
<span><strong class="command">Expect</strong></span> world. In <span><strong class="command">Expect</strong></span>,
the <span><strong class="command">send</strong></span> command sends data to a spawned
process, and the <span><strong class="command">expect</strong></span> command waits for a
specific pattern in the data received from a spawned process.
</p><p>
In <span><strong class="command">Network Expect</strong></span>, the command to send data
to the network, called <span><strong class="command">send_network</strong></span>, uses a
<span class="emphasis"><em>speaker</em></span> that specifies how the traffic will
be injected. <span><strong class="command">Network Expect</strong></span> speakers can
specify IPv4 or IPv6 sockets, in which case packets will be
injected at layer 3 and will be routed by the operating system
kernel. <span><strong class="command">Network Expect</strong></span> speakers can also
specify a physical interface in which case the packet will be
injected at layer 2. And finally, <span><strong class="command">Network
Expect</strong></span> speakers can specify a PCAP file (also known as
a "savefile") in which case packets will be written to this file
instead of injected to the network.
</p><p>
<span><strong class="command">Network Expect</strong></span> listeners, on the other hand,
specify where packets will be read from. Listeners can be
associated with either a physical interface, or with a PCAP
file. In either case an optional PCAP filter can be associated
with the listener to limit the type of packets the listener will
return. When reading from a PCAP file the inter-packet delay
between packets can be kept, or packets can be read at full
speed.
</p><p>
Both listeners and speakers are created with the
<span><strong class="command">Network Expect</strong></span> command
<span><strong class="command">spawn_network</strong></span>, just as in Don Libes'
<span><strong class="command">Expect</strong></span> spawned processes are created with
the <span><strong class="command">spawn</strong></span> command.
</p><p>
The way to specify network listeners and speakers in commands
that require them is the same regardless of the command. Network
listeners are always specified using the <span class="emphasis"><em>-i</em></span> switch (think
<span class="emphasis"><em>i</em></span>nput) followed by the name of the
listener, and network speakers are specified using the <span class="emphasis"><em>-o</em></span> switch (think
<span class="emphasis"><em>o</em></span>utput) followed by the name of the
speaker.
</p><p>
Network isteners and speakers are created using the
<span><strong class="command">spawn_network</strong></span> command. Just as in Don Libes'
<span><strong class="command">Expect</strong></span> one cannot choose the spawn ID
returned by the <span><strong class="command">spawn</strong></span> command, in
<span><strong class="command">Network Expect</strong></span> it is not possible to choose
the name of a network listener or speaker. However, one can
assign the network listener or speaker name to a variable and
use that variable whenever a network listener or speaker needs
to be specified.
</p><p>
What follows are a few examples of creation of network listeners
and speakers:
</p><pre class="programlisting">
spawn_network -i eth1 icmp and src host 172.16.1.1</pre><p>
This command creates a network listener on interface eth1 and
assigns the filter "icmp and src host 172.16.1.1", i.e. "listen
only for ICMP messages coming from host 172.16.1.1". Since the
<span class="emphasis"><em>-o</em></span> switch has not been specified
this command will not create a network speaker.
</p><pre class="programlisting">
spawn_network -o eth0 -r /tmp/packets.pcap tcp and host 172.16.1.1</pre><p>
This command creates a network speaker on interface eth0 and a
network listener that will read from the PCAP file
"/tmp/packets.pcap" TCP segments to or from the host 172.16.1.1.
</p><pre class="programlisting">
spawn_network -nolistener -6</pre><p>
This will create a network speaker for injecting IPv6 packets at
layer 3. Since the <span class="emphasis"><em>-nolistener</em></span>
switch has been specified, this command only creates a network
speaker and no listener.
</p><pre class="programlisting">
spawn_network -nolistener -w /tmp/mypackets.pcap</pre><p>
This only creates a speaker that will write packets to the PCAP
file "/tmp/mypackets.pcap". Note that when writing packets to a
PCAP file, injection implicitley takes place at layer 2, and
using an Ethernet header. This must be taken into consideration
when specifying the packet to send using the
<span><strong class="command">send_network</strong></span> command.
</p><p>
<span><strong class="command">Network Expect</strong></span> listeners and speakers play a
very important role in the operation of <span><strong class="command">Network
Expect</strong></span> so becoming confortable with them is key to
understanding <span><strong class="command">Network Expect</strong></span>'s philosophy.
</p></div><div class="refsect1" lang="en"><a name="id2488298"></a><h2>NUMERIC SPECIFICATIONS</h2><p>When defining packets, <span><strong class="command">Network Expect</strong></span>
allows to specify values for most fields in protocol headers
using a syntax that gives great flexibility. This syntax allows
to make the value of a field <span class="emphasis"><em>change</em></span> with
each packet that is created. The syntax is better presented with
an example. Suppose that we have an hypothetical command-line
switch <span class="emphasis"><em>-z</em></span> that is used to specify a 16-bit
value (please note that the same syntax is used for 8, 16 and
32-bit quantities) in the header of certain protocol.
</p><div class="itemizedlist"><ul type="disc"><li><p>
<span class="emphasis"><em>-z telnet</em></span> (fixed): the generated value
will always be 23, telnet's port number.
</p></li><li><p>
<span class="emphasis"><em>-z 23</em></span> (fixed): the generated value will
always be 23.
</p></li><li><p>
<span class="emphasis"><em>-z 23+</em></span> (increment): the generated value
will be 23 initially, and will be incremented by one with
each successive packet.
</p></li><li><p>
<span class="emphasis"><em>-z 23-</em></span> (increment): the generated value will
be 23 initially, and will be decremented by one with each
successive packet.
</p></li><li><p>
<span class="emphasis"><em>-z 23+5</em></span> (decrement): the generated
value will be 23 initially, and will be incremented by 5
with each successive packet.
</p></li><li><p>
<span class="emphasis"><em>-z 23-5</em></span> (decrement): the generated
value will be 23 initially, and will be decremented by 5
with each successive packet.
</p></li><li><p>
<span class="emphasis"><em>-z 23:25</em></span> (range): the generated value
will start with 23, will be incremented by one until it
reaches 25, and then will go back to 23.
</p></li><li><p>
<span class="emphasis"><em>-z 25:23</em></span> (range): the generated value
will start with 25, will be decremented by one until it
reaches 23, and then will go back to 25.
</p></li><li><p>
<span class="emphasis"><em>-z random</em></span>: the generated value will be
a random number in each successive packet.
</p></li></ul></div></div><div class="refsect1" lang="en"><a name="id2488435"></a><h2>COMMANDS</h2><p>
<span><strong class="command">Network Expect</strong></span> uses Tcl (Tool Command
Language). Tcl provides control flow (e.g., if, for, break),
expression evaluation and several other features such as
recursion, procedure definition, etc. Commands used here but
not defined (e.g., set, if, exec) are Tcl commands (see
tcl(3)). <span><strong class="command">Network Expect</strong></span>
introduces additional commands, described below. Unless
otherwise specified, commands return the empty string.
</p><p>
Commands are listed alphabetically so that they can be quickly
located. However, new users may find it easier to start by
reading the descriptions of <span><strong class="command">spawn_network</strong></span>,
<span><strong class="command">send_network</strong></span>,
<span><strong class="command">expect_network</strong></span>, and
<span><strong class="command">send_expect</strong></span>, in that order.
</p><div class="variablelist"><dl><dt><span class="term">
<span><strong class="command">barray</strong></span> <span class="emphasis"><em>new | length | delete |
examine | dump | slice | cmp | string
<args></em></span>
</span></dt><dd><p>
The <span><strong class="command">barray</strong></span> command is used to perform
management of variables of type <code class="varname">barray</code>
(byte array.)
</p><p>
<span><strong class="command">barray new <payload spec></strong></span> will
create and return a new <code class="varname">barray</code>
variable.
</p><p>
<span><strong class="command">barray length <barray variable></strong></span>
will return the number of bytes that the barray variable
uses.
</p><p>
<span><strong class="command">barray delete <barray variable></strong></span>
will delete a <code class="varname">barray</code> variable.
</p><p>
<span><strong class="command">barray examine <barray variable>
[/<FMT>] [<offset>]</strong></span> allows to
inspect the contents of the specified
<code class="varname">barray</code> variable, starting at the
optional <code class="option">offset</code>, and using the format
optionally specified with
<code class="option">/FMT</code>. <code class="option">/FMT</code> can include
an optional count followed by the display format, '<'
or '>' to specify little endian or big endian, and the
size of each element to display. Display formats are
strings ('s'), octal ('o'), hexadecimal ('x'), signed
decimal ('d'), and unsigned decimal ('u'). Sizes are byte
('b'), half-word ('h'), and word ('w').
</p><p>
<span><strong class="command">barray dump <barray variable></strong></span>
produces a hexadecimal dump of the specified
<code class="varname">barray</code> variable.
</p><p>
<span><strong class="command">barray slice <barray variable> <slice
spec></strong></span> returns a slice of the specified
<code class="varname">barray</code> variable. <code class="option">slice
spec</code> must be in the format <[start]:[end]>
where <code class="option">start</code> and <code class="option">end</code> are
offset into the <code class="varname">barray</code> variable. If
<code class="option">start</code> is ommited the start offset is 0,
and if <code class="option">end</code> is ommited the end offset is
the end of the <code class="varname">barray</code> variable.
</p><p>
<span><strong class="command">barray cmp <barray variable 1> <barray
variable 2></strong></span> compares two
<code class="varname">barray</code> variables and returns an integer
less than, equal to, or greater than zero if the first
<code class="option">n</code> bytes of <code class="varname">barray variable
1</code> are found, respectively, to be less than, to
match, or be greater than the first <code class="option">n</code>
bytes of <code class="varname">barray variable
2</code>. <code class="option">n</code> is calculated to be the
minimum of the lengths of both <code class="varname">barray</code>
variables.
</p></dd><dt><span class="term">
<span><strong class="command">close_network</strong></span>
<span class="emphasis"><em><listener/speaker name></em></span>
</span></dt><dd><p>
This command closes the network listener and/or speaker
referenced by <span class="emphasis"><em><listener/speaker
name></em></span>. Closing a network listener is not a
very important thing to do since the operation just
releases system resources used by the listener. However,
closing a network speaker is a very important, especially
when the speaker is associated with a PCAP file since
closing the speaker closes the associated PCAP file.
</p></dd><dt><span class="term">
<span><strong class="command">expect_network</strong></span> <span class="emphasis"><em>[-timeout
<timeout>]</em></span> <span class="emphasis"><em>[-i <listener
ID>] {condition1} {body1}</em></span> ... <span class="emphasis"><em>[-i
<listener ID>] {conditionN} {bodyN}</em></span>
</span></dt><dd><p>
The <span><strong class="command">expect_network</strong></span> command waits for
network traffic from one or more network listeners
(specified with the <code class="option">-i</code> option) until a
condition evaluates to true, until a specified time period
has passed, or until an end-of-file is seen (when the
listener is associated with a PCAP file.)
</p><p>
Conditions from the most recent
<span><strong class="command">expect_network_before</strong></span> command are
implicitly used before any other conditions. Conditions
from the most recent
<span><strong class="command">expect_network_after</strong></span> command are
implicitly used after any other conditions.
</p><p>
Conditions are regular Tcl expressions, and bodies are
sets of Tcl commands. If the final body is empty, it may
be omitted.
</p><p>
If the arguments to the entire
<span><strong class="command">expect_network</strong></span> statement require more
than one line, all the arguments may be "braced" into one
so as to avoid terminating each line with a backslash. In
this one case, the usual Tcl substitutions will occur
despite the braces.
</p><p>
If a condition is the keyword <code class="varname">eof</code>, the
corresponding body is executed upon end-of-file (this is
only meaningful when a listener is reading from a PCAP
file, not on a live capture from an interface.) If a
condition is the keyword <code class="varname">timeout</code>, the
corresponding body is executed upon timeout. If no
<code class="varname">timeout</code> keyword is used, an implicit
null action is executed upon timeout. The default timeout
period is 0.5 seconds but may be set, for example to 30,
by the command "set timeout 30". An infinite timeout may
be designated by the value -1.
</p><p>
If a condition is true, then the corresponding body is
executed. <span><strong class="command">expect_network</strong></span> returns the
result of the body (or the empty string if no condition
was true.) In the event that multiple conditions match,
the one appearing first is used to select a body.
</p><p>
Each time a new packet arrives, it is decoded and the
conditions are evaluated in the order they are listed.
When a packet is decoded, the values of the different
fields in the packet are made available to Tcl scripts via
Tcl variables. Scripts can use these values in an
condition for a <span><strong class="command">expect_network</strong></span> command.
</p><p>
In the following example, a listener for ARP requests on
interface eth0 is created and then an
<span><strong class="command">expect_network</strong></span> command is used to wait
for actual ARP requests and respond to them with an ARP
reply:
</p><pre class="programlisting">
# Spawn a listener for ARP requests
spawn_network -i eth0 host 192.168.1.1 and {arp[6:2]} == 1
expect_network {1} {
# Received an ARP request, send ARP reply
send_network -o eth0 \
ether(src = $mymac, dst = $arp(sha) )/ \
arp-reply(tha = $arp(sha), tip = $arp(sip), \
sha = $mymac, sip = 192.168.1.1)
nexp_continue
}
</pre><p>
The <code class="option">-timeout</code> flag causes the current
<span><strong class="command">expect_network</strong></span> command to use the
following value as a timeout instead of using the value of
the <code class="varname">timeout</code> variable.
</p><p>
Actions such as <span><strong class="command">break</strong></span> and
<span><strong class="command">continue</strong></span> cause control structures
(i.e., <span><strong class="command">for</strong></span>, <span><strong class="command">proc</strong></span>) to
behave in the usual way. The command
<span><strong class="command">nexp_continue</strong></span> allows
<span><strong class="command">expect_network</strong></span> itself to continue
executing rather than returning as it normally would.
</p><p>
This is useful for avoiding explicit loops or repeated
<span><strong class="command">expect_network</strong></span> statements. The
following example is part of a fragment to respond to ICMP
echo and ARP requests. The
<span><strong class="command">nexp_continue</strong></span> avoids having to write a
second <span><strong class="command">expect_network</strong></span> statement (to
look for the requests again.)
</p><pre class="programlisting">
# Spawn a listener for ARP requests
spawn_network -i $interface host $myip and {arp[6:2]} == 1
set arpl $listener_id
# Spawn a listener for ICMP messages sent to us
spawn_network -i $interface icmp and dst host $myip
set icmpl $listener_id
expect_network -i $arpl {1} {
# Received an ARP request, send ARP reply
send_network -o $interface \
ether(src = $mymac, dst = $arp(sha) )/ \
arp-reply(tha = $arp(sha), tip = $arp(sip), \
sha = $mymac, sip = $myip)
nexp_continue
} -i $icmpl {$icmp(type) == 8} {
# Received ICMP echo request, send echo reply
send_network -o ip \
ip(src = $myip, dst = $ip(src) )/ \
icmp-echoreply(id = $icmp(id), seq = $icmp(seq) )/ \
raw($raw)
nexp_continue
}
</pre></dd><dt><span class="term">
<span><strong class="command">expect_network_after</strong></span>
<span class="emphasis"><em>[expect_args]</em></span>
</span></dt><dd><p>
works identically to the
<span><strong class="command">expect_network_before</strong></span> except that if
conditions from both <span><strong class="command">expect_network</strong></span> and
<span><strong class="command">expect_network_after</strong></span> evaluate to true,
the <span><strong class="command">expect_network</strong></span> conditions is used.
See the <span><strong class="command">expect_network_before</strong></span> command
for more information.
</p></dd><dt><span class="term">
<span><strong class="command">expect_network_background</strong></span>
<span class="emphasis"><em>[expect_args]</em></span>
</span></dt><dd><p>
takes the same arguments as
<span><strong class="command">expect_network</strong></span>, however it returns
immediately. Conditions are evaluated whenever a new
packet arrives. The expression <code class="varname">timeout</code>
and <code class="varname">default</code> are meaningless to
<span><strong class="command">expect_network_background</strong></span> and are
silently discarded. Otherwise, the
<span><strong class="command">expect_network_background</strong></span> command uses
<span><strong class="command">expect_network_before</strong></span> and
<span><strong class="command">expect_network_after</strong></span> conditions just
like <span><strong class="command">expect_network</strong></span> does.
</p><p>
Please note that if a condition of the
<span><strong class="command">expect_network_background</strong></span> command
evaluates to true, the command will not continue to listen
for traffic unless the body includes a
<span><strong class="command">nexp_continue</strong></span> command that forces
<span><strong class="command">expect_network_background</strong></span> to continue
executing.
</p></dd><dt><span class="term">
<span><strong class="command">expect_network_before</strong></span>
<span class="emphasis"><em>[expect_args]</em></span>
</span></dt><dd><p>
takes the same arguments as
<span><strong class="command">expect_network</strong></span>, however it returns
immediately. Condition-action pairs from the most recent
<span><strong class="command">expect_network_before</strong></span> with the same
network listener ID are implicitly added to any following
<span><strong class="command">expect_network</strong></span> commands. If a condition
evaluates to true, it is treated as if it had been
specified in the <span><strong class="command">expect_network</strong></span> command
itself, and the associated body is executed in the context
of the <span><strong class="command">expect_network</strong></span> command. If
conditions from both
<span><strong class="command">expect_network_before</strong></span> and
<span><strong class="command">expect_network</strong></span> can evaluate to true,
the <span><strong class="command">expect_network_before</strong></span> condition is
used.
</p><p>
Unless overridden by a <code class="option">-i</code> flag,
<span><strong class="command">expect_network_before</strong></span> conditions are
evaluated against the network listener ID defined at the
time that the <span><strong class="command">expect_network_before</strong></span>
command was executed (not when its condition evaluated to
true.)
</p><p>
The <code class="option">-info</code> flag causes
<span><strong class="command">expect_network_before</strong></span> to return the
current specifications of what conditions will be
evaluated. By default, it reports on the current network
listener. An optional network listener ID specification
may be given for information on that network listener.
</p><p>
Instead of a network listener specification, the flag
<code class="option">-all</code> will cause <code class="option">-info</code> to
report on all network listeners.
</p><p>
The output of the <code class="option">-info</code> flag can be
reused as the argument to
<span><strong class="command">expect_network_before</strong></span>.
</p></dd><dt><span class="term">
<span><strong class="command">iflist</strong></span>
<span class="emphasis"><em>[<-refresh>]</em></span>
</span></dt><dd><p>
<span><strong class="command">iflist</strong></span> returns a list that contains the
name of all interfaces in the system. This list is built
when <span><strong class="command">Network Expect</strong></span> starts. If the
optional argument <code class="option">-refresh</code> is specified
then the list is refreshed before returning it.
</p></dd><dt><span class="term">
<span><strong class="command">nexp_continue</strong></span>
</span></dt><dd><p>
The command <span><strong class="command">nexp_continue</strong></span> allows expect
itself to continue executing rather than returning as it
normally would.
</p></dd><dt><span class="term">
<span><strong class="command">nexp_version</strong></span> <span class="emphasis"><em>[[-exit]
<version>]</em></span>
</span></dt><dd><p>
is useful for assuring that the script is compatible with
the current version of <span><strong class="command">Network Expect</strong></span>.
</p><p>
With no arguments, the current version of Expect is returned.
This version may then be encoded in your script. If you actually
know that you are not using features of recent versions, you can
specify an earlier version.
</p><p>
Versions consist of two numbers separated by dots. First
is the major number. Scripts written for versions of
<span><strong class="command">Network Expect</strong></span> with a different major
number will almost certainly not work.
<span><strong class="command">nexp_version</strong></span> returns an error if the
major numbers do not match.
</p><p>
Second is the minor number. Scripts written for a version
with a greater minor number than the current version may
depend upon some new feature and might not run.
<span><strong class="command">nexp_version</strong></span> returns an error if the
major numbers match, but the script minor number is
greater than that of the running <span><strong class="command">Network
Expect</strong></span>.
</p><p>
With the -exit flag, Expect prints an error and exits if
the version is out of date.
</p></dd><dt><span class="term">
<span><strong class="command">outif</strong></span> <span class="emphasis"><em><target></em></span>
</span></dt><dd><p>
The <span><strong class="command">outif</strong></span> command returns the outgoing
interface that would be used to reach
<code class="option">target</code>. <code class="option">target</code> can be an
IP address or host name.
</p></dd><dt><span class="term">
<span><strong class="command">packet</strong></span> <span class="emphasis"><em>decode | hash | data | dump
| ts | tdelta <args></em></span>
</span></dt><dd><p>
The <span><strong class="command">packet</strong></span> command allows to manage
variables of type <code class="varname">packet</code>.
</p><p>
<span><strong class="command">packet decode <packet variable></strong></span>
will decode the specified <code class="varname">packet</code>
variable, just as if the packet was received during the
execution of a <span><strong class="command">expect_network</strong></span> command.
</p><p>
<span><strong class="command">packet hash <packet variable></strong></span>
calculates a hash of the specified
<code class="varname">packet</code> variable.
</p><p>
<span><strong class="command">packet data <packet variable></strong></span>
returns a <code class="varname">barray</code> variable that contains
all of the packet's data.
</p><p>
<span><strong class="command">packet dump <packet variable></strong></span>
displays an hexadecimal dump (on standard output) of the
<code class="varname">packet</code>'s data.
</p><p>
<span><strong class="command">packet ts <packet variable></strong></span> will
return a <code class="varname">timeval</code> variable that
corresponds to the timestamp associated with the specified
<code class="varname">packet</code> variable.
</p><p>
<span><strong class="command">packet tdelta <packet variable 1>
<packet variable 2></strong></span> calculates the time
delta between the <code class="varname">timestamp</code> associated
with <packet variable 1> and the
<code class="varname">timestamp</code> associated with <packet
variable 2>.
</p></dd><dt><span class="term">
<span><strong class="command">pdu</strong></span> <span class="emphasis"><em><new | delete | dup |
append | count | build | list> <args></em></span>
</span></dt><dd><p>
The <span><strong class="command">pdu</strong></span> command is used to manage
variables of type <code class="varname">pdu</code> (Protocol Data
Unit.)
</p><p>
<span><strong class="command">pdu new <PDU definition></strong></span> will
create and return a new <code class="varname">pdu</code>
variable. The PDU definition is the same as used in other
<span><strong class="command">Network Expect</strong></span> commands like
<span><strong class="command">send_network</strong></span> and
<span><strong class="command">send_expect</strong></span>.
</p><p>
<span><strong class="command">pdu delete <pdu variable></strong></span> will
delete a <code class="varname">pdu</code> variable.
</p><p>
<span><strong class="command">pdu dup <pdu variable></strong></span> will
duplicate a <code class="varname">pdu</code> variable.
</p><p>
<span><strong class="command">pdu append <pdu variable 1> <pdu
variable 2></strong></span> will append the
<code class="varname">pdu</code> variable <code class="varname">pdu variable
2</code> to the <code class="varname">pdu</code> variable
<code class="varname">pdu variable 1</code>. The result is
<code class="varname">pdu variable 1</code>.
</p><p>
<span><strong class="command">pdu count <pdu variable></strong></span> returns
the number of packets that would be generated if that PDU
were to be sent using the <span><strong class="command">send_network</strong></span>
command, for example. The number of packets depends on the
numeric specifications used in the definition of the PDU.
</p><p>
<span><strong class="command">pdu build <pdu variable></strong></span> builds
the specified <code class="varname">pdu</code> variable and returns
a variable of type <code class="varname">packet</code>.
</p><p>
<span><strong class="command">pdu list</strong></span> displays the list of PDUs that
<span><strong class="command">Network Expect</strong></span> knows about, i.e. the
PDUs that can be created via certain <span><strong class="command">Network
Expect</strong></span> commands like
<span><strong class="command">send_network</strong></span>,
<span><strong class="command">send_expect</strong></span>, and <span><strong class="command">pdu
new</strong></span>.
</p></dd><dt><span class="term">
<span><strong class="command">random</strong></span> <span class="emphasis"><em>[x:y | number x:y | ip
[a.b.c.d/nn | a.b.c.d:e.f.g.h] | mac]</em></span>
</span></dt><dd><p>
The <span><strong class="command">random</strong></span> command can be used to
obtain a variety of random objects. Current supported
objects are numbers, IPv4 addresses, and MAC addresses. It
is possible to obtain a random number or IP address from
within a range. In the case of numbers the range is
specified using the notation x:y. In the case of IP
addresses the range can be specified using the IP
address/netmask bits notation, or using the
a.b.c.d:e.f.g.h notation. If no object is specified then a
random number is returned.
</p></dd><dt><span class="term">
<span><strong class="command">send_expect</strong></span> <span class="emphasis"><em>[-i <listener
ID>] [-o <speaker ID>] [-timeout <timeout>]
[-n <number of tries>] <PDU definition></em></span>
</span></dt><dd><p>
This command sends a number of packets to a target or list
of targets and then waits for responses. After responses
are received, the command matches sent packets with
received packets. This command was inspired by
the <span><strong class="command">sr()</strong></span> family of commands in
<span><strong class="command">Scapy</strong></span>, the packet manipulation tool by
Philippe Biondi.
</p><p>
The <code class="option">-i</code> flag specifies what listener to
use to read responses. It is possible to specify multiple
listeners by using this option multiple times.
</p><p>
The <code class="option">-o</code> flag specifies what speaker to use
when injecting the stimulus.
</p><p>
<code class="option">-timeout</code> specifies how long to wait,
after all packets have been sent, for answers to the
injected stimulus. The timeout is specified in seconds and
the default is 1 second.
</p><p>
<code class="option">-n</code> specifies how many times to retry
sending the stimulus if not all sent packets have a
corresponding received answer.
</p><p>
The definition of the PDU to send is the same as it used
in the <span><strong class="command">send_network</strong></span> command.
</p><p>
The <span><strong class="command">send_expect</strong></span> command creates three
lists: the list of sent packets and the corresponding list
of matching answers, and the list of unanswered packets. A
script can then use these lists, decode packets, and
present some useful information. For example, the
following code snippet sends ICMP echo requests to all hosts
in a network a displays which hosts replied:
</p><pre class="programlisting">
set network 192.168.1.1-192.168.1.254
# Spawn a listener. We don't really have to specify a filter,
# like in "spawn_network {icmp[icmptype] == icmp-echoreply}"
# because the send_expect command will intelligently match
# injected stimulus (ICMP echo requests) with received
# answers (ICMP echo replies). A filter means less work for
# the send_expect command, but other than that it adds nothing.
spawn_network
send_expect -n 2 -4 -D $network -icmp-echo random:random \
-payload "12345678901234567890" -delay .001
puts "\n[llength $_(received)] hosts sent echo-replies back:\n"
foreach r $_(received) s $_(sent) {
packet decode r
puts [format "$pdu(1,tot_len) bytes from $ip(src): ttl=$ip(ttl) time=%.3f ms" [expr [packet tdelta r s]*1000] ]
}
</pre></dd><dt><span class="term">
<span><strong class="command">send_network</strong></span> <span class="emphasis"><em>[options] <PDU
definition></em></span>
</span></dt><dd><p>
This command allows the creation custom TCP/IP packets
based on packet definitions provided by the user through
command-line arguments passed to the program, through a
command file specified in the command-line, or through a
mix of these two methods. <span><strong class="command">send_network</strong></span>
refers to instances of protocols in the TCP/IP protocol
suite as <span class="emphasis"><em>protocol data units</em></span>, or PDUs
for short. A protocol data unit always has a header (think
of the IP version 4 header, or the TCP header), may or may
not have options (think of the TCP options, or IP version
6 extension headers), and may or may not have a
payload. Due to the extensive use of encapsulation in the
TCP/IP protocol suite, the payload of a PDU can be another
payload. For example, an Ethernet frame can have as its
payload an IP version 4 packet, which in itself is another
PDU. Then this IP version 4 packet can have as its payload
a TCP segment, which is another PDU, and so on.
</p><p>
When defining PDUs that <span><strong class="command">send_network</strong></span>
will create you start by creating the definitions for PDUs
that are closer to the physical layer, and then move up
the protocol stack until you reach, in some cases, and if
that is what you want, the application layer. To implement
this approach through a command-line interface (CLI) you
start by entering the lower-level PDUs closer to the
beginning of the command-line, or, if you are reading your
packet definitions from a file, by entering definitions
for lower-level PDUs at the beginning of the file. After
you have defined a certain PDU you cannot define another
PDU that is lower than the previous in the protocol
stack. For example, you cannot define a TCP segment and
then create an Ethernet frame as the segment's payload,
just because that does not make sense (defining a TCP
segment and then a BGP message does make sense, for
example.) For this reason, order does matter in the
command-line (or in the file, if defining PDUs in a file)
when creating the PDUs.
</p></dd><dt><span class="term">
<span><strong class="command">sleep</strong></span> <span class="emphasis"><em><seconds></em></span>
</span></dt><dd><p>
causes the script to sleep for the given number of
seconds. <code class="option">seconds</code> may be a decimal
number. Interrupts are processed while <span><strong class="command">Network
Expect</strong></span> sleeps.
</p></dd><dt><span class="term">
<span><strong class="command">spawn_network</strong></span>
<span class="emphasis"><em>[-nolistener]</em></span>
<span class="emphasis"><em>[-fullspeed]</em></span>
<span class="emphasis"><em>[-info]</em></span> <span class="emphasis"><em>[-i <input
interface>]</em></span> <span class="emphasis"><em>[-o <output
interface>]</em></span> <span class="emphasis"><em>[-r <input PCAP
file>]</em></span> <span class="emphasis"><em>[-p]</em></span> <span class="emphasis"><em>[-s
<snaplen>]</em></span> <span class="emphasis"><em>[-w <output PCAP
file>]</em></span> <span class="emphasis"><em>[-hexdump]</em></span>
<span class="emphasis"><em>[-stdout]</em></span> <span class="emphasis"><em>[-4]</em></span>
<span class="emphasis"><em>[-6]</em></span> <span class="emphasis"><em>[<PCAP
filter>]</em></span>
</span></dt><dd><p>
The <span><strong class="command">spawn_network</strong></span> command is used to
find information about existing listeners and speakers or
to create network speakers and network listeners.
</p><p>
To find information about existing network listeners and
speakers the <span><strong class="command">spawn_network</strong></span> command
needs to be invoked with only the <code class="option">-info</code>
option.
</p><p>
Listeners are created by using the options
<code class="option">-i</code> or
<code class="option">-r</code>. <code class="option">-i</code> specifies an
interface to listen for traffic on, and
<code class="option">-r</code> specifies the use of a PCAP file for
reading instead of reading live traffic from an
interface. In both cases, an optional <code class="option">PCAP
filter</code> can be specified to limit the type of
traffic that will be read. If <code class="option">-i</code> is not
used then <span><strong class="command">Network Expect</strong></span> will try to
find a suitable interface. <code class="option">-fullspeed</code>
causes reading from a PCAP file at full speed, without
preserving the inter-packet delay present in the
savefile. If this option is not specified then the
inter-packet delay present in the savefile will not be
preserved.
</p><p>
Speakers are created using one of <code class="option">-o</code>,
<code class="option">-w</code>, <code class="option">hexdump</code>,
<code class="option">stdout</code>, <code class="option">-4</code>, or
<code class="option">-6</code>. <code class="option">-o</code> specifies the use
of an interface for injecting traffic into the
network. This implies the use of layer 2 injection in
which the Ethernet header is specified by the user.
<code class="option">-w</code> specifies that all traffic will be
sent to the PCAP file <code class="option">PCAP file</code>. This
option requires the use of layer 2 injection, i.e. the
Ethernet header must be
included. <code class="option">-hexdump</code> specifies that all
packets be sent to standard output in hexadecimal format.
<code class="option">-stdout</code> causes all packets to be sent to
standard output in raw format. The <code class="option">-4</code> and
<code class="option">-6</code> options specify the creation of layer
3 speakers to inject IPv4 and IPv6 traffic
respectively. In this case all routing decisions are
performed by the kernel.
</p><p>
The <code class="option">-p</code> and <code class="option">-s</code> options
are only meaningful for listener
creation. <code class="option">-p</code> specifies that the interface
be not put in promiscuous mode when creating a listener
that will listen on an interface. <code class="option">-s</code>
specifies the snapshot length of captured packets. These
two options are equivalent to the
tcpdump(8) options of the same names.
</p><p>
Note that if no options to create a speaker are specified,
i.e. <code class="option">-o</code>, <code class="option">-w</code>,
<code class="option">hexdump</code>, <code class="option">stdout</code>,
<code class="option">-4</code>, or <code class="option">-6</code>, the default
action is to only create a listener. If only a speaker is
desired one of the options to create a speaker must be
specified and the <code class="option">-nolistner</code> option must
be used.
</p><p>
As an example, the follow command creates a listener on
interface eth0 for ARP requests for the MAC address of
host 192.168.1.1:
</p><pre class="programlisting">
spawn_network -i eth0 host 192.168.1.1 and {arp[6:2]} == 1
</pre><p>
Note that curly braces are necessary around "arp[6:2]"
because brackets have special meaning in Tcl.
</p></dd><dt><span class="term">
<span><strong class="command">system</strong></span> <span class="emphasis"><em><system command>
[<args>]</em></span>
</span></dt><dd><p>
The <span><strong class="command">system</strong></span> command allows to run
arbitrary system commands from within a <span><strong class="command">Network
Expect</strong></span> script.
</p><p>
Please note that this command does no input validation at
all, which means that it is very insecure. For example
"system {ls;/bin/sh}" will give you a shell. If nexp is
run as roon then the shell will be a root shell.
</p><p>
The <span><strong class="command">exec</strong></span> command in the standard Tcl
distribution is a much better alternative to the
<span><strong class="command">Network Expect</strong></span>
<span><strong class="command">system</strong></span> command. Please see the Tcl
documentation for the <span><strong class="command">exec</strong></span> command for
additional information.
</p></dd><dt><span class="term">
<span><strong class="command">timeval</strong></span> <span class="emphasis"><em>new | delta
<args></em></span>
</span></dt><dd><p>
The <span><strong class="command">timeval</strong></span> command allows to manage
variables of type <code class="varname">timeval</code>.
</p><p>
<span><strong class="command">timeval new;</strong></span> returns a
<code class="varname">timeval</code> variable that corresponds to
the current system time.
</p><p>
<span><strong class="command">timeval tdelta <timeval variable 1>
<timeval variable 2></strong></span> calculates the time
delta between the <code class="varname">timeval variable 2</code> and
<code class="varname">timeval variable 1</code>.
</p></dd><dt><span class="term">
<span><strong class="command">txdelta</strong></span> <span class="emphasis"><em><speaker ID></em></span>
</span></dt><dd><p>
Every time a packet is sent, the time the packet was sent
is saved to the network speaker that was used to send
that packet. The <span><strong class="command">txdelta</strong></span> (short for
"transmission delta") command returns the number of
seconds that have elapsed since the last packet that was
sent via the specified <code class="option">speaker ID</code>.
</p><p>
The following example provides the foundation for a very
simple ping program, and shows how the
<span><strong class="command">txdelta</strong></span> command can be used:
</p><pre class="programlisting">
for {set id 0; set seq 0} {1} {incr seq} {
send_network -4 -D $target -icmp-echo $id:$seq -payload "12345678901234567890"
expect_network -timeout 1 {$icmp(type) == 0 && $icmp(id) == $id} {
puts [format "$pdu(2,tot_len) bytes from $ip(src): icmp_seq=$seq ttl=$ip(ttl) time=%.3f ms" [expr [txdelta ip]*1000 ] ]
sleep [expr 1.0 - [txdelta ip] ]
}
}
</pre></dd></dl></div></div><div class="refsect1" lang="en"><a name="id2490463"></a><h2>SPECIAL VARIABLES</h2><p>
Special variables.
</p></div><div class="refsect1" lang="en"><a name="id2490475"></a><h2>EXAMPLES</h2><p>
The <code class="filename">examples</code> directory in
the <span><strong class="command">Network Expect</strong></span> distribution contains
plenty of examples that should provide a very good idea of how
to use <span><strong class="command">Network Expect</strong></span>. A few selected
examples have been selected for this manual page.
</p><div class="orderedlist"><ol type="1"><li><p>
The following code snippet performs a TCP three-way
handshake. Everything is done by hand, which allows to play
with TCP initial sequence numbers (ISNs), window sizes, etc.
The code is a bit more complex that necessary because an
effort is made to handle error coditions (timeout, remote host
not listening on the destination port, strange combination of
TCP flags received in response to our initial SYN.)
</p><pre class="programlisting">
# Some useful constants
set SYN 0x02
set RST 0x04
set ACK 0x10
set retries 3
set isn [random]
set myip 192.168.1.2
set target 192.168.1.1
set sport [random 20000:65535]
set dport 21
set window 4096
# Spawn a listener for TCP segments coming from the FTP server to us
spawn_network -i $interface "tcp and src host $target and dst host $myip and src port $dport and dst port $sport"
# Send TCP SYN
send_network ip(src = $myip, dst = $target)/ \
tcp(src = $sport, dst = $dport, window = $window, \
syn, seq = $isn, ack-seq = 0)
# Wait for response from the server
expect_network {$tcp(flags) == ($SYN | $ACK)} {
# Got a SYN+ACK so we need to send the final segment of the
# 3-way HS
send_network ip(dst = $myip, dst = $target)/ \
tcp(dst = $tcp(dstport), dst = $tcp(srcport), \
window = $window, ack, seq = $tcp(ack), \
ack-seq = [expr $tcp(seq) + 1])
} {$tcp(flags) & $RST} {
puts "Connection refused"
exit 1
} {1} {
# Any other weird combination of TCP flags we respond to
# with a RST
send_network ip(src = $myip, dst = $target)/ \
tcp(src = $tcp(dstport), dst = $tcp(srcport), \
rst)
exit 1
} timeout {
# Our SYN got lost in transit or it was filtered - perform
# exponential backoff and retransmit the SYN...
if {$retries > 0} {
incr retries -1
set timeout [expr $timeout*2]
puts "SYN timeout, increasing timeout to $timeout"
send_network ip(src = $myip, dst = $target)/ \
tcp(src = $sport, dst = $dport, \
window = $window, syn, seq = $isn, \
ack-seq = 0)
nexp_continue
} else {
puts "Connection timed out"
exit 1
}
}
# TCP connection has been established. Now do something...
</pre></li><li><p>
This illustrates how to create a phantom host on the network
that can respond to ICMP echo requests, and therefore, to
ARP requests as well.
</p><pre class="programlisting">
set interface eth0
set myip 192.168.1.1
set mymac [random mac]
# Spawn a listener for ARP requests
spawn_network -i $interface host $myip and {arp[6:2]} == 1
set arpl $listener_id
# Spawn a listener for ICMP messages sent to us
spawn_network -i $interface icmp and dst host $myip
set icmpl $listener_id
expect_network_background -i $arpl {1} {
# Received an ARP request, send ARP reply
send_network -o $interface \
ether(src = $mymac, dst = $arp(sha) )/ \
arp-reply(tha = $arp(sha), tip = $arp(sip), \
sha = $mymac, sip = $myip)
nexp_continue
} -i $icmpl {$icmp(type) == 8} {
# Received ICMP echo request, send echo reply
send_network -o ip \
ip(src = $myip, dst = $ip(src) )/ \
icmp-echoreply(id = $icmp(id), seq = $icmp(seq) )/ \
raw($raw)
nexp_continue
}
</pre></li><li><p>
This example how to create a very simple traceroute program
that uses TCP probes to port 80 of the target host. It uses
the <span><strong class="command">send_expect</strong></span> command.
</p><pre class="programlisting">
set target "www.example.com"
set ttlrange "1:30"
set interface [outif $target]
# Spawn a listener. We don't really have to specify a filter because the
# send_expect command will intelligently match injected stimulus with
# received answers.
spawn_network -i $interface
send_expect -tries 2 -delay 0.001 \
ip(id = random, dst = $target, ttl = $ttlrange)/ \
tcp(src = random, dst = 80, syn)
foreach r $_(received) s $_(sent) {
packet decode r
set source $ip(src)
set pdu_type $pdu(1,type)
packet decode s
puts [format "$ip(ttl) $source %.3f ms $pdu_type" [expr [packet tdelta r s]*1000] ]
}
</pre></li><li><p>
This shows how to perform an ARP scan using regular
<span><strong class="command">send_network</strong></span> and
<span><strong class="command">expect_network</strong></span> commands:
</p><pre class="programlisting">
set interface eth0
set network "$iface($interface,ip)/$iface($interface,netmask)"
set arprequest [pdu new -o $interface
ether(dst = BROADCAST)/ \
arp-request(tha = BROADCAST, tip = $network, \
sha = $iface($interface,hw_addr), \
sip = $iface($interface,ip) ) ]
# Spawn a listener for ARP replies
spawn_network -i $interface {arp[6:2]} == 2
for {set i 0} {$i < [pdu count arprequest]} {incr i} {
# Send ARP request
send_network -count 1 arprequest
# Read ARP reply
expect_network -timeout .05 {1} {
puts "$arp(sip) is at $arp(sha)"
}
}
</pre></li><li><p>
This example shows how to do an ARP scan but in a more
efficient manner using the <span><strong class="command">send_expect</strong></span>
command:
</p><pre class="programlisting">
set interface eth0
set network "$iface($interface,ip)/$iface($interface,netmask)"
# Spawn a listener for ARP replies
spawn_network -i $interface {arp[6:2]} == 2
send_expect -o $interface -delay 0.001 -tries 2 \
ether(dst = BROADCAST)/ \
arp-request(tha = BROADCAST, tip = $network, \
sha = $iface($interface,hw_addr),
sip = $iface($interface,ip) )
puts "\nFound [llength $_(received)] hosts alive:\n"
foreach r $_(received) {
packet decode r
puts "$arp(sip) is at $arp(sha)"
}
</pre></li></ol></div></div><div class="refsect1" lang="en"><a name="id2490680"></a><h2>BUGS</h2><p>
<span><strong class="command">Network Expect</strong></span> does not run very well under
the Solaris operating system because in that operating system
select() does not seem to work well with packet capture file
descriptors (select() returns when there is no data ready to be
read.)
</p><p>
<span><strong class="command">Network Expect</strong></span> does not work at all in
Microsoft Windows because select() does not work at all with
packet capture file descriptors (pcap_get_selectable_fd() does
not exist under Microsoft Windows.)
</p><p>
Error-checking is almost non-existant.
</p><p>
These isn't input validation for the numeric specifications.
</p><p>
The parser of PDU definitions (and therefore the number of
tokens that the parser handles) has become a big, wild
beast. It's very easy to add new tokens and PDU definitions are
elegant and pretty, but the parser is huge. Guess can't have
everything.
</p></div><div class="refsect1" lang="en"><a name="id2490724"></a><h2>VERSION</h2><p>This man page is correct for version 1.0 of <span><strong class="command">Network
Expect</strong></span>.
</p></div><div class="refsect1" lang="en"><a name="id2490739"></a><h2>SEE ALSO</h2><p>
nexp-numspec(1),
nexp-payload(1),
nexp-ether(5),
nexp-gre(5),
nexp-ip(5),
nexp-mpls(5),
expect(1)
</p></div><div class="refsect1" lang="en"><a name="id2490774"></a><h2>AUTHOR</h2><p>
<span><strong class="command">Network Expect</strong></span> was written by Eloy Paris
<peloy@netexpect.org>. However, <span><strong class="command">Network
Expect</strong></span> borrows ideas from lots of Open Source tools
like Nemesis, Packit, hping, Expect, and Scapy. The
<span><strong class="command">Network Expect</strong></span> author is indebted to the
authors of these tools for their contribution.
</p><p>
This man page was written by Eloy Paris although it borrows
heavily from Expect's manual page.
</p></div></div></body></html>
|