0w0
[primalsecurity] 0x9 – Command Automation 본문
Primalsecurity/Primalsecurity_Python-tutorials
[primalsecurity] 0x9 – Command Automation
0w0 2019. 12. 31. 00:52728x90
반응형
os, subprocess 모듈을 사용해서 운영체제 명령어 수행
os 모듈
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
>>> import os
>>> os.system('uname -a')
Linux cell 3.11.0-20-generic #35~precise1-Ubuntu SMP Fri May 2 21:32:55 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
0
>>> os.system('id')
uid=1000(cell) gid=1000(cell) groups=1000(cell),0(root)
0
>>> os.system('ping -c 1 127.0.0.1')
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.043 ms
--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.043/0.043/0.043/0.000 ms
0
>>>
|
subprocess 모듈
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
>>> import subprocess
>>>
>>> com_str = 'uname -a'
>>> command = subprocess.Popen([com_str], stdout=subprocess.PIPE, shell=True)
>>> (output, error) = command.communicate()
>>> print output
Linux cell 3.11.0-20-generic #35~precise1-Ubuntu SMP Fri May 2 21:32:55 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
>>> com_str = 'id'
>>> command = subprocess.Popen([com_str], stdout=subprocess.PIPE, shell=True)
>>> (output, error) = command.communicate()
>>> print output
uid=1000(cell) gid=1000(cell) groups=1000(cell),0(root)
>>>
|
os보다 subporcess의 사용이 복잡하지만, subporcess는 표준출력에 대한 활용도가 높으며 효율적임
결과값으로 파일 작성 및 수정 가능
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
>>> com_str = 'id'
>>> command = subprocess.Popen([com_str], stdout=subprocess.PIPE, shell=True)
>>> (output, error) = command.communicate()
>>> output
'uid=1000(cell) gid=1000(cell) groups=1000(cell),0(root)n'
>>> f = open('file.txt', 'w')
>>> f.write(output)
>>> f.close()
>>> for line in open('file.txt', 'r'):
... print line
...
uid=1000(cell) gid=1000(cell) groups=1000(cell),0(root)
>>>
|
해당 내용은 운영체제 명령을 자동화에 효율적
인터프리터 명령을 스크립트 내에서도 사용가능
728x90
반응형
'Primalsecurity > Primalsecurity_Python-tutorials' 카테고리의 다른 글
[primalsecurity] 0xB – Pseudo-Terminal (가짜 터미널) (0) | 2020.01.01 |
---|---|
[primalsecurity] 0xA – Python for Metasploit Automation (0) | 2019.12.31 |
[primalsecurity] 0x8 – Whois Automation (0) | 2019.12.31 |
[primalsecurity] 0x7 – Web Scanning and Exploitation (0) | 2019.12.24 |
[primalsecurity] 0x6 – Spidering (0) | 2019.12.24 |
Comments