파이썬(Python)/화이트해킹,침투테스트(Hack,Penetration)

Python 에서 Nmap 설치 하고 네트워크 검색(port)해보기(설치시 에러 해결)

끄적끄적아무거나 2021. 7. 23. 09:03
반응형

Python 에서 Nmap 설치 하고 네트워크 검색해보기

 

현재 나는 윈도우 OS를 사용하고 파이썬 프로그래밍 툴로 Visual Studio를 사용한다. 이를 기초로 Nmap을 설치하고 동작 시켜볼 예정이다.

 


Nmap 설치

 

코드>>

import nmap
nm = nmap.PortScanner()
print(nm.scan('127.0.0.1', '22-443'))

 

위와 같이 단순한 코드를 동작 시켰다. 하지만 아래와 같은 에러메세지가 발생하였다.

AttributeError: module 'nmap' has no attribute 'PortScanner'

 

웹 검색결과 "pip install nmap" 으로 설치하는 것이 아닌 "pip install python-nmap" 으로 설치하라고 하여 다시 아래와 같이 설치 하였다.

PS C:\Users\for> pip install python-nmap
Collecting python-nmap
  Downloading python-nmap-0.6.4.tar.gz (43 kB)
     |████████████████████████████████| 43 kB 1.6 MB/s
Building wheels for collected packages: python-nmap
  Building wheel for python-nmap (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\forgo\appdata\local\programs\python\python39\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\forgo\\AppData\\Local\\Temp\\pip-install-xu4ogvk6\\python-nmap_ac893dbb5328450a9e77f37993c76272\\setup.py'"'"'; __file__='"'"'C:\\Users\\forgo\\AppData\\Local\\Temp\\pip-install-xu4ogvk6\\python-nmap_ac893dbb5328450a9e77f37993c76272\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\forgo\AppData\Local\Temp\pip-wheel-k8kvcqaf'
       cwd: C:\Users\forgo\AppData\Local\Temp\pip-install-xu4ogvk6\python-nmap_ac893dbb5328450a9e77f37993c76272\
  Complete output (2 lines):
  ----------------------------------------
  ERROR: Failed building wheel for python-nmap
  Running setup.py clean for python-nmap
Failed to build python-nmap
    Running setup.py install for python-nmap ... done
  DEPRECATION: python-nmap was installed using the legacy 'setup.py install' method, because a wheel could not be built for it. A possible replacement is to fix the wheel build issue reported above. You can find discussion regarding this at https://github.com/pypa/pip/issues/8368.

 

하지만 위와 같이 다시 에러 메세지가 발생하였다. 그리고 동작이 안되었다.

 

결론적으로 말하자면 잘못설치된 nmap을 지우고 다시 python-nmap을 설치하여야 한다. (아래 코드 참조)

PS C:\Users\for> pip uninstall nmap
Uninstalling nmap-0.0.1:
  Would remove:
    c:\users\forgo\appdata\local\programs\python\python39\lib\site-packages\nmap-0.0.1.dist-info\*
    c:\users\forgo\appdata\local\programs\python\python39\lib\site-packages\nmap\*
  Would not remove (might be manually added):
    c:\users\forgo\appdata\local\programs\python\python39\lib\site-packages\nmap\test_nmap.py
Proceed (y/n)? y

PS C:\Users\for> pip install python-nmap  
Collecting python-nmap
  Using cached python-nmap-0.6.4.tar.gz (43 kB)
Building wheels for collected packages: python-nmap
  Building wheel for python-nmap (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\forgo\appdata\local\programs\python\python39\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\forgo\\AppData\\Local\\Temp\\pip-install-y8gzj9fo\\python-nmap_b840366c9a444462a53da17510d5227e\\setup.py'"'"'; __file__='"'"'C:\\Users\\forgo\\AppData\\Local\\Temp\\pip-install-y8gzj9fo\\python-nmap_b840366c9a444462a53da17510d5227e\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\forgo\AppData\Local\Temp\pip-wheel-m9voz6x7'
       cwd: C:\Users\forgo\AppData\Local\Temp\pip-install-y8gzj9fo\python-nmap_b840366c9a444462a53da17510d5227e\
  Complete output (2 lines):
  Running setup.py clean for python-nmap
Failed to build python-nmap
Installing collected packages: python-nmap
    Running setup.py install for python-nmap ... done
  DEPRECATION: python-nmap was installed using the legacy 'setup.py install' method, because a wheel could not be built for it. A possible replacement is to fix the wheel build issue reported above. You can find discussion regarding this at https://github.com/pypa/pip/issues/8368.
Successfully installed python-nmap-0.6.4

 

그리고 코드를 실행시키니 결과가 아래와 같이 정상적으로 나왔다.

 

코드 결과>>

{'nmap': {'command_line': 'nmap -oX - -p 22-443 -sV 127.0.0.1', 'scaninfo': {'tcp': {'method': 'syn', 'services': '22-443'}}, 'scanstats': {'timestr': 'Thu Jul 22 09:01:51 2021', 'elapsed': '7.99', 'uphosts': '1', 'downhosts': '0', 'totalhosts': '1'}}, 'scan': {'127.0.0.1': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '127.0.0.1'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'localhost-response'}, 'tcp': {135: {'state': 'open', 'reason': 'syn-ack', 'name': 'msrpc', 'product': 'Microsoft Windows RPC', 'version': '', 'extrainfo': '', 'conf': '10', 'cpe': 'cpe:/o:microsoft:windows'}, 137: {'state': 'filtered', 'reason': 'no-response', 'name': 'netbios-ns', 'product': '', 'version': '', 'extrainfo': '', 'conf': '3', 'cpe': ''}}}}}

 

다음장부터는 nmap이용한 어플리케이션 제작을 해보겠다.

 

 

반응형