Wednesday, June 22, 2011

Python script to Stop/Sniff Popups

import socket
import os
def copyfile():
host_path = "C:\Windows\System32\drivers\etc\hosts"
host_repl = "myhosts"
os.system("copy %s %s" % (host_repl,host_path))
def main():
print "Hosts File/localhost Http Sniffer"
print "By: Mouseroot"
print "Windows Only!"
print "----------------------"
print "\n"
copyfile()
http_data = "HTTP/1.0 200 OK\r\n"
http_data +="Connection: close\r\n"
http_data +="Server: Localhost http Sniffer\r\n"
http_data += "Content-Type: text/html\r\n\r\n"
http_data += "%3Cscript%3Ewindow.close();%3C/script%3E"
count = 0
myServ = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
myServ.bind(("localhost",80))
myServ.listen(100)
while count != 100:
cli,addr = myServ.accept()
msg = cli.recv(2024).decode()
spl = msg.split(" ")
req = msg.split("?")
#print "Request: ", req[1], "\n"
#print "Url: ",spl[1], "\n"
print "Msg: ",msg,"\n"
count = count + 1
cli.send(http_data.encode());
cli.close()
if __name__ == "__main__":
main()
view raw sniff_popups.py hosted with ❤ by GitHub

Create a new File called myhosts and fill it with the DNS redirects you want then run the script
What this does is creates a psuedo web server that responds with a script to close the window
later i will modify it to load in a html script so you can respond with whatever you wish!

Thursday, March 3, 2011

My first Chrome Ext: Scroll top/bottom

So today i actually got down into some code and wrote an extension for chrome that scrolls you to the top and/or bottom of the page instantly none of that slow scroll crap
using pure javascript without the magical jquery library :)
you can download it here
lets take a look the code i used

Simply created 2 Elements and populated their properties so they would stay on top and scroll with the page thanks to CSS

and i simply set their href to a javascript injection of scroll(x-axis,y-axis)

Its nothing too fancy but I find it useful for many things :)

Wednesday, March 2, 2011

Java Post Requests Class

import java.io.*;
import java.net.*;
import javax.swing.*;
public class Requests {
static String domain;
static URL myurl;
static URLConnection myurlc;
static OutputStreamWriter osr;
static BufferedReader br;
static String line;
static String resp;
static StringBuilder sb;
public Requests(String d) {
domain = d;
}
public String sendPost(String data) {
try {
sb = new StringBuilder();
myurl = new URL(domain);
myurlc = myurl.openConnection();
myurlc.setDoOutput(true);
osr = new OutputStreamWriter(myurlc.getOutputStream());
osr.write(data);
osr.flush();
br = new BufferedReader(new InputStreamReader(myurlc.getInputStream()));
while((line = br.readLine()) != null)
{
sb.append(line);
}
br.close();
osr.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null,"IO Error");
}
finally {
return sb.toString();
}
}
}
view raw requests.java hosted with ❤ by GitHub

Tuesday, March 1, 2011

Pygame Game and Player Class

