IBM i Global

IBM i Global

Connect, learn, share, and engage with IBM Power.

 View Only
Expand all | Collapse all

Checking for configured ports on IBMi

  • 1.  Checking for configured ports on IBMi

    Posted Tue July 02, 2024 05:36 AM

    Hi,

    Is there any way to check which ports have been configured for use on IBMi (but not currently active).

    NETSTAT *CNN gives the "Active" ports at any point in time and CFGTCP , option 4 gives you any port restrictions.

    However, I am looking for an easy way to find out "list of all ports which are configured but NOT CURRENTLY ACTIVE"

    Any guidance would be appreciated. Thank you.



    ------------------------------
    Thomas Varkey
    ------------------------------


  • 2.  RE: Checking for configured ports on IBMi

    Posted Tue July 02, 2024 01:30 PM

    There's inbound ports, and there's outbound ports.  Inbound ports generally stay active unless you end the service.  For example, ftp port 21 stays active unless I ENDTCPSVR SERVER(*FTP).

    Outbound ports are a different animal and often can be quite random.  For example if I ftp FROM this IBM i to another server might go out on port 9393 (this time, as tested).

    I know that firewall/switch personnel like to lock down specific IP addresses and ports.  Most of them understand ephemeral ports (like the port 9393 example above).  Careful, as they will sometime track a weeks worth of usage and base their decision off of that.  Which may cause issues when you only run certain stuff outside of that test period such as SNDPTFORD, SNDSRVRQS.  Then there's also the case if your IBM i supports multiple IP addresses, like for multiple web sites, domino servers, H/A software routing, etc and some IP clients don't support "bind specific".  For example, if I telnet from an lpar with multiple IP addresses which one am I coming from?

    You can do a STRCMNTRC to capture some of this.  And it will even generate a pcap file, loved by many a network technician.  DMPCMNTRC CFGOBJ(LANLINSYS) CFGTYPE(*LIN) TOSTMF('/home/ROB/myfile.pcap') FORMAT(*PCAP)

    I don't believe there are any ways to journal these through journals, qhst, etc.

    See also:  https://www.ibm.com/docs/en/i/7.5?topic=is-communication-services



    ------------------------------
    Robert Berendt IBMChampion
    ------------------------------



  • 3.  RE: Checking for configured ports on IBMi

    Posted Wed July 03, 2024 01:43 AM

    Hello Robert,

    Thank you for your reply. I was actually asking about inbound ports. Is there any way to know which inbound ports have been configured. NETSTAT *CNN shows all the inbound ports that are ACTIVE at that moment in time. Is there any way to know which inbound ports have been configured BUT NOT CURRENTLY ACTIVE.

    For eg, if for a new application, I need to define an inbound port (say 1475), it would be good to know if this inbound port has already been used for some other application (which may not be active at that point in time.

    Appreciate your thoughts on this.



    ------------------------------
    Thomas Varkey
    ------------------------------



  • 4.  RE: Checking for configured ports on IBMi

    Posted Thu July 04, 2024 11:21 PM

    To get traffic log in a journal, one could use the integrated Packet Filter with an "allow all" filterset and journaling to "Full": https://www.ibm.com/docs/en/i/7.5?topic=mpr-journaling-auditing-packet-rules-actions-by-packet-rules .



    ------------------------------
    Sylvain Manceau
    ------------------------------



  • 5.  RE: Checking for configured ports on IBMi

    Posted Mon July 08, 2024 02:13 PM

    Now that is a very good answer that points in the right direction to definitively answer the question "what ports are being used on my system".

    The Packet Filtering that Sylvain references has the further ability to define rules that limit how much data you are going to journal.  In my mind, every packet for every connection is way too much to journal.

    If you are interested in which ports are being used, and who is using them, you really only journal the first packet for each connection.  Fortunately, for TCP/IP connections, the first packet is a SYN packet that starts the establishment of the connection.

    The packet rules allow you to select those specific packets with the TCP/STARTING protocol selector so you'll only log that first packet.

    I just tested this with the following ruleset on a lab system:

    FILTER SET LogConnections   ACTION = PERMIT   DIRECTION = INBOUND SRCADDR = *   DSTADDR = *   PROTOCOL = TCP/STARTING   DSTPORT = *   SRCPORT = *   JRN = FULL
    FILTER SET LogConnections      ACTION = PERMIT   DIRECTION = *   SRCADDR = *   DSTADDR = *   PROTOCOL = *   DSTPORT = *   SRCPORT = *   JRN = OFF
     
    FILTER_INTERFACE   IP_ADDRESS = {your interface ip address here}   SET = LogConnections
    The first rule says to accept all inbound TCP starting connections from anywhere on any port and log it to the journal
    The second rule overrides the default deny all rule and accepts all packets on all protocols without logging (basically this is what you get without any rules)
    With this rule installed, every TCP connection ATTEMPT gets logged with a M-TF entry in QIPFILTER journal.  That is the easiest way you will get a list of all ports being used.  Do note that this is connection attempts -- it will log attempts on ports that are not getting listened on also.  Ideally, you would log the outbound SYN-ACK that shows a connection was established instead of just the attempts, but that does not appear to be possible.

    Run that for a sufficient period to catch your normal traffic and then analyze with an SQL service:

    WITH filterlog AS (
            SELECT CAST(ENTRY_DATA AS VARCHAR(1000)) AS entry
                FROM TABLE (
                        QSYS2.DISPLAY_JOURNAL('QUSRSYS', 'QIPFILTER', JOURNAL_ENTRY_TYPES => 'TF')
                    )
        )
        SELECT SUBSTR(entry, 1, 10) AS line,
               SUBSTR(entry, 29, 15) AS Srcip,
               SUBSTR(entry, 44, 5) AS Srcport,
               SUBSTR(entry, 49, 15) AS DestIP,
               SUBSTR(entry, 64, 5) AS DestPort
            FROM filterlog
            Group BY DestPort;

    You can use the same rules to restrict the ports that can be used once you determine what is normal for your environment.  That would address the ability to have a program listen on any port.  It doesn't matter if they are listening if you block anyone that tries to talk to them.

    This only logs TCP traffic.  Other protocols, such as UDP and ICMP don't have that handshake that allows easy detection of the start of a conversation, so if you want to track those, you will need different rules.



    ------------------------------
    Vincent Greene
    IT Consultant
    Technology Expert labs
    IBM
    Vincent.Greene@ibm.com


    The postings on this site are my own and don't necessarily represent IBM's positions, strategies or opinions.
    ------------------------------



  • 6.  RE: Checking for configured ports on IBMi

    Posted Mon July 08, 2024 04:00 PM

    This is really cool info.  Thanks for taking the time and sharing this!



    ------------------------------
    Steven Riedmueller
    Certified IBM i Admin
    Speaker, Mentor, and Advocate
    ------------------------------



  • 7.  RE: Checking for configured ports on IBMi

    Posted Mon July 08, 2024 11:48 PM

    Thank you, Vincent, for your very detailed post! I wasn't aware of the "TCP/STARTING" protocol selector.

    Any thought about the "TCP SYN bit" mentioned here: https://www.ibm.com/docs/en/i/7.5?topic=filtering-ip-packet-header ?



    ------------------------------
    Sylvain Manceau
    ------------------------------



  • 8.  RE: Checking for configured ports on IBMi

    Posted Tue July 09, 2024 11:26 AM

    I didn't even know that the packet filtering could be used to write journal entries until I saw your post and started experimenting.  It is a very nice solution to a situation that I did not think could be easily solved on IBM i.  It was the search for the SYN bit mentioned on that page that led me to TCP/STARTING (since that is the function of the SYN bit)

    I did not find any definitive syntax reference for the rules, but my experimentation showed that the TCP/STARTING only captured the first packet in the exchange.  Using OUTBOUND & TCP/STARING only got me the first packet in the outbound connections, not the SYN/ACK that I would expect from the response to the initiation of a new connection.

    For those that don't know, TCP connections use a three way handshake to establish a connection:  SYN, SYN-ACK, and ACK

    The initiator sends a SYN packet (I want to connect to your port x from my port Y) (SYN Bit is set in the header)

    If the port is open and the connection is accepted by the target, it sends a SYN-ACK packet (SYN and ACK bits set in the header) which means "Glad to hear from you - lets talk, starting with this sequence number, etc"

    Finally the initiator acknowledges  the connection is open by sending a ACK packet ("Thanks for accepting my call") and the connection is open.

    I can still picture Professor Synder in the front of the classroom waving his hands and saying "NAK-NAK-NAK" while explaining these kinds of protocols :)

    If the port is not open, the target will send a different response (SYN-RST I think) telling the initiator to go away.  Different situations result in different sets of responses, such as open ports that don't allow specific addresses.  These different response may be manipulated by hackers (not necessarily the bad kind) to probe open ports.  The nmap documentation explains the different results pretty well:  https://nmap.org/book/scan-methods-null-fin-xmas-scan.html

    Internal port scans by security software are not unusual, so if you use this process and see a lot of TCP/STARTING in a journal from different ports from the same IP address, especially if no other addresses use those ports, you can probably trace that to a port scanner.  Ask your security team if it is theirs and you'll get some points for detecting them, or send them into a panic that someone else on the network is port scanning you.  

    In any case, if you use the technique of journaling the INBOUND TCP/STARTING packets, you might see connections that did not complete - if you see connections on ports that are only accessed by one IP, especially if that same IP has lots of ports it tries to access, that is probably not a port that is open -- its just a port scanner.

    If you see lots of IPs accessing lots of ports that are not likely open, that probably means you need to have a discussion with your security team about their firewall rules.



    ------------------------------
    Vincent Greene
    IT Consultant
    Technology Expert labs
    IBM
    Vincent.Greene@ibm.com


    The postings on this site are my own and don't necessarily represent IBM's positions, strategies or opinions.
    ------------------------------



  • 9.  RE: Checking for configured ports on IBMi

    Posted Wed July 10, 2024 01:35 AM

    Once again, thank you Vincent for your very detailed reply!



    ------------------------------
    Sylvain Manceau
    ------------------------------



  • 10.  RE: Checking for configured ports on IBMi

    Posted Wed July 03, 2024 03:47 AM

    You can try WRKSRVTBLE command. It shows if a service is assigned to a particular port. But use it as a hint only. If there's an entry for a port, it doesn't mean the port is ever used and you need to look into it further. Also, if there's no entry for a port, it doesn't mean the port is unused. But any serious application should have a relevant entry in that list. Using a specific port is application dependent and it sits in the application configuration. Technically (and in most cases) you can change the port number at any time in the configuration and the system doesn't track configuration changes. So until the application is started, the system wouldn't know the port number. As far as I remember, if you try to start application and the port is already active and used by other application then your service will not start properly.



    ------------------------------
    Krzysztof Łukawski
    ------------------------------



  • 11.  RE: Checking for configured ports on IBMi

    Posted Wed July 03, 2024 07:11 AM

    I think the easiest answer to your question is "no".  An inbound port is is simply something which listens on that port.  You can create a simple program that will listen on any available port you state.  Application port numbers could be in code or data objects.  Web Servers will use various, changeable, ports which would show up in their config.  Is there more to the question?



    ------------------------------
    Patrick Kelly
    ------------------------------



  • 12.  RE: Checking for configured ports on IBMi

    Posted Wed July 03, 2024 10:26 AM

    I think Patrick sums up the best answer.

    Others, me included, knew or assumed that Patrick's answer was a given and were trying to jump past that to try to provide work arounds.

    I use a MFT solution which stores their port number in an IFS file in their product directory.  I'm betting others do too.  So that's yet another place to look.



    ------------------------------
    Robert Berendt IBMChampion
    ------------------------------



  • 13.  RE: Checking for configured ports on IBMi

    Posted Wed July 03, 2024 10:01 AM

    For webservers, you could go into the HTTPAdmin and choose "All Servers" from the "Servers" dropdown.  This will show the IP:port configured in each webserver, even if they are not running at the moment.  I think you'll need to find similar resources (or create your own) for other types of services.  If you have any idea of the services that you're concerned about, we can probably help you hunt down the port details.



    ------------------------------
    Steven Riedmueller
    Certified IBM i Admin
    Speaker, Mentor, and Advocate
    ------------------------------



  • 14.  RE: Checking for configured ports on IBMi

    Posted Mon July 08, 2024 04:04 PM

    I have this piece of SQL that will search for the configured ports in your httpd config files.

    with ifsobjs (pathtypeas (
    select path_name, object_type
      from table(qsys2.IFS_OBJECT_STATISTICS( 
                       start_path_name => '/www',
                       subtree_directories => 'YES')) a
          where path_name like '%httpd.conf'
    )
    select path, line, line_number, type 
    from ifsobjs i, lateral (
      select * from table (
          qsys2.ifs_read(
            path_name => path
            end_of_line => 'ANY',
            maximum_line_length => default
            ignore_errors => 'NO'
        ) ) r
        where upper(line) like '%LISTEN %' 
    order by path ;



    ------------------------------
    Bryan Dietz
    ------------------------------



  • 15.  RE: Checking for configured ports on IBMi

    Posted Mon July 08, 2024 04:32 PM

    In the bash shell:

    cd /www
    for i in `find . | grep 'httpd\.conf'`
    do
    echo $i
    grep -n -i LISTEN $i
    if [ "$?" != "0" ]; then echo "LISTEN not found in $i"; fi
    done



    ------------------------------
    Jack Woehr
    ------------------------------



  • 16.  RE: Checking for configured ports on IBMi

    Posted Mon July 08, 2024 05:08 PM

    nice!!

    I added this to the begining:
    save_cwd=$(pwd)

    and to the end:
    cd $save_cwd

    just needed to be back where I started from  :-)



    ------------------------------
    Bryan Dietz
    ------------------------------



  • 17.  RE: Checking for configured ports on IBMi

    Posted Tue July 09, 2024 04:29 AM

    Or use the pushd and popd commands instead of your $save_cwd:

    pushd /www
    for i in `find . | grep 'httpd\.conf'`
    do
    echo $i
    grep -n -i LISTEN $i
    if [ "$?" != "0" ]; then echo "LISTEN not found in $i"; fi
    done
    popd

    Use --help option to read the help text, e.g. pushd --help, since there's no man page for the commands - or read the GNU manual:

    https://www.gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html



    ------------------------------
    Christian Jorgensen
    IT System Administrator | CEAC member
    Network of Music Partners A/S
    ------------------------------



  • 18.  RE: Checking for configured ports on IBMi

    Posted Wed July 03, 2024 10:17 AM

    I tried searching https://wiki.midrange.com/index.php/Main_Page for ports to see if anyone had created any table of commonly used ports on IBM i.  I didn't see any.



    ------------------------------
    Robert Berendt IBMChampion
    ------------------------------



  • 19.  RE: Checking for configured ports on IBMi

    Posted Mon July 08, 2024 02:36 AM

    Thank you for your thoughts on this.



    ------------------------------
    Thomas Varkey
    ------------------------------