Skip to content

Instantly share code, notes, and snippets.

@ranvijay-sachan
Last active June 17, 2017 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranvijay-sachan/42f3bc54beca9e34f55bda1613dbe6ed to your computer and use it in GitHub Desktop.
Save ranvijay-sachan/42f3bc54beca9e34f55bda1613dbe6ed to your computer and use it in GitHub Desktop.
parse unix passwd file
import os
from flask import Flask, request, render_template
from network.parse_passwd import get_remote_user_info
app = Flask(__name__)
@app.route('/login')
def login():
return render_template('login_form.html')
@app.route('/result', methods=['GET', 'POST'])
def result():
if request.method == 'POST':
data = get_remote_user_info(request.form['host'], request.form['username'], request.form['password'])
return render_template('result.html', result=data)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
<!doctype html>
<html>
<body>
<center><title> Fetch Remote Machine user's info </title>
<h2> Fetch root and interactive shell user info </h2>
<p>----------------------------------------------</p>
<form action = "http://localhost:5000/result" method="post">
<p>Server Host : <input type=text name=host>
<p>Username : <input type=text name=username>
<p>Password : <input type=text name=password>
<p><input type=submit value=submit>
</form>
</center>
</body>
</html>
#!/usr/bin/env python
import paramiko
def get_remote_user_info(host, user, password):
shell = {}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user, password=password, timeout=9.0)
sftp_client = ssh.open_sftp()
remote_file = sftp_client.open('/etc/passwd')
try:
for line in remote_file:
if len(line.rstrip("\n").split(":")) >= 6 and \
line.rstrip("\n").split(":")[6] in ["/bin/sh", "/bin/bash"]:
fields = line.rstrip().split(":")
shell[fields[0]] = fields[-1]
finally:
remote_file.close()
print shell
return shell
# for user in shell.keys():
# print user
if __name__ == '__main__':
get_remote_user_info('host', 'user', 'pass')
<!doctype html>
<html>
<body>
<center><title> root and interactive shell user </title>
<h3> root and interactive shell user user info </h3>
<table border = 1>
{% for key, value in result.iteritems() %}
<tr>
<td width="100"> {{ key }} </td>
<td width="100"> {{ value }} </td>
</tr>
{% endfor %}
</table>
</center>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment