Skip links
Picture of Vahagn Vardanian

Vahagn Vardanian

Co-founder and CTO of RedRays

Open-Source SAP Protocol Library for Penetration Testing

If you do SAP pentesting, you probably know pysap. It's the only open-source library that lets you talk to SAP systems at the protocol level - Diag, Message Server, Router, HANA, RFC, the whole stack. The problem? It only worked on Python 2.7, which has been dead since 2020. We fixed that.

We ported the entire library to Python 3, tested it against a live SAP S/4HANA 2025 system, and submitted the changes back to the OWASP repository (PR #84). Here's what we did and how you can use pysap in your SAP security work.

SAP Protocols
9
File Formats
4
Example Scripts
33
Unit Tests
67
CI Platforms
3

Why pysap matters

Most SAP security tools work at the application layer - they call RFC functions, hit HTTP endpoints, or parse ABAP code. pysap works one level below that. It implements the actual wire protocols that SAP GUI, sapRouter, and the message server use. You can craft raw Diag packets, forge message server registrations, or parse HANA auth handshakes byte by byte.

This matters because a lot of SAP attack surface sits at the protocol layer. Default credentials, exposed message servers, misconfigured router ACLs, weak HANA authentication - you can't test any of this with a web scanner. You need something that speaks SAP.

What's in the box

ProtocolModuleWhat you can do with it
SAP DiagSAPDiagAutomate SAP GUI login, grab login screens, brute force credentials
Message ServerSAPMSDump system config, read kernel params, enumerate app servers
SAP RouterSAPRouterFingerprint routers, scan through them, test ACLs
Gateway / RFCSAPRFCMonitor gateway connections, send RFC packets
HANASAPHDBTest HANA auth (SCRAM, JWT, SAML, Kerberos), find tenants
EnqueueSAPEnqueueMonitor lock server, test for DoS conditions
IGSSAPIGSTest image conversion service exploits
SSFS / Credv2 / PSESAPSSFS etc.Decrypt credential files, certificates, secure storage offline

What the migration actually involved

Running 2to3 on pysap gets you about 10% of the way there. The real work is in the binary protocol handling. In Python 2, str and bytes were the same thing. In Python 3, they're not, and every packet field, every socket read, every comparison breaks if you get it wrong.

C extension rewrite

pysapcompress.cpp

SAP's LZH/LZC compression is implemented as a C++ extension. The Python 2 module init (Py_InitModule3) doesn't exist in Python 3. We rewrote it with PyModuleDef, changed the format codes from "s#" to "y#", and added PY_SSIZE_T_CLEAN. That last one was fun to debug - without it, Py_BuildValue("y#", buf, len) reads 8 bytes off the stack on 64-bit Windows instead of 4, because Py_ssize_t != int. Looked like a MemoryError but was actually stack corruption.

str vs bytes across 14 modules

Every StrFixedLenField default, every protocol constant, every socket comparison. String literals like "\x1f\x9d" that worked fine in Python 2 need a b prefix in Python 3. Constants like SAPROUTER_PONG = "NI_PONG" need to be bytes because Scapy field values come back as bytes now. We also hit issues with Scapy 2.7 being stricter about duplicate field names in ConditionalField patterns - had to rename or restructure fields in SAPMS, SAPRFC, SAPRouter, and SAPCAR.

Crypto that actually works

SAP stores credentials in files encrypted with custom schemes (RSEC cipher, PKCS12 PBKDF1, SCRAM-SHA256). These had Python 2 patterns everywhere - chr() on integers, ord() on bytes, value.encode("hex"), integer division with / instead of //. One subtle bug: the original code had digest.update("" * 0x20) which in Python 2 is an empty string (because "" * 32 == ""). We initially "fixed" it to b"\x00" * 0x20 (32 null bytes), which broke all credential decryption. The correct fix is b"".

CI/CD on three platforms

The old pipeline ran Python 2.7 on Ubuntu 18.04. We replaced it with a matrix of Python 3.9 through 3.12 on Ubuntu, macOS, and Windows. 14 jobs, all green. The Windows build was the last to pass because of the Py_ssize_t issue mentioned above.

SAP pentest: where pysap fits in

When we do SAP security assessments, the first thing we want to know is what's exposed on the network. SAP systems typically have 10-20 open ports per instance. Here's how we use pysap at each stage.

Reconnaissance: Message Server (port 39xx)

If the internal message server port is reachable, you get everything. System name, kernel version, all connected application servers with their IPs and ports, active clients, HTTP/RFC/SMTP endpoints. One command gives you the full landscape.

# Get system info, connected servers, ACLs, statistics python3 examples/ms_dump_info.py -d 10.0.0.1 -p 3901 # Check 50+ security parameters (gateway ACLs, RFC auth, audit config) python3 examples/ms_dump_param.py -d 10.0.0.1 -p 3901 -f examples/list_sap_parameters

We tested this against a live S/4HANA 2025 system. Got the full system dump including kernel version (916.9160.75), all registered protocols (Diag:3200, RFC:3300, HTTP:50000, HTTPS:44300, SMTP:25000), gateway ACL settings, and patch level info.

Authentication testing: Diag protocol (port 32xx)

pysap simulates what SAP GUI does - connects to the dispatcher, gets the login screen, sends credentials, and checks the response. You can automate credential testing across multiple clients without installing SAP GUI anywhere.

# Grab login screen details (DB name, hostname, kernel, language) python3 examples/diag_login_screen_info.py -d 10.0.0.1 -p 3200 # Test credentials from file (format: user:password:client) python3 examples/diag_login_brute_force.py -d 10.0.0.1 -p 3200 -c creds.txt

The brute force script handles client discovery, detects expired passwords, duplicate logins, and distinguishes between "valid user, wrong password" vs "user doesn't exist". We verified it finds SAP* with correct credentials on client 000.

Network pivoting: SAP Router (port 3299)

SAP Router sits between the internet and internal SAP systems. If the ACL is misconfigured (and it often is), you can route through it to reach internal hosts. pysap can fingerprint the router, test routes, and set up port forwarding.

# Fingerprint router version python3 examples/router_fingerprint.py -d 10.0.0.1 -p 3299 # Scan internal network through the router python3 examples/router_scanner.py -d 10.0.0.1 -p 3299 # Tunnel traffic through the router python3 examples/router_portfw.py -d 10.0.0.1 -p 3299

Database layer: HANA (port 3xx13/3xx15)

pysap implements the HANA SQL Command Network Protocol with support for SCRAM-SHA256, SCRAM-PBKDF2-SHA256, JWT, SAML, and Kerberos auth. You can test HANA authentication and enumerate tenants without the HANA client.

python3 examples/hdb_auth.py -d 10.0.0.1 -p 30015 -u SYSTEM -l password python3 examples/hdb_discovery.py -d 10.0.0.1 -p 30015

Post-exploitation: credential files

SAP stores database passwords, RFC credentials, and SSO certificates in encrypted files on the filesystem (SSFS, Credv2, PSE). If you get read access to /usr/sap/<SID>/SYS/global/security/, pysap can decrypt them. The encryption keys are either embedded in the binary or stored in a key file next to the data.

# Decrypt SSFS secure store python3 -c " from pysap.SAPSSFS import SAPSSFSKey, SAPSSFSData key = SAPSSFSKey(open('SSFS_HDB.KEY','rb').read()) data = SAPSSFSData(open('SSFS_HDB.DAT','rb').read()) for r in data.records: print(r.key_name, r.get_plain_data(key)) "

Supports 3DES, AES256, and LPS-encrypted credentials. Also handles encrypted PSE files (PKCS12/PBES1) and SAP CAR archives.

Typical assessment workflow

1. Port scan - find SAP services (32xx, 33xx, 36xx, 39xx, 3xx13)
2. Message server recon - ms_dump_info.py + ms_dump_param.py to map the landscape and check config
3. Default credentials - diag_login_brute_force.py with default_sap_credentials file across all clients
4. Router testing - if 3299 is open, check ACLs and try internal routing
5. HANA auth - test database-level authentication on tenant and system databases
6. File-level - if you have filesystem access, decrypt SSFS/Credv2/PSE for stored passwords

Get started

# Install pip3 install git+https://github.com/redrays-io/pysap.git # Or from source git clone https://github.com/redrays-io/pysap.git cd pysap && pip3 install -e .

Needs Python 3.9+ and a C compiler (gcc/clang/MSVC) for the compression extension. Works on Linux, macOS, and Windows.

Links

redrays-io/pysap - Python 3 fork, 14/14 CI jobs green
OWASP/pysap PR #84 - migration PR, under review
OWASP/pysap - original repo
redrays.io - SAP security tools and research

Explore More

SAP Security Patch Day – March 2026

SAP has released its March 2026 security patch package containing 15 security notes addressing vulnerabilities across enterprise SAP environments. This release includes

SAP Security Patch Day February 2026

SAP has released its February 2026 security patch package containing 27 security notes addressing critical vulnerabilities across enterprise SAP environments. This release