This is a simple python class I wrote back when I was playing around with pygame its very basic and I used it as a foundation for my game projects
import pygame
from pygame.locals import *
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((640,480))
self.running = 1
self.clock = pygame.time.Clock()
self.rect = pygame.Rect(0,0,640,480)
def reDraw(self):
pygame.display.flip()
def tick(self,fps):
self.clock.tick(fps)
class Player:
def __init__(self,n):
self.name = n
self.x = 0
self.y = 0
self.vx = 0
self.vy = 0
self.speed = 1
self.image = None
self.rect = None
def getInput(self,inp):
if inp.type == KEYDOWN and inp.key == K_DOWN:
self.mvDown()
elif inp.type == KEYUP and inp.key == K_DOWN:
self.stopVertical()
elif inp.type == KEYDOWN and inp.key == K_UP:
self.mvUp()
elif inp.type == KEYUP and inp.key == K_UP:
self.stopVertical()
elif inp.type == KEYDOWN and inp.key == K_RIGHT:
self.mvRight()
elif inp.type == KEYUP and inp.key == K_RIGHT:
self.stopHoriz()
elif inp.type == KEYDOWN and inp.key == K_LEFT:
self.mvLeft()
elif inp.type == KEYUP and inp.key == K_LEFT:
self.stopHoriz()
def setImage(self,i):
self.image = pygame.image.load(i)
self.rect = self.image.get_rect()
def mvUp(self):
self.vy -= self.speed
def mvDown(self):
self.vy += self.speed
def mvLeft(self):
self.vx -= self.speed
def mvRight(self):
self.vx += self.speed
def stopVertical(self):
self.vy = 0
def stopHoriz(self):
self.vx = 0
def Move(self):
self.x += self.vx
self.y += self.vy
self.rect = pygame.Rect(self.x,self.y,self.image.get_width(),self.image.get_height())
def Draw(self,scr):
scr.blit(self.image,(self.x,self.y))
game = Game()
player = Player("Player")
player.setImage("player_image.bmp")
while game.running == 1:
for x in pygame.event.get():
player.getInput(x)
if x.type == QUIT:
game.running = 0
pygame.quit()
game.tick(90)
game.screen.fill((0,0,0))
player.Move()
player.Draw(g.screen)
game.reDraw()
view raw game.py hosted with ❤ by GitHub

Sunday, February 20, 2011

Simple Multithreaded Web Server in Python



Like before when a client connects its socket it passed to the clientThread Class and then we call start which starts the thread

in the main loop of the thread instead of trying to pinpoint where the GET or POST request is i simply split the string by a space and then get the 2nd element(in this case its the 1st since we start at 0) is the file the client wants
so we take this file and get the extension of the file by spiting the file by the "." so that the 2nd elements(again starting at 0) so that we get the ext of the file requested now we can do a small check and see if they want a plain text/css/javascript file or a exe or image or anything else that would need to be opened with the binary mode right after that we close the socket and stop the while loop
so the client knows we are done sending the requested file.

Saturday, February 19, 2011

Win32 Api Usage using Ctypes

from ctypes import *
class Win32api():
def __init__(self):
#msdn for what dll to use
self.SetWindowText = windll.user32.SetWindowTextA
self.FindWindow = windll.user32.FindWindowA
def String(self,s):
return c_char_p(s)
def Int(self,i):
return c_int(i)
def Long(self,l):
return c_long(l)
#small test open command prompt type title lol
test = Win32api()
#none in python is == NULL in C
ret = test.FindWindow(None,test.String("lol"))
#ret holds the window handle HWND(which is really a long/int)
test.SetWindowText(ret,test.String("Command Prompt :D"))
print("done")
view raw ctypes.py hosted with ❤ by GitHub

This utilizes the Ctypes library that allows you to use functions and methods from both Dlls and .so files in both the windows and *nix Operating system i assume it would also work in mac since its based on unix
This test simply uses the FindWindow Function with SetWindowText to change the title of the Window that was found.
This is great if you want to make alot of Api Calls but dont want to program in C just for the purpose of using windows api calls
for refrence on how to use the functions you should use msdn and search the function

Simple Multithreaded Echo Server in Python

from threading import Thread
import socket
class clientThread(Thread):
def __init__(self,sock):
Thread.__init__(self)
self.socket = sock
self.running = 1
def run(self):
while self.running == 1:
try:
msg = self.socket.recv(1024).decode()
print(msg)
self.socket.send(msg.encode())
except socket.error:
print("Dissconnected!")
self.running = 0
self.socket.close()
def main():
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((socket.gethostname(),1337))
s.listen(3)
print("Listening for 3 clients")
while 1:
cli,addrinfo=s.accept()
t = clientThread(cli)
t.start()
if __name__ == "__main__":
main()
view raw multiserver.py hosted with ❤ by GitHub

This listens for connections comming in and for each client that connects
it spawns a thread for each client by passing in the socket that is created in the while loop to the clientThread class (that has a base Class of Thread)