[프로젝트] 접근 제어 - 스크립트
본 프로젝트에서는 로컬 서버의 ~/.ssh/config 파일에 대상 서버와 중앙 서버(프록시 서버)를 등록하여
로컬 서버에서 대상 서버 ssh 접속 시, 중앙 서버를 거쳐서 대상 서버에 접속되는 상황을 구현했습니다.
이때 중앙 서버의 방화벽 정책을 통하여 대상 서버로의 접근을 허용/차단 할 수 있습니다.
● 환경
(OS) Rocky Linux release 8.10 (Green Obsidian)
(서버 1) 192.168.112.218, 로컬 서버 - 사용자
(서버 2) 192.168.112.219, 중앙 서버 - 접근 제어
● 파일
(a) 로컬 서버
(파일) ~/.ssh/config
※ 대상 서버와 중앙 서버(프록시 서버)를 등록하는 파일입니다.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Host slave
HostName 192.168.112.220
User root
ProxyJump root@192.168.112.219
Host galera1
HostName 192.168.112.210
User root
ProxyJump root@192.168.112.219
...
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
(파일) /home/access_list
※ 사용자로부터 호스트 정보를 입력받아 ~/.ssh/config 파일에 등록하는 스크립트입니다.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
import os
def add_ssh_config_entry(host, hostname, user, port=22):
# os.path.expanduser : 절대 경로로 변환 - /home/username/.ssh/config
config_path = os.path.expanduser("~/.ssh/config")
new_entry = f"""
Host {host}
HostName {hostname}
User {user}
ProxyJump root@192.168.112.219
"""
# a : append, 추가 모드
with open(config_path, 'a') as config_file:
config_file.write(new_entry)
print(f"New entry added to {config_path}")
if __name__ == "__main__":
print("Enter the SSH configuration details:")
host = input("Enter the host alias: ")
hostname = input("Enter the IP address: ")
user = input("Enter the username: ")
add_ssh_config_entry(host, hostname, user)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
=> 스크립트 실행 화면
=> 스크립트 실행 결과
(b) 중앙 서버 (프록시 서버)
(파일) /home/access_ssh.py
※ 사용자로부터 IP를 입력받아 방화벽 정책 등록하는 스크립트 입니다.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
import os
import subprocess
def add_iptables_rule(ip):
try:
command = f"iptables -A OUTPUT -p tcp -d {ip} --dport 22 -j REJECT"
# subprocess.check_call : 주어진 명령어 실행시키고, 성공 시 0 반환
subprocess.check_call(command, shell=True)
print(f"Rule added: {command}")
except subprocess.CalledProcessError as e:
print(f"Failed to add rule: {e}")
def delete_iptables_rule(ip):
try:
command = f"iptables -D OUTPUT -p tcp -d {ip} --dport 22 -j REJECT"
subprocess.check_call(command, shell=True)
print(f"Rule deleted: {command}")
except subprocess.CalledProcessError as e:
print(f"Failed to delete rule: {e}")
def main():
while True:
action = input("Do you want to add or delete the rule? (add/delete/exit): ").strip().lower()
if action == 'exit':
break
if action not in ['add', 'delete']:
print("Invalid action. Please enter 'add', 'delete', or 'exit'.")
continue
ip = input("Enter the IP address: ").strip()
if action == 'add':
add_iptables_rule(ip)
elif action == 'delete':
delete_iptables_rule(ip)
if __name__ == "__main__":
main()
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
=> 스크립트 실행 화면
=> 스크립트 실행 결과
(+) 중앙 서버(프록시 서버) 입력 받기
(+) 중앙 서버 등록 별도의 창에서 진행
(+) 이미 등록된 호스트 등록 불가