SFTP Known Hosts

Last modified by Danniar Firdausy on 2024/09/18 20:35

In our previous microlearning, we explored how to connect to an SFTP server using eMagiz and discussed an alternative security method for authentication. Now, we are going to delve deeper into securing your SFTP connections by storing the server’s unique fingerprint to protect against "man in the middle" attacks. This microlearning will guide you through the process of generating and managing a "known hosts" file to ensure you're always connecting to the correct SFTP server. 

Should you have any questions, please get in touch with academy@emagiz.com.

1. Prerequisites

  • Basic knowledge of the eMagiz platform
  • Novice course on File-based connectivity

2. Key concepts

This microlearning is about SFTP known host files.

  • By SFTP known hosts, we mean: Making sure that we are certain that we connect to the correct SFTP to prevent data theft as a result of a "man in the middle" attack.

3. SFTP Known Hosts

In this microlearning, we want to expand our knowledge and look at a way to store the unique fingerprint of the SFTP to avoid that someone else can pretend to be the SFTP when you want to send data (i.e. a "man in the middle attack"). To retrieve the unique fingerprint of an SFTP you first need to connect to the SFTP in question. This way you can retrieve the unique fingerprint and secure it in a file for future use to prevent the "man in the middle attack".

  • Each SFTP has a unique fingerprint that identifies the SFTP.
  • To prevent a "man in the middle" attack, this fingerprint needs to be stored client side.
  • There are two distinct methods to generate the known hosts file.

There are two distinct ways of retrieving and storing the unique fingerprint of the SFTP in a "known hosts file". The first option is portal based and the second option is command line based. The preferred option is the portal based one. Do note that the first option only works if the SFTP is publicly accessible without any IP restrictions.

3.1 Known Hosts File Generation - Portal

To configure the preferred option we start at the flow level in Create. Once the flow is opened please enter "Start Editing" mode and navigate to the Support object tab in the right panel. In here open the SFTP (caching) session factory that is used to connect to the SFTP.

intermediate-file-based-connectivity-sftp-known-hosts--sftp-session-factory-start.png

Once you see the configuration of the component please define a property for the known hosts field. This property will later on be used to generate and store the known hosts file while in Deploy.

intermediate-file-based-connectivity-sftp-known-hosts--sftp-session-known-hosts.png

When you are satisfied with the configuration you can save it, press "Stop editing", and create a new version of the flow. Now that we have a new version of the flow we navigate to the Deploy phase to trigger eMagiz to generate and store the known hosts file correctly on runtime level. To do so we need to create the property (with the correct name) in Deploy. If you are unsure on how to create properties in the first place, please check out this property management microlearning. 

When defining the value of the property we now have an additional option in the "Data type" dropdown called "Known hosts String". Once you have selected this a cogwheel icon will apear next to the value field.

intermediate-file-based-connectivity-sftp-known-hosts--known-hosts-string-property-start.png

Pressing this cogwheel icon will open a new pop-up in which you need to define the port and the host you want to connect to.

intermediate-file-based-connectivity-sftp-known-hosts--known-hosts-string-property-config-empty.png

After filling in the correct values press the button called "Generate known hosts string" to make sure that eMagiz will generate the file for you. As a result, eMagiz will show you the correct string that defines the known host. 

intermediate-file-based-connectivity-sftp-known-hosts--known-hosts-string-property-generated.png

Information

In case you have multiple SFTP connections within your flow (or runtime) you should use distinct property placeholder for each SFTP to utilize this functionality.

3.2 Known Hosts File Generation - Command Line

The secondary option, when there are IP restrictions, provides you with the option to execute several commands after gaining access to the correct server to generate the known hosts file. These steps are detailed below.
  

  • Gain access to the server in question from which you can reach the SFTP.
  • Execute the following command (compatible with both Linux and Windows): ssh-keyscan -p [port] [host] > [known-hosts target path]
    • An example would be: ssh-keyscan -p 22 localhost > C:\tmp\generated_known_hosts
  • Navigate to the generated file and open it. The file should contain one or more lines in the format hostname/ip key-type value
    e.g. 127.0.0.1 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAICZPI5pQ6PSXgS4QOPsai2QjC2EcVjFejvUM5RMYOmwbwW
  • Open eMagiz and navigate to the flow where the file is needed.
  • Upload the known_hosts file as a flow resource, and click on the "eye" icon to see its unique name.
  • Define a property for the known hosts field in the SFTP (caching) session factory.
    • Provide a value for the property upon deploying the release that follows the following structure: classpath:emagiz-resources/[filename]

4. Key takeaways

  • Each SFTP server has a unique fingerprint that serves as its identifier.
  • To protect against "man in the middle" attacks, you must securely store this fingerprint on the client side.
  • There are two methods for generating and managing the known hosts file: a portal-based approach and a command-line approach. The portal-based method is preferred for publicly accessible SFTP servers without IP restrictions.

5. Suggested Additional Readings

If you are interested in this topic and want more information, please read the release notes provided by eMagiz. Furthermore, check out these links: