{"id":524349,"date":"2025-09-10T15:20:22","date_gmt":"2025-09-10T15:20:22","guid":{"rendered":"https:\/\/www.ninjaone.com\/?post_type=script_hub&#038;p=524349"},"modified":"2025-09-10T15:20:22","modified_gmt":"2025-09-10T15:20:22","slug":"configurer-le-pare-feu-macos-avec-un-script-shell","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/fr\/script-hub\/configurer-le-pare-feu-macos-avec-un-script-shell\/","title":{"rendered":"Comment configurer le pare-feu macOS avec un script Shell"},"content":{"rendered":"<p>Avec les pr\u00e9occupations croissantes concernant la <a href=\"https:\/\/www.ninjaone.com\/fr\/blog\/quest-ce-que-la-securite-des-terminaux-et-comment-fonctionne-t-elle\/\">s\u00e9curit\u00e9 des terminaux<\/a>, la gestion des param\u00e8tres du pare-feu macOS \u00e0 grande \u00e9chelle est une responsabilit\u00e9 essentielle pour les professionnels de l&rsquo;informatique et les <a href=\"https:\/\/www.ninjaone.com\/fr\/quest-ce-quun-msp\/\">fournisseurs de services g\u00e9r\u00e9s (MSP)<\/a>. Qu&rsquo;il s&rsquo;agisse de renforcer les MacBooks de l&rsquo;entreprise ou d&rsquo;assurer une posture r\u00e9seau furtive pour les appareils sur le terrain, le fait de disposer d&rsquo;un moyen coh\u00e9rent et automatis\u00e9 d&rsquo;appliquer les configurations de pare-feu peut r\u00e9duire de mani\u00e8re significative les vuln\u00e9rabilit\u00e9s.<\/p>\n<p>Cet article pr\u00e9sente un script shell puissant qui permet aux administrateurs informatiques d&rsquo;<strong>activer, de d\u00e9sactiver et de configurer le pare-feu macOS<\/strong>\u00a0de mani\u00e8re programmatique, afin de garantir la conformit\u00e9 et d&rsquo;\u00e9liminer les erreurs manuelles.<\/p>\n<h2>Contexte<\/h2>\n<p>macOS comprend un pare-feu int\u00e9gr\u00e9, contr\u00f4l\u00e9 par <strong>socketfilterfw<\/strong>, qui offre un contr\u00f4le pr\u00e9cis sur les connexions r\u00e9seau entrantes et une visibilit\u00e9 en mode furtif. Cependant, la modification manuelle de ces param\u00e8tres via l&rsquo;interface utilisateur des param\u00e8tres du syst\u00e8me n&rsquo;est pas extensible pour les MSP ou les d\u00e9partements informatiques des grandes entreprises qui g\u00e8rent des centaines ou des milliers de terminaux. De plus, les changements bas\u00e9s sur l&rsquo;interface utilisateur peuvent \u00eatre ignor\u00e9s ou manqu\u00e9s, ce qui conduit \u00e0 des lignes de base de s\u00e9curit\u00e9 incoh\u00e9rentes.<\/p>\n<p>Ce script shell est con\u00e7u pour\u00a0<strong>configurer le pare-feu macOS de mani\u00e8re programmatique<\/strong>, prenant en charge Ventura (macOS 13) et les versions ult\u00e9rieures. En utilisant des param\u00e8tres tels que <strong>&#8211;firewallState<\/strong>, <strong>&#8211;blockAllIncomingConnections<\/strong> et <strong>&#8211;stealthMode<\/strong>, les professionnels de l&rsquo;informatique peuvent appliquer les politiques de l&rsquo;entreprise, se conformer aux cadres de s\u00e9curit\u00e9 et rationaliser le renforcement des terminaux lors de l&rsquo;int\u00e9gration, de la rem\u00e9diation \u00e0 distance ou de la pr\u00e9paration des audits.<\/p>\n<h2>Le script<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">#!\/usr\/bin\/env bash\r\n#\r\n# Description: This script configures the macOS firewall settings, including enabling\/disabling the firewall, blocking all incoming connections, and setting stealth mode to hide the computer from the network.\r\n# By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https:\/\/www.ninjaone.com\/terms-of-use.\r\n# Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. \r\n# Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. \r\n# Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. \r\n# Warranty Disclaimer: The script is provided \u201cas is\u201d and \u201cas available\u201d, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. \r\n# Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. \r\n# Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. \r\n# EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).\r\n#\r\n# Example: --firewallState \"Enabled\" --stealthMode \"Enabled\" --blockAllIncomingConnections \"Yes\"\r\n#\r\n# Verifying the current firewall state.\r\n# The macOS firewall state is already set to 'enabled'.\r\n# Successfully set the firewall state to 'enabled'.\r\n#\r\n# Verifying the current status of the 'Block all incoming connections' setting.\r\n# Setting the block all incoming connections state to 'blocking'.\r\n# Successfully set the block all incoming setting to 'blocking'.\r\n#\r\n# Verifying the current stealth mode state.\r\n# The stealth mode state is already set to 'enabled'.\r\n# Successfully set the stealth mode state to 'enabled'.\r\n#\r\n# Preset Parameter: --firewallState \"Enabled\"\r\n#\t  Enable or disable the overall firewall global state.\r\n#\r\n# Preset Parameter: --stealthMode \"Enabled\"\r\n#\t  Stealth mode hides the computer from the network by not responding to ICMP (ping) packets. Leave blank to not modify the setting.\r\n#\r\n# Preset Parameter: --blockAllIncomingConnections \"Yes\"\r\n#\t  Should all incoming connections be blocked or allowed. Leave blank to not modify the setting.\r\n#\r\n# Preset Parameter: --killSystemSettingsAppIfNecessary\r\n#   Changes to the firewall may not be visible in System Settings until the app is restarted.\r\n#\r\n# Preset Parameter: --help\r\n#\t  Displays some help text.\r\n#\r\n# Minimum OS Architecture Supported: macOS 13 (Ventura)\r\n# Release Notes: Initial Release\r\n\r\n_arg_firewallState=\r\n_arg_blockAllIncomingConnections=\r\n_arg_stealthMode=\r\n_arg_killSystemSettingsIfNecessary=\"off\"\r\n_exitCode=0\r\n\r\nprint_help() {\r\n  printf '\\n%s\\n\\n' 'Usage: [--firewallState|-f &lt;arg&gt;] [--stealthMode|-s &lt;arg&gt;] [--blockAllIncomingConnections|-b &lt;arg&gt;] [--killSystemSettingsAppIfNecessary|-k] [--help|-h]'\r\n  printf '%s\\n' 'Preset Parameter: --firewallState \"Enabled\"'\r\n  printf '\\t%s\\n' \"Enable or disable the overall firewall global state.\"\r\n  printf '%s\\n' 'Preset Parameter: --stealthMode \"Enabled\"'\r\n  printf '\\t%s\\n' \"Stealth mode hides the computer from the network by not responding to ICMP (ping) packets. Leave blank to not modify the setting.\"\r\n  printf '%s\\n' 'Preset Parameter: --blockAllIncomingConnections \"Yes\"'\r\n  printf '\\t%s\\n' \"Should all incoming connections be blocked or allowed. Leave blank to not modify the setting.\"\r\n  printf '%s\\n' 'Preset Parameter: --killSystemSettingsAppIfNecessary'\r\n  printf '\\t%s\\n' \"Changes to the firewall may not be visible in System Settings until the app is restarted.\"\r\n  printf '%s\\n' 'Preset Parameter: --help'\r\n  printf '\\t%s\\n' \"Displays this help menu.\"\r\n}\r\n\r\ndie() {\r\n  local _ret=\"${2:-1}\"\r\n  echo \"$1\" &gt;&amp;2\r\n  test \"${_PRINT_HELP:-no}\" = yes &amp;&amp; print_help &gt;&amp;2\r\n  exit \"${_ret}\"\r\n}\r\n\r\nparse_commandline() {\r\n  while test $# -gt 0; do\r\n    _key=\"$1\"\r\n    case \"$_key\" in\r\n    --firewallState | --firewallstate | -f)\r\n      test $# -lt 2 &amp;&amp; die \"[Error] Missing value for the required argument '$_key'.\" 1\r\n      _arg_firewallState=$2\r\n      shift\r\n      ;;\r\n    --firewallState=*)\r\n      _arg_firewallState=\"${_key##--firewallState=}\"\r\n      ;;\r\n    --blockAllIncomingConnections | --blockAll | -b)\r\n      test $# -lt 2 &amp;&amp; die \"[Error] Missing value for the optional argument '$_key'.\" 1\r\n      _arg_blockAllIncomingConnections=$2\r\n      shift\r\n      ;;\r\n    --blockAllIncomingConnections=*)\r\n      _arg_blockAllIncomingConnections=\"${_key##--blockAllIncomingConnections=}\"\r\n      ;;\r\n    --stealthMode | --stealthmode | -s)\r\n      test $# -lt 2 &amp;&amp; die \"[Error] Missing value for the optional argument '$_key'.\" 1\r\n      _arg_stealthMode=$2\r\n      shift\r\n      ;;\r\n    --stealthMode=*)\r\n      _arg_stealthMode=\"${_key##--stealthMode=}\"\r\n      ;;\r\n    --killSystemSettingsIfNecessary | -k)\r\n      _arg_killSystemSettingsIfNecessary=\"on\"\r\n      ;;\r\n    --help | -h)\r\n      _PRINT_HELP=yes die\r\n      ;;\r\n    *)\r\n      _PRINT_HELP=yes die \"[Error] Received an unexpected argument '$1'\" 1\r\n      ;;\r\n    esac\r\n    shift\r\n  done\r\n}\r\n\r\necho \" \"\r\nparse_commandline \"$@\"\r\n\r\n# If script form variables are used, replace the command line parameters with their value.\r\nif [[ -n $firewallState ]]; then\r\n  _arg_firewallState=\"$firewallState\"\r\nfi\r\nif [[ -n $blockAllIncomingConnections ]]; then\r\n  _arg_blockAllIncomingConnections=\"$blockAllIncomingConnections\"\r\nfi\r\nif [[ -n $stealthModeState ]]; then\r\n  _arg_stealthMode=\"$stealthModeState\"\r\nfi\r\nif [[ -n $killSystemSettingsAppIfNecessary &amp;&amp; \"$killSystemSettingsAppIfNecessary\" == \"true\" ]]; then\r\n  _arg_killSystemSettingsIfNecessary=\"on\"\r\nfi\r\n\r\n# Ensure the script is run with root permissions.\r\nif [[ $(id -u) -ne 0 ]]; then\r\n  _PRINT_HELP=no die \"[Error] This script must be run with root permissions. Try running it with sudo or as the system\/root user.\" 1\r\nfi\r\n\r\n# Trim whitespace from the firewall state argument.\r\nif [[ -n \"$_arg_firewallState\" ]]; then\r\n  _arg_firewallState=$(echo \"$_arg_firewallState\" | xargs)\r\nfi\r\n\r\n# Ensure a valid firewall state is provided.\r\nif [[ -z \"$_arg_firewallState\" ]]; then\r\n  _PRINT_HELP=yes die \"[Error] Please provide a valid firewall state to set such as 'Enabled' or 'Disabled'.\" 1\r\nfi\r\n\r\n# Convert firewall state to lowercase.\r\nif [[ -n \"$_arg_firewallState\" ]]; then\r\n  _arg_firewallState=$(echo \"$_arg_firewallState\" | tr '[:upper:]' '[:lower:]')\r\nfi\r\n\r\n# Validate the firewall state.\r\nif [[ \"$_arg_firewallState\" != \"enabled\" &amp;&amp; \"$_arg_firewallState\" != \"disabled\" ]]; then\r\n  _PRINT_HELP=yes die \"[Error] The firewall state '$_arg_firewallState' is invalid. Please provide a valid firewall state such as 'Enabled' or 'Disabled'.\" 1\r\nfi\r\n\r\n# Process block all incoming connections argument if provided.\r\nif [[ -n \"$_arg_blockAllIncomingConnections\" ]]; then\r\n  _arg_blockAllIncomingConnections=$(echo \"$_arg_blockAllIncomingConnections\" | xargs)\r\n\r\n  # Ensure a valid block all incoming connections state is provided.\r\n  if [[ -z \"$_arg_blockAllIncomingConnections\" ]]; then\r\n    _PRINT_HELP=yes die \"[Error] Please specify whether to block all incoming connections (Yes) or allow all incoming connections (No). You can also leave this setting blank to not modify the current setting.\" 1\r\n  fi\r\n\r\n  # Convert block all incoming connections state to lowercase.\r\n  if [[ -n \"$_arg_blockAllIncomingConnections\" ]]; then\r\n    _arg_blockAllIncomingConnections=$(echo \"$_arg_blockAllIncomingConnections\" | tr '[:upper:]' '[:lower:]')\r\n  fi\r\n\r\n  # Validate the block all incoming connections state.\r\n  if [[ \"$_arg_blockAllIncomingConnections\" != \"yes\" &amp;&amp; \"$_arg_blockAllIncomingConnections\" != \"no\" ]]; then\r\n    _PRINT_HELP=yes die \"[Error] The block all incoming connections setting of '$_arg_blockAllIncomingConnections' is invalid. Please specify 'Yes' to block all incoming connections or 'No' to allow all incoming connections.\" 1\r\n  fi\r\n\r\n  # Ensure block all incoming connections is not set to yes when firewall is disabled.\r\n  if [[ \"$_arg_firewallState\" == \"disabled\" &amp;&amp; \"$_arg_blockAllIncomingConnections\" == \"yes\" ]]; then\r\n    _PRINT_HELP=yes die \"[Error] The block all incoming connections setting of '$_arg_blockAllIncomingConnections' is invalid. Unable to block all incoming connections while the firewall is disabled.\" 1\r\n  fi\r\nfi\r\n\r\n# Process stealth mode argument if provided.\r\nif [[ -n \"$_arg_stealthMode\" ]]; then\r\n  _arg_stealthMode=$(echo \"$_arg_stealthMode\" | xargs)\r\n\r\n  # Ensure a valid stealth mode state is provided.\r\n  if [[ -z \"$_arg_stealthMode\" ]]; then\r\n    _PRINT_HELP=yes die \"[Error] Please specify a valid stealth mode state such as 'Enabled' or 'Disabled'.\" 1\r\n  fi\r\n\r\n  # Convert stealth mode state to lowercase.\r\n  if [[ -n \"$_arg_stealthMode\" ]]; then\r\n    _arg_stealthMode=$(echo \"$_arg_stealthMode\" | tr '[:upper:]' '[:lower:]')\r\n  fi\r\n\r\n  # Validate the stealth mode state.\r\n  if [[ \"$_arg_stealthMode\" != \"enabled\" &amp;&amp; \"$_arg_stealthMode\" != \"disabled\" ]]; then\r\n    _PRINT_HELP=yes die \"[Error] The stealth mode state of '$_arg_stealthMode' is invalid. Please specify a valid stealth mode state such as 'Enabled' or 'Disabled'.\" 1\r\n  fi\r\n\r\n  # Ensure stealth mode is not enabled when firewall is disabled.\r\n  if [[ \"$_arg_firewallState\" == \"disabled\" &amp;&amp; \"$_arg_stealthMode\" == \"enabled\" ]]; then\r\n    _PRINT_HELP=yes die \"[Error] The stealth mode state of '$_arg_stealthMode' is invalid. Unable to disable the firewall and enable stealth mode.\" 1\r\n  fi\r\n\r\n  currentIncomingConnectionsSetting=$(\/usr\/libexec\/ApplicationFirewall\/socketfilterfw --getblockall | grep -i \"disable\")\r\n  if [[ -z \"$currentIncomingConnectionsSetting\" &amp;&amp; \"$_arg_stealthMode\" == \"disabled\" &amp;&amp; (-z \"$_arg_blockAllIncomingConnections\" || \"$_arg_blockAllIncomingConnections\" == \"yes\") ]]; then\r\n    _PRINT_HELP=yes die \"[Error] The stealth mode state of '$_arg_stealthMode' is invalid. Unable to disable stealth mode when block all incoming connections is enabled.\" 1\r\n  fi\r\nfi\r\n\r\n# Verify the current firewall state.\r\necho \"Verifying the current firewall state.\"\r\ncurrentFirewallState=$(\/usr\/libexec\/ApplicationFirewall\/socketfilterfw --getglobalstate | grep -e 'State = [0-9]*' -o | sed 's\/[^0-9]\/\/g')\r\n\r\n# Determine the desired firewall state.\r\ncase \"$_arg_firewallState\" in\r\ndisabled)\r\n  firewallGlobalState=\"off\"\r\n  if [[ \"$currentFirewallState\" == \"0\" ]]; then\r\n    updateFirewallGlobalState=\"false\"\r\n  fi\r\n  ;;\r\nenabled)\r\n  firewallGlobalState=\"on\"\r\n  if [[ \"$currentFirewallState\" == \"1\" || \"$currentFirewallState\" == \"2\" ]]; then\r\n    updateFirewallGlobalState=\"false\"\r\n  fi\r\n  ;;\r\nesac\r\n\r\n# Update the firewall state if necessary.\r\nif [[ \"$updateFirewallGlobalState\" == \"false\" ]]; then\r\n  echo \"The macOS firewall state is already set to '${_arg_firewallState}'.\"\r\nelse\r\n  echo \"Setting the firewall state to '${_arg_firewallState}'.\"\r\n\r\n  if ! \/usr\/libexec\/ApplicationFirewall\/socketfilterfw --setglobalstate \"$firewallGlobalState\"; then\r\n    _PRINT_HELP=no die \"[Error] Failed to set the firewall state of '$_arg_firewallState' by setting the global state to '$firewallGlobalState'.\" 1\r\n  fi\r\nfi\r\n\r\n# Verify the firewall state was updated successfully.\r\ncurrentFirewallState=$(\/usr\/libexec\/ApplicationFirewall\/socketfilterfw --getglobalstate | grep -e 'State = [0-9]*' -o | sed 's\/[^0-9]\/\/g')\r\ncase \"$_arg_firewallState\" in\r\ndisabled)\r\n  if [[ \"$currentFirewallState\" != \"0\" ]]; then\r\n    _PRINT_HELP=no die \"[Error] Failed to set the firewall state of '$_arg_firewallState' by setting the global state to '$firewallGlobalState'. The current state is '$currentFirewallState'.\" 1\r\n  fi\r\n  ;;\r\nenabled)\r\n  if [[ \"$currentFirewallState\" != \"1\" &amp;&amp; \"$currentFirewallState\" != \"2\" ]]; then\r\n    _PRINT_HELP=no die \"[Error] Failed to set the firewall state of '$_arg_firewallState' by setting the global state to '$firewallGlobalState'. The current state is '$currentFirewallState'.\" 1\r\n  fi\r\n  ;;\r\nesac\r\n\r\necho \"Successfully set the firewall state to '${_arg_firewallState}'.\"\r\necho \"\"\r\n\r\n# Process block all incoming connections if provided.\r\nif [[ -n \"$_arg_blockAllIncomingConnections\" ]]; then\r\n\r\n  # Verify the current block all incoming connections setting.\r\n  echo \"Verifying the current status of the 'Block all incoming connections' setting.\"\r\n  currentIncomingConnectionsSetting=$(\/usr\/libexec\/ApplicationFirewall\/socketfilterfw --getblockall | grep -i \"disable\")\r\n\r\n  case \"$_arg_blockAllIncomingConnections\" in\r\n  yes)\r\n    blockAllIncoming=\"on\"\r\n    desiredIncomingStatus=\"blocking\"\r\n\r\n    if [[ -z \"$currentIncomingConnectionsSetting\" ]]; then\r\n      updateBlockAllIncomingConnections=\"false\"\r\n    else\r\n      updateBlockAllIncomingConnections=\"true\"\r\n    fi\r\n\r\n    ;;\r\n  no)\r\n    echo \"[Warning] Setting the block all incoming setting to a state of 'disabled'.\"\r\n    blockAllIncoming=\"off\"\r\n    desiredIncomingStatus=\"disabled\"\r\n\r\n    if [[ -n \"$currentIncomingConnectionsSetting\" ]]; then\r\n      updateBlockAllIncomingConnections=\"false\"\r\n    else\r\n      updateBlockAllIncomingConnections=\"true\"\r\n    fi\r\n    ;;\r\n  esac\r\n\r\n  if [[ \"$updateBlockAllIncomingConnections\" == \"false\" ]]; then\r\n    echo \"The block all incoming state is already set to '${desiredIncomingStatus}'.\"\r\n  else\r\n    echo \"Setting the block all incoming connections state to '${desiredIncomingStatus}'.\"\r\n\r\n    if ! \/usr\/libexec\/ApplicationFirewall\/socketfilterfw --setblockall \"$blockAllIncoming\"; then\r\n      _PRINT_HELP=no die \"[Error] Failed to set the block all incoming state to '$desiredIncomingStatus'.\" 1\r\n    fi\r\n  fi\r\n\r\n  # Verify the block all incoming connections setting was updated successfully.\r\n  currentIncomingConnectionsSetting=$(\/usr\/libexec\/ApplicationFirewall\/socketfilterfw --getblockall | grep -i \"disable\")\r\n  case \"$_arg_blockAllIncomingConnections\" in\r\n  yes)\r\n    if [[ -n \"$currentIncomingConnectionsSetting\" ]]; then\r\n      _PRINT_HELP=no die \"[Error] Failed to set the block all incoming state to '$desiredIncomingStatus'.\" 1\r\n    fi\r\n    ;;\r\n  no)\r\n    if [[ -z \"$currentIncomingConnectionsSetting\" ]]; then\r\n      _PRINT_HELP=no die \"[Error] Failed to set the block all incoming state to '$desiredIncomingStatus'.\" 1\r\n    fi\r\n    ;;\r\n  esac\r\n\r\n  echo \"Successfully set the block all incoming setting to '${desiredIncomingStatus}'.\"\r\n  echo \"\"\r\nfi\r\n\r\n# Process stealth mode if provided.\r\nif [[ -n \"$_arg_stealthMode\" ]]; then\r\n  # Verify the current stealth mode state.\r\n  echo \"Verifying the current stealth mode state.\"\r\n  currentStealthModeState=$(\/usr\/libexec\/ApplicationFirewall\/socketfilterfw --getstealthmode | grep -E \"off|disable\")\r\n\r\n  case \"$_arg_stealthMode\" in\r\n  disabled)\r\n    desiredStealthModeState=\"off\"\r\n\r\n    if [[ -n \"$currentStealthModeState\" ]]; then\r\n      updateStealthModeState=\"false\"\r\n    else\r\n      updateStealthModeState=\"true\"\r\n    fi\r\n    ;;\r\n  enabled)\r\n    desiredStealthModeState=\"on\"\r\n\r\n    if [[ -z \"$currentStealthModeState\" ]]; then\r\n      updateStealthModeState=\"false\"\r\n    else\r\n      updateStealthModeState=\"true\"\r\n    fi\r\n    ;;\r\n  esac\r\n\r\n  if [[ \"$updateStealthModeState\" == \"false\" ]]; then\r\n    echo \"The stealth mode state is already set to '${_arg_stealthMode}'.\"\r\n  else\r\n    echo \"Setting the stealth mode state to '${_arg_stealthMode}'.\"\r\n\r\n    if ! \/usr\/libexec\/ApplicationFirewall\/socketfilterfw --setstealthmode \"$desiredStealthModeState\"; then\r\n      _PRINT_HELP=no die \"[Error] Failed to set the stealth mode state of '$_arg_stealthMode' by setting the state to '$desiredStealthModeState'.\" 1\r\n    fi\r\n  fi\r\n\r\n  # Verify the stealth mode state was updated successfully.\r\n  currentStealthModeState=$(\/usr\/libexec\/ApplicationFirewall\/socketfilterfw --getstealthmode | grep -E \"off|disable\")\r\n\r\n  case \"$_arg_stealthMode\" in\r\n  disabled)\r\n    if [[ -z \"$currentStealthModeState\" ]]; then\r\n      _PRINT_HELP=no die \"[Error] Failed to set the stealth mode state of '$_arg_stealthMode' by setting the state to '$desiredStealthModeState'.\" 1\r\n    fi\r\n    ;;\r\n  enabled)\r\n    if [[ -n \"$currentStealthModeState\" ]]; then\r\n      _PRINT_HELP=no die \"[Error] Failed to set the stealth mode state of '$_arg_stealthMode' by setting the state to '$desiredStealthModeState'.\" 1\r\n    fi\r\n    ;;\r\n  esac\r\n\r\n  echo \"Successfully set the stealth mode state to '${_arg_stealthMode}'.\"\r\n  echo \"\"\r\nfi\r\n\r\n# Check if the 'System Settings' application is running.\r\nsystemSettingsProcess=$(pgrep -x \"System Settings\")\r\n\r\n# If the 'System Settings' application is running and the kill flag is not set, warn the user.\r\nif [[ \"$_arg_killSystemSettingsIfNecessary\" != \"on\" &amp;&amp; -n \"$systemSettingsProcess\" ]]; then\r\n  echo \"[Warning] The 'System Settings' application is currently running with process ID (PID) '$systemSettingsProcess'.\"\r\n  echo \"[Warning] You may need to restart this application for the firewall changes to become visible.\"\r\nfi\r\n\r\n# If the kill flag is set and the 'System Settings' application is running, kill the application.\r\nif [[ \"$_arg_killSystemSettingsIfNecessary\" == \"on\" &amp;&amp; -n \"$systemSettingsProcess\" ]]; then\r\n  for pid in $systemSettingsProcess; do\r\n    echo \"[Warning] Killing the 'System Settings' application with process ID (PID) '$pid'.\"\r\n\r\n    # Attempt to kill the process and handle any errors.\r\n    if ! kill -9 \"$pid\"; then\r\n      echo \"[Error] Failed to kill the application with process ID (PID) '$pid'.\" &gt;&amp;2\r\n      _exitCode=1\r\n    else\r\n      echo \"Killed process with PID $pid.\"\r\n    fi\r\n  done\r\nfi\r\n\r\n# If the 'System Settings' application is not running, inform the user that the changes are complete.\r\nif [[ -z \"$systemSettingsProcess\" ]]; then\r\n  echo \"The 'System Settings' application is not currently running. The changes are now complete.\"\r\nfi\r\n\r\nexit \"$_exitCode\"<\/pre>\n<p>&nbsp;<\/p>\n\n<h2>Description d\u00e9taill\u00e9e<\/h2>\n<p>Ce script est structur\u00e9 de mani\u00e8re \u00e0 g\u00e9rer quatre op\u00e9rations principales :<\/p>\n<ol>\n<li><strong>Analyser les arguments de la ligne de commande<\/strong>\u00a0pour d\u00e9terminer les param\u00e8tres \u00e0 appliquer.<\/li>\n<li><strong>Valider les entr\u00e9es utilisateur<\/strong>\u00a0pour l&rsquo;\u00e9tat du pare-feu, le mode furtif et les param\u00e8tres de blocage.<\/li>\n<li><strong>Appliquer les changements en utilisant l&rsquo;outil CLI\u00a0<\/strong><strong>socketfilterfw<\/strong><strong>\u00a0de macOS<\/strong>.<\/li>\n<li><strong>Il est possible de d\u00e9sactiver l&rsquo;interface graphique \u00ab\u00a0Param\u00e8tres du syst\u00e8me\u00a0\u00bb<\/strong>\u00a0afin de refl\u00e9ter les modifications apport\u00e9es au pare-feu en temps r\u00e9el.<\/li>\n<\/ol>\n<h3>Param\u00e8tres cl\u00e9s<\/h3>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><strong>Param\u00e8tres<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>Action<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>&#8211;firewallState<\/strong><\/td>\n<td>Active ou d\u00e9sactive le pare-feu global (Activ\u00e9 \/ D\u00e9sactiv\u00e9)<\/td>\n<\/tr>\n<tr>\n<td><strong>&#8211;blockAllIncomingConnections<\/strong><\/td>\n<td>Bloque tout le trafic entrant (Oui \/ Non)<\/td>\n<\/tr>\n<tr>\n<td><strong>&#8211;stealthMode<\/strong><\/td>\n<td>Active le mode furtif (ignore les requ\u00eates ping et ICMP)<\/td>\n<\/tr>\n<tr>\n<td><strong>&#8211;killSystemSettingsAppIfNecessary<\/strong><\/td>\n<td>Red\u00e9marrage forc\u00e9 des param\u00e8tres du syst\u00e8me pour que l&rsquo;interface utilisateur refl\u00e8te le nouvel \u00e9tat<\/td>\n<\/tr>\n<tr>\n<td><strong>&#8211;help<\/strong><\/td>\n<td>Affiche les instructions d&rsquo;utilisation<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Cette approche modulaire en fait un outil id\u00e9al pour l&rsquo;int\u00e9gration avec des plateformes d&rsquo;automatisation comme NinjaOne.<\/p>\n<h2>Cas d&rsquo;utilisation potentiels<\/h2>\n<h3>Cas de figure\u00a0: Mise en conformit\u00e9 \u00e0 distance<\/h3>\n<p>Un MSP du secteur de la sant\u00e9 g\u00e8re plus de 150 ordinateurs portables macOS utilis\u00e9s par des cliniciens travaillant hors site. Pour se conformer \u00e0 la <a href=\"https:\/\/www.ninjaone.com\/blog\/hipaa-compliance\/\">loi HIPAA<\/a>, chaque dispositif doit bloquer toutes les connexions entrantes et fonctionner en mode furtif. Le MSP d\u00e9ploie ce script via le moteur d&rsquo;automatisation de NinjaOne avec les param\u00e8tres :<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\">\n<tbody>\n<tr>\n<td style=\"width: 100%;\">bash<\/p>\n<p>CopyEdit<\/p>\n<p><strong>.\/configure_firewall.sh &#8211;firewallState \u00ab\u00a0Enabled\u00a0\u00bb &#8211;stealthMode \u00ab\u00a0Enabled\u00a0\u00bb &#8211;blockAllIncomingConnections \u00ab\u00a0Yes\u00a0\u00bb &#8211;killSystemSettingsAppIfNecessary<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Cela permet de s&rsquo;assurer que tous les dispositifs sont renforc\u00e9s pour r\u00e9pondre aux normes r\u00e9glementaires, sans intervention manuelle de la part de l&rsquo;utilisateur.<\/p>\n<h3><strong>Comparaisons<\/strong><\/h3>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><strong>Approche<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>Avantages<\/strong><\/td>\n<td style=\"text-align: center;\"><strong>Inconv\u00e9nients<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>Ce script shell<\/strong><\/td>\n<td>Automatisation compl\u00e8te, validation r\u00e9p\u00e9table, bas\u00e9e sur l&rsquo;interface de programmation<\/td>\n<td>N\u00e9cessite un acc\u00e8s root<\/td>\n<\/tr>\n<tr>\n<td><strong>Configuration manuelle de l&rsquo;interface utilisateur<\/strong><\/td>\n<td>Aucune connaissance en mati\u00e8re de script n&rsquo;est n\u00e9cessaire<\/td>\n<td>Non \u00e9volutive, sujette aux erreurs<\/td>\n<\/tr>\n<tr>\n<td><strong>Profils MDM (Jamf, Kandji)<\/strong><\/td>\n<td>Centralis\u00e9, conforme<\/td>\n<td>N\u00e9cessite une plateforme MDM sous licence<\/td>\n<\/tr>\n<tr>\n<td><strong>Apple Configurator<\/strong><\/td>\n<td>Id\u00e9al pour le provisionnement initial<\/td>\n<td>Impossible \u00e0 mettre en \u0153uvre apr\u00e8s le d\u00e9ploiement<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Bien que les MDM offrent un contr\u00f4le au niveau de l&rsquo;entreprise, de nombreuses entreprises pr\u00e9f\u00e8rent des outils l\u00e9gers, bas\u00e9s sur des scripts, pour leur flexibilit\u00e9 et leurs frais g\u00e9n\u00e9raux minimes, en particulier lorsqu&rsquo;ils sont int\u00e9gr\u00e9s dans des outils tels que <a href=\"https:\/\/www.ninjaone.com\/fr\/\">NinjaOne<\/a>.<\/p>\n<h2>Questions fr\u00e9quentes<\/h2>\n<h3>Question 1\u00a0: Ce script prend-il en charge les anciennes versions de macOS comme Catalina ou Big Sur ?<\/h3>\n<p>Non. Il est con\u00e7u pour macOS 13 Ventura et les versions ult\u00e9rieures. Le comportement des versions ant\u00e9rieures n&rsquo;est pas garanti.<\/p>\n<h3>Question 2\u00a0: Puis-je activer le mode furtif lorsque le pare-feu est d\u00e9sactiv\u00e9 ?<\/h3>\n<p>Non. Le script emp\u00eache l&rsquo;activation du mode furtif lorsque le pare-feu est d\u00e9sactiv\u00e9 afin d&rsquo;\u00e9viter les erreurs de configuration.<\/p>\n<h3>Question 3\u00a0: Les param\u00e8tres du syst\u00e8me refl\u00e8tent-ils imm\u00e9diatement ces changements ?<\/h3>\n<p>Pas toujours. C&rsquo;est pourquoi le script inclut l&rsquo;option <strong>&#8211;killSystemSettingsAppIfNecessary<\/strong> pour forcer une actualisation.<\/p>\n<h3>Question 4\u00a0: Puis-je omettre des param\u00e8tres pour laisser certains r\u00e9glages inchang\u00e9s ?<\/h3>\n<p>Oui. Le script autorise des param\u00e8tres facultatifs pour <strong>&#8211;stealthMode<\/strong> et <strong>&#8211;blockAllIncomingConnections<\/strong>.<\/p>\n<h2>Implications<\/h2>\n<p>L&rsquo;ex\u00e9cution de ce script a des cons\u00e9quences imm\u00e9diates sur la\u00a0<strong>visibilit\u00e9 du r\u00e9seau macOS et le contr\u00f4le d&rsquo;acc\u00e8s<\/strong>:<\/p>\n<ul>\n<li><strong>Am\u00e9lioration de la s\u00e9curit\u00e9 des terminaux<\/strong>: En activant le mode furtif et en bloquant le trafic entrant, la surface d&rsquo;attaque est consid\u00e9rablement r\u00e9duite.<\/li>\n<li><strong>R\u00e9duction du risque de d\u00e9couverte<\/strong>: Les appareils deviennent invisibles aux balayages ping et aux analyses non sollicit\u00e9es.<\/li>\n<li><strong>Pr\u00e9paration \u00e0 la mise en conformit\u00e9<\/strong>: S&rsquo;aligne sur des cadres tels que CIS Benchmarks, NIST et HIPAA.<\/li>\n<\/ul>\n<p>Cependant, des param\u00e8tres agressifs peuvent\u00a0<strong>perturber des flux de travail l\u00e9gitimes<\/strong>, tels que AirDrop, les <a href=\"https:\/\/www.ninjaone.com\/fr\/blog\/outil-dacces-a-distance\/\">outils d&rsquo;acc\u00e8s \u00e0 distance<\/a> ou les environnements de d\u00e9veloppement. Une planification minutieuse est essentielle.<\/p>\n<h2>Recommandations<\/h2>\n<ul>\n<li><strong>Toujours tester dans un environnement d&rsquo;essai<\/strong>\u00a0avant de proc\u00e9der \u00e0 un d\u00e9ploiement massif.<\/li>\n<li><strong>Associer \u00e0 des scripts de journalisation\/audit<\/strong>\u00a0pour confirmer que les param\u00e8tres ont \u00e9t\u00e9 appliqu\u00e9s.<\/li>\n<li><strong>Utiliser la logique conditionnelle<\/strong>\u00a0dans les outils de d\u00e9ploiement pour d\u00e9tecter la version de macOS avant l&rsquo;ex\u00e9cution.<\/li>\n<li><strong>Informer les utilisateurs finaux<\/strong>\u00a0si les changements ont un impact sur le comportement de leur r\u00e9seau (par exemple, en bloquant les partages de fichiers entrants).<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>La gestion des param\u00e8tres du pare-feu macOS \u00e0 grande \u00e9chelle n&rsquo;est plus optionnelle, mais une n\u00e9cessit\u00e9 dans un monde de travail \u00e0 distance, de mandats de conformit\u00e9 et de menaces en constante \u00e9volution. Ce script shell fournit un moyen fiable et flexible de\u00a0<strong>configurer le pare-feu dans macOS<\/strong>, que vous cherchiez \u00e0\u00a0<strong>l&rsquo;activer<\/strong>,\u00a0<strong>le d\u00e9sactiver<\/strong>, ou\u00a0<strong>affiner son fonctionnement<\/strong>.<\/p>\n<p>Avec\u00a0<strong>NinjaOne<\/strong>, les \u00e9quipes informatiques peuvent d\u00e9ployer ce script de mani\u00e8re optimale sur l&rsquo;ensemble de leur parc macOS. L&rsquo;automatisation des politiques, la prise en charge des scripts et la surveillance en temps r\u00e9el font de NinjaOne la plateforme id\u00e9ale pour appliquer les contr\u00f4les de s\u00e9curit\u00e9 du r\u00e9seau \u00e0 grande \u00e9chelle, sans la complexit\u00e9 des solutions MDM traditionnelles.<\/p>\n","protected":false},"author":35,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","_lmt_disableupdate":"no","_lmt_disable":""},"operating_system":[4210],"use_cases":[4287,4286],"class_list":["post-524349","script_hub","type-script_hub","status-publish","hentry","script_hub_category-macos"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/script_hub\/524349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/comments?post=524349"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/media?parent=524349"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/operating_system?post=524349"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/use_cases?post=524349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}