Last active
December 19, 2015 22:39
-
-
Save matthewrmshin/6029486 to your computer and use it in GitHub Desktop.
An attempt to repeat the `/bin/sh: FILE: /bin/bash: bad interpreter: Text file busy` problem. Write 10000 script files and executes them using a pool 10 threads.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from glob import glob | |
from Queue import Queue | |
import os | |
import stat | |
from subprocess import call, PIPE | |
from threading import Thread | |
from time import sleep | |
def main(): | |
try: | |
q = Queue() | |
for i in range(10): | |
t = Thread(target=worker, args=[q]) | |
t.daemon = True | |
t.start() | |
for i in range(10000): | |
q.put(i) | |
q.join() | |
finally: | |
for name in glob("hello-*"): | |
os.unlink(name) | |
def worker(q): | |
while True: | |
i = q.get() | |
write_and_exec(i) | |
q.task_done() | |
def write_and_exec(i): | |
f_name = "hello-%04d" % i | |
f = open(f_name, "w") | |
f.write("#!/bin/bash\ntrue\n") | |
f.flush() | |
f.close() | |
os.chmod(f_name, os.stat(f_name).st_mode | stat.S_IXUSR) | |
call([f_name], shell=True, close_fds=True) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment