(만약 본 게시글이 저작권 법에 위촉된다면 바로 삭제 하겠습니다.)
어제 안한거 까지 포함해서 2가지를 다룰려고 합니다.
일단 첫번째는 portScan을 nmap에 연동한겁니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import nmap import optparse def nmapScan(tgtHost, tgtPort): nmScan = nmap.PortScanner() nmScan.scan(tgtHost, tgtPort) state=nmScan[tgtHost]['tcp'][int(tgtPort)]['state'] print " [*] " + tgtHost + ' tcp/'+tgtPort +" "+state def main(): parser = optparse.OptionParser("usage%prog " + \ "-H <target host> -p <target port>") parser.add_option('-H', dest='tgtHost', type='string', \ help='specify target Host') parser.add_option('-P', dest='tgtPort', type='string', \ help='specify target port[s] separated by comma') (options, args) = parser.parse_args() tgtHost = options.tgtHost tgtPorts = str(options.tgtPort).split(', ') if (tgtHost == None) | (tgtPorts[0] == None): print parser.usage exit(0) for tgtPort in tgtPorts: nmapScan(tgtHost, tgtPort) if __name__ == '__main__': main() | cs |
일단 nmap을 연동할려면
<<pip install python-nmap>>으로 설치를 먼저 해야되야 작동을 하지만(왜 저는 계속 안되는거죠....)
일단 작동은 전에 작성한 portScan과 똑같지만 nmap을 연동하면 필터되는지의 여부를 알 수 있습니다.
간편하게 할 수 있는점에서는 nmap을 사용하는게 더 강력한거 같습니다.
다음은 ssh접속입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import pexpect PROMPT = ['# ','>>> ', '> ', '\$ '] def send_command(child, cmd): child.sendline(cmd) child.expect(PROMPT) print child.before def connect(user, host, password): ssh_newkey = 'Are you sure you want to continue connecting' connstr = 'ssh ' + user + '@' + host child = pexpect.spawn(connstr) ret = child.expect([pexpect.TIMEOUT, ssh_newkey, \ '[P|p]assword:']) if ret == 0: print '[-] Error connecting' return if ret == 1: child.sendline('yes') ret = child.expect([pexpect.TIMEOUT, \ '[P|p]assword: ']) if ret == 0: print '[-] Error connecting' return child.sendline(password) child.expect(PROMPT) return child def main(): host = 'localhost' user = 'root' password = 'password' child = connect(user, host, password) send_command(child, 'cat /etc/shadow | grep root') if __name__ == '__main__': main() | cs |
일단 pexpect를 사용하려면 <<pip install pexpect>>로 설치해야됩니다.
뭐 이것도 별 어려운건 없지만 우분투에서 실행했을때는 오류로 sendline이란 속성이 없다고 하네요....음...왠지 main()먼저 선언하고 하면 될거 같기도 하지만 말이죠..일단
그 노가다는 글을 다 쓴다음에 해본다음 진행상황을 글로 쓸려고 합니다.
'파이썬' 카테고리의 다른 글
해커의 언어, 치명적 파이썬 sshBrute (0) | 2017.08.18 |
---|---|
오류 고침 (0) | 2017.08.18 |
해커의 언어, 치명적 파이썬 portscan (0) | 2017.08.15 |
해커의 언어, 치명적 파이썬 unzip (0) | 2017.08.14 |
'해커의 언어, 치명적 파이썬 공부 시작 (0) | 2017.08.14 |