Skip to content

Commit a939819

Browse files
author
Hysia
authored
Merge pull request #128 from hxer/add/update
add/pocsuite auto update from github
2 parents 4f741a4 + b2ab6c1 commit a939819

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

pocsuite/lib/core/common.py

+25
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import posixpath
1717
import marshal
1818
import unicodedata
19+
import time
1920
from pocsuite.lib.core.data import conf
2021
from pocsuite.lib.core.convert import stdoutencode
2122
from pocsuite.lib.core.log import LOGGER_HANDLER
@@ -428,3 +429,27 @@ def reIndent(s, numSpace):
428429
leadingSpace = numSpace * ' '
429430
lines = [leadingSpace + line for line in s.splitlines()]
430431
return '\n'.join(lines)
432+
433+
434+
def poll_process(process, suppress_errors=False):
435+
"""
436+
Checks for process status (prints . if still running)
437+
"""
438+
439+
while True:
440+
dataToStdout(".")
441+
time.sleep(1)
442+
443+
returncode = process.poll()
444+
445+
if returncode is not None:
446+
if not suppress_errors:
447+
if returncode == 0:
448+
dataToStdout(" done\n")
449+
elif returncode < 0:
450+
dataToStdout(" process terminated by signal %d\n"
451+
% returncode)
452+
elif returncode > 0:
453+
dataToStdout(" quit unexpectedly with return code %d\n"
454+
% returncode)
455+
break

pocsuite/lib/core/update.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Copyright (c) 2014-2016 pocsuite developers (https://seebug.org)
6+
See the file 'docs/COPYING' for copying permission
7+
"""
8+
9+
import locale
10+
import os
11+
import re
12+
import subprocess
13+
import time
14+
15+
from pocsuite.lib.core.common import dataToStdout
16+
from pocsuite.lib.core.common import poll_process
17+
from pocsuite.lib.core.data import conf
18+
from pocsuite.lib.core.data import logger
19+
from pocsuite.lib.core.data import paths
20+
from pocsuite.lib.core.revision import getRevisionNumber
21+
from pocsuite.lib.core.settings import GIT_REPOSITORY
22+
from pocsuite.lib.core.settings import IS_WIN
23+
from pocsuite.lib.core.settings import SITE
24+
25+
26+
def update():
27+
if not conf.update:
28+
return
29+
30+
success = False
31+
32+
if not os.path.exists(os.path.join(
33+
os.path.dirname(paths.POCSUITE_ROOT_PATH), ".git")):
34+
err_msg = ("not a git repository. Please checkout the 'pocsuite' "
35+
"repository from GitHub (e.g. 'git clone --depth 1"
36+
"https://github.com/knownsec/Pocsuite.git pocsuite')")
37+
logger.error(err_msg)
38+
else:
39+
info_msg = ("updating pocsuite to the latest development version from "
40+
"the GitHub repository")
41+
logger.info(info_msg)
42+
43+
debug_msg = "pocsuite will try to update itself using 'git' command"
44+
logger.debug(debug_msg)
45+
46+
dataToStdout("\r[%s] [INFO] update in progress " % time.strftime("%X"))
47+
48+
try:
49+
# Reference:
50+
# http://blog.stastnarodina.com/honza-en/spot/python-unicodeencodeerror/
51+
process = subprocess.Popen(
52+
"git checkout . && git pull %s HEAD" % GIT_REPOSITORY,
53+
shell=True,
54+
stdout=subprocess.PIPE,
55+
stderr=subprocess.PIPE,
56+
cwd=paths.POCSUITE_ROOT_PATH.encode(
57+
locale.getpreferredencoding())
58+
)
59+
poll_process(process, True)
60+
stdout, stderr = process.communicate()
61+
success = not process.returncode
62+
except (IOError, OSError) as ex:
63+
success = False
64+
stderr = "{}".format(ex)
65+
66+
if success:
67+
info_msg = "{0} the latest revision '{1}'".format(
68+
"already at" if "Already" in stdout else "updated to",
69+
getRevisionNumber())
70+
logger.info(info_msg)
71+
else:
72+
if "Not a git repository" in stderr:
73+
err_msg = ("not a valid git repository. Please checkout the "
74+
"'pocsuite' repository from GitHub "
75+
"(e.g. 'git clone --depth 1 "
76+
"https://github.com/knownsec/Pocsuite.git "
77+
"pocsuite')")
78+
logger.error(err_msg)
79+
else:
80+
err_msg = "update could not be completed ('{}')".format(
81+
re.sub(r"\W+", " ", stderr).strip())
82+
logger.error(err_msg)
83+
84+
if not success:
85+
if IS_WIN:
86+
info_msg = ("for Windows platform it's recommended to use a Github"
87+
"for Windows client for updating purposes "
88+
"(http://windows.github.com/) or just download the "
89+
"latest snapshot from {}".format(SITE))
90+
else:
91+
info_msg = ("for Linux platform it's required to install a "
92+
"standard 'git' package (e.g.: "
93+
"'sudo apt-get install git')")
94+
logger.info(info_msg)

pocsuite/lib/parse/parser.py

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def parseCmdOptions():
2525
parser.add_argument("--version", action="version",
2626
version=VERSION, help="Show program's version number and exit")
2727

28+
parser.add_argument("--update", dest="update", action="store_true",
29+
help="Update Pocsuite")
30+
2831
target = parser.add_argument_group('target')
2932

3033
target.add_argument("-u", "--url", dest="url",

pocsuite/pocsuite_cli.py

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from .lib.core.option import init
3232
from .lib.core.common import delModule
3333
from .lib.core.common import getUnicode
34+
from .lib.core.update import update
3435

3536

3637
def main():
@@ -73,6 +74,10 @@ def doNothin(*args, **kw):
7374
dataToStdout("[!] legal disclaimer: %s\n\n" % LEGAL_DISCLAIMER)
7475
dataToStdout("[*] starting at %s\n\n" % time.strftime("%X"))
7576

77+
if argsDict['update']:
78+
update()
79+
return
80+
7681
if argsDict['dork']:
7782
z = ZoomEye(paths.POCSUITE_RC_PATH)
7883
if z.newToken():

0 commit comments

Comments
 (0)