Python script add field with sequence values - python

I have the following python script that gets values from a txt and construct a file with some needed values:
f = open("url-threatview.txt")
data = f.read().strip().split("\n")
f.close()
for line in data:
protocol,null,hostport,*url = line.split("/")
uri = "/".join(url)
host,*port = hostport.split(":")
def makerule(host,port,uri):
return 'alert http any %s -> any any (http.host;content:"%s";http.uri;content:"%s";classtype:bad-url;)' % (port,host,uri)
What i want to do is to insert a new field into each one of the alert rule that is created. This field must be a number always different from each rule that is created when executing the script. For example:
alert http any 8080 -> any any (http.host;content:"www.sermifleksiks.com";http.uri;content:"tab_home_active.js";classtype:bad-url;id:20)
alert http any 8080 -> any any (http.host;content:"www.sermifleksiks.com";http.uri;content:"tab_home_active.js";classtype:bad-url;id:21)`
Tried to use count function but i cannot make that work because i am not a python expert

Related

Run python file as admin

I'm trying to create a program that ask the user link of any websites to block it (in hosts)
import requests
print('Time to block some websites')
ask = input('> Give me the link... ') ; link = {} # Ask user
try:
r = requests.get(ask) # Try the url
print('Protocol : ' , r.url [:r.url.find(":")]) # Check protocol of the url
url = r.url
print('your url : ' , url)
except:
raise ValueError('Give the url with https/http', ask)
url = url.split('/') ; urlist = list(url) # Split url
link.update({'linko': urlist[2]}) ; link.update({'host': '127.0.0.1 '}) # Add host and url to link
x = link['linko'] ; y = link['host'] # Transform value of dict in string
z = str(y+x) # Assign host and link
f = open('hosts', 'a') # Open document in append mode
f.write(z) ; f.write('\n') # Write z and newline for future use
f.close() # Always close after use
f = open('hosts' , 'r') # Open document in read mode
print(f.read()) # Read document
f.close() # Always close after use
Traceback :
File "C:\Windows\System32\drivers\etc\block.py", line 17, in <module>
f = open('hosts', 'a') # Open document in append mode
PermissionError: [Errno 13] Permission denied: 'hosts'
When I tried to execute the program with runas administrator :
RUNAS ERROR: Unable to run - C:\Windows\System32\drivers\etc\block.py
193: C:\Windows\System32\drivers\etc\block.py is not a valid Win32 application.
How do I get the program to have permissions to add sites to hosts?
Thanks for your answers, I found a 'manually' solution :
Running cmd.exe as admin then move to C:\Windows\System32\drivers\etc and execute block.py
Command prompt :
C:\Windows\system32>cd drivers
C:\Windows\System32\drivers>cd etc
C:\Windows\System32\drivers\etc>python block.py
Time to block some websites
> Give me the link... https://stackoverflow.com/questions/64506935/run-python-file-as-admin
Protocol : https
https://stackoverflow.com/questions/64506935/run-python-file-as-admin
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 stackoverflow.com
C:\Windows\System32\drivers\etc>

python-ldap3 is unable to add user to and existing LDAP group

I am able successfully connect using LDAP3 and retrieve my LDAP group members as below.
from ldap3 import Server, Connection, ALL, SUBTREE
from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups as addMembersToGroups
>>> conn = Connection(Server('ldaps://ldap.****.com:***', get_info=ALL),check_names=False, auto_bind=False,user="ANT\*****",password="******", authentication="NTLM")
>>>
>>> conn.open()
>>> conn.search('ou=Groups,o=****.com', '(&(cn=MY-LDAP-GROUP))', attributes=['cn', 'objectclass', 'memberuid'])
it returns True and I can see members by printing
conn.entries
>>>
The above line says MY-LDAP-GROUP exists and returns TRUE while searching but throws LDAP group not found when I try to an user to the group as below
>>> addMembersToGroups(conn, ['myuser'], 'MY-LDAP-GROUP')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/****/anaconda3/lib/python3.7/site-packages/ldap3/extend/microsoft/addMembersToGroups.py", line 69, in ad_add_members_to_groups
raise LDAPInvalidDnError(group + ' not found')
ldap3.core.exceptions.LDAPInvalidDnError: MY-LDAP-GROUP not found
>>>
The above line says MY-LDAP-GROUP exists and returns TRUE
Returning True just means that the search succeeded. It doesn't mean that anything was found. Is there anything in conn.entries?
But I suspect your real problem is something different. If this is the source code for ad_add_members_to_groups, then it is expecting the distinguishedName of the group (notice the parameter name group_dn), but you're passing the cn (common name). For example, your code should be something like:
addMembersToGroups(conn, ['myuser'], 'CN=MY-LDAP-GROUP,OU=Groups,DC=example,DC=com')
If you don't know the DN, then ask for the distinguishedName attribute from the search.
A word of warning: that code for ad_add_members_to_groups retrieves all the current members before adding the new member. You might run into performance problems if you're working with groups that have large membership because of that (e.g. if the group has 1000 members, it will load all 1000 before adding anyone). You don't actually need to do that (you can add a new member without looking at the current membership). I think what they're trying to avoid is the error you get when you try to add someone who is already in the group. But I think there are better ways to handle that. It might not matter to you if you're only working with small groups.
After so many trial and errors, I got frustrated and used the older python-ldap library to add existing users. Now my code is a mixture of ldap3 and ldap.
I know this is not what the OP has desired. But this may help someone.
Here the user Dinesh Kumar is already part of a group group1. I am trying to add him
to another group group2 which is successful and does not disturb the existing group
import ldap
import ldap.modlist as modlist
def add_existing_user_to_group(user_name, user_id, group_id):
"""
:return:
"""
# ldap expects a byte string.
converted_user_name = bytes(user_name, 'utf-8')
converted_user_id = bytes(user_id, 'utf-8')
converted_group_id = bytes(group_id, 'utf-8')
# Add all the attributes for the new dn
ldap_attr = {}
ldap_attr['uid'] = converted_user_name
ldap_attr['cn'] = converted_user_name
ldap_attr['uidNumber'] = converted_user_id
ldap_attr['gidNumber'] = converted_group_id
ldap_attr['objectClass'] = [b'top', b'posixAccount', b'inetOrgPerson']
ldap_attr['sn'] = b'Kumar'
ldap_attr['homeDirectory'] = b'/home/users/dkumar'
# Establish connection to server using ldap
conn = ldap.initialize(server_uri, bytes_mode=False)
bind_resp = conn.simple_bind_s("cn=admin,dc=testldap,dc=com", "password")
dn_new = "cn={},cn={},ou=MyOU,dc=testldap,dc=com".format('Dinesh Kumar','group2')
ldif = modlist.addModlist(ldap_attr)
try:
response = conn.add_s(dn_new, ldif)
except ldap.error as e:
response = e
print(" The response is ", response)
conn.unbind()
return response

Variable Route Not Working in For Loop

I tried to create multiple routes in one go by using the variables from the database and a for loop.
I tried this
temp = "example"
#app.route("/speaker/<temp>")
def getSpeakerAtr(temp):
return '''%s''' % temp
It works very well. BUT:
for x in models.Speaker.objects:
temp = str(x.name)
#app.route("/speaker/<temp>")
def getSpeakerAtr(temp):
return '''%s''' % temp
Doesn't work. The error message:
File "/Users/yang/Documents/CCPC-Website/venv/lib/python2.7/site-packages/flask/app.py", line 1013, in decorator
02:03:04 web.1 | self.add_url_rule(rule, endpoint, f, **options)
**The reason I want to use multiple routes is that I need to get the full data of an object by querying from the route. For example:
if we type this url:
//.../speaker/sam
we can get the object who has the 'name' value as 'sam'. Then I can use all of the values in this object like bio or something.**
You don't need multiple routes. Just one route that validates its value, eg:
#app.route('/speaker/<temp>')
def getSpeakerAtr(temp):
if not any(temp == str(x.name) for x in models.Speaker.objects):
# do something appropriate (404 or something?)
# carry on doing something else
Or as to your real intent:
#app.route('/speaker/<name>')
def getSpeakerAtr(name):
speaker = # do something with models.Speaker.objects to lookup `name`
if not speaker: # or whatever check is suitable to determine name didn't exist
# raise a 404, or whatever's suitable
# we have a speaker object, so use as appropriate

Biopython pubmed lookup - "No connection could be made because the target machine actively refused it" error 10061

I'm trying to retrieve ids of a specific set of keywords from pubmed using the following standard code,
import os
from Bio import Entrez
from Bio import Medline
#Defining keyword file
keywords_file = "D:\keywords.txt"
# Splitting keyword file into a list
keyword_list = []
keyword_list = open(keywords_file).read().split(',')
#print keyword_list
# Creating folders by keywords and creating a text file of the same keyword in each folder
for item in keyword_list:
create_file = item +'.txt.'
path = r"D:\Thesis"+'\\'+item
#print path
if not os.path.exists(path):
os.makedirs(path)
#print os.getcwd()
os.chdir(path)
f = open(item+'.txt','a')
f.close()
# Using biopython to fetch ids of the keyword searches
limit = 10
def fetch_ids(keyword,limit):
for item in keyword:
print item
print "Fetching search for "+item+"\n"
#os.environ['http_proxy'] = '127.0.0.1:13828'
Entrez.email = 'A.N.Other#example.com'
search = Entrez.esearch(db='pubmed',retmax=limit,term = '"'+item+'"')
print term
result = Entrez.read(search)
ids = result['IdList']
#print len(ids)
return ids
print fetch_ids(keyword_list,limit)
id_res = fetch_ids(keyword_list,limit)
print id_res
def write_ids_in_file(id_res):
with open(item+'.txt','w') as temp_file:
temp_file.write('\n'.join(ids))
temp_file.close()
write_ids_in_file(id_res)
In a nutshell what I'm trying to do is to create folders with the same name as each of the keywords, create a text file within the folder, fetch the ids from pubmed through the code and save the ids in the text files. My program worked fine when I initially tested it, however, after a couple of tries it started throwing me the target machine actively refused connection error. Some more details that could be useful,
header information
Host = 'eutils.ncbi.nlm.nih.gov'
Connection = 'close'
User-Agent = 'Python-urllib/2.7'
URL
http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?term=%22inflammasome%22&retmax=10&db=pubmed&tool=biopython&email=A.N.Other%40example.com
host = '127.0.0.1:13828'
I know that this question has been asked many times with the response that the port is not listening but what I want to know is if this is my issue as well, then how do get the application to work on this specific port. I've already gone to my firewall settings and opened a port 13828 but I'm not sure what to do beyond this. If this is not the case, what could potentially be a work around solution?
Thanks!
You need search.close() after result = Entrez.read(search). Check official instruction here. http://biopython.org/DIST/docs/api/Bio.Entrez-module.html
Shut down port or TCP due to too many open connections is a normal behavior for a public website.

How do I manipulate a binary plist retrieved using urllib2.urlopen into a readable xml plist without saving the file locally using Python?

We use a Python script to communicate with our Deploy Studio server to automate the updating of user and computer information using Deploy Studio's web access URLs. Due to a recent change, they are now storing the Plists for computers in the binary plist format as opposed to XML.
Here is what currently works with old version of DS (source: http://macops.ca/interfacing-with-deploystudio-using-http/):
#!/usr/bin/python
import urllib2
import plistlib
from random import randrange
host = 'https://my.ds.repo:60443'
adminuser = 'testdsuser'
adminpass = '12345'
def setupAuth():
"""Install an HTTP Basic Authorization header globally so it's used for
every request."""
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='DeployStudioServer',
uri=host,
user=adminuser,
passwd=adminpass)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
def getHostData(machine_id):
"""Return the full plist for a computer entry"""
machine_data = urllib2.urlopen(host + '/computers/get/entry?id=%s' % machine_id)
plist = plistlib.readPlistFromString(machine_data.read())
# if id isn't found, result will be an empty plist
return plist
def updateHostProperties(machine_id, properties, key_mac_addr=False, create_new=False):
"""Update the computer at machine_id with properties, a dict of properties and
values we want to set with new values. Return the full addinfourl object or None
if we found no computer to update and we aren't creating a new one. Set create_new
to True in order to enable creating new entries."""
found_comp = getHostData(machine_id)
# If we found no computer and we don't want a new record created
if not found_comp and not create_new:
return None
new_data = {}
if found_comp:
# Computer data comes back as plist nested like: {'SERIALNO': {'cn': 'my-name'}}
# DeployStudioServer expects a /set/entry POST like: {'cn': 'my-new-name'}
# so we copy the keys up a level
update = dict((k, v) for (k, v) in found_comp[machine_id].items())
new_data = update.copy()
else:
# No computer exists for this ID, we need to set up two required keys:
# 'dstudio-host-primary-key' and one of 'dstudio-host-serial-number'
# or 'dstudio-mac-addr' is required, otherwise request is ignored
# - IOW: you can't only rely on status codes
# - primary key is a server-level config, but we seem to need this per-host
if key_mac_addr:
new_data['dstudio-host-primary-key'] = 'dstudio-mac-addr'
else:
new_data['dstudio-host-primary-key'] = 'dstudio-host-serial-number'
new_data[new_data['dstudio-host-primary-key']] = machine_id
for (k, v) in properties.items():
new_data[k] = v
plist_to_post = plistlib.writePlistToString(new_data)
result = urllib2.urlopen(host + '/computers/set/entry?id=' + machine_id,
plist_to_post)
return result
def main():
setupAuth()
# Update HOWSANNIE with a new computer name (assuming this entry already exists)
random_name = 'random-id-' + str(randrange(100))
result = updateHostProperties('HOWSANNIE', {'cn': random_name,
'dstudio-hostname': random_name})
# Update DOUGLASFIRS with a new computername and custom properties, or create
# it if it doesn't already exist
random_name = 'random-id-' + str(randrange(100))
updateHostProperties('DOUGLASFIRS',
{'cn': random_name,
'dstudio-hostname': random_name,
'dstudio-custom-properties': [{
'dstudio-custom-property-key': 'ASSET_TAG',
'dstudio-custom-property-label': 'My Great Asset Tag',
'dstudio-custom-property-value': 'BL4CKL0DG3'}]
},
create_new=True)
if __name__ == "__main__":
main()
We use this in conjunction with a home-grown web interface for our technicians to enter in the information when re-imaging a machine and automatically update the information in our DS database.
I've tried using libraries such as biplist to no avail. I'd prefer to not have to store the file locally on the server and then convert it using the bash command plutil. Is there anyway I can manipulate the variable the information gets stored into? In this case it would be 'machine_data'.
I've had success using the bash command curl with the -o flag to indicate saving the file as a .plist file which works, however as said before, I would like to do this without saving the file locally if possible.
Deploy Studio web services available: .
Just putting together all of #martijn-pieters comments into an answer:
pip install biplist
import urllib2
import io
import biplist
sock = urllib2.urlopen('https://cfpropertylist.googlecode.com/svn/trunk/examples/sample.binary.plist' )
buf = io.BytesIO(sock.read())
xml = biplist.readPlist(buf)
print xml
Output:
{'Pets Names': [], 'Name': 'John Doe', 'Picture': '<B\x81\xa5\x81\xa5\x99\x81B<', 'Year Of Birth': 1965, 'Date Of Graduation': datetime.datetime(2004, 6, 22, 19, 23, 43), 'City Of Birth': 'Springfield', 'Kids Names': ['John', 'Kyra']}

Categories

Resources