First commit of the Windows Divert project
This commit is contained in:
@@ -0,0 +1,619 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
@@ -0,0 +1,5 @@
|
||||
Divert: Windows Packet Divert
|
||||
-----------------------------
|
||||
|
||||
For more information about this project, see doc/divert.html
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
!INCLUDE $(NTMAKEENV)\makefile.def
|
||||
+1912
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,9 @@
|
||||
LIBRARY divert
|
||||
EXPORTS
|
||||
DivertDllEntry
|
||||
DivertOpen
|
||||
DivertRecv
|
||||
DivertSend
|
||||
DivertClose
|
||||
DivertHelperParse
|
||||
DivertHelperCalcChecksums
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
# sources
|
||||
# (C) 2011, all rights reserved,
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
TARGETNAME=divert
|
||||
TARGETTYPE=DYNLINK
|
||||
TARGETPATH=..\install
|
||||
TARGETLIBS=\
|
||||
$(SDK_LIB_PATH)\advapi32.lib \
|
||||
$(SDK_LIB_PATH)\setupapi.lib \
|
||||
$(SDK_LIB_PATH)\user32.lib \
|
||||
$(SDK_LIB_PATH)\kernel32.lib \
|
||||
$(SDK_LIB_PATH)\ws2_32.lib
|
||||
C_DEFINES=/DWIN32 /D_WINDOWS /D_USRDLL /DUSE_STDAFX /DUNICODE /D_UNICODE
|
||||
DLLENTRY=DivertDllEntry
|
||||
DLLDEF=divert.def
|
||||
USE_MSVCRT=1
|
||||
INCLUDES=$(DDK_INC_PATH);$(KMDF_INC_PATH)\$(KMDF_VER_PATH);..\include
|
||||
SOURCES=divert.c
|
||||
|
||||
+938
@@ -0,0 +1,938 @@
|
||||
<!doctype html>
|
||||
<!--
|
||||
divert.html
|
||||
(C) 2011, all rights reserved,
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<title>Divert Documentation</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Divert: Windows Packet Divert</h1>
|
||||
<h2>Table of Contents</h2>
|
||||
<ul>
|
||||
<li><a href="#introduction">1. Introduction</a></li>
|
||||
<li><a href="#building">2. Building</a></li>
|
||||
<ul>
|
||||
<li><a href="#driver_signing">2.2 Driver Signing</a></li>
|
||||
</ul>
|
||||
<li><a href="#installing">3. Installing</a></li>
|
||||
<li><a href="#uninstalling">4. Uninstalling</a></li>
|
||||
<li><a href="#programming_api">5. Programming API</a></li>
|
||||
<ul>
|
||||
<li><a href="#divert_packet">5.1 DIVERT_PACKET</a></li>
|
||||
<li><a href="#divert_open">5.2 DivertOpen</a></li>
|
||||
<li><a href="#divert_recv">5.3 DivertRecv</a></li>
|
||||
<li><a href="#divert_send">5.4 DivertSend</a></li>
|
||||
<li><a href="#divert_close">5.5 DivertClose</a></li>
|
||||
</ul>
|
||||
<li><a href="#helper_programming_api">6. Helper Programming API</a></li>
|
||||
<ul>
|
||||
<li><a href="#divert_iphdr">6.1 DIVERT_IPHDR</a></li>
|
||||
<li><a href="#divert_ipv6hdr">6.2 DIVERT_IPV6HDR</a></li>
|
||||
<li><a href="#divert_icmphdr">6.3 DIVERT_ICMPHDR</a></li>
|
||||
<li><a href="#divert_icmpv6hdr">6.4 DIVERT_ICMPV6HDR</a></li>
|
||||
<li><a href="#divert_tcphdr">6.5 DIVERT_TCPHDR</a></li>
|
||||
<li><a href="#divert_udphdr">6.6 DIVERT_UDPHDR</a></li>
|
||||
<li><a href="#divert_helper_parse">6.7 DivertHelperParse</a></li>
|
||||
<li><a href="#divert_helper_calc_checksums">6.8 DivertHelperCalcChecksums</a></li>
|
||||
</ul>
|
||||
<li><a href="#filter_language">7. Filter Language</a></li>
|
||||
<ul>
|
||||
<li><a href="#filter_examples">7.2 Filter Examples</a></li>
|
||||
</ul>
|
||||
<li><a href="#samples">8. Samples</a></li>
|
||||
<li><a href="#known_issues">9. Known Issues</a></li>
|
||||
<li><a href="#license">10. License</a></li>
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
<a name="introduction"><h2>1. Introduction</h2></a>
|
||||
<p>
|
||||
This package (<tt>divert</tt>) provides user-mode packet
|
||||
capture/modification/blocking/re-injection for Windows Vista and later.
|
||||
</p>
|
||||
<p>
|
||||
The main features of the <tt>divert</tt> package are:
|
||||
<ul>
|
||||
<li> User-mode packet capture, dropping, modification.
|
||||
<li> Simple, high-level, programming API.
|
||||
<li> Fully documented with sample programs.
|
||||
<li> Full IPv6 support.
|
||||
<li> Modern WDF/WFP driver implementation, as opposed to an older
|
||||
WDM/NDIS intermediate driver.
|
||||
<li> Open source; Licensed under GNU General Public License version 3.
|
||||
See the <a href="#license">License</a> for more information.
|
||||
</ul>
|
||||
</p><p>
|
||||
The functionality provided by <tt>divert</tt> is very similar to
|
||||
<tt>DIVERT</tt> sockets in FreeBSD/MacOS and <tt>NETLINK</tt> sockets in Linux,
|
||||
as well as some commercial packages, e.g. <tt>WinPktFilter</tt>, for Windows.
|
||||
</p>
|
||||
|
||||
<hr>
|
||||
<a name="building"><h2>2. Building</h2></a>
|
||||
<p>
|
||||
To build the <tt>divert</tt> package from source, you must follow these
|
||||
steps:
|
||||
<ol>
|
||||
<li> Download and install the latest <a href="http://www.microsoft.com/whdc/devtools/wdk/default.mspx">Windows Driver Kit</a>.
|
||||
<li> Open a <i>Free Build Environment</i> console (or
|
||||
<i>Checked Build Environment</i> for debugging).
|
||||
<li> In the <tt>divert</tt> root directory, run the command:
|
||||
<pre>
|
||||
build -cZg
|
||||
</pre>
|
||||
This will build the following files and place them in the
|
||||
<tt>divert\install</tt> subdirectory:
|
||||
<ul>
|
||||
<li> <tt>divert.dll</tt>: User-mode library.
|
||||
<li> <tt>divert.sys</tt>: Kernel-mode WDF/WFP callout driver.
|
||||
<li> <tt>divert.inf</tt>: INF file for <tt>divert.sys</tt>.
|
||||
<li> <tt>WdfCoInstaller*.dll</tt>: Microsoft WDF co-installer for
|
||||
<tt>divert.sys</tt>.
|
||||
<li> <tt>*.exe</tt>: Sample applications from the <tt>divert\examples</tt>
|
||||
directory.
|
||||
</ul>
|
||||
</ol>
|
||||
</p>
|
||||
|
||||
<a name="driver_signing"><h3>2.2 Driver Signing</h3></a>
|
||||
<p>
|
||||
Before the <tt>divert</tt> package can be used, the <tt>divert.sys</tt> driver
|
||||
must be digitally signed.
|
||||
See <a href="http://msdn.microsoft.com/en-us/windows/hardware/gg487317.aspx">Driver Signing Requirements for Windows</a>
|
||||
for more information.
|
||||
</p>
|
||||
<p>
|
||||
Driver signing is <b>not</b> provided by this package, although this is
|
||||
something we wish to change in the future.
|
||||
If you wish to use this package, you must sign the driver yourself.
|
||||
</p>
|
||||
<p>
|
||||
If you wish to test this package, you can set up a <i>test certificate</i>.
|
||||
See <a href="http://msdn.microsoft.com/en-us/library/ff554705.aspx">
|
||||
Test-Signing Driver Packages</a> for more information.
|
||||
</p>
|
||||
|
||||
<hr>
|
||||
<a name="installing"><h2>3. Installing</h2></a>
|
||||
<p>
|
||||
The <tt>divert</tt> package does not require any special installation.
|
||||
Simply ensure that the <tt>divert.dll</tt>, <tt>divert.sys</tt>,
|
||||
<tt>divert.inf</tt>, and <tt>WdfCoInstaller*.dll</tt> files are in your
|
||||
application's home directory.
|
||||
</p>
|
||||
<p>
|
||||
The <tt>divert</tt> driver is installed on demand, i.e., when your application
|
||||
makes a call to <tt>DivertOpen()</tt> from <tt>divert.dll</tt>
|
||||
(see programming API below).
|
||||
</p>
|
||||
<p>
|
||||
The <tt>WdfCoInstaller*.dll</tt> file is relatively bloated compared to the
|
||||
other files.
|
||||
Blame Microsoft.
|
||||
</p>
|
||||
|
||||
<hr>
|
||||
<a name="uninstalling"><h2>4. Uninstalling</h2></a>
|
||||
<p>
|
||||
To uninstall, simply delete the <tt>divert.dll</tt>, <tt>divert.sys</tt>,
|
||||
<tt>divert.inf</tt>, and <tt>WdfCoInstaller*.dll</tt> files.
|
||||
If the <tt>divert</tt> driver was already demand-started, it will be removed
|
||||
automatically after the next reboot.
|
||||
To immediately remove it, your uninstaller can issue the following commands:
|
||||
<pre>
|
||||
sc stop divert
|
||||
sc delete divert
|
||||
</pre>
|
||||
Note however this is not recommended, as this may interfere with other
|
||||
applications using the <tt>divert</tt> package.
|
||||
</p>
|
||||
|
||||
<hr>
|
||||
<a name="programming_api"><h2>5. Programming API</h2></a>
|
||||
<p>
|
||||
To use the <tt>divert</tt> package, a program/application must:
|
||||
<ol>
|
||||
<li> Include the <tt>divert.h</tt> header file
|
||||
<pre>
|
||||
#include "divert.h"
|
||||
</pre>
|
||||
<li> Link or dynamically load the <tt>divert.dll</tt> dynamic link library.
|
||||
</ol>
|
||||
|
||||
<a name="divert_packet"><h3>5.1 DIVERT_PACKET</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
typedef struct
|
||||
{
|
||||
UINT8 Reserved[7];
|
||||
UINT8 Direction;
|
||||
UINT32 IfIdx;
|
||||
UINT32 SubIfIdx;
|
||||
} <b>DIVERT_PACKET</b>, *<b>PDIVERT_PACKET</b>;
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<p>
|
||||
<b>Fields</b>
|
||||
<ul>
|
||||
<li> <tt>Reserved</tt>: Reserved for internal use. This field may be
|
||||
left uninitialized.</li>
|
||||
<li> <tt>Direction</tt>: The packet's direction.
|
||||
The possible values are
|
||||
<ul>
|
||||
<li> <tt>DIVERT_PACKET_DIRECTION_OUTBOUND</tt> with value 0 for outbound
|
||||
packets.</li>
|
||||
<li> <tt>DIVERT_PACKET_DIRECTION_INBOUND</tt> with value 1 for inbound
|
||||
packets.</li>
|
||||
</ul></li>
|
||||
<li> <tt>IfIdx</tt>: The interface index on which the packet arrived
|
||||
(for inbound packets), or is to be sent (for outbound packets).</li>
|
||||
<li> <tt>SubIfIdx</tt>: The sub-interface index for <tt>IfIdx</tt>.</li>
|
||||
</ul>
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
The <tt>DIVERT_PACKET</tt> structure represents a captured or injected packet.
|
||||
The packet's contents, i.e., IP/TCP/UDP headers and data, immediately follow
|
||||
a DIVERT_PACKET header in memory.
|
||||
</p>
|
||||
</dd></dl>
|
||||
|
||||
<a name="divert_open"><h3>5.2 DivertOpen</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
HANDLE <b>DivertOpen</b>(
|
||||
__in const char *filter
|
||||
);
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<p>
|
||||
<b>Parameters</b><br>
|
||||
<ul>
|
||||
<li> <tt>filter</tt>: A packet filter string specified in the <tt>divert</tt>
|
||||
<a href="#filter_language">filter language</a>.</li>
|
||||
</ul>
|
||||
</p><p>
|
||||
<b>Return Value</b><br>
|
||||
A valid <tt>divert</tt> <tt>HANDLE</tt> on success, or
|
||||
<tt>INVALID_HANDLE_VALUE</tt> if an error occurred.
|
||||
Use <tt>GetLastError()</tt> to get the reason for the error.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
Opens a <tt>divert</tt> packet capture handle for the given filter.
|
||||
Any packet that matches the filter will be diverted to the handle, and
|
||||
can be read by calling <tt>DivertRecv()</tt>.
|
||||
</p>
|
||||
<p>
|
||||
A typical application is only interested in a subset of all traffic.
|
||||
In this case the filter should <i>match this subset as closely as
|
||||
possible</i>.
|
||||
This avoids unnecessary overheads introduced by diverting packets to an
|
||||
application, only to have the application re-inject them.
|
||||
</p>
|
||||
<p>
|
||||
Calling <tt>DivertOpen()</tt> for the first time will automatically load
|
||||
the <tt>divert.sys</tt> driver.
|
||||
This driver will remain installed until the next reboot, or if the driver
|
||||
is explicitly removed, e.g. by issuing the following commands:
|
||||
<pre>
|
||||
sc stop divert
|
||||
sc delete divert
|
||||
</pre>
|
||||
This model helps ensure the driver is not loaded unless it is required to be.
|
||||
</p>
|
||||
</dd></dl>
|
||||
|
||||
<a name="divert_recv"><h3>5.3 DivertRecv</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
BOOL <b>DivertRecv</b>(
|
||||
__in HANDLE handle,
|
||||
__out PDIVERT_PACKET pPacket,
|
||||
__in UINT packetLen,
|
||||
__out_opt UINT *recvLen
|
||||
);
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<p>
|
||||
<b>Parameters</b><br>
|
||||
<ul>
|
||||
<li> <tt>handle</tt>: A valid <tt>divert</tt> handle created by
|
||||
<tt>DivertOpen()</tt>.</li>
|
||||
<li> <tt>pPacket</tt>: A pointer to a <tt>DIVERT_PACKET</tt> header and free
|
||||
space to write the captured packet to.
|
||||
The free space is assumed to immediately follow the
|
||||
<tt>DIVERT_PACKET</tt> header.</li>
|
||||
<li> <tt>packetLen</tt>: The total length of the <tt>DIVERT_PACKET</tt>
|
||||
header and the free space.</li>
|
||||
<li> <tt>recvLen</tt>: The total number of bytes written to <tt>pPacket</tt>.
|
||||
Can be <tt>NULL</tt> if this information is not required.</li>
|
||||
</ul>
|
||||
</p><p>
|
||||
<b>Return Value</b><br>
|
||||
<tt>TRUE</tt> if a packet was successfully received, or <tt>FALSE</tt> if
|
||||
an error occurred.
|
||||
Use <tt>GetLastError()</tt> to get the reason for the error.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
Receives a diverted packet that matches the filter passed to
|
||||
<tt>DivertOpen()</tt>.
|
||||
The received packet is guaranteed to match the filter.
|
||||
</p>
|
||||
<p>
|
||||
The <tt>pPacket</tt> parameter is intended to be a buffer large enough to
|
||||
store a <tt>DIVERT_PACKET</tt> header, and enough space to store the diverted
|
||||
packet.
|
||||
This would typically be achieved by the following declarations:
|
||||
<pre>
|
||||
char packet[MAX_SIZE]; // packet buffer space
|
||||
PDIVERT_PACKET pPacket = (PDIVERT_PACKET)packet; // cast packet to a PDIVERT_PACKET
|
||||
...
|
||||
if (!DivertRecv(handle, pPacket, sizeof(packet), &recvLen))
|
||||
{
|
||||
// Recv error
|
||||
}
|
||||
...
|
||||
</pre>
|
||||
</p>
|
||||
<p>
|
||||
An application should call <tt>DivertRecv()</tt> <i>as soon as possible</i>
|
||||
after a successful call to <tt>DivertOpen()</tt>.
|
||||
When a <tt>divert</tt> handle is open, any packet that matches the filter will
|
||||
be captured and queued until handled by <tt>DivertRecv()</tt>.
|
||||
The packet queue never exceeds a fixed length (currently 1024 packets), after
|
||||
which packets are dropped.
|
||||
Furthermore packets are not queued indefinitely.
|
||||
A packet that has been queued longer than a specific time (currently 100ms)
|
||||
will be dropped.
|
||||
To avoid packets being dropped that application must process packets as fast
|
||||
as possible.
|
||||
</p>
|
||||
</dd></dl>
|
||||
|
||||
<a name="divert_send"><h3>5.4 DivertSend</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
BOOL <b>DivertSend</b>(
|
||||
__in HANDLE handle,
|
||||
__in PDIVERT_PACKET pPacket,
|
||||
__in UINT packetLen,
|
||||
__out_opt UINT *sendLen
|
||||
);
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<p>
|
||||
<b>Parameters</b><br>
|
||||
<ul>
|
||||
<li> <tt>handle</tt>: A valid <tt>divert</tt> handle created by
|
||||
<tt>DivertOpen()</tt>.</li>
|
||||
<li> <tt>pPacket</tt>: A pointer to a <tt>DIVERT_PACKET</tt> header and the
|
||||
packet to be injected.
|
||||
The packet is assumed to immediately follow the
|
||||
<tt>DIVERT_PACKET</tt> header.</li>
|
||||
<li> <tt>packetLen</tt>: The total length of the <tt>DIVERT_PACKET</tt>
|
||||
header and packet to be injected.</li>
|
||||
<li> <tt>sendLen</tt>: The total number of bytes injected.
|
||||
Can be <tt>NULL</tt> if this information is not required.</li>
|
||||
</ul>
|
||||
</p><p>
|
||||
<b>Return Value</b><br>
|
||||
<tt>TRUE</tt> if a packet was successfully injected, or <tt>FALSE</tt> if
|
||||
an error occurred.
|
||||
Use <tt>GetLastError()</tt> to get the reason for the error.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
Injects a packet into the network stack.
|
||||
The injected packet may be one received from <tt>DivertRecv()</tt>, or a
|
||||
modified version, or a completely new packet.
|
||||
Injected packets cannot be read again by <tt>DivertRecv()</tt>.
|
||||
</p><p>
|
||||
The <tt>DIVERT_PACKET</tt> header determines how the packet is injected.
|
||||
If the <tt>Direction</tt> field is <tt>DIVERT_PACKET_DIRECTION_OUTBOUND</tt>,
|
||||
the packet is injected into the <i>outbound</i> path (i.e. a packet leaving
|
||||
this computer).
|
||||
Else, if <tt>Direction</tt> is <tt>DIVERT_PACKET_DIRECTION_INBOUND</tt>,
|
||||
the packet is injected into the <i>inbound</i> path (i.e. a packet arriving at
|
||||
this computer).
|
||||
Note that the <tt>Direction</tt> field, and <i>not</i> the IP addresses in
|
||||
the injected packet, is used to determine the packet's direction.
|
||||
</p><p>
|
||||
For packets injected into the <i>inbound</i> path, the <tt>IfIdx</tt> and
|
||||
<tt>SubIfIdx</tt> fields are assumed to contain valid interface numbers.
|
||||
These may be retrieved from <tt>DivertRecv()</tt> (for packet modification),
|
||||
or from the <a href="http://msdn.microsoft.com/en-us/library/aa366073%28v=VS.85%29.aspx">IP Helper API</a>.
|
||||
</p><p>
|
||||
For <i>outbound</i> injected packets, the <tt>IfIdx</tt> and <tt>SubIfIdx</tt>
|
||||
fields are ignored and may be arbitrary values.
|
||||
Injecting an inbound packet on the outbound path <i>may</i> work (for some
|
||||
types of packets), however this should be considered "undocumented" behavior,
|
||||
and may change in future.
|
||||
</p>
|
||||
</dd></dl>
|
||||
|
||||
<a name="divert_close"><h3>5.5 DivertClose</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
BOOL <b>DivertClose</b>(
|
||||
__in HANDLE handle
|
||||
);
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<p>
|
||||
<b>Parameters</b><br>
|
||||
<ul>
|
||||
<li> <tt>handle</tt>: A valid <tt>divert</tt> handle created by
|
||||
<tt>DivertOpen()</tt>.</li>
|
||||
</ul>
|
||||
</p><p>
|
||||
<b>Return Value</b><br>
|
||||
<tt>TRUE</tt> if successful, <tt>FALSE</tt> if an error occurred.
|
||||
Use <tt>GetLastError()</tt> to get the reason for the error.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
Closes a handle created by <tt>DivertOpen()</tt>.
|
||||
</p>
|
||||
<dd></dl>
|
||||
|
||||
<hr>
|
||||
<a name="helper_programming_api"><h2>6. Helper Programming API</h2></a>
|
||||
|
||||
The <tt>divert</tt> helper programming API is a collection of definitions
|
||||
and functions designed to make writing <tt>divert</tt> applications easier.
|
||||
The use of the helper API is completely optional.
|
||||
|
||||
<a name="divert_iphdr"><h3>6.1 DIVERT_IPHDR</h3>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
typedef struct
|
||||
{
|
||||
UINT8 HdrLength:4;
|
||||
UINT8 Version:4;
|
||||
UINT8 TOS;
|
||||
UINT16 Length;
|
||||
UINT16 Id;
|
||||
UINT16 <i>...</i>;
|
||||
UINT8 TTL;
|
||||
UINT8 Protocol;
|
||||
UINT16 Checksum;
|
||||
UINT32 SrcAddr;
|
||||
UINT32 DstAddr;
|
||||
} <b>DIVERT_IPHDR</b>, *<b>PDIVERT_IPHDR</b>;
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<p>
|
||||
<b>Fields</b><br>
|
||||
See <a href="http://en.wikipedia.org/wiki/IPv4#Packet_structure">here</a>
|
||||
for more information.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
IPv4 header definition.
|
||||
</p><p>
|
||||
The following fields can only be get/set using the following macro
|
||||
definitions:
|
||||
<ul>
|
||||
<li><i>FragOff</i> with <tt>DIVERT_IPHDR_GET_FRAGOFF(<i>hdr</i>)</tt> and
|
||||
<tt>DIVERT_IPHDR_SET_FRAGOFF(<i>hdr</i>, <i>val</i>)</tt></li>
|
||||
<li><i>MF</i> with <tt>DIVERT_IPHDR_GET_MF(<i>hdr</i>)</tt> and
|
||||
<tt>DIVERT_IPHDR_SET_MF(<i>hdr</i>, <i>val</i>)</tt></li>
|
||||
<li><i>DF</i> with <tt>DIVERT_IPHDR_GET_DF(<i>hdr</i>)</tt> and
|
||||
<tt>DIVERT_IPHDR_SET_DF(<i>hdr</i>, <i>val</i>)</tt></li>
|
||||
<li><i>Reserved</i> with <tt>DIVERT_IPHDR_GET_RESERVED(<i>hdr</i>)</tt> and
|
||||
<tt>DIVERT_IPHDR_SET_RESERVED(<i>hdr</i>, <i>val</i>)</tt></li>
|
||||
</ul>
|
||||
</p>
|
||||
</dl></dd>
|
||||
|
||||
<a name="divert_ipv6hdr"><h3>6.2 DIVERT_IPV6HDR</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
typedef struct
|
||||
{
|
||||
UINT32 Version:4;
|
||||
UINT32 ...:28;
|
||||
UINT16 Length;
|
||||
UINT8 NextHdr;
|
||||
UINT8 HopLimit;
|
||||
UINT32 SrcAddr[4];
|
||||
UINT32 DstAddr[4];
|
||||
} <b>DIVERT_IPV6HDR</b>, *<b>PDIVERT_IPV6HDR</b>;
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<b>Fields</b><br>
|
||||
See <a href="http://en.wikipedia.org/wiki/IPv6_packet#Fixed_header">here</a>
|
||||
for more information.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
IPv6 header definition.
|
||||
</p><p>
|
||||
The following fields can only be get/set using the following macro
|
||||
definitions:
|
||||
<ul>
|
||||
<li><i>TrafficClass</i> with
|
||||
<tt>DIVERT_IPV6HDR_GET_TRAFFICCLASS(<i>hdr</i>)</tt> and
|
||||
<tt>DIVERT_IPV6HDR_SET_TRAFFICCLASS(<i>hdr</i>, <i>val</i>)</tt></li>
|
||||
<li><i>FlowLabel</i> with <tt>DIVERT_IPV6HDR_GET_FLOWLABEL(<i>hdr</i>)</tt> and
|
||||
<tt>DIVERT_IPV6HDR_SET_FLOWLABEL(<i>hdr</i>, <i>val</i>)</tt></li>
|
||||
</ul>
|
||||
</p>
|
||||
</dl></dd>
|
||||
|
||||
<a name="divert_icmphdr"><h3>6.3 DIVERT_ICMPHDR</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
typedef struct
|
||||
{
|
||||
UINT8 Type;
|
||||
UINT8 Code;
|
||||
UINT16 Checksum;
|
||||
UINT32 Body;
|
||||
} <b>DIVERT_ICMPHDR</b>, *<b>PDIVERT_ICMPHDR</b>;
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<b>Fields</b><br>
|
||||
See <a href="http://en.wikipedia.org/wiki/Internet_Control_Message_Protocol#ICMP_segment_structure">here</a>
|
||||
for more information.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
ICMP header definition.
|
||||
</p>
|
||||
</dl></dd>
|
||||
|
||||
<a name="divert_icmpv6hdr"><h3>6.4 DIVERT_ICMPV6HDR</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
typedef struct
|
||||
{
|
||||
UINT8 Type;
|
||||
UINT8 Code;
|
||||
UINT16 Checksum;
|
||||
UINT32 Body;
|
||||
} <b>DIVERT_ICMPV6HDR</b>, *<b>PDIVERT_ICMPV6HDR</b>;
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<b>Fields</b><br>
|
||||
See <a href="http://en.wikipedia.org/wiki/ICMPv6#Packet_format">here</a> for
|
||||
more information.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
ICMPv6 header definition.
|
||||
</p>
|
||||
</dl></dd>
|
||||
|
||||
<a name="divert_tcphdr"><h3>6.5 DIVERT_TCPHDR</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
typedef struct
|
||||
{
|
||||
UINT16 SrcPort;
|
||||
UINT16 DstPort;
|
||||
UINT32 SeqNum;
|
||||
UINT32 AckNum;
|
||||
UINT16 Reserved1:4;
|
||||
UINT16 HdrLength:4;
|
||||
UINT16 Fin:1;
|
||||
UINT16 Syn:1;
|
||||
UINT16 Rst:1;
|
||||
UINT16 Psh:1;
|
||||
UINT16 Ack:1;
|
||||
UINT16 Urg:1;
|
||||
UINT16 Reserved2:2;
|
||||
UINT16 Window;
|
||||
UINT16 Checksum;
|
||||
UINT16 UrgPtr;
|
||||
} <b>DIVERT_TCPHDR</b>, *<b>PDIVERT_TCPHDR</b>;
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<b>Fields</b><br>
|
||||
See <a href="http://en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_segment_structure">here</a>
|
||||
for more information.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
TCP header definition.
|
||||
</p>
|
||||
</dl></dd>
|
||||
|
||||
<a name="divert_udphdr"><h3>6.6 DIVERT_UDPHDR</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
typedef struct
|
||||
{
|
||||
UINT16 SrcPort;
|
||||
UINT16 DstPort;
|
||||
UINT16 Length;
|
||||
UINT16 Checksum;
|
||||
} <b>DIVERT_UDPHDR</b>, *<b>PDIVERT_UDPHDR</b>;
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<b>Fields</b><br>
|
||||
See <a href="http://en.wikipedia.org/wiki/User_Datagram_Protocol#Packet_structure">here</a>
|
||||
for more information.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
UDP header definition.
|
||||
</p>
|
||||
</dl></dd>
|
||||
|
||||
<a name="divert_helper_parse"><h3>6.7 DivertHelperParse</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
BOOL <b>DivertHelperParse</b>(
|
||||
__in PDIVERT_PACKET pPacket,
|
||||
__in UINT packetLen,
|
||||
__out_opt PDIVERT_IPHDR *ppIpHdr,
|
||||
__out_opt PDIVERT_IPV6HDR *ppIpv6Hdr,
|
||||
__out_opt PDIVERT_ICMPHDR *ppIcmpHdr,
|
||||
__out_opt PDIVERT_ICMPV6HDR *ppIcmpv6Hdr,
|
||||
__out_opt PDIVERT_TCPHDR *ppTcpHdr,
|
||||
__out_opt PDIVERT_UDPHDR *ppUdpHdr,
|
||||
__out_opt PVOID *ppData,
|
||||
__out_opt UINT *pDataLen
|
||||
);
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<p>
|
||||
<b>Parameters</b><br>
|
||||
<ul>
|
||||
<li> <tt>pPacket</tt>: The packet to be parsed.</li>
|
||||
<li> <tt>packetLen</tt>: The total length of the packet and the
|
||||
<tt>DIVERT_PACKET</tt> header.</li>
|
||||
<li> <tt>ppIpHdr</tt>: Output pointer to a <tt>DIVERT_IPHDR</tt>.</li>
|
||||
<li> <tt>ppIpv6Hdr</tt>: Output pointer to a <tt>DIVERT_IPV6HDR</tt>.</li>
|
||||
<li> <tt>ppIcmpHdr</tt>: Output pointer to a <tt>DIVERT_ICMPHDR</tt>.</li>
|
||||
<li> <tt>ppIcmpv6Hdr</tt>: Output pointer to a <tt>DIVERT_ICMPV6HDR</tt>.</li>
|
||||
<li> <tt>ppTcpHdr</tt>: Output pointer to a <tt>DIVERT_TCPHDR</tt>.</li>
|
||||
<li> <tt>ppUdpHdr</tt>: Output pointer to a <tt>DIVERT_UDPHDR</tt>.</li>
|
||||
<li> <tt>ppData</tt>: Output pointer to the packet's data/payload.</li>
|
||||
<li> <tt>pDataLen</tt> Output data/payload length.</li>
|
||||
</ul>
|
||||
</p><p>
|
||||
<b>Return Value</b><br>
|
||||
<tt>TRUE</tt> if all expected (non-<tt>NULL</tt>) outputs were present,
|
||||
<tt>FALSE</tt> otherwise.
|
||||
Note that <tt>FALSE</tt> may sometimes be a legitimate return value, e.g.,
|
||||
when both <tt>ppIpHdr</tt> and <tt>ppIpv6Hdr</tt> are non-<tt>NULL</tt>.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
Parses a raw packet (e.g. one captured using <tt>DivertRecv</tt>) into the
|
||||
various packet headers and/or payloads that may or may not be present.
|
||||
</p><p>
|
||||
Each output parameter may be <tt>NULL</tt> or non-<tt>NULL</tt>.
|
||||
For non-<tt>NULL</tt> parameters, this function will write the pointer to
|
||||
the corresponding header/payload if it exists, or will write <tt>NULL</tt>
|
||||
otherwise.
|
||||
Any non-<tt>NULL</tt> pointer that is returned
|
||||
<ol>
|
||||
<li> Is a pointer into the original <tt>pPacket</tt> packet; and</li>
|
||||
<li> There is enough space in <tt>pPacket</tt> to fit the header.</li>
|
||||
</ol>
|
||||
</p><p>
|
||||
This function does not do any verification of the header/payload contents,
|
||||
other length and the minimal information required to parse the headers
|
||||
themselves.
|
||||
<p>
|
||||
</dd></dl>
|
||||
|
||||
<a name="divert_helper_calc_checksums"><h3>6.8 DivertHelperCalcChecksums</h3></a>
|
||||
<table border="1" cellpadding="5"><tr><td>
|
||||
<pre>
|
||||
UINT <b>DivertHelperCalcChecksums</b>(
|
||||
__inout PDIVERT_PACKET pPacket,
|
||||
__in UINT packetLen,
|
||||
__in UINT64 flags
|
||||
);
|
||||
</pre>
|
||||
</td></tr></table>
|
||||
<dl><dd>
|
||||
<p>
|
||||
<b>Parameters</b><br>
|
||||
<ul>
|
||||
<li> <tt>pPacket</tt>: The packet to be modified.</li>
|
||||
<li> <tt>packetLen</tt>: The total length of the packet and the
|
||||
<tt>DIVERT_PACKET</tt> header.</li>
|
||||
<li> <tt>flags</tt>: One or more of the following flags:
|
||||
<ul>
|
||||
<li> <tt>DIVERT_HELPER_NO_IP_CHECKSUM</tt>: Do not calculate the IPv4
|
||||
checksum.</li>
|
||||
<li> <tt>DIVERT_HELPER_NO_ICMP_CHECKSUM</tt>: Do not calculate the ICMP
|
||||
checksum.</li>
|
||||
<li> <tt>DIVERT_HELPER_NO_ICMPV6_CHECKSUM</tt>: Do not calculate the ICMPv6
|
||||
checksum.</li>
|
||||
<li> <tt>DIVERT_HELPER_NO_TCP_CHECKSUM</tt>: Do not calculate the TCP
|
||||
checksum.</li>
|
||||
<li> <tt>DIVERT_HELPER_NO_UDP_CHECKSUM</tt>: Do not calculate the UDP
|
||||
checksum.</li>
|
||||
</ul></li>
|
||||
</ul>
|
||||
</p><p>
|
||||
<b>Return Value</b><br>
|
||||
The number of checksums calculated.
|
||||
</p><p>
|
||||
<b>Remarks</b><br>
|
||||
(Re)calculates the checksum for any IPv4/ICMP/ICMPv6/TCP/UDP checksum present
|
||||
in the given packet.
|
||||
Individual checksum calculations may be disabled via the appropriate flag.
|
||||
Typically this function should be used before a packet is injected.
|
||||
</p><p>
|
||||
This function will calculate each checksum from scratch, even if the existing
|
||||
checksum is correct.
|
||||
This may be inefficient for some applications.
|
||||
For better performance, incremental checksum calculations should be used
|
||||
instead (not provided by this API).
|
||||
<p>
|
||||
</dd></dl>
|
||||
|
||||
<hr>
|
||||
<a name="filter_language"><h2>7. Filter Language</h2></a>
|
||||
|
||||
<p>
|
||||
The <tt>DivertOpen()</tt> function accepts a string containing a
|
||||
<i>filter expression</i>.
|
||||
Only packets that match the filter expression are diverted.
|
||||
Any other packet is allowed to continue as per normal.
|
||||
</p><p>
|
||||
Filter allows an application to select only the subset of traffic that is of
|
||||
interest.
|
||||
For example, a URL blacklist filter would only be interested in packets that
|
||||
contain URLs.
|
||||
This could be achieved via the following filter.
|
||||
<pre>
|
||||
HANDLE handle = DivertOpen(
|
||||
"outbound and "
|
||||
"data and "
|
||||
"tcp.DstPort == 80");
|
||||
</pre>
|
||||
This filter specifies that we should only divert traffic that is
|
||||
<ol>
|
||||
<li>outbound;</li>
|
||||
<li>contains data; and</li>
|
||||
<li>has TCP destination port 80 (i.e. HTTP web traffic).
|
||||
</ol>
|
||||
</p><p>
|
||||
A <i>filter</i> is a Boolean expression of the form:
|
||||
<pre>
|
||||
<i>FILTER</i> := true | false | <i>FILTER</i> and <i>FILTER</i> | <i>FILTER</i> or <i>FILTER</i> | (<i>FILTER</i>) | <i>TEST</i>
|
||||
</pre>
|
||||
C-style syntax <tt>&&</tt>, <tt>||</tt>, and <tt>!</tt> may also
|
||||
be used instead of <tt>and</tt>, <tt>or</tt>, and <tt>not</tt>, respectively.
|
||||
A <i>test</i> is of the following form:
|
||||
<pre>
|
||||
<i>TEST</i> := <i>TEST0</i> | not <i>TEST0</i>
|
||||
<i>TEST0</i> := <i>FIELD</i> | <i>FIELD</i> op <i>VAL</i>
|
||||
</pre>
|
||||
where <tt>op</tt> is one of the following:
|
||||
</p><p>
|
||||
<center>
|
||||
<table border="1" cellpadding="5">
|
||||
<tr><td><tt>==</tt> or <tt>=</tt></td><td>Equal</td></tr>
|
||||
<tr><td><tt>!=</tt></td><td>Not equal</td></tr>
|
||||
<tr><td><tt><</tt></td><td>Less-than</td></tr>
|
||||
<tr><td><tt>></tt></td><td>Greater-than</td></tr>
|
||||
<tr><td><tt><=</tt></td><td>Less-than-or-equal</td></tr>
|
||||
<tr><td><tt>>=</tt></td><td>Greater-than-or-equal</td></tr>
|
||||
</table>
|
||||
</center>
|
||||
</p><p>
|
||||
and <tt><i>VAL</i></tt> is a decimal number, hexadecimal number, or IP
|
||||
address.
|
||||
If the "<tt>op <i>VAL</i></tt>" is missing, the test is implicitly
|
||||
"<tt><i>FIELD</i> != 0</tt>".
|
||||
</p><p>
|
||||
Finally a <i>field</i> is some property about the packet.
|
||||
The possible fields are:
|
||||
</p><p>
|
||||
<center>
|
||||
<table border="1" cellpadding="5">
|
||||
<tr><td><tt>outbound</tt></td><td>Is outbound?</td></tr>
|
||||
<tr><td><tt>inbound</tt></td><td>Is inbound?</td></tr>
|
||||
<tr><td><tt>ifIdx</tt></td><td>Interface index</td></tr>
|
||||
<tr><td><tt>subIfIdx</tt></td><td>Sub-interface index</td></tr>
|
||||
<tr><td><tt>ip</tt></td><td>Is IPv4?</td></tr>
|
||||
<tr><td><tt>ipv6</tt></td><td>Is IPv6?</td></tr>
|
||||
<tr><td><tt>icmp</tt></td><td>Is ICMP?</td></tr>
|
||||
<tr><td><tt>icmpv6</tt></td><td>Is ICMPv6?</td></tr>
|
||||
<tr><td><tt>tcp</tt></td><td>Is TCP?</td></tr>
|
||||
<tr><td><tt>udp</tt></td><td>Is UDP?</td></tr>
|
||||
<tr><td><tt>ip.*</tt></td><td>IPv4 fields (see <tt>DIVERT_IPHDR</tt>)</td></tr>
|
||||
<tr><td><tt>ipv6.*</tt></td><td>IPv6 fields (see <tt>DIVERT_IPV6HDR</tt>)</td></tr>
|
||||
<tr><td><tt>icmp.*</tt></td><td>ICMP fields (see <tt>DIVERT_ICMPHDR</tt>)</td></tr>
|
||||
<tr><td><tt>icmpv6.*</tt></td><td>ICMPV6 fields (see <tt>DIVERT_ICMPV6HDR</tt>)</td></tr>
|
||||
<tr><td><tt>tcp.*</tt></td><td>TCP fields (see <tt>DIVERT_TCPHDR</tt>)</td></tr>
|
||||
<tr><td><tt>tcp.PayloadLength</tt></td><td>The TCP payload length</td></tr>
|
||||
<tr><td><tt>udp.*</tt></td><td>UDP fields (see <tt>DIVERT_UDPHDR</tt>)</td></tr>
|
||||
<tr><td><tt>udp.PayloadLength</tt></td><td>The UDP payload length</td></tr>
|
||||
</table>
|
||||
</center>
|
||||
</p><p>
|
||||
A <i>test</i> also fails if the field is missing.
|
||||
E.g. the test "<tt>tcp.DstPort == 80</tt>" will fail if the packet does not
|
||||
contain a TCP header.
|
||||
</p>
|
||||
|
||||
<a name="filter_examples"><h3>7.1 Filter Examples</h3></a>
|
||||
|
||||
<p>
|
||||
<ol>
|
||||
<li>
|
||||
Divert all outbound web traffic:
|
||||
<pre>
|
||||
HANDLE handle = DivertOpen(
|
||||
"outbound and "
|
||||
"(tcp.DstPort == 80 or udp.DstPort == 53)"
|
||||
);
|
||||
</pre>
|
||||
</li>
|
||||
<li>
|
||||
Divert all inbound TCP SYNs:
|
||||
<pre>
|
||||
HANDLE handle = DivertOpen(
|
||||
"inbound and "
|
||||
"tcp.Syn"
|
||||
);
|
||||
</pre>
|
||||
</li>
|
||||
<li>
|
||||
Divert only (inbound) local traffic:
|
||||
<pre>
|
||||
HANDLE handle = DivertOpen(
|
||||
"inbound and ("
|
||||
"(ip.DstAddr >= 127.0.0.1 and ip.DstAddr <= 127.255.255.255) or"
|
||||
"ipv6.DstAddr == ::1)"
|
||||
);
|
||||
</pre>
|
||||
</li>
|
||||
<li>
|
||||
Divert all traffic:
|
||||
<pre>
|
||||
HANDLE handle = DivertOpen("true");
|
||||
</pre>
|
||||
</li>
|
||||
<li>
|
||||
Divert no traffic:
|
||||
<pre>
|
||||
HANDLE handle = DivertOpen("false");
|
||||
</pre>
|
||||
(This is not very useful).
|
||||
</ol>
|
||||
|
||||
<hr>
|
||||
<a name="samples"><h2>8. Samples</h2></a>
|
||||
|
||||
<p>
|
||||
Some samples have been provided to demonstrate the <tt>divert</tt> API.
|
||||
The sample programs are:
|
||||
<ul>
|
||||
<li><tt>webfilter.exe</tt>: A simple URL blacklist filter.
|
||||
This program monitors outbound HTTP traffic.
|
||||
If it finds a URL request that matches the blacklist, it hijacks the
|
||||
TCP connection, reseting the connection at the server's end, and
|
||||
sending a simple block-page to the browser.
|
||||
The blacklist(s) are specified at the command-line.</li>
|
||||
<li><tt>netdump.exe</tt>: A simple packet sniffer based on the
|
||||
<tt>divert</tt> filter language.
|
||||
This program takes a filter specified at the command line, and prints
|
||||
information about any packet that matches the filter.
|
||||
Unlike the other samples, this program does not modify or block packets,
|
||||
it simply re-injects them.</li>
|
||||
<li><tt>netfilter.exe</tt>: A simple firewall based on the <tt>divert</tt>
|
||||
filter language.
|
||||
This program takes a filter specified at the command line, and blocks
|
||||
any packet that matches the filter.
|
||||
It blocks TCP by sending a TCP reset, UDP by an ICMP message, and all
|
||||
other traffic it simply drops.
|
||||
This is similar to the Linux <tt>iptables</tt> command with the
|
||||
<tt>-j REJECT</tt> option.</li>
|
||||
</ul>
|
||||
</p><p>
|
||||
The samples are intended for educational purposes only, and are not
|
||||
fully-featured applications.
|
||||
</p>
|
||||
|
||||
<hr>
|
||||
<a name="known_issues"><h2>9. Known Issues</h2></a>
|
||||
|
||||
<p>
|
||||
There are some limitations to the <tt>divert</tt> package.
|
||||
They are
|
||||
<ul>
|
||||
<li><i>Injecting inbound ICMP/ICMPv6 messages</i>:
|
||||
For some ICMP/ICMPv6 messages, inbound injection does not work.
|
||||
An error will be returned and the packet will be lost.
|
||||
It is suspected that this is an issue with the WFP framework on which
|
||||
<tt>divert</tt> is built.
|
||||
The work-around is to inject inbound ICMP messages as <tt>outbound</tt>.
|
||||
</li>
|
||||
<li><i>No IPv6 extension header support</i>:
|
||||
Currently there is no filter support for IPv6 packets with extension
|
||||
headers.
|
||||
The work around is to capture all IPv6 traffic.
|
||||
<li><i>Injected packets are never re-captured</i>:
|
||||
An injected packet will <i>never</i> be captured again by any
|
||||
<tt>divert</tt> handle.
|
||||
This is necessary to prevent packet loops and deadlocks.
|
||||
In the future we intend to implement priorities for <tt>divert</tt>
|
||||
handles to allow packets to be seen by multiple <tt>divert</tt> handles.
|
||||
<li><i>Speed</i>:
|
||||
The <tt>divert</tt> driver is not re-entrant, and thus is not as
|
||||
efficient as it could be.
|
||||
In the future we plan to rectify this.
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
<hr>
|
||||
<a name="license"><h2>10. License</h2></a>
|
||||
<p>
|
||||
This package is distributed <i>strictly</i> under the
|
||||
<a href="http://www.gnu.org/licenses/gpl-3.0.txt">GNU Public License (GPL) Version 3</a>.
|
||||
Please note the following:
|
||||
<pre>
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
</pre>
|
||||
</p>
|
||||
<p>
|
||||
Other licenses (including commercial licenses) <i>may</i> be available on
|
||||
request.
|
||||
For more information please contact:<br>
|
||||
<tt>basil</tt> AT <tt>reqrypt</tt> DOT <tt>org</tt>
|
||||
</p>
|
||||
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
|
||||
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
|
||||
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,4 @@
|
||||
DIRS= \
|
||||
netdump \
|
||||
netfilter \
|
||||
webfilter
|
||||
@@ -0,0 +1 @@
|
||||
!INCLUDE $(NTMAKEENV)\makefile.def
|
||||
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
* netdump.c
|
||||
* (C) 2011, all rights reserved,
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* DESCRIPTION:
|
||||
* This is a simple traffic monitor.
|
||||
*
|
||||
* usage: netdump.exe divert-filter
|
||||
*
|
||||
* NOTE: Using Divert for this purpose is rather inefficient, as each captured
|
||||
* packet must be reinjected. For packet sniffing, it's better to use a
|
||||
* package that copies packets, not copies-and-drops such as Divert.
|
||||
*/
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "divert.h"
|
||||
|
||||
#define MAXBUF 2048
|
||||
|
||||
/*
|
||||
* Entry.
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
HANDLE handle, console;
|
||||
size_t slen, flen;
|
||||
UINT i;
|
||||
char filter[MAXBUF];
|
||||
char packet[MAXBUF];
|
||||
PDIVERT_PACKET ppacket = (PDIVERT_PACKET)packet;
|
||||
UINT ppacket_len;
|
||||
PDIVERT_IPHDR ip_header;
|
||||
PDIVERT_IPV6HDR ipv6_header;
|
||||
PDIVERT_ICMPHDR icmp_header;
|
||||
PDIVERT_ICMPV6HDR icmpv6_header;
|
||||
PDIVERT_TCPHDR tcp_header;
|
||||
PDIVERT_UDPHDR udp_header;
|
||||
UINT8 *data;
|
||||
UINT data_len;
|
||||
|
||||
// Concat all command line args into a filter string.
|
||||
flen = 0;
|
||||
for (i = 1; (int)i < argc; i++)
|
||||
{
|
||||
slen = strlen(argv[i]);
|
||||
if (flen + slen + 1 >= MAXBUF)
|
||||
{
|
||||
fprintf(stderr, "error: filter too long\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
strcpy(filter+flen, argv[i]);
|
||||
flen += slen;
|
||||
filter[flen] = ' ';
|
||||
flen++;
|
||||
}
|
||||
filter[flen] = '\0';
|
||||
|
||||
// Get console for pretty colors.
|
||||
console = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
|
||||
// Divert traffic matching the filter:
|
||||
handle = DivertOpen(filter);
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (GetLastError() == ERROR_INVALID_PARAMETER)
|
||||
{
|
||||
fprintf(stderr, "error: filter syntax error\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fprintf(stderr, "error: failed to open Divert device (%d)\n",
|
||||
GetLastError());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Main loop:
|
||||
while (TRUE)
|
||||
{
|
||||
// Read a matching packet.
|
||||
if (!DivertRecv(handle, ppacket, sizeof(packet), &ppacket_len))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to read packet (%d)\n",
|
||||
GetLastError());
|
||||
continue;
|
||||
}
|
||||
|
||||
// Re-inject the matching packet.
|
||||
if (!DivertSend(handle, ppacket, ppacket_len, NULL))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to reinject packet (%d)\n",
|
||||
GetLastError());
|
||||
}
|
||||
|
||||
// Print info about the matching packet.
|
||||
DivertHelperParse(ppacket, ppacket_len, &ip_header, &ipv6_header,
|
||||
&icmp_header, &icmpv6_header, &tcp_header, &udp_header, NULL,
|
||||
NULL);
|
||||
if (ip_header == NULL && ipv6_header == NULL)
|
||||
{
|
||||
fprintf(stderr, "warning: junk packet\n");
|
||||
}
|
||||
|
||||
// Dump packet info:
|
||||
putchar('\n');
|
||||
SetConsoleTextAttribute(console, FOREGROUND_RED);
|
||||
printf("Packet [Direction=%u IfIdx=%u SubIfIdx=%u]\n",
|
||||
ppacket->Direction, ppacket->IfIdx, ppacket->SubIfIdx);
|
||||
if (ip_header != NULL)
|
||||
{
|
||||
UINT8 *src_addr = (UINT8 *)&ip_header->SrcAddr;
|
||||
UINT8 *dst_addr = (UINT8 *)&ip_header->DstAddr;
|
||||
SetConsoleTextAttribute(console,
|
||||
FOREGROUND_GREEN | FOREGROUND_RED);
|
||||
printf("IPv4 [Version=%u HdrLength=%u TOS=%u Length=%u Id=0x%.4X "
|
||||
"Reserved=%u DF=%u MF=%u FragOff=%u TTL=%u Protocol=%u "
|
||||
"Checksum=0x%.4X SrcAddr=%u.%u.%u.%u DstAddr=%u.%u.%u.%u]\n",
|
||||
ip_header->Version, ip_header->HdrLength,
|
||||
ntohs(ip_header->TOS), ntohs(ip_header->Length),
|
||||
ntohs(ip_header->Id), DIVERT_IPHDR_GET_RESERVED(ip_header),
|
||||
DIVERT_IPHDR_GET_DF(ip_header), DIVERT_IPHDR_GET_MF(ip_header),
|
||||
ntohs(DIVERT_IPHDR_GET_FRAGOFF(ip_header)), ip_header->TTL,
|
||||
ip_header->Protocol, ntohs(ip_header->Checksum),
|
||||
src_addr[0], src_addr[1], src_addr[2], src_addr[3],
|
||||
dst_addr[0], dst_addr[1], dst_addr[2], dst_addr[3]);
|
||||
}
|
||||
if (ipv6_header != NULL)
|
||||
{
|
||||
UINT16 *src_addr = (UINT16 *)&ipv6_header->SrcAddr;
|
||||
UINT16 *dst_addr = (UINT16 *)&ipv6_header->DstAddr;
|
||||
SetConsoleTextAttribute(console,
|
||||
FOREGROUND_GREEN | FOREGROUND_RED);
|
||||
printf("IPv6 [Version=%u TrafficClass=%u FlowLabel=%u Length=%u "
|
||||
"NextHdr=%u HopLimit=%u SrcAddr=",
|
||||
ipv6_header->Version,
|
||||
DIVERT_IPV6HDR_GET_TRAFFICCLASS(ipv6_header),
|
||||
ntohl(DIVERT_IPV6HDR_GET_FLOWLABEL(ipv6_header)),
|
||||
ntohs(ipv6_header->Length), ipv6_header->NextHdr,
|
||||
ipv6_header->HopLimit);
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
printf("%x%c", ntohs(src_addr[i]), (i == 7? ' ': ':'));
|
||||
}
|
||||
fputs("DstAddr=", stdout);
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
printf("%x", ntohs(dst_addr[i]));
|
||||
if (i != 7)
|
||||
{
|
||||
putchar(':');
|
||||
}
|
||||
}
|
||||
fputs("]\n", stdout);
|
||||
}
|
||||
if (icmp_header != NULL)
|
||||
{
|
||||
SetConsoleTextAttribute(console, FOREGROUND_RED);
|
||||
printf("ICMP [Type=%u Code=%u Checksum=0x%.4X Body=0x%.8X]\n",
|
||||
icmp_header->Type, icmp_header->Code,
|
||||
ntohs(icmp_header->Checksum), ntohl(icmp_header->Body));
|
||||
}
|
||||
if (icmpv6_header != NULL)
|
||||
{
|
||||
SetConsoleTextAttribute(console, FOREGROUND_RED);
|
||||
printf("ICMPV6 [Type=%u Code=%u Checksum=0x%.4X Body=0x%.8X]\n",
|
||||
icmpv6_header->Type, icmpv6_header->Code,
|
||||
ntohs(icmpv6_header->Checksum), ntohl(icmpv6_header->Body));
|
||||
}
|
||||
if (tcp_header != NULL)
|
||||
{
|
||||
SetConsoleTextAttribute(console, FOREGROUND_GREEN);
|
||||
printf("TCP [SrcPort=%u DstPort=%u SeqNum=%u AckNum=%u "
|
||||
"HdrLength=%u Reserved1=%u Reserved2=%u Urg=%u Ack=%u "
|
||||
"Psh=%u Rst=%u Syn=%u Fin=%u Window=%u Checksum=0x%.4X "
|
||||
"UrgPtr=%u]\n",
|
||||
ntohs(tcp_header->SrcPort), ntohs(tcp_header->DstPort),
|
||||
ntohl(tcp_header->SeqNum), ntohl(tcp_header->AckNum),
|
||||
tcp_header->HdrLength, tcp_header->Reserved1,
|
||||
tcp_header->Reserved2, tcp_header->Urg, tcp_header->Ack,
|
||||
tcp_header->Psh, tcp_header->Rst, tcp_header->Syn,
|
||||
tcp_header->Fin, ntohs(tcp_header->Window),
|
||||
ntohs(tcp_header->Checksum), ntohs(tcp_header->UrgPtr));
|
||||
}
|
||||
if (udp_header != NULL)
|
||||
{
|
||||
SetConsoleTextAttribute(console, FOREGROUND_GREEN);
|
||||
printf("UDP [SrcPort=%u DstPort=%u Length=%u "
|
||||
"Checksum=0x%.4X]\n",
|
||||
ntohs(udp_header->SrcPort), ntohs(udp_header->DstPort),
|
||||
ntohs(udp_header->Length), ntohs(udp_header->Checksum));
|
||||
}
|
||||
SetConsoleTextAttribute(console, FOREGROUND_GREEN | FOREGROUND_BLUE);
|
||||
data = DIVERT_PACKET_DATA(ppacket);
|
||||
data_len = ppacket_len - sizeof(DIVERT_PACKET);
|
||||
for (i = 0; i < data_len; i++)
|
||||
{
|
||||
if (i % 20 == 0)
|
||||
{
|
||||
printf("\n\t");
|
||||
}
|
||||
printf("%.2X", (unsigned)data[i]);
|
||||
}
|
||||
SetConsoleTextAttribute(console, FOREGROUND_RED | FOREGROUND_BLUE);
|
||||
for (i = 0; i < data_len; i++)
|
||||
{
|
||||
if (i % 40 == 0)
|
||||
{
|
||||
printf("\n\t");
|
||||
}
|
||||
if (isprint(data[i]))
|
||||
{
|
||||
putchar(data[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
putchar('.');
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
SetConsoleTextAttribute(console,
|
||||
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# sources
|
||||
# (C) 2011, all rights reserved,
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
TARGETNAME=netdump
|
||||
TARGETTYPE=PROGRAM
|
||||
TARGETPATH=..\..\install
|
||||
TARGETLIBS=\
|
||||
$(SDK_LIB_PATH)\setupapi.lib \
|
||||
$(SDK_LIB_PATH)\user32.lib \
|
||||
$(SDK_LIB_PATH)\ws2_32.lib \
|
||||
$(TARGETPATH)\$(_BUILDARCH)\divert.lib
|
||||
UMTYPE=console
|
||||
UMENTRY=main
|
||||
USE_MSVCRT=1
|
||||
INCLUDES=$(DDK_INC_PATH);$(KMDF_INC_PATH)\$(KMDF_VER_PATH);..\..\include
|
||||
SOURCES=netdump.c
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
!INCLUDE $(NTMAKEENV)\makefile.def
|
||||
@@ -0,0 +1,445 @@
|
||||
/*
|
||||
* netfilter.c
|
||||
* (C) 2011, all rights reserved,
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* DESCRIPTION:
|
||||
* This is a simple traffic filter
|
||||
*
|
||||
* usage: netfilter.exe divert-filter
|
||||
*
|
||||
* Any traffic that matches the divert-filter will be blocked using one of
|
||||
* the following methods:
|
||||
* - TCP: send a TCP RST to the packet's source.
|
||||
* - UDP: send a ICMP(v6) "destination unreachable" to the packet's source.
|
||||
* - ICMP/ICMPv6: Drop the packet.
|
||||
*
|
||||
* This program is similar to Linux's iptables with the "-j REJECT" target.
|
||||
*/
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "divert.h"
|
||||
|
||||
#define MAXBUF 2048
|
||||
|
||||
/*
|
||||
* Pre-fabricated packets.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
DIVERT_PACKET divert;
|
||||
DIVERT_IPHDR ip;
|
||||
} PACKET, *PPACKET;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
DIVERT_PACKET divert;
|
||||
DIVERT_IPV6HDR ipv6;
|
||||
} PACKETV6, *PPACKETV6;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
PACKET header;
|
||||
DIVERT_TCPHDR tcp;
|
||||
} TCPPACKET, *PTCPPACKET;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
PACKETV6 header;
|
||||
DIVERT_TCPHDR tcp;
|
||||
} TCPV6PACKET, *PTCPV6PACKET;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
PACKET header;
|
||||
DIVERT_ICMPHDR icmp;
|
||||
UINT8 data[];
|
||||
} ICMPPACKET, *PICMPPACKET;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
PACKETV6 header;
|
||||
DIVERT_ICMPV6HDR icmpv6;
|
||||
UINT8 data[];
|
||||
} ICMPV6PACKET, *PICMPV6PACKET;
|
||||
|
||||
/*
|
||||
* Prototypes.
|
||||
*/
|
||||
static void PacketIpInit(PPACKET packet);
|
||||
static void PacketIpTcpInit(PTCPPACKET packet);
|
||||
static void PacketIpIcmpInit(PICMPPACKET packet);
|
||||
static void PacketIpv6Init(PPACKETV6 packet);
|
||||
static void PacketIpv6TcpInit(PTCPV6PACKET packet);
|
||||
static void PacketIpv6Icmpv6Init(PICMPV6PACKET packet);
|
||||
|
||||
/*
|
||||
* Entry.
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
HANDLE handle, console;
|
||||
size_t slen, flen;
|
||||
UINT i;
|
||||
char filter[MAXBUF];
|
||||
char packet[MAXBUF];
|
||||
PDIVERT_PACKET ppacket = (PDIVERT_PACKET)packet;
|
||||
UINT ppacket_len;
|
||||
PDIVERT_IPHDR ip_header;
|
||||
PDIVERT_IPV6HDR ipv6_header;
|
||||
PDIVERT_ICMPHDR icmp_header;
|
||||
PDIVERT_ICMPV6HDR icmpv6_header;
|
||||
PDIVERT_TCPHDR tcp_header;
|
||||
PDIVERT_UDPHDR udp_header;
|
||||
UINT payload_len;
|
||||
|
||||
TCPPACKET reset0;
|
||||
PTCPPACKET reset = &reset0;
|
||||
UINT8 dnr0[sizeof(ICMPPACKET) + 0x0F*sizeof(UINT32) + 8 + 1];
|
||||
PICMPPACKET dnr = (PICMPPACKET)dnr0;
|
||||
|
||||
TCPV6PACKET resetv6_0;
|
||||
PTCPV6PACKET resetv6 = &resetv6_0;
|
||||
UINT8 dnrv6_0[sizeof(ICMPV6PACKET) + sizeof(DIVERT_IPV6HDR) +
|
||||
sizeof(DIVERT_TCPHDR)];
|
||||
PICMPV6PACKET dnrv6 = (PICMPV6PACKET)dnrv6_0;
|
||||
|
||||
// Concat all command line args into a filter string.
|
||||
flen = 0;
|
||||
for (i = 1; (int)i < argc; i++)
|
||||
{
|
||||
slen = strlen(argv[i]);
|
||||
if (flen + slen + 1 >= MAXBUF)
|
||||
{
|
||||
fprintf(stderr, "error: filter too long\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
strcpy(filter+flen, argv[i]);
|
||||
flen += slen;
|
||||
filter[flen] = ' ';
|
||||
flen++;
|
||||
}
|
||||
filter[flen] = '\0';
|
||||
|
||||
// Initialize all packets.
|
||||
PacketIpTcpInit(reset);
|
||||
reset->tcp.Rst = 1;
|
||||
reset->tcp.Ack = 1;
|
||||
PacketIpIcmpInit(dnr);
|
||||
dnr->icmp.Type = 3; // Destination not reachable.
|
||||
dnr->icmp.Code = 3; // Port not reachable.
|
||||
PacketIpv6TcpInit(resetv6);
|
||||
resetv6->tcp.Rst = 1;
|
||||
resetv6->tcp.Ack = 1;
|
||||
PacketIpv6Icmpv6Init(dnrv6);
|
||||
dnrv6->header.ipv6.Length = htons(sizeof(DIVERT_ICMPV6HDR) + 4 +
|
||||
sizeof(DIVERT_IPV6HDR) + sizeof(DIVERT_TCPHDR));
|
||||
dnrv6->icmpv6.Type = 1; // Destination not reachable.
|
||||
dnrv6->icmpv6.Code = 4; // Port not reachable.
|
||||
|
||||
// Get console for pretty colors.
|
||||
console = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
|
||||
// Divert traffic matching the filter:
|
||||
handle = DivertOpen(filter);
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (GetLastError() == ERROR_INVALID_PARAMETER)
|
||||
{
|
||||
fprintf(stderr, "error: filter syntax error\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fprintf(stderr, "error: failed to open Divert device (%d)\n",
|
||||
GetLastError());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Main loop:
|
||||
while (TRUE)
|
||||
{
|
||||
// Read a matching packet.
|
||||
if (!DivertRecv(handle, ppacket, sizeof(packet), &ppacket_len))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to read packet\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Print info about the matching packet.
|
||||
DivertHelperParse(ppacket, ppacket_len, &ip_header, &ipv6_header,
|
||||
&icmp_header, &icmpv6_header, &tcp_header, &udp_header, NULL,
|
||||
&payload_len);
|
||||
if (ip_header == NULL && ipv6_header == NULL)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Dump packet info:
|
||||
SetConsoleTextAttribute(console, FOREGROUND_RED);
|
||||
fputs("BLOCK ", stdout);
|
||||
SetConsoleTextAttribute(console,
|
||||
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
|
||||
if (ip_header != NULL)
|
||||
{
|
||||
UINT8 *src_addr = (UINT8 *)&ip_header->SrcAddr;
|
||||
UINT8 *dst_addr = (UINT8 *)&ip_header->DstAddr;
|
||||
printf("ip.SrcAddr=%u.%u.%u.%u ip.DstAddr=%u.%u.%u.%u ",
|
||||
src_addr[0], src_addr[1], src_addr[2], src_addr[3],
|
||||
dst_addr[0], dst_addr[1], dst_addr[2], dst_addr[3]);
|
||||
}
|
||||
if (ipv6_header != NULL)
|
||||
{
|
||||
UINT16 *src_addr = (UINT16 *)&ipv6_header->SrcAddr;
|
||||
UINT16 *dst_addr = (UINT16 *)&ipv6_header->DstAddr;
|
||||
fputs("ipv6.SrcAddr=", stdout);
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
printf("%x%c", ntohs(src_addr[i]), (i == 7? ' ': ':'));
|
||||
}
|
||||
fputs(" ipv6.DstAddr=", stdout);
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
printf("%x%c", ntohs(dst_addr[i]), (i == 7? ' ': ':'));
|
||||
}
|
||||
putchar(' ');
|
||||
}
|
||||
if (icmp_header != NULL)
|
||||
{
|
||||
printf("icmp.Type=%u icmp.Code=%u ",
|
||||
icmp_header->Type, icmp_header->Code);
|
||||
// Simply drop ICMP
|
||||
}
|
||||
if (icmpv6_header != NULL)
|
||||
{
|
||||
printf("icmpv6.Type=%u icmpv6.Code=%u ",
|
||||
icmpv6_header->Type, icmpv6_header->Code);
|
||||
// Simply drop ICMPv6
|
||||
}
|
||||
if (tcp_header != NULL)
|
||||
{
|
||||
printf("tcp.SrcPort=%u tcp.DstPort=%u tcp.Flags=",
|
||||
ntohs(tcp_header->SrcPort), ntohs(tcp_header->DstPort));
|
||||
if (tcp_header->Fin)
|
||||
{
|
||||
fputs("[FIN]", stdout);
|
||||
}
|
||||
if (tcp_header->Rst)
|
||||
{
|
||||
fputs("[RST]", stdout);
|
||||
}
|
||||
if (tcp_header->Urg)
|
||||
{
|
||||
fputs("[URG]", stdout);
|
||||
}
|
||||
if (tcp_header->Syn)
|
||||
{
|
||||
fputs("[SYN]", stdout);
|
||||
}
|
||||
if (tcp_header->Psh)
|
||||
{
|
||||
fputs("[PSH]", stdout);
|
||||
}
|
||||
if (tcp_header->Ack)
|
||||
{
|
||||
fputs("[ACK]", stdout);
|
||||
}
|
||||
putchar(' ');
|
||||
|
||||
|
||||
if (ip_header != NULL)
|
||||
{
|
||||
reset->header.divert.IfIdx = ppacket->IfIdx;
|
||||
reset->header.divert.SubIfIdx = ppacket->SubIfIdx;
|
||||
reset->header.divert.Direction = !ppacket->Direction;
|
||||
reset->header.ip.SrcAddr = ip_header->DstAddr;
|
||||
reset->header.ip.DstAddr = ip_header->SrcAddr;
|
||||
reset->tcp.SrcPort = tcp_header->DstPort;
|
||||
reset->tcp.DstPort = tcp_header->SrcPort;
|
||||
reset->tcp.SeqNum =
|
||||
(tcp_header->Ack? tcp_header->AckNum: 0);
|
||||
reset->tcp.AckNum =
|
||||
(tcp_header->Syn?
|
||||
htonl(ntohl(tcp_header->SeqNum) + 1):
|
||||
htonl(ntohl(tcp_header->SeqNum) + payload_len));
|
||||
|
||||
DivertHelperCalcChecksums((PDIVERT_PACKET)reset,
|
||||
sizeof(TCPPACKET), 0);
|
||||
if (!DivertSend(handle, (PDIVERT_PACKET)reset,
|
||||
sizeof(TCPPACKET), NULL))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to send TCP reset (%d)\n",
|
||||
GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
if (ipv6_header != NULL)
|
||||
{
|
||||
resetv6->header.divert.IfIdx = ppacket->IfIdx;
|
||||
resetv6->header.divert.SubIfIdx = ppacket->SubIfIdx;
|
||||
resetv6->header.divert.Direction = !ppacket->Direction;
|
||||
memcpy(resetv6->header.ipv6.SrcAddr, ipv6_header->DstAddr,
|
||||
sizeof(resetv6->header.ipv6.SrcAddr));
|
||||
memcpy(resetv6->header.ipv6.DstAddr, ipv6_header->SrcAddr,
|
||||
sizeof(resetv6->header.ipv6.DstAddr));
|
||||
resetv6->tcp.SrcPort = tcp_header->DstPort;
|
||||
resetv6->tcp.DstPort = tcp_header->SrcPort;
|
||||
resetv6->tcp.SeqNum =
|
||||
(tcp_header->Ack? tcp_header->AckNum: 0);
|
||||
resetv6->tcp.AckNum =
|
||||
(tcp_header->Syn?
|
||||
htonl(ntohl(tcp_header->SeqNum) + 1):
|
||||
htonl(ntohl(tcp_header->SeqNum) + payload_len));
|
||||
|
||||
DivertHelperCalcChecksums((PDIVERT_PACKET)resetv6,
|
||||
sizeof(TCPV6PACKET), 0);
|
||||
if (!DivertSend(handle, (PDIVERT_PACKET)resetv6,
|
||||
sizeof(TCPV6PACKET), NULL))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to send TCP (IPV6) "
|
||||
"reset (%d)\n", GetLastError());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (udp_header != NULL)
|
||||
{
|
||||
printf("udp.SrcPort=%u udp.DstPort=%u ",
|
||||
ntohs(udp_header->SrcPort), ntohs(udp_header->DstPort));
|
||||
|
||||
if (ip_header != NULL)
|
||||
{
|
||||
// NOTE: For some ICMP error messages, WFP does not seem to
|
||||
// support INBOUND injection. As a work-around, we
|
||||
// always inject OUTBOUND.
|
||||
UINT icmp_length = ip_header->HdrLength*sizeof(UINT32) + 8;
|
||||
memcpy(dnr->data, ip_header, icmp_length);
|
||||
icmp_length += sizeof(ICMPPACKET);
|
||||
dnr->header.divert.IfIdx = ppacket->IfIdx;
|
||||
dnr->header.divert.SubIfIdx = ppacket->SubIfIdx;
|
||||
dnr->header.divert.Direction =
|
||||
DIVERT_PACKET_DIRECTION_OUTBOUND;
|
||||
dnr->header.ip.Length =
|
||||
htons(icmp_length - sizeof(DIVERT_PACKET));
|
||||
dnr->header.ip.SrcAddr = ip_header->DstAddr;
|
||||
dnr->header.ip.DstAddr = ip_header->SrcAddr;
|
||||
DivertHelperCalcChecksums((PDIVERT_PACKET)dnr, icmp_length, 0);
|
||||
if (!DivertSend(handle, (PDIVERT_PACKET)dnr, icmp_length,
|
||||
NULL))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to send ICMP message "
|
||||
"(%d)\n", GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
if (ipv6_header != NULL)
|
||||
{
|
||||
UINT icmpv6_length = sizeof(DIVERT_IPV6HDR) +
|
||||
sizeof(DIVERT_TCPHDR);
|
||||
memcpy(dnrv6->data, ipv6_header, icmpv6_length);
|
||||
icmpv6_length += sizeof(ICMPV6PACKET);
|
||||
dnrv6->header.divert.IfIdx = ppacket->IfIdx;
|
||||
dnrv6->header.divert.SubIfIdx = ppacket->SubIfIdx;
|
||||
dnrv6->header.divert.Direction =
|
||||
DIVERT_PACKET_DIRECTION_OUTBOUND;
|
||||
memcpy(dnrv6->header.ipv6.SrcAddr, ipv6_header->DstAddr,
|
||||
sizeof(dnrv6->header.ipv6.SrcAddr));
|
||||
memcpy(dnrv6->header.ipv6.DstAddr, ipv6_header->SrcAddr,
|
||||
sizeof(dnrv6->header.ipv6.DstAddr));
|
||||
DivertHelperCalcChecksums((PDIVERT_PACKET)dnrv6, icmpv6_length,
|
||||
0);
|
||||
if (!DivertSend(handle, (PDIVERT_PACKET)dnrv6, icmpv6_length,
|
||||
NULL))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to send ICMPv6 message "
|
||||
"(%d)\n", GetLastError());
|
||||
}
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a PACKET.
|
||||
*/
|
||||
static void PacketIpInit(PPACKET packet)
|
||||
{
|
||||
memset(packet, 0, sizeof(PACKET));
|
||||
packet->ip.Version = 4;
|
||||
packet->ip.HdrLength = sizeof(DIVERT_IPHDR) / sizeof(UINT32);
|
||||
packet->ip.Id = ntohs(0xDEAD);
|
||||
packet->ip.TTL = 64;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a TCPPACKET.
|
||||
*/
|
||||
static void PacketIpTcpInit(PTCPPACKET packet)
|
||||
{
|
||||
memset(packet, 0, sizeof(TCPPACKET));
|
||||
PacketIpInit(&packet->header);
|
||||
packet->header.ip.Length = htons(sizeof(TCPPACKET) -
|
||||
sizeof(DIVERT_PACKET));
|
||||
packet->header.ip.Protocol = IPPROTO_TCP;
|
||||
packet->tcp.HdrLength = sizeof(DIVERT_TCPHDR) / sizeof(UINT32);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize an ICMPPACKET.
|
||||
*/
|
||||
static void PacketIpIcmpInit(PICMPPACKET packet)
|
||||
{
|
||||
memset(packet, 0, sizeof(ICMPPACKET));
|
||||
PacketIpInit(&packet->header);
|
||||
packet->header.ip.Protocol = IPPROTO_ICMP;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a PACKETV6.
|
||||
*/
|
||||
static void PacketIpv6Init(PPACKETV6 packet)
|
||||
{
|
||||
memset(packet, 0, sizeof(PACKETV6));
|
||||
packet->ipv6.Version = 6;
|
||||
packet->ipv6.HopLimit = 64;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a TCPV6PACKET.
|
||||
*/
|
||||
static void PacketIpv6TcpInit(PTCPV6PACKET packet)
|
||||
{
|
||||
memset(packet, 0, sizeof(TCPV6PACKET));
|
||||
PacketIpv6Init(&packet->header);
|
||||
packet->header.ipv6.Length = htons(sizeof(DIVERT_TCPHDR));
|
||||
packet->header.ipv6.NextHdr = IPPROTO_TCP;
|
||||
packet->tcp.HdrLength = sizeof(DIVERT_TCPHDR) / sizeof(UINT32);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize an ICMP PACKET.
|
||||
*/
|
||||
static void PacketIpv6Icmpv6Init(PICMPV6PACKET packet)
|
||||
{
|
||||
memset(packet, 0, sizeof(ICMPV6PACKET));
|
||||
PacketIpv6Init(&packet->header);
|
||||
packet->header.ipv6.NextHdr = IPPROTO_ICMPV6;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# sources
|
||||
# (C) 2011, all rights reserved,
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
TARGETNAME=netfilter
|
||||
TARGETTYPE=PROGRAM
|
||||
TARGETPATH=..\..\install
|
||||
TARGETLIBS=\
|
||||
$(SDK_LIB_PATH)\setupapi.lib \
|
||||
$(SDK_LIB_PATH)\user32.lib \
|
||||
$(SDK_LIB_PATH)\ws2_32.lib \
|
||||
$(TARGETPATH)\$(_BUILDARCH)\divert.lib
|
||||
UMTYPE=console
|
||||
UMENTRY=main
|
||||
USE_MSVCRT=1
|
||||
INCLUDES=$(DDK_INC_PATH);$(KMDF_INC_PATH)\$(KMDF_VER_PATH);..\..\include
|
||||
SOURCES=netfilter.c
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
!INCLUDE $(NTMAKEENV)\makefile.def
|
||||
@@ -0,0 +1,30 @@
|
||||
# sources
|
||||
# (C) 2011, all rights reserved,
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
TARGETNAME=webfilter
|
||||
TARGETTYPE=PROGRAM
|
||||
TARGETPATH=..\..\install
|
||||
TARGETLIBS=\
|
||||
$(SDK_LIB_PATH)\setupapi.lib \
|
||||
$(SDK_LIB_PATH)\user32.lib \
|
||||
$(SDK_LIB_PATH)\ws2_32.lib \
|
||||
$(TARGETPATH)\$(_BUILDARCH)\divert.lib
|
||||
UMTYPE=console
|
||||
UMENTRY=main
|
||||
USE_MSVCRT=1
|
||||
INCLUDES=$(DDK_INC_PATH);$(KMDF_INC_PATH)\$(KMDF_VER_PATH);..\..\include
|
||||
SOURCES=webfilter.c
|
||||
|
||||
@@ -0,0 +1,596 @@
|
||||
/*
|
||||
* webfilter.c
|
||||
* (C) 2011, all rights reserved,
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* DESCRIPTION:
|
||||
* This is a simple web (HTTP) filter using the Divert device.
|
||||
*
|
||||
* It works by intercepting outbound HTTP GET/POST requests and matching
|
||||
* the URL against a blacklist. If the URL is matched, we hijack the TCP
|
||||
* connection, reseting the connection at the server end, and sending a
|
||||
* blockpage to the browser.
|
||||
*/
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "divert.h"
|
||||
|
||||
#define MAXBUF 2048
|
||||
|
||||
/*
|
||||
* URL and blacklist representation.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
char *domain;
|
||||
char *uri;
|
||||
} URL, *PURL;
|
||||
typedef struct
|
||||
{
|
||||
UINT size;
|
||||
UINT length;
|
||||
PURL *urls;
|
||||
} BLACKLIST, *PBLACKLIST;
|
||||
|
||||
/*
|
||||
* Pre-fabricated packets.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
DIVERT_PACKET divert;
|
||||
DIVERT_IPHDR ip;
|
||||
DIVERT_TCPHDR tcp;
|
||||
} PACKET, *PPACKET;
|
||||
typedef struct
|
||||
{
|
||||
PACKET header;
|
||||
UINT8 data[];
|
||||
} DATAPACKET, *PDATAPACKET;
|
||||
|
||||
/*
|
||||
* THe block page contents.
|
||||
*/
|
||||
const char block_data[] =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Content-Type: text/html\r\n"
|
||||
"\r\n"
|
||||
"<!doctype html>\n"
|
||||
"<html>\n"
|
||||
"\t<head>\n"
|
||||
"\t\t<title>BLOCKED!</title>\n"
|
||||
"\t</head>\n"
|
||||
"\t<body>\n"
|
||||
"\t\t<h1>BLOCKED!</h1>\n"
|
||||
"\t\t<hr>\n"
|
||||
"\t\t<p>This URL has been blocked!</p>\n"
|
||||
"\t</body>\n"
|
||||
"</html>\n";
|
||||
|
||||
/*
|
||||
* Prototypes
|
||||
*/
|
||||
static void PacketInit(PPACKET packet);
|
||||
static int UrlCompare(const void *a, const void *b);
|
||||
static int UrlMatch(PURL urla, PURL urlb);
|
||||
static PBLACKLIST BlackListInit(void);
|
||||
static void BlackListInsert(PBLACKLIST blacklist, PURL url);
|
||||
static void BlackListSort(PBLACKLIST blacklist);
|
||||
static BOOL BlackListMatch(PBLACKLIST blacklist, PURL url);
|
||||
static void BlackListRead(PBLACKLIST blacklist, const char *filename);
|
||||
static BOOL BlackListPayloadMatch(PBLACKLIST blacklist, char *data,
|
||||
UINT16 len);
|
||||
|
||||
/*
|
||||
* Entry.
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
HANDLE handle;
|
||||
UINT8 packet[MAXBUF];
|
||||
PDIVERT_PACKET ppacket = (PDIVERT_PACKET)packet;
|
||||
UINT ppacket_len;
|
||||
PDIVERT_IPHDR ip_header;
|
||||
PDIVERT_TCPHDR tcp_header;
|
||||
PVOID payload;
|
||||
UINT payload_len;
|
||||
PACKET reset0;
|
||||
PPACKET reset = &reset0;
|
||||
PDATAPACKET blockpage;
|
||||
UINT16 blockpage_len;
|
||||
PBLACKLIST blacklist;
|
||||
unsigned i;
|
||||
|
||||
// Read the blacklists.
|
||||
if (argc <= 1)
|
||||
{
|
||||
fprintf(stderr, "usage: %s blacklist.txt [blacklist2.txt ...]\n",
|
||||
argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
blacklist = BlackListInit();
|
||||
for (i = 1; i < (UINT)argc; i++)
|
||||
{
|
||||
BlackListRead(blacklist, argv[i]);
|
||||
}
|
||||
BlackListSort(blacklist);
|
||||
|
||||
// Initialize the pre-frabricated packets:
|
||||
blockpage_len = sizeof(DATAPACKET)+sizeof(block_data)-1;
|
||||
blockpage = (PDATAPACKET)malloc(blockpage_len);
|
||||
if (blockpage == NULL)
|
||||
{
|
||||
fprintf(stderr, "error: memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
PacketInit(&blockpage->header);
|
||||
blockpage->header.ip.Length =
|
||||
htons(blockpage_len - sizeof(DIVERT_PACKET));
|
||||
blockpage->header.tcp.SrcPort = htons(80);
|
||||
blockpage->header.tcp.Psh = 1;
|
||||
blockpage->header.tcp.Ack = 1;
|
||||
memcpy(blockpage->data, block_data, sizeof(block_data)-1);
|
||||
PacketInit(reset);
|
||||
reset->tcp.Rst = 1;
|
||||
reset->tcp.Ack = 1;
|
||||
|
||||
// Open the Divert device:
|
||||
handle = DivertOpen(
|
||||
"outbound && " // Outbound traffic only
|
||||
"ip && " // Only IPv4 supported
|
||||
"tcp.DstPort == 80 && " // HTTP (port 80) only
|
||||
"tcp.PayloadLength > 0" // TCP data packets only
|
||||
);
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
fprintf(stderr, "error: failed to open Divert device (%d)\n",
|
||||
GetLastError());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("OPENED divert\n");
|
||||
|
||||
// Main loop:
|
||||
while (TRUE)
|
||||
{
|
||||
if (!DivertRecv(handle, ppacket, sizeof(packet), &ppacket_len))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to read packet (%d)\n",
|
||||
GetLastError());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!DivertHelperParse(ppacket, ppacket_len, &ip_header, NULL, NULL,
|
||||
NULL, &tcp_header, NULL, &payload, &payload_len) ||
|
||||
!BlackListPayloadMatch(blacklist, payload, (UINT16)payload_len))
|
||||
{
|
||||
// Packet does not match the blacklist; simply reinject it.
|
||||
if (!DivertSend(handle, ppacket, ppacket_len, NULL))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to reinject packet (%d)\n",
|
||||
GetLastError());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// The URL matched the blacklist; we block it by hijacking the TCP
|
||||
// connection.
|
||||
|
||||
// (1) Send a TCP RST to the server; immediately closing the
|
||||
// connection at the server's end.
|
||||
reset->divert.IfIdx = ppacket->IfIdx;
|
||||
reset->divert.SubIfIdx = ppacket->SubIfIdx;
|
||||
reset->divert.Direction = ppacket->Direction;
|
||||
reset->ip.SrcAddr = ip_header->SrcAddr;
|
||||
reset->ip.DstAddr = ip_header->DstAddr;
|
||||
reset->tcp.SrcPort = tcp_header->SrcPort;
|
||||
reset->tcp.DstPort = htons(80);
|
||||
reset->tcp.SeqNum = tcp_header->SeqNum;
|
||||
reset->tcp.AckNum = tcp_header->AckNum;
|
||||
DivertHelperCalcChecksums((PDIVERT_PACKET)reset, sizeof(PACKET), 0);
|
||||
if (!DivertSend(handle, (PDIVERT_PACKET)reset, sizeof(PACKET), NULL))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to send reset packet (%d)\n",
|
||||
GetLastError());
|
||||
}
|
||||
|
||||
// (2) Send the blockpage to the browser:
|
||||
blockpage->header.divert.IfIdx = ppacket->IfIdx;
|
||||
blockpage->header.divert.SubIfIdx = ppacket->SubIfIdx;
|
||||
blockpage->header.divert.Direction = !ppacket->Direction;
|
||||
blockpage->header.ip.SrcAddr = ip_header->DstAddr;
|
||||
blockpage->header.ip.DstAddr = ip_header->SrcAddr;
|
||||
blockpage->header.tcp.DstPort = tcp_header->SrcPort;
|
||||
blockpage->header.tcp.SeqNum = tcp_header->AckNum;
|
||||
blockpage->header.tcp.AckNum =
|
||||
htonl(ntohl(tcp_header->SeqNum) + payload_len);
|
||||
DivertHelperCalcChecksums((PDIVERT_PACKET)blockpage, blockpage_len, 0);
|
||||
if (!DivertSend(handle, (PDIVERT_PACKET)blockpage, blockpage_len,
|
||||
NULL))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to send block page packet (%d)\n",
|
||||
GetLastError());
|
||||
}
|
||||
|
||||
// (3) Send a TCP RST to the browser; closing the connection at the
|
||||
// browser's end.
|
||||
reset->divert.IfIdx = ppacket->IfIdx;
|
||||
reset->divert.SubIfIdx = ppacket->SubIfIdx;
|
||||
reset->divert.Direction = !ppacket->Direction;
|
||||
reset->ip.SrcAddr = ip_header->DstAddr;
|
||||
reset->ip.DstAddr = ip_header->SrcAddr;
|
||||
reset->tcp.SrcPort = htons(80);
|
||||
reset->tcp.DstPort = tcp_header->SrcPort;
|
||||
reset->tcp.SeqNum =
|
||||
htonl(ntohl(tcp_header->AckNum) + sizeof(block_data) - 1);
|
||||
reset->tcp.AckNum =
|
||||
htonl(ntohl(tcp_header->SeqNum) + payload_len);
|
||||
DivertHelperCalcChecksums((PDIVERT_PACKET)reset, sizeof(PACKET), 0);
|
||||
if (!DivertSend(handle, (PDIVERT_PACKET)reset, sizeof(PACKET), NULL))
|
||||
{
|
||||
fprintf(stderr, "warning: failed to send reset packet (%d)\n",
|
||||
GetLastError());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a PACKET.
|
||||
*/
|
||||
static void PacketInit(PPACKET packet)
|
||||
{
|
||||
memset(packet, 0, sizeof(PACKET));
|
||||
packet->ip.Version = 4;
|
||||
packet->ip.HdrLength = sizeof(DIVERT_IPHDR) / sizeof(UINT32);
|
||||
packet->ip.Length = htons(sizeof(PACKET) - sizeof(DIVERT_PACKET));
|
||||
packet->ip.TTL = 64;
|
||||
packet->ip.Protocol = IPPROTO_TCP;
|
||||
packet->tcp.HdrLength = sizeof(DIVERT_TCPHDR) / sizeof(UINT32);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize an empty blacklist.
|
||||
*/
|
||||
static PBLACKLIST BlackListInit(void)
|
||||
{
|
||||
PBLACKLIST blacklist = (PBLACKLIST)malloc(sizeof(BLACKLIST));
|
||||
if (blacklist == NULL)
|
||||
{
|
||||
goto memory_error;
|
||||
}
|
||||
blacklist->urls = (PURL *)malloc(MAXBUF*sizeof(PURL));
|
||||
if (blacklist->urls == NULL)
|
||||
{
|
||||
goto memory_error;
|
||||
}
|
||||
blacklist->size = MAXBUF;
|
||||
blacklist->length = 0;
|
||||
|
||||
return blacklist;
|
||||
|
||||
memory_error:
|
||||
fprintf(stderr, "error: failed to allocate memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Insert a URL into a blacklist.
|
||||
*/
|
||||
static void BlackListInsert(PBLACKLIST blacklist, PURL url)
|
||||
{
|
||||
if (blacklist->length >= blacklist->size)
|
||||
{
|
||||
blacklist->size = (blacklist->size*3) / 2;
|
||||
printf("GROW blacklist to %u\n", blacklist->size);
|
||||
blacklist->urls = (PURL *)realloc(blacklist->urls,
|
||||
blacklist->size*sizeof(PURL));
|
||||
if (blacklist->urls == NULL)
|
||||
{
|
||||
fprintf(stderr, "error: failed to reallocate memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
blacklist->urls[blacklist->length++] = url;
|
||||
}
|
||||
|
||||
/*
|
||||
* Sort the blacklist (for searching).
|
||||
*/
|
||||
static void BlackListSort(PBLACKLIST blacklist)
|
||||
{
|
||||
qsort(blacklist->urls, blacklist->length, sizeof(PURL), UrlCompare);
|
||||
}
|
||||
|
||||
/*
|
||||
* Match a URL against the blacklist.
|
||||
*/
|
||||
static BOOL BlackListMatch(PBLACKLIST blacklist, PURL url)
|
||||
{
|
||||
int lo = 0, hi = ((int)blacklist->length)-1;
|
||||
|
||||
while (lo <= hi)
|
||||
{
|
||||
INT mid = (lo + hi) / 2;
|
||||
int cmp = UrlMatch(url, blacklist->urls[mid]);
|
||||
if (cmp > 0)
|
||||
{
|
||||
hi = mid-1;
|
||||
}
|
||||
else if (cmp < 0)
|
||||
{
|
||||
lo = mid+1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Read URLs from a file.
|
||||
*/
|
||||
static void BlackListRead(PBLACKLIST blacklist, const char *filename)
|
||||
{
|
||||
char domain[MAXBUF+1];
|
||||
char uri[MAXBUF+1];
|
||||
int c;
|
||||
UINT16 i, j;
|
||||
PURL url;
|
||||
FILE *file = fopen(filename, "r");
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
fprintf(stderr, "error: could not open blacklist file %s\n",
|
||||
filename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Read URLs from the file and add them to the blacklist:
|
||||
while (TRUE)
|
||||
{
|
||||
while (isspace(c = getc(file)))
|
||||
;
|
||||
if (c == EOF)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (c != '-' && !isalnum(c))
|
||||
{
|
||||
while (!isspace(c = getc(file)) && c != EOF)
|
||||
;
|
||||
if (c == EOF)
|
||||
{
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
i = 0;
|
||||
domain[i++] = (char)c;
|
||||
while ((isalnum(c = getc(file)) || c == '-' || c == '.') && i < MAXBUF)
|
||||
{
|
||||
domain[i++] = (char)c;
|
||||
}
|
||||
domain[i] = '\0';
|
||||
j = 0;
|
||||
if (c == '/')
|
||||
{
|
||||
while (!isspace(c = getc(file)) && c != EOF && j < MAXBUF)
|
||||
{
|
||||
uri[j++] = (char)c;
|
||||
}
|
||||
uri[j] = '\0';
|
||||
}
|
||||
else if (isspace(c))
|
||||
{
|
||||
uri[j] = '\0';
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!isspace(c = getc(file)) && c != EOF)
|
||||
;
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("ADD %s/%s\n", domain, uri);
|
||||
|
||||
url = (PURL)malloc(sizeof(URL));
|
||||
if (url == NULL)
|
||||
{
|
||||
goto memory_error;
|
||||
}
|
||||
url->domain = (char *)malloc((i+1)*sizeof(char));
|
||||
url->uri = (char *)malloc((j+1)*sizeof(char));
|
||||
if (url->domain == NULL || url->uri == NULL)
|
||||
{
|
||||
goto memory_error;
|
||||
}
|
||||
strcpy(url->uri, uri);
|
||||
for (j = 0; j < i; j++)
|
||||
{
|
||||
url->domain[j] = domain[i-j-1];
|
||||
}
|
||||
url->domain[j] = '\0';
|
||||
|
||||
BlackListInsert(blacklist, url);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return;
|
||||
|
||||
memory_error:
|
||||
fprintf(stderr, "error: memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Attempt to parse a URL and match it with the blacklist.
|
||||
*/
|
||||
static BOOL BlackListPayloadMatch(PBLACKLIST blacklist, char *data, UINT16 len)
|
||||
{
|
||||
static const char get_str[] = "GET /";
|
||||
static const char post_str[] = "POST /";
|
||||
static const char http_host_str[] = " HTTP/1.1\r\nHost: ";
|
||||
char domain[MAXBUF];
|
||||
char uri[MAXBUF];
|
||||
URL url = {domain, uri};
|
||||
UINT16 i = 0, j;
|
||||
BOOL result;
|
||||
HANDLE console;
|
||||
|
||||
if (len <= sizeof(post_str) + sizeof(http_host_str))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
if (strncmp(data, get_str, sizeof(get_str)-1) == 0)
|
||||
{
|
||||
i += sizeof(get_str)-1;
|
||||
}
|
||||
else if (strncmp(data, post_str, sizeof(post_str)-1) == 0)
|
||||
{
|
||||
i += sizeof(post_str)-1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
for (j = 0; i < len && data[i] != ' '; j++, i++)
|
||||
{
|
||||
uri[j] = data[i];
|
||||
}
|
||||
uri[j] = '\0';
|
||||
if (i + sizeof(http_host_str)-1 >= len)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (strncmp(data+i, http_host_str, sizeof(http_host_str)-1) != 0)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
i += sizeof(http_host_str)-1;
|
||||
|
||||
for (j = 0; i < len && data[i] != '\r'; j++, i++)
|
||||
{
|
||||
domain[j] = data[i];
|
||||
}
|
||||
if (i >= len)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
if (j == 0)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
if (domain[j-1] == '.')
|
||||
{
|
||||
// Nice try...
|
||||
j--;
|
||||
if (j == 0)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
domain[j] = '\0';
|
||||
|
||||
printf("URL %s/%s: ", domain, uri);
|
||||
|
||||
// Reverse the domain:
|
||||
for (i = 0; i < j / 2; i++)
|
||||
{
|
||||
char t = domain[i];
|
||||
domain[i] = domain[j-i-1];
|
||||
domain[j-i-1] = t;
|
||||
}
|
||||
|
||||
// Search the blacklist:
|
||||
result = BlackListMatch(blacklist, &url);
|
||||
|
||||
// Print the verdict:
|
||||
console = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if (result)
|
||||
{
|
||||
SetConsoleTextAttribute(console, FOREGROUND_RED);
|
||||
puts("BLOCKED!");
|
||||
}
|
||||
else
|
||||
{
|
||||
SetConsoleTextAttribute(console, FOREGROUND_GREEN);
|
||||
puts("allowed");
|
||||
}
|
||||
SetConsoleTextAttribute(console,
|
||||
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* URL comparison.
|
||||
*/
|
||||
static int UrlCompare(const void *a, const void *b)
|
||||
{
|
||||
PURL urla = *(PURL *)a;
|
||||
PURL urlb = *(PURL *)b;
|
||||
int cmp = strcmp(urla->domain, urlb->domain);
|
||||
if (cmp != 0)
|
||||
{
|
||||
return cmp;
|
||||
}
|
||||
return strcmp(urla->uri, urlb->uri);
|
||||
}
|
||||
|
||||
/*
|
||||
* URL matching
|
||||
*/
|
||||
static int UrlMatch(PURL urla, PURL urlb)
|
||||
{
|
||||
UINT16 i;
|
||||
|
||||
for (i = 0; urla->domain[i] && urlb->domain[i]; i++)
|
||||
{
|
||||
int cmp = (int)urlb->domain[i] - (int)urla->domain[i];
|
||||
if (cmp != 0)
|
||||
{
|
||||
return cmp;
|
||||
}
|
||||
}
|
||||
if (urla->domain[i] == '\0' && urlb->domain[i] != '\0')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (i = 0; urla->uri[i] && urlb->uri[i]; i++)
|
||||
{
|
||||
int cmp = (int)urlb->uri[i] - (int)urla->uri[i];
|
||||
if (cmp != 0)
|
||||
{
|
||||
return cmp;
|
||||
}
|
||||
}
|
||||
if (urla->uri[i] == '\0' && urlb->uri[i] != '\0')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
/*
|
||||
* divert.c
|
||||
* (C) 2011, all rights reserved,
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __DIVERT_H
|
||||
#define __DIVERT_H
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#ifndef DIVERTEXPORT
|
||||
#define DIVERTEXPORT __declspec(dllimport)
|
||||
#endif /* DIVERTEXPORT */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/****************************************************************************/
|
||||
/* DIVERT API */
|
||||
/****************************************************************************/
|
||||
|
||||
/*
|
||||
* Divert packet.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
UINT8 Reserved[7]; // Reserved for internal use.
|
||||
UINT8 Direction; // Packet's direction.
|
||||
UINT32 IfIdx; // Packet's interface index.
|
||||
UINT32 SubIfIdx; // Packet's sub-interface index.
|
||||
} DIVERT_PACKET, *PDIVERT_PACKET;
|
||||
|
||||
#ifndef DIVERT_PACKET_DIRECTION_OUTBOUND
|
||||
#define DIVERT_PACKET_DIRECTION_OUTBOUND 0
|
||||
#define DIVERT_PACKET_DIRECTION_INBOUND 1
|
||||
#endif /* DIVERT_PACKET_DIRECTION_OUTBOUND */
|
||||
|
||||
#define DIVERT_PACKET_DIRECTION(pPacket) (pPacket->Direction)
|
||||
#define DIVERT_PACKET_INTERFACE_INDEX(pPacket) (pPacket->IfIdx)
|
||||
#define DIVERT_PACKET_SUB_INTERFACE_INDEX(pPacket) (pPacket->SubIfIdx)
|
||||
#define DIVERT_PACKET_DATA(pPacket) \
|
||||
((PVOID)(pPacket+1))
|
||||
|
||||
/*
|
||||
* Open a handle to the divert device with the given filter.
|
||||
*/
|
||||
extern DIVERTEXPORT HANDLE DivertOpen(
|
||||
__in const char *filter);
|
||||
|
||||
/*
|
||||
* Receive (read) a packet from the Divert handle.
|
||||
*/
|
||||
extern DIVERTEXPORT BOOL DivertRecv(
|
||||
__in HANDLE handle,
|
||||
__inout PDIVERT_PACKET pPacket,
|
||||
__in UINT packetLen,
|
||||
__out_opt UINT *readLen);
|
||||
|
||||
/*
|
||||
* Send (write/inject) a packet to the Divert handle.
|
||||
*/
|
||||
extern DIVERTEXPORT BOOL DivertSend(
|
||||
__in HANDLE handle,
|
||||
__in PDIVERT_PACKET pPacket,
|
||||
__in UINT packetLen,
|
||||
__out_opt UINT *writeLen);
|
||||
|
||||
/*
|
||||
* Close a Divert handle.
|
||||
*/
|
||||
extern DIVERTEXPORT BOOL DivertClose(
|
||||
__in HANDLE handle);
|
||||
|
||||
/****************************************************************************/
|
||||
/* DIVERT HELPER API */
|
||||
/****************************************************************************/
|
||||
|
||||
#ifndef DIVERT_NO_HELPER_API
|
||||
|
||||
/*
|
||||
* IPv4/IPv6/ICMP/ICMPv6/TCP/UDP header definitions.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
UINT8 HdrLength:4;
|
||||
UINT8 Version:4;
|
||||
UINT8 TOS;
|
||||
UINT16 Length;
|
||||
UINT16 Id;
|
||||
UINT16 FragOff0;
|
||||
UINT8 TTL;
|
||||
UINT8 Protocol;
|
||||
UINT16 Checksum;
|
||||
UINT32 SrcAddr;
|
||||
UINT32 DstAddr;
|
||||
} DIVERT_IPHDR, *PDIVERT_IPHDR;
|
||||
|
||||
#define DIVERT_IPHDR_GET_FRAGOFF(hdr) \
|
||||
(((hdr)->FragOff0) & 0xFF1F)
|
||||
#define DIVERT_IPHDR_GET_MF(hdr) \
|
||||
((((hdr)->FragOff0) & 0x0020) != 0)
|
||||
#define DIVERT_IPHDR_GET_DF(hdr) \
|
||||
((((hdr)->FragOff0) & 0x0040) != 0)
|
||||
#define DIVERT_IPHDR_GET_RESERVED(hdr) \
|
||||
((((hdr)->FragOff0) & 0x0080) != 0)
|
||||
|
||||
#define DIVERT_IPHDR_SET_FRAGOFF(hdr, val) \
|
||||
do \
|
||||
{ \
|
||||
(hdr)->FragOff0 = (((hdr)->FragOff0) & 0x00E0) | \
|
||||
((val) & 0xFF1F); \
|
||||
} \
|
||||
while (FALSE)
|
||||
#define DIVERT_IPHDR_SET_MF(hdr, val) \
|
||||
do \
|
||||
{ \
|
||||
(hdr)->FragOff0 = (((hdr)->FragOff0) & 0xFFDF) | \
|
||||
(((val) & 0x0001) << 5); \
|
||||
} \
|
||||
while (FALSE)
|
||||
#define DIVERT_IPHDR_SET_DF(hdr, val) \
|
||||
do \
|
||||
{ \
|
||||
(hdr)->FragOff0 = (((hdr)->FragOff0) & 0xFFBF) | \
|
||||
(((val) & 0x0001) << 6); \
|
||||
} \
|
||||
while (FALSE)
|
||||
#define DIVERT_IPHDR_SET_RESERVED(hdr, val) \
|
||||
do \
|
||||
{ \
|
||||
(hdr)->FragOff0 = (((hdr)->FragOff0) & 0xFF7F) | \
|
||||
(((val) & 0x0001) << 7); \
|
||||
} \
|
||||
while (FALSE)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
UINT8 TrafficClass0:4;
|
||||
UINT8 Version:4;
|
||||
UINT8 FlowLabel0:4;
|
||||
UINT8 TrafficClass1:4;
|
||||
UINT16 FlowLabel1;
|
||||
UINT16 Length;
|
||||
UINT8 NextHdr;
|
||||
UINT8 HopLimit;
|
||||
UINT32 SrcAddr[4];
|
||||
UINT32 DstAddr[4];
|
||||
} DIVERT_IPV6HDR, *PDIVERT_IPV6HDR;
|
||||
|
||||
#define DIVERT_IPV6HDR_GET_TRAFFICCLASS(hdr) \
|
||||
((((hdr)->TrafficClass0) << 4) | ((hdr)->TrafficClass1))
|
||||
#define DIVERT_IPV6HDR_GET_FLOWLABEL(hdr) \
|
||||
((((UINT32)(hdr)->FlowLabel0) << 16) | ((UINT32)(hdr)->FlowLabel1))
|
||||
|
||||
#define DIVERT_IPV6HDR_SET_TRAFFICCLASS(hdr, val) \
|
||||
do \
|
||||
{ \
|
||||
(hdr)->TrafficClass0 = ((UINT8)(val) >> 4); \
|
||||
(hdr)->TrafficClass1 = (UINT8)(val); \
|
||||
} \
|
||||
while (FALSE)
|
||||
#define DIVERT_IPV6HDR_SET_FLOWLABEL(hdr, val) \
|
||||
do \
|
||||
{ \
|
||||
(hdr)->FlowLabel0 = (UINT8)((val) >> 16); \
|
||||
(hdr)->FlowLabel1 = (UINT16)(val); \
|
||||
} \
|
||||
while (FALSE)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
UINT8 Type;
|
||||
UINT8 Code;
|
||||
UINT16 Checksum;
|
||||
UINT32 Body;
|
||||
} DIVERT_ICMPHDR, *PDIVERT_ICMPHDR;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
UINT8 Type;
|
||||
UINT8 Code;
|
||||
UINT16 Checksum;
|
||||
UINT32 Body;
|
||||
} DIVERT_ICMPV6HDR, *PDIVERT_ICMPV6HDR;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
UINT16 SrcPort;
|
||||
UINT16 DstPort;
|
||||
UINT32 SeqNum;
|
||||
UINT32 AckNum;
|
||||
UINT16 Reserved1:4;
|
||||
UINT16 HdrLength:4;
|
||||
UINT16 Fin:1;
|
||||
UINT16 Syn:1;
|
||||
UINT16 Rst:1;
|
||||
UINT16 Psh:1;
|
||||
UINT16 Ack:1;
|
||||
UINT16 Urg:1;
|
||||
UINT16 Reserved2:2;
|
||||
UINT16 Window;
|
||||
UINT16 Checksum;
|
||||
UINT16 UrgPtr;
|
||||
} DIVERT_TCPHDR, *PDIVERT_TCPHDR;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
UINT16 SrcPort;
|
||||
UINT16 DstPort;
|
||||
UINT16 Length;
|
||||
UINT16 Checksum;
|
||||
} DIVERT_UDPHDR, *PDIVERT_UDPHDR;
|
||||
|
||||
/*
|
||||
* Flags for DivertHelperCalcChecksums()
|
||||
*/
|
||||
#define DIVERT_HELPER_NO_IP_CHECKSUM 1
|
||||
#define DIVERT_HELPER_NO_ICMP_CHECKSUM 2
|
||||
#define DIVERT_HELPER_NO_ICMPV6_CHECKSUM 4
|
||||
#define DIVERT_HELPER_NO_TCP_CHECKSUM 8
|
||||
#define DIVERT_HELPER_NO_UDP_CHECKSUM 16
|
||||
|
||||
/*
|
||||
* Parse IPv4/IPv6/ICMP/ICMPv6/TCP/UDP headers from a raw packet.
|
||||
*/
|
||||
extern DIVERTEXPORT BOOL DivertHelperParse(
|
||||
__in PDIVERT_PACKET pPacket,
|
||||
__in UINT packetLen,
|
||||
__out_opt PDIVERT_IPHDR *ppIpHdr,
|
||||
__out_opt PDIVERT_IPV6HDR *ppIpv6Hdr,
|
||||
__out_opt PDIVERT_ICMPHDR *ppIcmpHdr,
|
||||
__out_opt PDIVERT_ICMPV6HDR *ppIcmpv6Hdr,
|
||||
__out_opt PDIVERT_TCPHDR *ppTcpHdr,
|
||||
__out_opt PDIVERT_UDPHDR *ppUdpHdr,
|
||||
__out_opt PVOID *ppData,
|
||||
__out_opt UINT *pDataLen);
|
||||
|
||||
/*
|
||||
* Calculate IPv4/IPv6/ICMP/ICMPv6/TCP/UDP checksums.
|
||||
*/
|
||||
extern DIVERTEXPORT UINT DivertHelperCalcChecksums(
|
||||
__inout PDIVERT_PACKET pPacket,
|
||||
__in UINT packetLen,
|
||||
__in UINT64 flags);
|
||||
|
||||
#endif /* DIVERT_NO_HELPER_API */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __DIVERT_H */
|
||||
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* divert_device.h
|
||||
* (C) 2011, all rights reserved,
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* NOTE: This file is NOT part of the divert API. For the divert API, include
|
||||
* "divert.h" instead.
|
||||
*/
|
||||
|
||||
#ifndef __DIVERT_DEVICE_H
|
||||
#define __DIVERT_DEVICE_H
|
||||
|
||||
#define DIVERT_DEVICE_NAME L"\\Device\\Divert"
|
||||
#define DIVERT_DOS_DEVICE_NAME L"\\??\\Divert"
|
||||
|
||||
#define DIVERT_VERSION 0
|
||||
#define DIVERT_MAGIC 0xF8D3
|
||||
|
||||
#define DIVERT_FILTER_FIELD_ZERO 0
|
||||
#define DIVERT_FILTER_FIELD_INBOUND 1
|
||||
#define DIVERT_FILTER_FIELD_OUTBOUND 2
|
||||
#define DIVERT_FILTER_FIELD_IFIDX 3
|
||||
#define DIVERT_FILTER_FIELD_SUBIFIDX 4
|
||||
#define DIVERT_FILTER_FIELD_IP 5
|
||||
#define DIVERT_FILTER_FIELD_IPV6 6
|
||||
#define DIVERT_FILTER_FIELD_ICMP 7
|
||||
#define DIVERT_FILTER_FIELD_TCP 8
|
||||
#define DIVERT_FILTER_FIELD_UDP 9
|
||||
#define DIVERT_FILTER_FIELD_ICMPV6 10
|
||||
#define DIVERT_FILTER_FIELD_IP_HDRLENGTH 11
|
||||
#define DIVERT_FILTER_FIELD_IP_TOS 12
|
||||
#define DIVERT_FILTER_FIELD_IP_LENGTH 13
|
||||
#define DIVERT_FILTER_FIELD_IP_ID 14
|
||||
#define DIVERT_FILTER_FIELD_IP_DF 15
|
||||
#define DIVERT_FILTER_FIELD_IP_MF 16
|
||||
#define DIVERT_FILTER_FIELD_IP_FRAGOFF 17
|
||||
#define DIVERT_FILTER_FIELD_IP_TTL 18
|
||||
#define DIVERT_FILTER_FIELD_IP_PROTOCOL 19
|
||||
#define DIVERT_FILTER_FIELD_IP_CHECKSUM 20
|
||||
#define DIVERT_FILTER_FIELD_IP_SRCADDR 21
|
||||
#define DIVERT_FILTER_FIELD_IP_DSTADDR 22
|
||||
#define DIVERT_FILTER_FIELD_IPV6_TRAFFICCLASS 23
|
||||
#define DIVERT_FILTER_FIELD_IPV6_FLOWLABEL 24
|
||||
#define DIVERT_FILTER_FIELD_IPV6_LENGTH 25
|
||||
#define DIVERT_FILTER_FIELD_IPV6_NEXTHDR 26
|
||||
#define DIVERT_FILTER_FIELD_IPV6_HOPLIMIT 27
|
||||
#define DIVERT_FILTER_FIELD_IPV6_SRCADDR 28
|
||||
#define DIVERT_FILTER_FIELD_IPV6_DSTADDR 29
|
||||
#define DIVERT_FILTER_FIELD_ICMP_TYPE 30
|
||||
#define DIVERT_FILTER_FIELD_ICMP_CODE 31
|
||||
#define DIVERT_FILTER_FIELD_ICMP_CHECKSUM 32
|
||||
#define DIVERT_FILTER_FIELD_ICMP_BODY 33
|
||||
#define DIVERT_FILTER_FIELD_ICMPV6_TYPE 34
|
||||
#define DIVERT_FILTER_FIELD_ICMPV6_CODE 35
|
||||
#define DIVERT_FILTER_FIELD_ICMPV6_CHECKSUM 36
|
||||
#define DIVERT_FILTER_FIELD_ICMPV6_BODY 37
|
||||
#define DIVERT_FILTER_FIELD_TCP_SRCPORT 38
|
||||
#define DIVERT_FILTER_FIELD_TCP_DSTPORT 39
|
||||
#define DIVERT_FILTER_FIELD_TCP_SEQNUM 40
|
||||
#define DIVERT_FILTER_FIELD_TCP_ACKNUM 41
|
||||
#define DIVERT_FILTER_FIELD_TCP_HDRLENGTH 42
|
||||
#define DIVERT_FILTER_FIELD_TCP_URG 43
|
||||
#define DIVERT_FILTER_FIELD_TCP_ACK 44
|
||||
#define DIVERT_FILTER_FIELD_TCP_PSH 45
|
||||
#define DIVERT_FILTER_FIELD_TCP_RST 46
|
||||
#define DIVERT_FILTER_FIELD_TCP_SYN 47
|
||||
#define DIVERT_FILTER_FIELD_TCP_FIN 48
|
||||
#define DIVERT_FILTER_FIELD_TCP_WINDOW 49
|
||||
#define DIVERT_FILTER_FIELD_TCP_CHECKSUM 50
|
||||
#define DIVERT_FILTER_FIELD_TCP_URGPTR 51
|
||||
#define DIVERT_FILTER_FIELD_TCP_PAYLOADLENGTH 52
|
||||
#define DIVERT_FILTER_FIELD_UDP_SRCPORT 53
|
||||
#define DIVERT_FILTER_FIELD_UDP_DSTPORT 54
|
||||
#define DIVERT_FILTER_FIELD_UDP_LENGTH 55
|
||||
#define DIVERT_FILTER_FIELD_UDP_CHECKSUM 56
|
||||
#define DIVERT_FILTER_FIELD_UDP_PAYLOADLENGTH 57
|
||||
#define DIVERT_FILTER_FIELD_MAX \
|
||||
DIVERT_FILTER_FIELD_UDP_PAYLOADLENGTH
|
||||
|
||||
#define DIVERT_FILTER_TEST_EQ 0
|
||||
#define DIVERT_FILTER_TEST_NEQ 1
|
||||
#define DIVERT_FILTER_TEST_LT 2
|
||||
#define DIVERT_FILTER_TEST_LEQ 3
|
||||
#define DIVERT_FILTER_TEST_GT 4
|
||||
#define DIVERT_FILTER_TEST_GEQ 5
|
||||
#define DIVERT_FILTER_TEST_MAX DIVERT_FILTER_TEST_GEQ
|
||||
|
||||
#define DIVERT_FILTER_MAXLEN 64
|
||||
|
||||
#define DIVERT_FILTER_RESULT_ACCEPT (DIVERT_FILTER_MAXLEN+1)
|
||||
#define DIVERT_FILTER_RESULT_REJECT (DIVERT_FILTER_MAXLEN+2)
|
||||
|
||||
/*
|
||||
* Packet definitions.
|
||||
*/
|
||||
#ifndef DIVERT_PACKET_DIRECTION_OUTBOUND
|
||||
#define DIVERT_PACKET_DIRECTION_OUTBOUND 0
|
||||
#define DIVERT_PACKET_DIRECTION_INBOUND 1
|
||||
#endif /* DIVERT_PACKET_DIRECTION_OUTBOUND */
|
||||
|
||||
/*
|
||||
* Message definitions.
|
||||
*/
|
||||
struct divert_message_s
|
||||
{
|
||||
UINT16 magic; // DIVERT_MAGIC
|
||||
UINT8 version; // DIVERT_VERSION
|
||||
UINT8 reserved; // Reserved (set to 0x0)
|
||||
};
|
||||
typedef struct divert_message_s *divert_message_t;
|
||||
|
||||
/*
|
||||
* IOCTL structures.
|
||||
*/
|
||||
struct divert_ioctl_filter_s
|
||||
{
|
||||
UINT8 field; // DIVERT_FILTER_FIELD_IP_*
|
||||
UINT8 test; // DIVERT_FILTER_TEST_*
|
||||
UINT8 success; // Success continuation.
|
||||
UINT8 failure; // Fail continuation.
|
||||
UINT32 arg[4]; // Argument.
|
||||
};
|
||||
typedef struct divert_ioctl_filter_s *divert_ioctl_filter_t;
|
||||
|
||||
/*
|
||||
* IOCTL codes.
|
||||
*/
|
||||
#define IOCTL_DIVERT_SET_FILTER \
|
||||
CTL_CODE(FILE_DEVICE_NETWORK, 0x90A, METHOD_BUFFERED, FILE_ANY_ACCESS)
|
||||
|
||||
#endif // __DIVERT_DEVICE_H
|
||||
@@ -0,0 +1 @@
|
||||
!INCLUDE $(NTMAKEENV)\makefile.def
|
||||
@@ -0,0 +1,4 @@
|
||||
$(TARGETPATH)\$(_BUILDARCH)\divert.inf: divert.inf
|
||||
copy /y divert.inf $@
|
||||
$(TARGETPATH)\$(_BUILDARCH)\WdfCoInstaller01009.dll:
|
||||
copy /y $(BASEDIR)\redist\wdf\$(_BUILDARCH)\WdfCoInstaller01009.dll $@
|
||||
+2457
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,25 @@
|
||||
; divert.inf
|
||||
; (C) 2011, all rights reserved,
|
||||
;
|
||||
; This program is free software: you can redistribute it and/or modify
|
||||
; it under the terms of the GNU General Public License as published by
|
||||
; the Free Software Foundation, either version 3 of the License, or
|
||||
; (at your option) any later version.
|
||||
;
|
||||
; This program is distributed in the hope that it will be useful,
|
||||
; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
; GNU General Public License for more details.
|
||||
;
|
||||
; You should have received a copy of the GNU General Public License
|
||||
; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
[Version]
|
||||
Signature="$WINDOWS NT$"
|
||||
|
||||
[divert.NT.Wdf]
|
||||
KmdfService = divert, divert_WdfSection
|
||||
|
||||
[divert_WdfSection]
|
||||
KmdfLibraryVersion = 1.9
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* divert.rc
|
||||
* (C) 2011, all rights reserved,
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <windows.h>
|
||||
#include <ntverp.h>
|
||||
|
||||
#define VER_FILETYPE VFT_DRV
|
||||
#define VER_FILESUBTYPE VFT2_DRV_NETWORK
|
||||
#define VER_FILEDESCRIPTION_STR \
|
||||
"Divert Driver for network packet capture and injection"
|
||||
#define VER_INTERNALNAME_STR "divert.sys"
|
||||
#define VER_ORIGINALFILENAME_STR "divert.sys"
|
||||
|
||||
#include "common.ver"
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
# sources
|
||||
# (C) 2011, all rights reserved,
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
TARGETNAME=divert
|
||||
TARGETTYPE=DRIVER
|
||||
TARGETPATH=..\install
|
||||
TARGETLIBS=\
|
||||
$(DDK_LIB_PATH)\wdmsec.lib \
|
||||
$(DDK_LIB_PATH)\ndis.lib \
|
||||
$(DDK_LIB_PATH)\fwpkclnt.lib \
|
||||
$(SDK_LIB_PATH)\uuid.lib
|
||||
NTTARGETFILES=\
|
||||
$(TARGETPATH)\$(_BUILDARCH)\divert.inf \
|
||||
$(TARGETPATH)\$(_BUILDARCH)\WdfCoInstaller01009.dll
|
||||
KMDF_VERSION_MAJOR=1
|
||||
C_DEFINES=$(C_DEFINES) -DBINARY_COMPATIBLE=0 -DNT -DUNICODE -D_UNICODE \
|
||||
-DNDIS60 -DNDIS_SUPPORT_NDIS60
|
||||
INCLUDES=$(DDK_INC_PATH);..\include
|
||||
SOURCES=divert.rc divert.c
|
||||
|
||||
Reference in New Issue
Block a user